var sfname = "";
var slname = "";
var semail = "";
var saddr1 = "";
var saddr2 = "";
var scity = "";
var sstate = "";
var scountry = "";
var szip = "";
var sphone = "";
//TODO: remove all static list of form input names
function InitSaveVariables(form) 
{
	sfname = form.sfirstname.value;
	slname = form.slastname.value;
	semail = form.semail.value;
	saddr1 = form.saddr1.value;
	saddr2 = form.saddr2.value;
	scity = form.scity.value;
	szip = form.szip.value;
	sstate = form.sstate.value;
	scountry = form.scountry.value;
	sphone = form.sphone.value;
}

function copyInfo(form)
{
	if (form.copy.checked)
	{
		InitSaveVariables(form);
		form.sfirstname.value = form.bfirstname.value;
		form.slastname.value = form.blastname.value;
		form.semail.value = form.bemail.value;
		form.saddr1.value = form.baddr1.value;
		form.saddr2.value = form.baddr2.value;
		form.scity.value = form.bcity.value;
		form.szip.value = form.bzip.value;
		form.sstate.value = form.bstate.value;
		form.scountry.value = form.bcountry.value;
		form.sphone.value = form.bphone.value;
	}
	else 
	{
		form.sfirstname.value = sfname;
		form.slastname.value = slname;
		form.semail.value = semail;
		form.saddr1.value = saddr1;
		form.saddr2.value = saddr2;
		form.scity.value = scity;
		form.szip.value = szip;
		form.sstate.value = sstate;
		form.scountry.value = scountry;
		form.sphone.value = sphone;
   }
}

