// form checking javascript

var highlight_on_color = "#FFE1E1";
var highlight_offleft_color = "#FFFFFF";
var highlight_offright_color = "#FFFFFF";
var focus_field = "";
var errorMsg = "";

function isblank(s)
{
	for(var i =0; i<s.length;i++)
	{
		var c = s.charAt(i);
		if((c!=' ') && (c!='\n') && (c!='\t')) return false;
	}
	return true;
}
function isEmail(thefield) {
	var email_format = /^[A-Za-z0-9]+([0-9a-zA-Z_\.\-]+)*@[A-Za-z0-9]+([0-9a-zA-Z_\.\-]+)+([\.])+([0-9a-zA-Z_\.\-]+)+[A-Za-z0-9]$/; 
	if(!email_format.test(thefield.value)) {
		return false;
	} else {
		return true;
	}
}

function isNumeric(thefield) {
	var numeric_format = /^[0-9]+$/;
	if(!numeric_format.test(thefield.value)) {
		return false;
	} else {	
		return true;
	}
}

function handlefield_exceed(thefield, left_td, right_td, exceed, msg)
{	
	if ((thefield.value.length > exceed))
	{	document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
		if(errorMsg!="") errorMsg += '<br>';
			errorMsg += msg;
		if(!focus_field)
			focus_field = thefield;
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}
}
function handlefield(thefield, left_td, right_td, msg)
{	if (isblank(thefield.value))
	{	document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
		if(errorMsg!="") errorMsg += '<br>';
			errorMsg += msg;
		if(!focus_field)
			focus_field = thefield;
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}
}
function handlemultifields(thefield, thefield2, thefield3, left_td, right_td, msg)
{	if (isblank(thefield.value) && isblank(thefield2.value) && isblank(thefield3.value))
	{	document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
		if(errorMsg!="") errorMsg += '<br>';
			errorMsg += msg;
		if(!focus_field)
			focus_field = thefield;
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}
}
function handlefield_radio(thefield, left_td, right_td, msg)
{
var noofunckecked = 0;

	for (var i = 0; i < thefield.length; i++) {
	   if (thefield[i].checked == false){
	   		noofunckecked = 1 + noofunckecked;
	   	}
	}

	if (noofunckecked == thefield.length){
		if(errorMsg!="") errorMsg += '<br>';
			errorMsg += msg;
		document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
	}

	else{
		document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}

}
function handlefield_select(thefield, left_td, right_td, msg)
{	if (thefield.selectedIndex==0)
	{	document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
		if(errorMsg!="") errorMsg += '<br>';
			errorMsg += msg;
		if(!focus_field)
			focus_field = thefield;
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}
}
function handlefield_email(thefield, left_td, right_td, msg)
{	var email_format = /^[A-Za-z0-9]+([0-9a-zA-Z_\.\-]+)*@[A-Za-z0-9]+([0-9a-zA-Z_\.\-]+)+([\.])+([0-9a-zA-Z_\.\-]+)+[A-Za-z0-9]$/; 
	if(!email_format.test(thefield.value))
	{	document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
		if(errorMsg!="") errorMsg += '<br>';
			errorMsg += msg;
		if(!focus_field)
			focus_field = thefield;
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}
}
function unhandlefield_email(left_td, right_td)
{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
	document.getElementById(right_td).style.backgroundColor= highlight_offright_color;	
}
function handlefield_compare(thefield, thefield2, left_td, right_td, msg)
{	var numeric_format = /^[0-9]+$/;

	if(thefield.value!="" && thefield2.value!="")
	{	if(numeric_format.test(thefield.value)  && numeric_format.test(thefield2.value) && parseInt(thefield2.value) < parseInt(thefield.value))
		{	document.getElementById(left_td).style.backgroundColor= highlight_on_color;
			document.getElementById(right_td).style.backgroundColor= highlight_on_color;
			if(errorMsg!="") errorMsg += '<br>';
				errorMsg += msg;
			if(!focus_field)
				focus_field = thefield;
		}
		else 
		{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
			document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
		}
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}	
}
function handlefield_numeric(thefield, digit_from, digit_to, left_td, right_td, msg)
{	var numeric_format = /^[0-9]+$/;

	/*
	if(thefield.value!="")
	{	
	*/
		if(!numeric_format.test(thefield.value) || thefield.value.length < digit_from || thefield.value.length > digit_to )
		{	document.getElementById(left_td).style.backgroundColor= highlight_on_color;
			document.getElementById(right_td).style.backgroundColor= highlight_on_color;
			if(errorMsg!="") errorMsg += '<br>';
				errorMsg += msg;
			if(!focus_field)
				focus_field = thefield;
		}
		else 
		{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
			document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
		}
	/*
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}	
	*/
}
function unhandlefield_numeric(left_td, right_td)
{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
	document.getElementById(right_td).style.backgroundColor= highlight_offright_color;	
}
function handlefield_date(field_year,field_month,field_day,left_td,right_td,msg)
{	if (!valid_date(field_year.value,field_month.value,field_day.value))
	{	document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
		if(errorMsg!="") errorMsg += '<br>';
			errorMsg += msg;
		if(!focus_field)
			focus_field = field_year;
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}

}
function valid_date(input_yr,input_mth,input_date)
{	if (input_yr%4!=0)
	{	if (input_mth==2 && input_date > 28)
		{	//alert(input_mth + "月份只有28日");
			return false
		}
	}
	else
	{	if (input_mth==2 && input_date > 29)
		{	 //alert(input_mth + "月份只有29日");
			return false
		}			
	}
	if(input_mth ==4||input_mth ==6||input_mth ==9||input_mth ==11)
    {	if (input_date>30)
         {   //alert(input_mth + "月份只有30日");
			return false
		}			
	}
	return true;
}

