/*
	This is a list of the Javascript functions in this file.
	Please keep it up to date.
	
	enableOnCheckCopyShippingNew - copies shipping info to billing info for the NEW shopping cart code
	enableOnCheckCopyShippingFormField - copies formfield shipping info to formfield billing info using NEW shopping cart code.
	resetForm - given the form name, it resets the fields (only certain ones)
	clearText - given the form name and field name, it clears the field
	setFocusOnSearchBox - gives the search field focus; used in onload; note that it looks for a specific form name
	enableOnCheckCopyShipping - (OLD WAY) copies shipping info to billing info in cart
*/









/*
	This uses the NEW form field names that work with the NEW shopping cart code.
*/
function enableOnCheckCopyShippingNew(formName) {
	var copy = eval('document.' + formName + '.copy_address');
	
	if(!copy)
		copy = document.getElementById('copy_address');
	
	var billing_address_1 = eval('document.' + formName + '.BILLING_ADDRESS_1');
	var billing_address_2 =eval('document.' + formName + '.BILLING_ADDRESS_2');
	var billing_city = eval('document.' + formName + '.BILLING_CITY');
	var billing_country_id = eval('document.' + formName + '.BILLING_COUNTRY_ID');
	var billing_zip_code = eval('document.' + formName + '.BILLING_ZIP_CODE');
	var billing_state_id = eval('document.' + formName + '.BILLING_STATE_ID');
	var billing_company_name = eval('document.' + formName + '.BILLING_COMPANY_NAME');

	var shipping_address_1 = eval('document.' + formName + '.shipping_address_1');
	var shipping_address_2 = eval('document.' + formName + '.shipping_address_2');
	var shipping_city = eval('document.' + formName + '.shipping_city');
	var shipping_country_id = eval('document.' + formName + '.shipping_country_id');
	var shipping_zip_code = eval('document.' + formName + '.shipping_zip_code');
	var shipping_state_id = eval('document.' + formName + '.shipping_state_id');
	var shipping_company_name = eval('document.' + formName + '.shipping_company_name');
	
	var shipping_first_name = eval('document.' + formName + '.shipping_first_name');
	var shipping_last_name = eval('document.' + formName + '.shipping_last_name');
	
	var name_on_card = eval('document.' + formName + '.BILLING_NAME');
	var first_name_on_card = eval('document.' + formName + '.BILLING_FIRST_NAME');
	var last_name_on_card = eval('document.' + formName + '.BILLING_LAST_NAME');
	
	if (copy.checked) {
		billing_address_1.value = shipping_address_1.value;
		if (billing_address_2) {
			billing_address_2.value = shipping_address_2.value;
		}
		billing_city.value = shipping_city.value;
		
		billing_zip_code.value = shipping_zip_code.value;
		billing_state_id.value = shipping_state_id.value;
		
		if (billing_country_id) {
			billing_country_id.value = shipping_country_id.value;
		}
		
		if (shipping_first_name && shipping_last_name){
			if(name_on_card)
				name_on_card.value = shipping_first_name.value + ' ' + shipping_last_name.value;
			if(first_name_on_card)
				first_name_on_card.value = shipping_first_name.value + ' ' + shipping_last_name.value;
			if(last_name_on_card)
				last_name_on_card.value = shipping_first_name.value + ' ' + shipping_last_name.value;
		} else {
			var noc = "";
			if (shipping_first_name.value != "")
				noc = noc + shipping_first_name.value + " ";
			if (shipping_middle_name.value != "")
				noc = noc + shipping_middle_name.value + " ";
			if (shipping_last_name.value != "")
				noc = noc + shipping_last_name.value + " ";
			if(name_on_card)
				name_on_card.value= noc;	
			if(first_name_on_card)
				first_name_on_card.value= noc;	
			if(last_name_on_card)
				last_name_on_card.value= noc;	
		}
		
		if (billing_company_name && shipping_company_name) {
			billing_company_name.value = shipping_company_name.value;
		}
	} else {
		billing_address_1.value = "";
		if (billing_address_2) {
			billing_address_2.value = "";
		}
		billing_city.value = "";
		
		billing_zip_code.value = "";
		billing_state_id.value = 0;
		
		if (billing_country_id) {
			billing_country_id.value = 0;
		}
		
		if (billing_company_name)	{
			billing_company_name.value="";
		}
		if(name_on_card)
			name_on_card.value = "";
		if(first_name_on_card)
			first_name_on_card.value = "";
		if(last_name_on_card)
			last_name_on_card.value = "";
	}
}	


