/*-----------------------------------------------------------------------
Created by: PP
Created date:10th March 2006
File description: Checkout
Special instructions-notes:Java script Validation
Tables used:None
Stored procedures:None
Triggers used:None
-----------------------------------------------------------------------*/

function Validate(theForm)
{
	var errMesg = "";
	var displayMesg = "";

	var loginName = theForm.username.value = theForm.username.value.toLowerCase();

	//Validate the login name
	if ( !isCharsInBag( loginName, "abcdefghijklmnopqrstuvwxyz0123456789.-_@" ))
	{
		errMesg += "Login name has invalid characters.\n" ;
	}
	else if ( loginName.length < 3 )
	{
		errMesg += "Login name must be 3 or more characters.\n" ;
	}
	else
	{
		var count;
		for( count=0; count < loginName.length; count++ )
		{
			if (loginName.charAt(count) == "_")
			{
				if (loginName.charAt(count+1) == "_")
				{
					errMesg += "Login name may not contain more than one consecutive underscore.\n";
				}
			}
		}
	}

	//Check for password
	if ( theForm.pwd.value.length < 5 )
	{
		errMesg += "Password must be at least 5 characters.\n";
	}
	else if (theForm.pwd.value != theForm.retype_pwd.value )
	{
		errMesg += "Your passwords do not match. Please retype and try again.\n" ;
	}	
		

	var Q = ""; // this block determines lifespan of Q
	{
		if (isWhitespace(theForm.title.value))
		{
			Q += "  Title\n";
		}
	
		if (isWhitespace(theForm.first_name.value))
		{
			Q += "  First Name\n";
		}
		else if(!isCharsInBag( theForm.first_name.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' "))
		{
			errMesg += "First Name contains Invalid Characters\n";
		}
		if (isWhitespace(theForm.last_name.value))
		{
			Q += "  Last Name\n";
		}
		else if(!isCharsInBag( theForm.last_name.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' "))
		{
			errMesg += "Last Name contains Invalid Characters\n";
		}

		if (isWhitespace(theForm.company.value))
		{
			Q += "  Company\n";
		}
		if (isWhitespace(theForm.email.value))
		{
			Q += "  E-mail\n";
		}
		else if(echeck(theForm.email.value))
		{
		   errMesg += "Invalid Email Address\n";
		}	
		else if(!isCharsInBag( theForm.email.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-"))
		{
			errMesg += "Email Address contains Invalid Characters\n";
		}
		if (isWhitespace(theForm.phone.value))
		{
			Q += "  Phone\n";
		}
		else if(!isCharsInBag( theForm.phone.value, "1234567890- "))
		{
			errMesg += "Phone contains Invalid Characters\n";
		}		
		
		if (theForm.fax.value!="" && !isCharsInBag( theForm.fax.value, "1234567890- "))
		{
			errMesg += "Fax contains Invalid Characters\n";
		}


		if (isWhitespace(theForm.address.value))
		{
			Q += "  Billing Address\n";
		}	
		if( theForm.address.value!="" && theForm.address.value.length > 500 )
		{
			errMesg += "Address should not be greater than 500 characters\n";
		}
		if (isWhitespace(theForm.city.value))
		{
			Q += "  City\n";
		}	
		if (theForm.country.value==0)
		{
			Q += "  Country\n";
		}
		/*if (theForm.country.value==1 && theForm.state.value=="")
		{
			errMesg += "Please select state\n";
		}
		*/
		if (isWhitespace(theForm.zipcode.value))
		{
			Q += "  Zip/Postal\n";
		}
		

		if (isWhitespace(theForm.username.value))
		{
			Q += "  Username\n";
		}	
		
		if (isWhitespace(theForm.pwd.value))
		{
			Q += "  Password\n";
		}	
	
		if (isWhitespace(theForm.retype_pwd.value))
		{
			Q += "  Re-type Password\n";
		}
		
		if(theForm.promotional_code)
		{
			if (theForm.promotional_code.value!="" && theForm.go_flg.value=="")
			{
				errMesg += "Please Click on Recalc Button\n";
			}
		}

		if(theForm.card_type)
		{
			if (isWhitespace(theForm.card_type.value))
			{
				Q += "  Card Type\n";
			}
		}
		
		if(	theForm.card_number)
		{	
			if (isWhitespace(theForm.card_number.value))
			{
				Q += "  Card Number\n";
			}
			else if(!isCharsInBag(theForm.card_number.value, "0123456789"))
			{
				errMesg += "Card Number contains Invalid Characters\n";
			}
			
			if(theForm.card_number.value!="")
			{
				if(theForm.card_type.value!="American Express")
				{
					if (theForm.card_number.value.length != 16 )
					{
						errMesg += "Card Number should be 16 digits\n";
					}
				 }
				 else
				 {
					if (theForm.card_number.value.length != 15 )
					{
						errMesg += "Card Number should be 15 digits\n";
					} 	     
				 }
			}
		}

		if(theForm.security_number)
		{	
			if (isWhitespace(theForm.security_number.value))
			{
				Q += "  Security Number\n";
			}
			
			else if(!isCharsInBag(theForm.security_number.value, "0123456789"))
			{
				errMesg += "Security Number contains Invalid Characters\n";
			}
		}
	
		if(	theForm.expire_month)
		{
			if (isWhitespace(theForm.expire_month.value))
			{
				Q += "  Expiration Month\n";
			}
		}
		
		if (theForm.expire_year)
		{	
			if (isWhitespace(theForm.expire_year.value))
			{
				Q += "  Expiration Year\n";
			}
		}

		if(theForm.expire_month.value!="" && theForm.expire_year.value!="")
		{
			var odate = new Date();
			
			tmonth=odate.getMonth()+1;
			tyear=odate.getFullYear();
			
			tdate='1'+'-'+tmonth+'-'+tyear;
			edate='1'+'-'+theForm.expire_month.value+'-'+theForm.expire_year.value;
			
			getvalno=checkDates(tdate,edate)
			if(getvalno==0)
			{
				errMesg += "Please check the Expiration Date\n";
			}
		}
	
		if (theForm.terms.checked==false)
		{
			Q += "  Terms and Conditions\n";
		}
	
		if ( Q.length > 0 )
		{
			displayMesg = "Please provide Valid values for\n" + Q ;
		}
	}
	
	if (errMesg == "" && displayMesg == "")
	{
		theForm.mainform_flg.value="1";
		return true;
	}
	else
	{
		if(displayMesg != "")
		{
			alert(displayMesg);
			return false;			
		}
		else
		{
			alert(errMesg);
			return false;
		}	
	}
}

function Validate_renew(theForm)
{
	var errMesg = "";
	var displayMesg = "";

	var Q = ""; // this block determines lifespan of Q
	{
		if (isWhitespace(theForm.address.value))
		{
			Q += "  Billing Address\n";
		}	
		if( theForm.address.value!="" && theForm.address.value.length > 500 )
		{
			errMesg += "Address should not be greater than 500 characters\n";
		}
		if (isWhitespace(theForm.city.value))
		{
			Q += "  City\n";
		}	
		if (theForm.country.value==0)
		{
			Q += "  Country\n";
		}
		/*if (theForm.country.value==1 && theForm.state.value=="")
		{
			errMesg += "Please select state\n";
		}
		*/
		if (isWhitespace(theForm.zipcode.value))
		{
			Q += "  Zip/Postal\n";
		}
		

		if(theForm.promotional_code)
		{
			if (theForm.promotional_code.value!="" && theForm.go_flg.value=="")
			{
				errMesg += "Please Click on Recalc Button\n";
			}
		}

		if(theForm.card_type)
		{
			if (isWhitespace(theForm.card_type.value))
			{
				Q += "  Card Type\n";
			}
		}
		
		if(	theForm.card_number)
		{	
			if (isWhitespace(theForm.card_number.value))
			{
				Q += "  Card Number\n";
			}
			else if(!isCharsInBag(theForm.card_number.value, "0123456789"))
			{
				errMesg += "Card Number contains Invalid Characters\n";
			}
			
			if(theForm.card_number.value!="")
			{
				if(theForm.card_type.value!="American Express")
				{
					if (theForm.card_number.value.length != 16 )
					{
						errMesg += "Card Number should be 16 digits\n";
					}
				 }
				 else
				 {
					if (theForm.card_number.value.length != 15 )
					{
						errMesg += "Card Number should be 15 digits\n";
					} 	     
				 }
			}
		}

		if(theForm.security_number)
		{	
			if (isWhitespace(theForm.security_number.value))
			{
				Q += "  Security Number\n";
			}
			
			else if(!isCharsInBag(theForm.security_number.value, "0123456789"))
			{
				errMesg += "Security Number contains Invalid Characters\n";
			}
		}
	
		if(	theForm.expire_month)
		{
			if (isWhitespace(theForm.expire_month.value))
			{
				Q += "  Expiration Month\n";
			}
		}
		
		if (theForm.expire_year)
		{	
			if (isWhitespace(theForm.expire_year.value))
			{
				Q += "  Expiration Year\n";
			}
		}

		if(theForm.expire_month.value!="" && theForm.expire_year.value!="")
		{
			var odate = new Date();
			
			tmonth=odate.getMonth()+1;
			tyear=odate.getFullYear();
			
			tdate='1'+'-'+tmonth+'-'+tyear;
			edate='1'+'-'+theForm.expire_month.value+'-'+theForm.expire_year.value;
			
			getvalno=checkDates(tdate,edate)
			if(getvalno==0)
			{
				errMesg += "Please check the Expiration Date\n";
			}
		}
	
		if (theForm.terms.checked==false)
		{
			Q += "  Terms and Conditions\n";
		}
	
		if ( Q.length > 0 )
		{
			displayMesg = "Please provide Valid values for\n" + Q ;
		}
	}
	
	if (errMesg == "" && displayMesg == "")
	{
		theForm.mainform_flg.value="1";
		return true;
	}
	else
	{
		if(displayMesg != "")
		{
			alert(displayMesg);
			return false;			
		}
		else
		{
			alert(errMesg);
			return false;
		}	
	}
	
	
}


function checkDates(paramStartDate, paramEndDate)
{
	dtfrom=paramStartDate;
	if(dtfrom!="")
	{
		dt1_s=dtfrom.split("-");
		dt1_d=dt1_s[0]	
		dt1_m=dt1_s[1]
		dt1_y=dt1_s[2]		
	}
	
	dtto=paramEndDate;
	if(dtto!="")
	{
		dt2_s=dtto.split("-");
		dt2_d=dt2_s[0]	
		dt2_m=dt2_s[1]
		dt2_y=dt2_s[2]		
	}
	
	var dteStart = new Date(dt1_y, dt1_m, dt1_d,0,0,0);
	var dteEnd = new Date(dt2_y, dt2_m, dt2_d,0,0,0);

	var dteCurrent = new Date();	

	if(dteStart.valueOf() - dteEnd.valueOf() > 0)
	{
	   getvalno=0
	}
	else
	{
		getvalno=1;
	}
	return getvalno;				
}

function chkgo( btn_val )
{
	if( btn_val.length > 0 )
	{
		if( document.getElementById( "id_promotional_code" ).value.length == 0 )
		{
			alert( "Please enter Promotional Code" );
			document.getElementById( "id_promotional_code" ).focus();
			return false;
		}
		else
		{
			document.chkform.go_flg.value = "1";
			document.chkform.submit();
		}
	}
}

function FuncMailingAddress()
{
	var chkship = document.getElementById( "id_chkship" ).checked
	var billaddress = document.getElementById( "id_billaddress" ).value
	var billcity = document.getElementById( "id_billcity" ).value
	var billstate = document.getElementById( "id_billstate" ).value
	var billcountry = document.getElementById( "id_billcountry" ).value
	var billzipcode = document.getElementById( "id_billzipcode" ).value
	
	if( chkship == true )
	{
		document.getElementById( "id_shipaddress" ).value = billaddress;
		document.getElementById( "id_shipcity" ).value = billcity;
		document.getElementById( "id_shipstate" ).value = billstate;
		document.getElementById( "id_shipcountry" ).value = billcountry;
		document.getElementById( "id_shipzipcode" ).value = billzipcode;
	}
	else if( chkship == false )
	{
		document.getElementById( "id_shipaddress" ).value = "";
		document.getElementById( "id_shipcity" ).value = "";
		document.getElementById( "id_shipstate" ).value = "";
		document.getElementById( "id_shipcountry" ).value = 0;
		document.getElementById( "id_shipzipcode" ).value = "";
	}
}

function FuncButtonSubmit()
{
	//document.getElementById( "id_username" ).disabled = true;
	//document.getElementById( "id_pwd" ).disabled = true;
	//document.getElementById( "id_retype_pwd" ).disabled = true;
	
	var qty = document.getElementById( "id_qty" ).value;
	if( document.getElementById( "id_promotional_code" ).value == "" )
	{
		document.getElementById( "id_totamt" ).innerHTML = "$ " + parseFloat(qty*299).toFixed(2);
	}
	document.getElementById( "id_btn_submit" ).innerHTML = "<input type=image src=images/buttons/process_payment.gif name=btn_payment border=0 alt=Process Payment>";
}

function FuncUpdateAmount( qty )
{
	document.getElementById( "id_totamt" ).innerHTML = "$ " + parseFloat(qty*299).toFixed(2);
}