function compare_two_date(start_date, end_date) 
{
	var larger = end_date >= start_date;
	return larger;  
}

function handlefield_compare_date(field_year1,field_month1,field_day1,left_td1,right_td1,field_year2,field_month2,field_day2,left_td2,right_td2,msg)
{	
	var start_date = new Date();		
	var end_date = new Date();				

	if (!isblank(field_year1.value) && !isblank(field_month1.value) && !isblank(field_day1.value) 
		&& !isblank(field_year2.value) && !isblank(field_month2.value) && !isblank(field_day2.value)) 
	{
		start_date.setFullYear(field_year1.value , field_month1.value - 1, field_day1.value);
		end_date.setFullYear(field_year2.value , field_month2.value - 1, field_day2.value);

		if (!compare_two_date(start_date, end_date))
		{	
			document.getElementById(left_td1).style.backgroundColor= highlight_on_color;
			document.getElementById(right_td1).style.backgroundColor= highlight_on_color;

			document.getElementById(left_td2).style.backgroundColor= highlight_on_color;
			document.getElementById(right_td2).style.backgroundColor= highlight_on_color;

			if(errorMsg!="") errorMsg += '<br>';
				errorMsg += msg;
			if(!focus_field)
					focus_field = field_year1;
		}
		else 
		{	
			document.getElementById(left_td1).style.backgroundColor= highlight_offleft_color;
			document.getElementById(right_td1).style.backgroundColor= highlight_offright_color;

			document.getElementById(left_td2).style.backgroundColor= highlight_offleft_color;
			document.getElementById(right_td2).style.backgroundColor= highlight_offright_color;
		}
	} 
}