/*
	This uses the NEW form field names that work with the NEW shopping cart code.
*/
function enableOnCheckCopyShippingFormField(formName) {
	var copy = eval('document.' + formName + '.copy_address');
	
	if(!copy)
		copy = document.getElementById('copy_address');
	
	var billing_address_1 = eval('document.' + formName + '.BILLING_ADDRESS_1');
	var billing_address_2 =eval('document.' + formName + '.BILLING_ADDRESS_2');
	var billing_city = eval('document.' + formName + '.BILLING_CITY');
	var billing_country_id = eval('document.' + formName + '.BILLING_COUNTRY_ID');
	var billing_zip_code = eval('document.' + formName + '.BILLING_ZIP_CODE');
	var billing_state_id = eval('document.' + formName + '.BILLING_STATE_ID');
	var billing_company_name = eval('document.' + formName + '.BILLING_COMPANY_NAME');

	var shipping_address_1 = eval('document.' + formName + '.SHIPPING_ADDRESS_1');
	var shipping_address_2 = eval('document.' + formName + '.SHIPPING_ADDRESS_2');
	var shipping_city = eval('document.' + formName + '.SHIPPING_CITY');
	var shipping_country_id = eval('document.' + formName + '.SHIPPING_COUNTRY_ID');
	var shipping_zip_code = eval('document.' + formName + '.SHIPPING_ZIP_CODE');
	var shipping_state_id = eval('document.' + formName + '.SHIPPING_STATE_ID');
	var shipping_company_name = eval('document.' + formName + '.SHIPPING_COMPANY_NAME');
	
	var shipping_first_name = eval('document.' + formName + '.SHIPPING_FIRST_NAME');
	var shipping_last_name = eval('document.' + formName + '.SHIPPING_LAST_NAME');
	
	var name_on_card = eval('document.' + formName + '.BILLING_NAME');
	var billing_address_name = eval('document.' + formName + '.BILLING_ADDRESS_NAME');
	
	var billing_first_name = eval('document.' + formName + '.BILLING_FIRST_NAME');
	var billing_last_name = eval('document.' + formName + '.BILLING_LAST_NAME');
	
	var shipping_phone_number = eval('document.' + formName + '.SHIPPING_PHONE_NUMBER');
	
	var shipping_phone_number_1 = eval('document.' + formName + '.SHIPPING_PHONE_NUMBER_PART_1');
	var shipping_phone_number_2 = eval('document.' + formName + '.SHIPPING_PHONE_NUMBER_PART_2');
	var shipping_phone_number_3 = eval('document.' + formName + '.SHIPPING_PHONE_NUMBER_PART_3');
	
	var billing_phone_number = eval('document.' + formName + '.BILLING_PHONE_NUMBER');
	
	var billing_phone_number_1 = eval('document.' + formName + '.BILLING_PHONE_NUMBER_PART_1');
	var billing_phone_number_2 = eval('document.' + formName + '.BILLING_PHONE_NUMBER_PART_2');
	var billing_phone_number_3 = eval('document.' + formName + '.BILLING_PHONE_NUMBER_PART_3');
	
	if (copy.checked) {
		billing_address_1.value = shipping_address_1.value;
		if (billing_address_2) {
			billing_address_2.value = shipping_address_2.value;
		}
		billing_city.value = shipping_city.value;
		
		billing_zip_code.value = shipping_zip_code.value;
		billing_state_id.value = shipping_state_id.value;
		
		if (billing_country_id) {
			billing_country_id.value = shipping_country_id.value;
		}
		
		if (shipping_first_name && shipping_last_name){
			
			if(name_on_card) {
				name_on_card.value = shipping_first_name.value + ' ' + shipping_last_name.value;
			} else if(billing_address_name) {
			
				billing_address_name.value = shipping_first_name.value + ' ' + shipping_last_name.value;
				
			} else if(billing_first_name && billing_last_name) {
				billing_first_name.value = shipping_first_name.value;
				billing_last_name.value = shipping_last_name.value;
			}
		} else {
			var noc = "";
			if (shipping_first_name.value != "")
				noc = noc + shipping_first_name.value + " ";
			if (shipping_middle_name.value != "")
				noc = noc + shipping_middle_name.value + " ";
			if (shipping_last_name.value != "")
				noc = noc + shipping_last_name.value + " ";
	
			if(name_on_card)
				name_on_card.value = noc;	
			
			if(billing_address_name)
				billing_address_name.value = noc;
		}
		
		if (billing_company_name && shipping_company_name) {
			billing_company_name.value = shipping_company_name.value;
		}
		
		if (billing_phone_number_1 && shipping_phone_number_1) {
			billing_phone_number_1.value = shipping_phone_number_1.value;
		}
		if (billing_phone_number_2 && shipping_phone_number_2) {
			billing_phone_number_2.value = shipping_phone_number_2.value;
		}
		if (billing_phone_number_3 && shipping_phone_number_3) {
			billing_phone_number_3.value = shipping_phone_number_3.value;
		}
		
		if (billing_phone_number && shipping_phone_number) {
			billing_phone_number.value = shipping_phone_number.value;
		}
	} else {
		billing_address_1.value = "";
		if (billing_address_2) {
			billing_address_2.value = "";
		}
		billing_city.value = "";
		
		billing_zip_code.value = "";
		billing_state_id.value = 0;
		
		if (billing_country_id) {
			billing_country_id.value = 0;
		}
		
		if (billing_company_name)	{
			billing_company_name.value="";
		}
		
		if(name_on_card) {
			name_on_card.value = "";
		}
		
		if(billing_address_name) {
			billing_address_name.value = "";
		}
		
		if(billing_first_name) {
			billing_first_name.value="";
		}
		if(billing_last_name) {
			billing_last_name.value="";
		}
		
		if (billing_phone_number_1) {
			billing_phone_number_1.value = "";
		}
		if (billing_phone_number_2) {
			billing_phone_number_2.value = "";
		}
		if (billing_phone_number_3) {
			billing_phone_number_3.value = "";
		}
		
		if (billing_phone_number) {
			billing_phone_number.value = "";
		}
	}
}	