function check_form_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
}
function check_form_checkinteger(object_value)
{
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
		return check_form_checknumber(object_value);
    else
		return false;
}
function form_check_creditcard(object_value)
{
	var white_space = " -";
	var creditcard_string="";
	var check_char;
    if (object_value.length == 0)
        //return true;
        return false;
	// squish out the white space
	for (var i = 0; i < object_value.length; i++)
	{
		check_char = white_space.indexOf(object_value.charAt(i))
		if (check_char < 0)
			creditcard_string += object_value.substring(i, (i + 1));
	}	
	// if all white space return error
    if (creditcard_string.length == 0)
        return false;
	// make sure number is a valid integer
	if (creditcard_string.charAt(0) == "+")
        return false;
	if (!check_form_checkinteger(creditcard_string))
		return false;
    // now check mod10
	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;
	for (var i = 0; i < creditcard_string.length; i++)
	{
		tempdigit = eval(creditcard_string.charAt(i))
		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);
			if ((tempdigit / 10) >= 1.0)
			{
				checkdigit++;
			}
			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;
}
function form_check_expiration_date(object)
{
	var curr_date = new Date();
	var curr_year = curr_date.getUTCFullYear();
	var curr_month = curr_date.getUTCMonth();
	if(object.getUTCFullYear() <= curr_year)
	{
		if(object.getUTCFullYear() < curr_year)
		{
			return false;
		}
		if(object.getUTCFullYear() == curr_year)
		{
			if(object.getUTCMonth() < curr_month)
			{
				return false;
			}
		}
	}
	return true;
}
function check_form(form_this)
{
	var response = true;
	var alert_str = '';
	var expired_date = new Date();
	
	if(!check_form_warn_about_time_and_damage(form_this))
	{
		alert('Please, read and check two notices about time and damage');
		return false;
	}
	
	expired_date.setFullYear(form_this.cc_expired_date_yy.value);
	expired_date.setMonth(form_this.cc_expired_date_mm.value-1);
	contact_info_message = check_form_contact_info(form_this);
	model_info_message = check_form_model_info(form_this);
	
	if(contact_info_message.length > 0)
	{
		response = false;
		alert_str = alert_str+'\n'+contact_info_message;
	}
	if(!form_check_creditcard(form_this.cc_number.value))
	{
		response = false;
		alert_str = alert_str+'\nA valid credit card number is required';
	}
	if(!form_check_expiration_date(expired_date))
	{
		response = false;
		alert_str = alert_str+'\nA valid credit card expiration date is required';
	}
	if(form_this.cc_code.value.length > 10 || form_this.cc_code.value.length < 3)
	{
		response = false;
		alert_str = alert_str+'\nCredit card security code is required';
	}
	if(model_info_message.length > 0)
	{
		response = false;
		alert_str = alert_str+'\n'+model_info_message;
	}
	if(!response)
	{
		alert(alert_str);
	}
	return response;
}
function check_form_model_info(form_this)
{
	message = '';
	error_flag = false;
	
	if(form_this.model_name.value.length == 0)
	{
		message = message +  'Model name is required\n';
		error_flag = true;
	}
	if(form_this.model_year.value.length == 0)
	{
		message = message +  'Model year is required\n';
		error_flag = true;
	}
	if(!error_flag)
	{
		return '';
	}
	else
	{
		return message;
	}
}
function check_form_contact_info_bcountry_and_bstate(form_this)
{
	var str_1 = "United States";
	var str_2 = "Canada";
	var str_3 = "-choose or type-";
	
	var message = "";
	var error_flag = false;
	
	if(form_this.bstate.value == str_3 && (form_this.bcountry.value == str_1 || form_this.bcountry.value == str_2))
	{
		message = message +  "Billing State/Province is required for countries " + str_1 + " and " + str_2 + " \n";
		error_flag = true;
	}
	//if(form_this.bstate.value != str_3 && form_this.bcountry.value != str_1 && form_this.bcountry.value != str_2)
	if(form_this.bstate.value != str_3 && form_this.bcountry.value == '')
	{
		message = message +  "Billing Country is required for not empty State/Province\n";
		error_flag = true;
	}
	if(!error_flag)
	{
		return '';
	}
	else
	{
		return message;
	}
}
function check_form_contact_info_scountry_and_sstate(form_this)
{
	var str_1 = "United States";
	var str_2 = "Canada";
	var str_3 = "-choose or type-";
	
	var message = "";
	var error_flag = false;
	
	if(form_this.sstate.value == str_3 && (form_this.scountry.value == str_1 || form_this.scountry.value == str_2))
	{
		message = message +  "Shipping State/Province is required for countries " + str_1 + " and " + str_2 + " \n";
		error_flag = true;
	}
	//if(form_this.sstate.value != str_3 && form_this.scountry.value != str_1 && form_this.scountry.value != str_2)
	if(form_this.sstate.value != str_3 && form_this.scountry.value == '')
	{
		message = message +  "Shipping Country is required for not empty State/Province\n";
		error_flag = true;
	}
	if(!error_flag)
	{
		return '';
	}
	else
	{
		return message;
	}
}
function check_form_contact_info(form_this)
{
	message = '';
	error_flag = false;
	
	bcountry_and_bstate_message = check_form_contact_info_bcountry_and_bstate(form_this);
	scountry_and_sstate_message = check_form_contact_info_scountry_and_sstate(form_this);
	
	if(form_this.bfirstname.value.length == 0)
	{
		message = message +  'Billing Firstname is required\n';
		error_flag = true;
	}
	if(form_this.blastname.value.length == 0)
	{
		message = message +  'Billing Lastname is required\n';
		error_flag = true;
	}
	if(form_this.bemail.value.length == 0)
	{
		message = message +  'Billing Email is required\n';
		error_flag = true;
	}
	if(form_this.baddr1.value.length == 0)
	{
		message = message +  'Billing Address 1 is required\n';
		error_flag = true;
	}
	if(form_this.bcity.value.length == 0)
	{
		message = message +  'Billing City is required\n';
		error_flag = true;
	}
	if(bcountry_and_bstate_message.length > 0)
	{
		message = message +  bcountry_and_bstate_message + '\n';
		error_flag = true;
	}
	if(form_this.bzip.value.length == 0)
	{
		message = message +  'Billing Zip is required\n';
		error_flag = true;
	}
	if(form_this.bphone.value.length == 0)
	{
		message = message +  'Billing Phone is required\n';
		error_flag = true;
	}
	if(form_this.sfirstname.value.length == 0)
	{
		message = message +  'Shipping Firstname is required\n';
		error_flag = true;
	}
	if(form_this.slastname.value.length == 0)
	{
		message = message +  'Shipping Lastname is required\n';
		error_flag = true;
	}
	if(form_this.semail.value.length == 0)
	{
		message = message +  'Shipping Email is required\n';
		error_flag = true;
	}
	if(form_this.saddr1.value.length == 0)
	{
		message = message +  'Shipping Address 1 is required\n';
		error_flag = true;
	}
	if(form_this.scity.value.length == 0)
	{
		message = message +  'Shipping City is required\n';
		error_flag = true;
	}
	if(scountry_and_sstate_message.length > 0)
	{
		message = message +  scountry_and_sstate_message + '\n';
		error_flag = true;
	}
	if(form_this.szip.value.length == 0)
	{
		message = message +  'Shipping Zip is required\n';
		error_flag = true;
	}
	if(form_this.sphone.value.length == 0)
	{
		message = message +  'Shipping Phone is required\n';
		error_flag = true;
	}
	if(!error_flag)
	{
		return '';
	}
	else
	{
		return message;
	}
}

