/*
 * (c) 2001,2002 Alexey Kulentsov, Otamedia Ltd. 
 *
 * History:
 * 14.03.2001 - Started
 * 16.03.2001 - Works
 * 07.05.2001 - 'Required' parameter added
 * 25.01.2001 - Required and Mask logic separated
 * 13.02.2001 - date_valid() and time_valid() added
 * 25.07.2002 - FormCheck rewrited completely
 *		FormName_onSubmit(form) calling added
 *		Credit Card checking
 * 03.11.2002 - datetime and time controls fixed
 * 06.02.2003 - errors in radio_get_value catched
 * 08.02.2003 - error in date_get_value fixed
 * 09.11.2005 - zero tests added to date_valid and time_valid
 */

function FormCheck(form,MaskArray)
{
	var i;
	var result;

	// Check controls
	for(i=0;i<MaskArray.length;++i)
	{     var control=MaskArray[i];
		
		if(typeof(control.errMsg)=='undefined')
			control.errMsg='Value in field '+control.name+' is invalid';

		if(typeof(control.getValue)=='string')
		{	eval('value='+control.getValue+'(form,control.name);');
		}else
		{	value=get_control_value(form,control.name);
		}
		if(value==null || (typeof(value)=='string' && !value.length))
		{
			if(typeof(control.required)!=='undefined' && control.required==1)
			{
				if(typeof(control.requireMsg)=='string')
					alert(control.requireMsg);
				else	alert(control.errMsg);
				return false;
			}
		}else
		{	if(typeof(control.mask)=='string')
			{
				var Mask=new RegExp(control.mask);

				if(!Mask.test(value))
				{	alert(control.errMsg);
					return false;
				}
				delete Mask;
			}
			if(typeof(control.special)=='string')
			{	
				eval('result='+control.special+'(form,control.name);');
				if(!result)
				{
					alert(control.errMsg);
					return false;
				}
			}
		}
	}

	// User form submit handler
	eval('result=typeof('+form.name+'_onSubmit);');
	if(result=='function')
	{	eval('result='+form.name+'_onSubmit(form);');
		return result;
	}

	return true;
}

function radio_get_value(form,name)
{
	var i;
	var radios=form.elements[name];
try{
	for(i=0;i<radios.length;i++)
		if(radios[i].checked)
			return radios[i].value;
}catch(e)
{
  alert(e+' in control '+name);
}
	return null;
}

function select_get_value(form,name)
{
	var select=form.elements[name];
	if(select.selectedIndex==-1) return null;
	return select.options[select.selectedIndex].value;
}

function get_control_value(form,name)
{
	var control=form.elements[name];
	if(typeof(control)=='undefined')
		alert('Unknown control '+name);

	if(typeof(control.value)!='undefined')
		return control.value;
	if(typeof(control.options)!='undefined')
		return select_get_value(form,name);
	if(typeof(control.length)!=undefined)
		return radio_get_value(form,name);

	alert("I can\'t understand type of control "+name);
	return null;
}

function date_get_value(form,name)
{
	var year=get_control_value(form,name+'_yyyy');
	var month=get_control_value(form,name+'_mm');
	var day=get_control_value(form,name+'_dd');
	if(!year.length || !month.length || !day.length) return null;
	return new Date(year,month-1,day);
}

function date_valid(form,control)
{
	var yearString,monthString,dayString;
	var year=parseInt(yearString=form.elements[control+'_yyyy'].value,10);
	var month=parseInt(monthString=form.elements[control+'_mm'].value,10);
	var day=parseInt(dayString=form.elements[control+'_dd'].value,10);

	// zero test
	if((year==0 || yearString=='') && (month==0 || monthString=='') && (day==0 || dayString==''))
		return true;

	if(isNaN(year) || isNaN(month) || isNaN(day) || year==0 || month==0 || day==0) 
		return false;

	d=new Date(year,--month,day);
	if(d.getFullYear()!=year || d.getMonth()!=month || d.getDate()!=day)
		return false;
	return true;
}