function enableOnCheckCopyShippingFormFieldByElementId(formName) {
	var copy = document.getElementById('copy_address');
	
	var billing_address_1 = document.getElementById('BILLING_ADDRESS_1');
	var billing_address_2 =document.getElementById('BILLING_ADDRESS_2');
	var billing_city = document.getElementById('BILLING_CITY');
	var billing_country_id = document.getElementById('BILLING_COUNTRY_ID');
	var billing_zip_code = document.getElementById('BILLING_ZIP_CODE');
	var billing_state_id = document.getElementById('BILLING_STATE_ID');
	var billing_company_name = document.getElementById('BILLING_COMPANY_NAME');

	var shipping_address_1 = document.getElementById('SHIPPING_ADDRESS_1');
	var shipping_address_2 = document.getElementById('SHIPPING_ADDRESS_2');
	var shipping_city = document.getElementById('SHIPPING_CITY');
	var shipping_country_id = document.getElementById('SHIPPING_COUNTRY_ID');
	var shipping_zip_code = document.getElementById('SHIPPING_ZIP_CODE');
	var shipping_state_id = document.getElementById('SHIPPING_STATE_ID');
	var shipping_company_name = document.getElementById('SHIPPING_COMPANY_NAME');
	
	var shipping_first_name = document.getElementById('SHIPPING_FIRST_NAME');
	var shipping_last_name = document.getElementById('SHIPPING_LAST_NAME');
	
	var name_on_card = document.getElementById('BILLING_NAME');
	var billing_address_name = document.getElementById('BILLING_ADDRESS_NAME');
	
	var billing_first_name = document.getElementById('BILLING_FIRST_NAME');
	var billing_last_name = document.getElementById('BILLING_LAST_NAME');
	
	var shipping_phone_number = document.getElementById('SHIPPING_PHONE_NUMBER');
	
	var shipping_phone_number_1 = document.getElementById('SHIPPING_PHONE_NUMBER_PART_1');
	var shipping_phone_number_2 = document.getElementById('SHIPPING_PHONE_NUMBER_PART_2');
	var shipping_phone_number_3 = document.getElementById('SHIPPING_PHONE_NUMBER_PART_3');
	
	var billing_phone_number = document.getElementById('BILLING_PHONE_NUMBER');
	
	var billing_phone_number_1 = document.getElementById('BILLING_PHONE_NUMBER_PART_1');
	var billing_phone_number_2 = document.getElementById('BILLING_PHONE_NUMBER_PART_2');
	var billing_phone_number_3 = document.getElementById('BILLING_PHONE_NUMBER_PART_3');
	
	if (copy.checked) {
		billing_address_1.value = shipping_address_1.value;
		if (billing_address_2) {
			billing_address_2.value = shipping_address_2.value;
		}
		billing_city.value = shipping_city.value;
		
		billing_zip_code.value = shipping_zip_code.value;
		billing_state_id.value = shipping_state_id.value;
		
		if (billing_country_id) {
			billing_country_id.value = shipping_country_id.value;
		}
		
		if (shipping_first_name && shipping_last_name){
			
			if(name_on_card) {
				name_on_card.value = shipping_first_name.value + ' ' + shipping_last_name.value;
			} else if(billing_address_name) {
			
				billing_address_name.value = shipping_first_name.value + ' ' + shipping_last_name.value;
				
			} else if(billing_first_name && billing_last_name) {
				billing_first_name.value = shipping_first_name.value;
				billing_last_name.value = shipping_last_name.value;
			}
		} else {
			var noc = "";
			if (shipping_first_name.value != "")
				noc = noc + shipping_first_name.value + " ";
			if (shipping_middle_name.value != "")
				noc = noc + shipping_middle_name.value + " ";
			if (shipping_last_name.value != "")
				noc = noc + shipping_last_name.value + " ";
	
			if(name_on_card)
				name_on_card.value = noc;	
			
			if(billing_address_name)
				billing_address_name.value = noc;
		}
		
		if (billing_company_name && shipping_company_name) {
			billing_company_name.value = shipping_company_name.value;
		}
		
		if (billing_phone_number_1 && shipping_phone_number_1) {
			billing_phone_number_1.value = shipping_phone_number_1.value;
		}
		if (billing_phone_number_2 && shipping_phone_number_2) {
			billing_phone_number_2.value = shipping_phone_number_2.value;
		}
		if (billing_phone_number_3 && shipping_phone_number_3) {
			billing_phone_number_3.value = shipping_phone_number_3.value;
		}
		
		if (billing_phone_number && shipping_phone_number) {
			billing_phone_number.value = shipping_phone_number.value;
		}
	} else {
		billing_address_1.value = "";
		if (billing_address_2) {
			billing_address_2.value = "";
		}
		billing_city.value = "";
		
		billing_zip_code.value = "";
		billing_state_id.value = 0;
		
		if (billing_country_id) {
			billing_country_id.value = 0;
		}
		
		if (billing_company_name)	{
			billing_company_name.value="";
		}
		
		if(name_on_card) {
			name_on_card.value = "";
		}
		
		if(billing_address_name) {
			billing_address_name.value = "";
		}
		
		if(billing_first_name) {
			billing_first_name.value="";
		}
		if(billing_last_name) {
			billing_last_name.value="";
		}
		
		if (billing_phone_number_1) {
			billing_phone_number_1.value = "";
		}
		if (billing_phone_number_2) {
			billing_phone_number_2.value = "";
		}
		if (billing_phone_number_3) {
			billing_phone_number_3.value = "";
		}
		
		if (billing_phone_number) {
			billing_phone_number.value = "";
		}
	}
}	