function JS_in_array(objArray, elementNeedle)
{
	for(objIndex in objArray)
	{
		if(objArray[objIndex] == elementNeedle)
		{
			return true;
		}
	}
	return false;
}

var us_str = 'United States';
var canada_str = 'Canada';
var choose_or_type_str = '-choose or type-';
//var countries_arr = new Array
//(
//	  us_str
//	, canada_str
//);
//TODO: remove all static input ids
//var bstate_id = 'bstate_id';
//var sstate_id = 'sstate_id';
//var bcountry_id = 'bcountry_id';
//var scountry_id = 'scountry_id';

//var bmsg_1 = 'Billing State information will be dropped!';
//var smsg_1 = 'Shipping State information will be dropped!';
//var bmsg_2 = 'Billing Country will be ';
//var smsg_2 = 'Shipping Country will be ';

//var prefix_arr = new Array('b', 's');

//function ProceedCountry(selObj)
//{
//	alert_flag = 0;
//	alert_str = '';
//		
//	var tmp_cid_val = '';
//	var tmp_parentNode_val = '';
//	var tmp_astr_val = '';
//	var tmp_sobj_val = '';
//	
//	var valueId = selObj.getAttribute('id');
//	var valueObj = document.getElementById(valueId);
//	
//	for(j = 0; j < prefix_arr.length; j++)
//	{
//		tmp_cid_val = eval(prefix_arr[j]+'country_id');
//		tmp_astr_val = eval(prefix_arr[j]+'msg_1');
//		tmp_parentNode_val = document.getElementById(eval(prefix_arr[j]+'state_id'));
//		tmp_sobj_val = tmp_parentNode_val;
//		
//		if((typeof tmp_parentNode_val != "undefined") && (typeof tmp_parentNode_val.options[tmp_parentNode_val.options.selectedIndex].parentNode != "undefined"))
//		{		
//			tmp_parentNode_val = tmp_parentNode_val.options[tmp_parentNode_val.options.selectedIndex].parentNode;
//			for(i = 0; i < countries_arr.length; i++)
//			{
//				if((tmp_cid_val == valueId) && (valueObj.value == countries_arr[i]) && (valueObj.value != tmp_parentNode_val.getAttribute('label')) && (tmp_sobj_val.value != ''))
//				{
//					alert_flag = 1;
//					alert_str = tmp_astr_val;
//					tmp_sobj_val.value = '';
//				}
//			}
//			if((!JS_in_array(countries_arr, valueObj.value)) && (valueId == tmp_cid_val)  && (tmp_sobj_val.value != ''))
//			{
//				alert_flag = 1;
//				alert_str = tmp_astr_val;
//				tmp_sobj_val.value = '';
//			}
//		}
//	}
//	if(alert_flag)
//	{
//		return false;
//	}
//	return true;
//}
//function ProceedState(StateElementId, CountryElementId)
//{
//	var tmp_cname_val = '';
//	var tmp_astr_val = '';
//	var tmpIndex = '';
//	var tmpObj = '';
//	var alert_flag = 0;
//	var alert_str = '';
//	var parentId = '';
//	
//	var selIndex = document.getElementById(selObj.getAttribute('id')).options.selectedIndex;
//	var parentLabel = selObj.options[selIndex].parentNode.getAttribute('label');
//	
//	if(selObj.getAttribute('id') == bstate_id) parentId = bcountry_id;
//	if(selObj.getAttribute('id') == sstate_id) parentId = scountry_id;
//
//	
//	if(typeof parentValue != "indefined")
//	{
//		for(j = 0; j < prefix_arr.length; j++)
//		{
//			tmpObj = document.getElementById(eval(prefix_arr[j]+'country_id'));
//			tmpIndex = tmpObj.options.selectedIndex;
//			tmp_cname_val = tmpObj.options[tmpIndex].getAttribute('value');
//			
//			tmp_astr_val = eval(prefix_arr[j]+'msg_2');
//			if(typeof tmp_cname_val != "indefined")
//			{
//				if((parentLabel != tmp_cname_val) && (parentId == eval(prefix_arr[j]+'country_id')) && (parentLabel != null))
//				{
//					alert_flag = 1;
//					alert_str = tmp_astr_val+' '+parentLabel;
//					tmpObj.value = parentLabel;
//				}
//			}
//		}
//	}
//	if(alert_flag)
//	{
//		return false;
//	}
//	return true;
//}
function Proceed2Country(CountryObject, StateObject, ComboBoxIdPostfix)
{
	var tempObject;
	var tempIndex;
	alert_flag = 0;
	alert_str = '';
	
	tempObject = StateObject;
	tempObject = tempObject.getAttribute('id');	
	
	tempObject = document.getElementById(tempObject+ComboBoxIdPostfix);
	tempIndex = tempObject.options.selectedIndex;
	alert(tempObject.options[tempIndex]);
//	if(tempObject.parentNode)
//	{
//		alert(tempObject.parentNode.getAttribute('label'));
//	}
	
	if(!CountryObject || !StateObject)
	{
		return false;
	}
	if(CountryObject.value !=  us_str && CountryObject.value != canada_str)
	{
		StateObject.value = choose_or_type_str;
	}
//	if(CountryObject.value !=  us_str && )
//	{
//		StateObject.value = choose_or_type_str;
//	}
	return true;
}
function Proceed2State(StateElementId, CountryElementId)
{
	var tmp_cname_val = '';
	var tmp_astr_val = '';
	var tmpIndex = '';
	var tmpObj = '';
	var alert_flag = 0;
	var alert_str = '';
	var parentId = '';
	
	var selIndex = document.getElementById(selObj.getAttribute('id')).options.selectedIndex;
	var parentLabel = selObj.options[selIndex].parentNode.getAttribute('label');
	
	if(selObj.getAttribute('id') == bstate_id) parentId = bcountry_id;
	if(selObj.getAttribute('id') == sstate_id) parentId = scountry_id;

	
	if(typeof parentValue != "indefined")
	{
		for(j = 0; j < prefix_arr.length; j++)
		{
			tmpObj = document.getElementById(eval(prefix_arr[j]+'country_id'));
			tmpIndex = tmpObj.options.selectedIndex;
			tmp_cname_val = tmpObj.options[tmpIndex].getAttribute('value');
			
			tmp_astr_val = eval(prefix_arr[j]+'msg_2');
			if(typeof tmp_cname_val != "undefined")
			{
				if((parentLabel != tmp_cname_val) && (parentId == eval(prefix_arr[j]+'country_id')) && (parentLabel != null))
				{
					alert_flag = 1;
					alert_str = tmp_astr_val+' '+parentLabel;
					tmpObj.value = parentLabel;
				}
			}
		}
	}
	if(alert_flag)
	{
		return false;
	}
	return true;
}
function open_new_window(href, width, height, screenX, screenY, params)
{
	var wnd = '';
	var str = '';
	if(href == '') return false;
	str = 'width='+width+',height='+height+',screenX='+screenX+',screenY='+screenY;
	if(params != '') var str = str+','+params;
	window.open(href,"",str);
	return false;
}
function ItemAdded(href)
{
	if(href == '') href = "/shop/confirmation.php";
	window.open(href, '','resizable=yes,menubar=no,scrollbars=no,width=438,height=150,top=200,left=30');
	return false;
}
function check_form_warn_about_time_and_damage(form_this)
{
//	alert(form_this.time.getAttribute('value'));
//	alert(form_this.damage.getAttribute('value'));
	if(form_this.time.getAttribute('value') == "0")
	{
		return false;
	}
	if(form_this.damage.getAttribute('value') == "0")
	{
		return false;
	}
//	if(!form_this.time.getAttribute('checked'))
//	{
//		return false;
//	}
//	if(!form_this.damage.getAttribute('checked'))
//	{
//		return false;
//	}
//	
	return true;
}