function check_password(thefield, left_td, right_td, msg, msg2){
var errorcode = 0;
//var numeric_format = /^[a-zA-Z0-9]/;
var space_format = /[\s\b]/;
var totalerrorMsg = "";
	if (isblank(thefield.value))
	{
		totalerrorMsg = msg;
		errorcode = 1;

	}
	else if (space_format.test(thefield.value) || thefield.value.length<5 || thefield.value.length>20)
	{
		totalerrorMsg = msg2;
		errorcode = 1;
	}

	if (errorcode == 1)
	{
		document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
		if(errorMsg!="") errorMsg += '<br>';
		errorMsg += totalerrorMsg;
		if(!focus_field)
		focus_field = thefield;
		return false;
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
		return true;
	}

}
function compare_password(thefield, thefield2, left_td, right_td, left_td2, right_td2, msg)
{	


	if(thefield.value != thefield2.value)
	{	document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
		document.getElementById(left_td2).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td2).style.backgroundColor= highlight_on_color;
		if(errorMsg!="") errorMsg += '<br>';
			errorMsg += msg;
		if(!focus_field)
			focus_field = thefield;
	}
	else 
	{	document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
		document.getElementById(left_td2).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td2).style.backgroundColor= highlight_offright_color;
	}
}
function highlight_field(thefield, status, left_td, right_td, msg){
	if (status=='Y'){
		if (msg!=""){
			if(errorMsg!="") errorMsg += '<br>';
			errorMsg += msg;
		}
		document.getElementById(left_td).style.backgroundColor= highlight_on_color;
		document.getElementById(right_td).style.backgroundColor= highlight_on_color;
	}
	else{
		document.getElementById(left_td).style.backgroundColor= highlight_offleft_color;
		document.getElementById(right_td).style.backgroundColor= highlight_offright_color;
	}
}
function handlefield_same_select(thefield1, thefield2, left_td1, right_td1, left_td2, right_td2, msg)
{	
	if (thefield1.selectedIndex!=0 && thefield2.selectedIndex!=0) 
	{
		if (thefield1.selectedIndex==thefield2.selectedIndex)
		{	
			document.getElementById(left_td1).style.backgroundColor= highlight_on_color;
			document.getElementById(right_td1).style.backgroundColor= highlight_on_color;

			document.getElementById(left_td2).style.backgroundColor= highlight_on_color;
			document.getElementById(right_td2).style.backgroundColor= highlight_on_color;

			if(errorMsg!="") errorMsg += '<br>';
				errorMsg += msg;
			if(!focus_field)
					focus_field = thefield1;
		}
		else 
		{	
			document.getElementById(left_td1).style.backgroundColor= highlight_offleft_color;
			document.getElementById(right_td1).style.backgroundColor= highlight_offright_color;

			document.getElementById(left_td2).style.backgroundColor= highlight_offleft_color;
			document.getElementById(right_td2).style.backgroundColor= highlight_offright_color;
		}
	}
}

function show_msg(msg,focus_field,msg_layer)
{	var outError = "";
	outError += '<table width="100%" border="1" cellpadding="4" cellspacing="0" class="subtitle">';
	outError += '  <tr>';
	outError += '    <td bgcolor="#FFE1E1" class="errormessage"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="errormessage">';
	outError += '        <tr>';
	outError += '          <td width="50" valign="top"><img src="../images/instantpost/error_car.gif" width="42" height="43"></td>';
	outError += '          <td valign="middle"><font color="#FF0000">';
	outError += msg ;
	outError += '            </font></td>';
	outError += '        </tr>';
	outError += '    </table></td>';
	outError += '  </tr>';
	outError += '</table><br>';
	msg_layer.innerHTML = outError;
	document.location.hash = "#";
	if(focus_field)
		focus_field.focus();
}	

function show_msg_small(msg,focus_field,msg_layer)
{	var outError = "";
	outError += '<table width="550" border="1" cellpadding="4" cellspacing="0" class="subtitle">';
	outError += '  <tr>';
	outError += '    <td bgcolor="#FFE1E1" class="errormessage"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="errormessage">';
	outError += '        <tr>';
	outError += '          <td width="50" valign="top"><img src="../images/instantpost/error_car.gif" width="42" height="43"></td>';
	outError += '          <td valign="middle"><font color="#FF0000">';
	outError += msg ;
	outError += '            </font></td>';
	outError += '        </tr>';
	outError += '    </table></td>';
	outError += '  </tr>';
	outError += '</table><br>';
	msg_layer.innerHTML = outError;
	document.location.hash = "#";
	if(focus_field)
		focus_field.focus();
}	


function show_msg_home(msg,focus_field,msg_layer)
{	var outError = "";
	outError += '<font size="1" color="#FF0000">';
	outError += msg ;
	outError += '</font><br>';
	msg_layer.innerHTML = outError;
	document.location.hash = "#";
	if(focus_field)
		focus_field.focus();
}	

function hiddenother(theform, hiddenfield) {
	if (theform.type == 'checkbox') {
		if (theform.value == "OT" && theform.checked) {
			document.getElementById("label_"+hiddenfield).style.visibility = 'visible';
		} else {
			document.getElementById("label_"+hiddenfield).style.visibility = 'hidden';	
		}
	}
	if (theform.type == 'select-one') {
		if (document.getElementById(hiddenfield)[document.getElementById(hiddenfield).selectedIndex].text == "Others" ) {
			document.getElementById("label_"+hiddenfield).style.visibility = 'visible';
		} else {
			document.getElementById("label_"+hiddenfield).style.visibility = 'hidden';	
		}
	}
}