/* Clears all values from a form except for few types (hidden, submit, button, etc). */
function resetForm(formName) {
	var form = eval('document.' + formName);
	var no_elements = form.elements.length;
	
	/* Select boxes start with "select"; seems select boxes are named "select-one", etc. */
	var select_pattern = /select.*/;
	
	for (var i = 0; i < no_elements; i++) {
		var element = form.elements[i];
		if (element.type != 'hidden' && element.type != 'submit' && element.type != 'button') {
			if (element.type == 'checkbox') {
				element.checked = false;
			} else if (element.type == 'radio') {
				var group = form[element.name];
				
				if (group) {
					var group_count = group.length;
					for (var e = 0; e < group_count; e++) {
						group[e].checked = false;
					}
				}
			} else if (element.type.match(select_pattern)) {
				element.value = 0;
			} else {
				element.value = '';
			}
		}
	}
}

/* Clears a form's field. */
function clearText(formName, fieldName) {
	var field = eval('document.' + formName + '.' + fieldName);
	if (field) {
		field.value = '';
	}
}

/* Gives focus to the search text box - used in body.onload for constructioncomplete */
function setFocusOnSearchBox() {
	var textbox = eval('document.dosearch.searchterm');
	
	if (textbox) {
		textbox.focus();
	}
}

