// JavaScript Document

function showCardCodeInfo(){
	imagetoshow = "<BR><a href='' onClick='hideCardCodeInfo();return false;'><img src='../images/cc.jpg' border='0'></a>";
	$("#cardcode_whatis").html(imagetoshow);
}
function hideCardCodeInfo(){
	$("#cardcode_whatis").html("");
}

function stringReplace(str){
	str = str.replace(/\#/g, " ");
	str = str.replace(/\^/g, " ");
	str = str.replace(/\&/g, " ");
	str = str.replace(/\!/g, " ");
	str = str.replace(/\*/g, " ");
	str = str.replace(/\$/g, " ");
	str = str.replace(/\%/g, " ");
	return str;
}

function stringFilter (s) {
	filteredValues = "1234567890";     // Characters stripped out
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) != -1) returnString += c;
	}
	return returnString;
}

function trimNumber(s) {
	return s.replace(/^0+/, '');
} 

function validateForm(){
	
	// Sigh validation.
	
	var submit_str = "";
	var error_occured = 0;

	if($("#account_firstname").val().length < 1 || $("#account_firstname").val().length > 40){
		$("#account_firstname_err").html("First name character length needs to be between 1 and 40<BR>");
		error_occured = 1;
	}
	else{
		$("#account_firstname_err").html("");
	}
	
	if($("#billing_firstname").val().length < 1 || $("#billing_firstname").val().length > 40){
		$("#billing_firstname_err").html("First name character length needs to be between 1 and 40<BR>");
		error_occured = 1;
	}
	else{
		//submit_str += "firstname="+$("#firstname").val();
		$("#billing_firstname_err").html("");
	}

	if($("#account_lastname").val().length < 2 || $("#account_lastname").val().length > 40){
		$("#account_lastname_err").html("Last name character length needs to be between 1 and 40<BR>");
		error_occured = 1;
	}
	else{
		$("#account_lastname_err").html("");
	}
	
	if($("#billing_lastname").val().length < 2 || $("#billing_lastname").val().length > 40){
		$("#billing_lastname_err").html("Last name character length needs to be between 2 and 40<BR>");
		error_occured = 1;
	}
	else{
		$("#billing_lastname_err").html("");
	}

	if($("#billing_email").val().length < 5 || $("#billing_email").val().length > 40 || $("#billing_email").val().indexOf("@") < 1 ){
		$("#billing_email_err").html("email must be correctly formatted<BR>");
		error_occured = 1;
	}
	else{
		$("#billing_email_err").html("");
	}
	
	if($("#account_email").val().length < 5 || $("#account_email").val().length > 40 || $("#account_email").val().indexOf("@") < 1 ){
		$("#account_email_err").html("email must be correctly formatted<BR>");
		error_occured = 1;
	}
	else{
		$("#account_email_err").html("");
	}
	
	/*if($("#billing_address1").val().length < 2 || $("#billing_address1").val().length > 40){
		$("#billing_address1_err").html("address character length needs to be between 2 and 40<BR>");
		error_occured = 1;
	}
	else{
		$("#billing_address1").val(stringReplace($("#billing_address1").val()));
		$("#billing_address2").val(stringReplace($("#billing_address2").val()));
		$("#billing_address1_err").html("");
	}
*/
	/*
	if($("#account_address1").val().length < 2 || $("#account_address1").val().length > 40){
		$("#account_address1_err").html("address character length needs to be between 2 and 40<BR>");
		error_occured = 1;
	}
	else{
		$("#account_address1").val(stringReplace($("#account_address1").val()));
		$("#account_address2").val(stringReplace($("#account_address2").val()));
		$("#account_address1_err").html("");
	}
	*/
	
	/*if($("#billing_city").val().length < 2 || $("#billing_city").val().length > 35){
		$("#billing_city_err").html("city character length needs to be between 2 and 35<BR>");
		error_occured = 1;
	}
	else{
		$("#billing_city_err").html("");
	}*/
	
	if($("#account_city").val().length < 2 || $("#account_city").val().length > 35){
		$("#account_city_err").html("city character length needs to be between 2 and 35<BR>");
		error_occured = 1;
	}
	else{
		$("#account_city_err").html("");
	}
	
	if($("#billing_state option[@selected]").val().length != 2){
		$("#billing_state_err").html("please choose a state<BR>");
		error_occured = 1;
	}
	else{
		$("#billing_state_err").html("");
	}
	
	if($("#account_state option[@selected]").val().length != 2){
		$("#account_state_err").html("please choose a state<BR>");
		error_occured = 1;
	}
	else{
		$("#account_state_err").html("");
	}
		
	if($("#billing_zip").val().length < 5 || $("#billing_zip").val().length > 15){
		$("#billing_zip_err").html("zip code character length needs to be between 5 and 15<BR>");
		error_occured = 1;
	}
	else{
		$("#billing_zip_err").html("");
	}

	/*
	if($("#account_zip").val().length < 5 || $("#account_zip").val().length > 15){
		$("#account_zip_err").html("zip code character length needs to be between 5 and 15<BR>");
		error_occured = 1;
	}
	else{
		$("#account_zip_err").html("");
	}
	*/

	// only want the integers
	$("#creditcardnumber").val(stringFilter($("#creditcardnumber").val()));
	var cc_str = stringFilter ($("#creditcardnumber").val());
	
	switch(	$("#card_type").val() ){
		case "Master Card":
		case "Visa":
		case "Discover":
				if(cc_str.length != 16){
					$("#cc_err").html("for this credit card type the credit card number should contain 16 digits<BR>");
					error_occured = 1;
				}
				else{
					$("#cc_err").html("");
				}
				break;
		case "Amex":
				if(cc_str.length != 15){
					$("#cc_err").html("for this credit card type the credit card number should contain 15 digits<BR>");
					error_occured = 1;
				}
				else{
					$("#cc_err").html("");					
				}
				break;
	}
	
	$("#creditcardcode").val(stringFilter($("#creditcardcode").val()));
	var cc_code_str = stringFilter ($("#creditcardcode").val());
	switch(	$("#card_type").val() ){
		case "Master Card":
		case "Visa":
		case "Discover":
				if(cc_code_str.length != 3){
					$("#cardcode_err").html("for this credit card type the card code should contain 3 digits from the back of the card<BR>");
					error_occured = 1;
				}
				else{
					$("#cardcode_err").html("");
				}
				break;
		case "Amex":
				if(cc_code_str.length != 4){
					$("#cardcode_err").html("for this credit card type the card code should contain 4 digits from the front of the card<BR>");
					error_occured = 1;
				}
				else{
					$("#cardcode_err").html("");
				}
				break;
	}

	if ($("#agree_terms:checked").val() != "agreed_terms"){
  		$("#agree_terms_err").html("you must check this box to agree to the terms<BR>");
		error_occured = 1;
	}
	else{
		$("#agree_terms_err").html("");
	}

	// Add check to see that account start date is in the future?  Or do we care...?
	
	//check expiration date 
	var currentTime = new Date();
	
	var requestCurrentTime = new Date();
	var requestTime = new Date();
	
	var requestYear = parseInt($("#request_year option[@selected]").val());
	var requestMonth = parseInt(trimNumber($("#request_month option[@selected]").val())) - 1;
	var requestDay = trimNumber($("#request_day option[@selected]").val());
	
	requestTime.setFullYear(requestYear, requestMonth, requestDay);
	requestCurrentTime.setFullYear(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate());
	
	if (requestCurrentTime > requestTime) {
		$("#requestdate_err").html("The date you requested is in the past.  Please select a different date to begin the subscription.");
		error_occured = 1;
	} else {
		$("#requestdate_err").html("");
	}

	
/*	var currentTime = new Date();
	var theyear = currentTime.getFullYear(); 
	var themonth = currentTime.getMonth();
	
	if(parseInt($("#the_exp_year option[@selected]").val()) > theyear){
		submit_str += "&the_exp_year="+$("#the_exp_year option[@selected]").val()+"&the_exp_month="+$("#the_exp_month option[@selected]").val();
		$("#expire_err").html("");
	}
	else{
		if(parseInt($("#the_exp_month option[@selected]").val()) >= themonth){
			submit_str += "&the_exp_year="+$("#the_exp_year option[@selected]").val()+"&the_exp_month="+$("#the_exp_month option[@selected]").val();
			$("#expire_err").html("");
		}
		else{
			$("#expire_err").html("expiration date needs to be in the future");
			error_occured = 1;
		}
	}*/
	//submit_str += "&price="+$("#price").val();		
	//submit_str += "&product="+$("#product").val();		
	//submit_str += "&description="+$("#description").val();		
	//submit_str += "&phone="+$("#phone").val();
	//submit_str += "&referral_id="+$("#referral_id").val();
	//submit_str += "&card_type="+$("#card_type option[@selected]").val()
		
	return (error_occured == 0); 
}