function time_get_value(form,name)
{
	var hour=get_control_value(form,name+'_hh');
	var minutes=get_control_value(form,name+'_mm');
	var seconds='00';
	if(typeof(form.elements[name+'_ss'])!='undefined')
		seconds=get_control_value(form,name+'_ss');

	if(!hour.length || !minutes.length) return null;
	return new Date(hour+':'+minutes+':'+(seconds.length?seconds:'00'));
}

function time_valid(form,control)
{
	var hourString,minutesString;
	var hour=parseInt(hourString=form.elements[control+'_hh'].value);
	var minutes=parseInt(minutesString=form.elements[control+'_ii'].value);

	var seconds=0;
	if(typeof(form.elements[name+'_ss'])!='undefined')
		seconds=parseInt(form.elements[control+'_ss'].value);

	// zero test
	if((hourString=='' || hour==0) && (minutesString=='' || minutes==0) && (seconds=='' || seconds==0)) 
		return true;

	if(isNaN(hour) || isNaN(minutes)) 
		return false;

	if(hour<0 || minutes<0 || hour>23 || minutes>59 || (!isNaN(seconds) && (seconds<0 || seconds>59)))
		return false;
	return true;
}

function datetime_get_value(form,control)
{
	return date_get_value(form,control)+time_get_value(form,control);
}

function datetime_valid(form,control)
{
	return date_valid(form,control) && time_valid(form,control);
}


function ccdate_get_value(form,name)
{
	var year=get_control_value(form,name+'_yyyy');
	var month=get_control_value(form,name+'_mm');
	if(!year.length || !month.length) return null;
	return new Date(year,month-1,31);
}

function ccdate_valid(form,name)
{
	var d=ccdate_get_value(form,name);
	d=d.valueOf();
	var now=new Date();
	now=now.valueOf();
	return (d>=now);
}

function combobox_switcher(span)
{
	var field=span.nextSibling;
	var select=field.nextSibling;
	var txt=span.firstChild;

	if(select.style.display=='none')
	{
		field.style.display='none';
		select.style.display='';
		txt.data='<';
		field.value=select.options[select.selectedIndex].value;
	}else
	{
		field.style.display='';
		select.style.display='none';
		txt.data='>';
	}
}

function card_get_value(form,name)
{
	return form.elements[name].value.replace(/[^0-9]/,'');
}

function card_valid(form,name)
{
	return ccNumber_valid(form.elements[name].value);
}

function ccNumber_valid(ccNumber) 
{
	var summ = 0;
	var isEven  = false;
	var nlen=0;

	if(ccNumber.search(/[^0-9 -]/)!=-1)
		return false;
	ccNumber=ccNumber.replace(/[^0-9]/,'');
   
	for(i=ccNumber.length-1; i>=0; i--) 
	{
		var curDigit=parseInt(ccNumber.charAt(i));
		if(isEven)
			if((curDigit*=2)>9)
				curDigit-=9;

		summ+=curDigit;
		isEven=!isEven;
	}
	if(summ%10) return false;

	if(ccNumber.search(/^([34].{15})|((5[1-5]).{14})|(3[47].{13})|(4|(36)|(38)|(6011).{12})|((30[0-5])|(1800)|(2014)|(2149)|(2131).{11})$/)!=0)
		return false;

	return true;
}

/*
4	16	15
3	16	15
51-55	16	14
34	15	13
37	15	13
4	13	12
36	14	12
38	14	12
6011	16	12
300	14	11
301	14	11
302	14	11
303	14	11
304	14	11
305	14	11
2014	15	11
2149	15	11
2131	15	11
1800	15	11

*/
function dbl_pass_valid(form,control)
{
	var primary=form.elements[control].value;
	var secondary=form.elements[control+'_copy'].value;

	if(primary!=secondary || primary.length==0) return false;

	return true;
}