/* Copies shipping address to billing address on check. */
function enableOnCheckCopyShipping(formName) {
	var copy = eval('document.' + formName + '.copy_address');
	
	var billing_address = eval('document.' + formName + '.billing_address');
	var billing_address2 =eval('document.' + formName + '.billing_address2');
	var billing_city = eval('document.' + formName + '.billing_city');
	var billing_country = eval('document.' + formName + '.billing_country');
	var billing_country_id = eval('document.' + formName + '.billing_country_id');
	var billing_zip_code = eval('document.' + formName + '.billing_zip_code');
	var billing_state_id = eval('document.' + formName + '.billing_state_id');
	var billing_company_name = eval('document.' + formName + '.billing_company_name');

	var shipping_address = eval('document.' + formName + '.shipping_address');
	var shipping_address2 = eval('document.' + formName + '.shipping_address2');
	var shipping_city = eval('document.' + formName + '.shipping_city');
	var shipping_country = eval('document.' + formName + '.shipping_country');
	var shipping_country_id = eval('document.' + formName + '.shipping_country_id');
	var shipping_zip_code = eval('document.' + formName + '.shipping_zip_code');
	var shipping_state_id = eval('document.' + formName + '.shipping_state_id');
	var shipping_company_name = eval('document.' + formName + '.shipping_company_name');
	
	var first_name = eval('document.' + formName + '.first_name');
	var last_name = eval('document.' + formName + '.last_name');

	var shipping_first_name = eval('document.' + formName + '.shipping_first_name');
	var shipping_middle_name = eval('document.' + formName + '.shipping_middle_name');
	var shipping_last_name = eval('document.' + formName + '.shipping_last_name');
	
	var name_on_card = eval('document.' + formName + '.name_on_card');
	if (copy.checked) {
	
		billing_address.value = shipping_address.value;
		if(billing_address2)
		{
			billing_address2.value = shipping_address2.value;
		}
		billing_city.value = shipping_city.value;
		
		if (billing_country) {
			billing_country.value = shipping_country.value;
		}
		
		billing_zip_code.value = shipping_zip_code.value;
		billing_state_id.value = shipping_state_id.value;
		
		if (billing_country_id) {
			billing_country_id.value = shipping_country_id.value;
		}
		
		if(first_name && last_name)
		{
			name_on_card.value = first_name.value + ' ' + last_name.value;
		}else if(shipping_first_name && shipping_last_name){
			name_on_card.value = shipping_first_name.value + ' ' + shipping_last_name.value;
		}else
		{
			var noc = "";
			if(shipping_first_name.value != "")
				noc = noc + shipping_first_name.value + " ";
			if(shipping_middle_name.value != "")
				noc = noc + shipping_middle_name.value + " ";
			if(shipping_last_name.value != "")
				noc = noc + shipping_last_name.value + " ";
			name_on_card.value= noc;	
		}
		
		if(billing_company_name && shipping_company_name)
		{
			billing_company_name.value = shipping_company_name.value;
		}
	} else {
		billing_address.value = "";
		if(billing_address2)
		{
			billing_address2.value = "";
		}
		billing_city.value = "";
		
		if (billing_country) {
			billing_country.value = "";
		}
		
		billing_zip_code.value = "";
		billing_state_id.value = 0;
		
		if (billing_country_id) {
			billing_country_id.value = 0;
		}
		
		if(billing_company_name)
		{
			billing_company_name.value="";
		}
		name_on_card.value = "";
	}
}	


function checkforcounty(formName, body_id) {
	var id = eval('document.' + formName + '.shipping_state_id.value');
	var county = eval('document.' + formName + '.shipping_county_id');
	var county_element = document.getElementById(body_id);
	if(id  == '37'){
		if (county_element) {
			county_element.className = "tbody_show";
		}
	}else{
		if (county_element) {
			county_element.className = "tbody_hide";
		}
		if (county) {
			county.value = "0";
		}
	}
}

/*
	this allows you to have a second submit button for a form that does something totally different by changing the
	form values onClick
*/
function changeForm(formName, new_action, new_action_call, new_success, new_failure) {
	var form = eval('document.' + formName);
	
	if (form) {
		form.action = new_action;
		form.action_call.value = new_action_call;
		form.success.value = new_success;
		form.failure.value = new_failure;
	}
}



function bookmark(url, description)
{
	msg ="Please hit CTRL+D to add a bookmark to this site."
	if (navigator.appName=='Microsoft Internet Explorer')
	{
		window.external.AddFavorite(url, description);
	}
	else if (navigator.appName=='Netscape')
	{
		alert(msg);
	}
}

function getQueryStringValue(field)
{
	var query = location.search.substring(1);
	var pairs = query.split(",");
	for(var i=0; i < pairs.length; i++)
	{
		var pos = pairs[i].indexOf('=');
		if(pos == -1) continue;
		var argname = pairs[i].substring(0, pos)
		var value = pairs[i].substring(pos+1);
		if(argname == field)
			return value;
	}
	return "";
}

function popUpWindow(url, title, a_width, a_height) {
	var newwindow='';
	newwindow = window.open(url, title, "width=" + a_width + ",height=" + a_height + ",status=0, toolbar=0,location=0,status=0,resizable=1");
	newwindow.focus();
}

function popUpWindowWithScrollbar(url, title, a_width, a_height) {
	var newwindow='';
	newwindow = window.open(url, title, "width=" + a_width + ",height=" + a_height + ",status=0, scrollbars=1,toolbar=0,location=0,status=0,resizable=1");
	newwindow.focus();
}

// "handler_func" is the name of the function.
function asyncGet(url, handler_func, arg) {
	var req;
	
	try {
		req = new XMLHttpRequest();
	} catch (e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	}

	if (req) {
		var currentTime = new Date();
		req.open("GET", url + "?time=" + currentTime.getTime(), true);
		req.onreadystatechange = function() {
			try {
				if (req.readyState == 4 && req.status == 200) {
					var response = req.responseText;
					eval(handler_func + '(response, arg)');
				}
			} catch (e) {}
		}
		
		req.send(null);
	}
}

// similiar to asyncGet, but allows extra url parameteres to be appended on calling URL
function asyncGet(url, handler_func, arg, url_params) {
	var req;
	
	try {
		req = new XMLHttpRequest();
	} catch (e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	}

	if (req) {
		var currentTime = new Date();
		req.open("GET", url + "?time=" + currentTime.getTime() + "&" + url_params, true);
		req.onreadystatechange = function() {
			try {
				if (req.readyState == 4) {
					if(req.status == 200){
						var response = req.responseText;
						eval(handler_func + '(response, arg)');
					}else{
						if(arg && arg.staticURL && arg.stopCall != 'true'){
							arg.stopCall = 'true';//prevent infinited recursive
							asyncGet(arg.staticURL, handler_func, arg, url_params);
						}
					}
				}
			} catch (e) {}
		}
		
		req.send(null);
	}
}

/*
 * a timeout version of asyncGet, specified with $timeout in milliseconds, when it couldn't
 * get the dynamic content from the first ajax call, it will try to load the arg.staticURL if possible
 */
var req_arr = new Array();//xmlhttprequest objects
var abort_arr = new Array();//when request object to abort
var arg_arr = new Array();//arg sent for every request
var handler_func_arr = new Array();//handler for every request
function asyncGetWithTimeout(url, handler_func, arg, url_params, timeout) {
        var req;

        try {
                req = new XMLHttpRequest();
        } catch (e) {
                try {
                        req = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try {
                                req = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) { }
                }
        }

        if (req) {

                var req_index = req_arr.length;
                req_arr[req_index] = req;
                arg_arr[req_index] = arg;
                handler_func_arr[req_index] = handler_func;
                var currentTime = new Date();
                req.open("GET", url + "?time=" + currentTime.getTime() + "&" + url_params, true);
                req.onreadystatechange = function() {
                        if (req.readyState == 4) {
                                if(!abort_arr[req_index]){
                                clearTimeout(xhrTimeout);
                                if(req.status == 200){
                                        var response = req.responseText;
                                        eval(handler_func + '(response, arg)');
                                }else{
                                        if(arg && arg.staticURL && !arg.stopCall){
                                        //abnormal response 500 ex... check if there's an static url to get
                                                arg.stopCall = 'true';//prevent infinited recursive
                                                asyncGet(arg.staticURL, handler_func, arg, url_params);
                                        }
                                }
                                }
                        }
                }

                req.send(null);
                var xhrTimeout=setTimeout("ajaxTimeout("+req_index+");",timeout);
        }
}

function ajaxTimeout(req_index){
   abort_arr[req_index] = true;
   req_arr[req_index].abort();
   //alert(arg_arr[req_index].staticURL);
        if(arg_arr[req_index] && arg_arr[req_index].staticURL){
           asyncGet(arg_arr[req_index].staticURL, handler_func_arr[req_index], arg_arr[req_index], '');
        }
}

function createCookie(name,value,days) {
	createCookie(name,value,days,null)
}

function createCookie(name,value,days,domain) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	
	if(domain != null)
		document.cookie = name+"="+value+expires+"; path=/; domain=" + domain;
	else
		document.cookie = name+"="+value+expires+"; path=/";
		
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* depending on the current class of the button element, it will show/hide another element by changing its class */
function hideShowTbody(elementId, checkboxEl) {
	if (checkboxEl) {
		if (checkboxEl.checked) {
			document.getElementById(elementId).className = "tbody_show";
		} else {
			document.getElementById(elementId).className = "tbody_hide";
		}
	}
}

/* useful if you want to know the value of the checked radio button */
function getCheckedRadioValue(radioObj) {
	if (!radioObj) {
		return "";
	}
	
	var radioLength = radioObj.length;
	
	if (radioLength == undefined) {
		if (radioObj.checked) {
			return radioObj.value;
		} else {
			return "";
		}
	}
	
	for (var i = 0; i < radioLength; i++) {
		if (radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	
	return "";	
}


	function asyncPost(url, f, handler_func, arg) {
		var req;
		
		try {
			req = new XMLHttpRequest();
		} catch (e) {
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) { }
			}
		}
		
		if (req) {
			var currentTime = new Date();
			var formValues = getFormValues(f) + "&time="+currentTime.getTime();
			req.open("POST", url, true);
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					//var response = req.responseText.replace(/^\s+|\s+$/g, '');
					var response = req.responseText;
					eval(handler_func + '(response, arg)');
				}
			}
			
			req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length", formValues.length);
			req.setRequestHeader("Connection", "close");
			req.send(formValues);
		}
	}

	// MOVED - START USING THE VERSION IN:
	// form.js - get_form_values()
	function getFormValues(fobj)
	{
	   var str = "";
	   for(var i = 0; i < fobj.elements.length;i++)
	   {
	   
		   switch(fobj.elements[i].type)
	       {
	       		
	       
	           case "text":
	                str += fobj.elements[i].name +
	                 "=" + escape(fobj.elements[i].value) + "&";
	                 break;
	           case "hidden":
	                str += fobj.elements[i].name +
	                 "=" + escape(fobj.elements[i].value) + "&";
	                 break;      
	           case "button":
	                str += fobj.elements[i].name +
	                 "=" + escape(fobj.elements[i].value) + "&";
	                 break;      
	           case "select-one":
	                str += fobj.elements[i].name +
	                "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
	                break;
	           case "radio":
	           		if(fobj.elements[i].checked) {
		           		str += fobj.elements[i].name +
	    	             "=" + escape(fobj.elements[i].value) + "&";
	    	        }
	        	    break;
	           case "textarea":
	           		str += fobj.elements[i].name +
	                 "=" + escape(fobj.elements[i].value) + "&";
	                 break;
	           case "password":
		             str += fobj.elements[i].name +
	                 "=" + escape(fobj.elements[i].value) + "&";
	                 break;
	            case "checkbox":
	            	if(fobj.elements[i].checked) {
		            	str += fobj.elements[i].name +
	    	             "=" + escape(fobj.elements[i].value) + "&";
	            	}
	            	break;
	       }
	   }
	   str = str.substr(0,(str.length - 1));
	   return str;
	}

	// MOVED - START USING THE VERSION IN:
	// form.js - add_hidden_field()
	function addHiddenField(elementID, fieldName, fieldValue) {
		
		var element = document.getElementById(elementID);
		element.innerHTML = "<input type=\"hidden\" name=\"" + fieldName + "\" value=\"" + fieldValue + "\" />";
	
	}

	/* Place a product, based on product_id,  into session as a recently viewed item */
	function recentlyViewedProduct(context, product_id)
	{
		var url = context + "/servlet/RecentlyViewedServlet";
		var queryString = "ref=PRODUCT&max_number_products=2&product_id=" + product_id;
		asyncGet(url, "", "", queryString);	
	}
	
	/* Get products from session as a recently viewed item */
	function getRecentlyViewedProduct(context, element_id)
	{
		var queryString = context + "/templates/most_recently_viewed_handler.jsp?timeStamp=" + new Date().getTime();
		xmlreqGET(queryString, element_id, 'fill');	
	}


	/*
		arg is a json element ({'elementId': 'co'})
		By default, it will look into the JSON value for variable "elementId" and set response as element innerHTML 
	*/
	function copyToElementInnerHtml(response, arg) {
		
		document.getElementById(arg.elementId).innerHTML= response;

	}
	
	function showElement(d) {
		
		var b = document.getElementById(d);
		b.style.display = "block";
		
	}
	
	function hideElement(d) {
		
		if(d.length < 1) { return; }
		   document.getElementById(d).style.display = "none";
	}
		
	function swap_image(element_name, new_image_src) {
		if (document.images) {
			var imageElement = eval('document.images.' + element_name);
			if(imageElement)
				imageElement.src = new_image_src;
		}
			
	}
	
	function change_mouseover_image(mouseover_element_name, mouseover_element_name_that_change, mouseover_image, additionalAttributes) {
	
		if (document.images) {
			var mouseoverElement = eval('document.images.' + mouseover_element_name);
			if(mouseoverElement)
				mouseoverElement.onmouseover =function() {swap_image(mouseover_element_name_that_change, mouseover_image); eval(additionalAttributes)};
		}
	}
	
	/* Similiar to change_mouseover_image, but accounts of IE6 browsers */
	function change_mouseover_image_ie6(mouseover_element_name, mouseover_element_name_that_change, mouseover_image, additionalAttributes, mouseover_element_name_that_change_id) {
	
		if (document.images) {
			var mouseoverElement = eval('document.images.' + mouseover_element_name);
			if(mouseoverElement){
				var arVersion = navigator.appVersion.split("MSIE")
				var version = parseFloat(arVersion[1])
	
				if ((version >= 5.5) && (document.body.filters)){
					mouseoverElement.onmouseover =function() {document.getElementById(mouseover_element_name_that_change_id).src = mouseover_image; pngfixerproduct(document.getElementById('main_view'), '', '234', '282', ''); eval(additionalAttributes); };
				}else{
					mouseoverElement.onmouseover =function() {swap_image(mouseover_element_name_that_change, mouseover_image); eval(additionalAttributes)};
				}	
				
			}
		}
	}
	
	function change_mouseover_image_ie6_v2(mouseover_element_name, mouseover_element_name_that_change, color_tag, additionalAttributes, mouseover_element_name_that_change_id) {
                var mouseover_image = mainViewImages[color_tag];
                //alert(color_tag);
                //dumpProps(mainViewImages);
                if (document.images) {
                        var mouseoverElement = eval('document.images.' + mouseover_element_name);
                        if(mouseoverElement){
                                var arVersion = navigator.appVersion.split("MSIE")
                                var version = parseFloat(arVersion[1])

                                if ((version >= 5.5) && (version <= 6) && (document.body.filters)){
                                        mouseoverElement.onclick =function() {
                                                var mouseover_image = mainViewImages[color_tag];
                                                document.getElementById(mouseover_element_name_that_change_id).src = mouseover_image; pngfixerproduct(document.getElementById('main_view'), '', '234', '282', ''); eval(additionalAttributes); 

                                                change_mouseover_image_ie6_v2_common(mouseover_element_name, mouseover_image);        
                                        };
                                                
                                }else{
                                        mouseoverElement.onclick =function() {
                                                var mouseover_image = mainViewImages[color_tag];
                                                swap_image(mouseover_element_name_that_change, mouseover_image); eval(additionalAttributes)
                                                
                                                change_mouseover_image_ie6_v2_common(mouseover_element_name, mouseover_image);
                                        };      
                                                
                                }

                        }
                }
        }
	
	function change_mouseover_image_ie6_v2_common(mouseover_element_name, mouseover_image){
        //alert(zoomifyObj);
        if(zoomifyObj){
        	//alert(mouseover_element_name);
        	zoomifyObj.updateImageByView(mouseover_element_name, mouseover_image);
        	switchView(mouseover_element_name);
        }


		if(selectedSkuInfo){
        	selectedSkuInfo.mainImage = mouseover_image;
		}
		
		if(mouseover_element_name == 'view1'){
			pageTracker._trackEvent('ProductPage', 'View', 'Top');
		}else if(mouseover_element_name == 'view2'){
			pageTracker._trackEvent('ProductPage', 'View', 'Angle');
		}else if(mouseover_element_name == 'view3'){
			pageTracker._trackEvent('ProductPage', 'View', 'Side1');
		}else if(mouseover_element_name == 'view4'){
			pageTracker._trackEvent('ProductPage', 'View', 'Side2');
		}
	}
	
	function swap_image_with_mouseover(thumbname_element_name, thumbname_image, mouseover_element_name, mouseover_image) {
		if (document.images) {
			if(thumbname_element_name.length > 0)
				var imageElement = eval('document.images.' + thumbname_element_name);
			if(imageElement)
				imageElement.src = thumbname_image;
			
			imageElement.onmouseover =function() {swap_image(mouseover_element_name, mouseover_image);};
		}
	}
	
	function isCookieEnabled(){
		var tmpcookie = new Date();
	    chkcookie = (tmpcookie.getTime() + '');
	    document.cookie = "chkcookie=" + chkcookie + "; path=/";
	    if (document.cookie.indexOf(chkcookie,0) < 0) {
	      return false;
	      }
	    else {
	      return true;
	    }
		
	}

/**
*	print all the properties inside a javascript object
*
**/
function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to
      // build the message. Message includes i (the object's property name)
      // then the object's property value on a new line
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they
      // click "CANCEL" then quit this level of recursion
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object
      if (typeof obj[i] == "object") {
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
} 

//limits the textarea to a specific length
//ex. onkeyup="imposeMaxLength(this, 256);"
function imposeMaxLength(Object, MaxLen)
{
  if(Object.value.length > MaxLen)
	Object.value=Object.value.substring(0,MaxLen);
}

//use along with asyncGet methods. This method simply takes an AJAX response and puts it into an element specificed in the arg 
// ex. asyncGet('${pageContext.request.contextPath}/product/ajax/size_select.jsp', 'handlePopulateElementWithResponse', {'elementId': 'size_select'}, 'color=' + colorChoiceValueIdVar + '&product_id=${product.key}');
function handlePopulateElementWithResponse(response, arg){
	var item = document.getElementById(arg.elementId);
	item.innerHTML = response;				
}


//duration in milli seconds
function SlideShow(slideImages, targetImage, duration){
	this.active = true;
	this.slideTime = duration;
	this.currentIndex = 0;
	this.slideObjs = slideImages;
	
	var obj = this;
	
	this.start = function(){
		this.active = true;
		showImage();
	}

	this.stop = function(){
		this.active = false;
	}

	var showImage = function(){
		//document.getElementById('slide_show').innerHTML = obj.slideObjs[obj.currentIndex];
		document.getElementById(targetImage).src = obj.slideObjs[obj.currentIndex];
		obj.currentIndex = (obj.currentIndex+1) % obj.slideObjs.length;

		if(obj.active == true){
			setTimeout(showImage, obj.slideTime);
		}
	}
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
