function logffAlert(to_type,CID){
	if(CID != "" ){
		if(to_type.indexOf('to_designer') != -1){
			var flag = confirm("To leave Customer Mode, press OK. Otherwise, hit cancel.");
			if(flag) window.location = "logoff.php?type=customer";
			else return;
		}
		if(to_type.indexOf('to_normal') != -1){
			var flag = confirm("To leave Designer Mode, press OK. Otherwise, hit cancel.");
			if(flag) window.location = "logoff.php?type=designer";
			else return;
		}
	}else{
		if(to_type.indexOf('to_designer') != -1) window.location = "login.php?l=d";
		else window.location = "login.php";
	}
}



function validatePassword(){

	var error = false;
	if(document.getElementById('oldpassword').value==""){
		alert("Please enter old password");
		document.getElementById('oldpassword').focus();
		error = true;
		return;
	}
	
	if(document.getElementById('newpassword').value==""){
		alert("Please enter new password");
		document.getElementById('oldpassword').focus();
		error = true;
		return;
	}
	
	if(document.getElementById('confirmpassword').value==""){
		alert("Please enter confirm password");
		document.getElementById('confirmpassword').focus();
		error = true;
		return;
	}
	
	if(document.getElementById('newpassword').value!=document.getElementById('confirmpassword').value){
		alert("New password is not matching with confirm password");
		document.getElementById('confirmpassword').focus();
		error = true;
		return;
	}
	
	if(error==false){
		document.reset_password.submit();
	}
}


function popupWindow(url) 
{
	window.open(url,'popupWindow','width=600,height=400')
}

function update_additional_and_total_cost(name, quantity, unitprice)
{
	var total_cost ;
	var additional_total_cost;
	
	if ( isNaN(quantity) || ( quantity < 0 ))
	{
		alert('Only positive numeric value is acceptable for quantity.');
		return false;
	}

	total_cost = (quantity * unitprice);
	
	switch (name)
	{
		case 'wax_quantity':
			document.cart_quantity.wax_total_cost.value = total_cost;
			break;
		
		case 'resin_quantity':
			document.cart_quantity.resin_total_cost.value = total_cost;
			break;
			
		case 'copper_quantity':
			document.cart_quantity.copper_total_cost.value = total_cost;
			break;
	}
	
	document.cart_quantity.additional_total_cost.value =  parseInt(document.cart_quantity.additional_total_cost.value) + parseInt(document.cart_quantity.wax_total_cost.value) +parseInt(document.cart_quantity.resin_total_cost.value) + parseInt(document.cart_quantity.copper_total_cost.value);
	
	return false;
}

function update_price(downloadable_design_unit_price, wax_model_unit_price, resin_model_unit_price, copper_model_unit_price, quantity)
{
	if ( quantity == "" || isNaN(quantity) || ( quantity < 0 ))
	{
		document.cart_quantity.wax_quantity.value = "0";
		//alert('Only positive numeric value is acceptable for quantity.');
		//return false;
	}
	
	var wax_quantity, resin_quantity, copper_quantity;
	
	// getting the quantity for each
	wax_quantity = document.cart_quantity.wax_quantity.value;
	resin_quantity = 0;
	copper_quantity = 0;
	
	// setting the cost for wax, resin and copper
	document.cart_quantity.wax_total_cost.value = (parseInt(wax_quantity) * parseInt(wax_model_unit_price)).toFixed(2);
	//document.cart_quantity.resin_total_cost.value = parseInt(resin_quantity) * parseInt(resin_model_unit_price)
	//document.cart_quantity.copper_total_cost.value = parseInt(copper_quantity) * parseInt(copper_model_unit_price)
	
	// total cost
	document.cart_quantity.additional_total_cost.value =  (parseInt(downloadable_design_unit_price) + parseInt(document.cart_quantity.wax_total_cost.value)).toFixed(2);
	
	//document.cart_quantity.additional_total_cost.value =  parseInt(downloadable_design_unit_price) + parseInt(document.cart_quantity.wax_total_cost.value) + parseInt(document.cart_quantity.resin_total_cost.value) + parseInt(document.cart_quantity.copper_total_cost.value);
	
	return true;
}


function displayImage(big_image, small_image){
	
	
	
		document.getElementById('products_image').innerHTML = "<img src= '" + big_image + "' width='483' height='353' >";
	 	
	 	if(small_image == 'product_image_small'){
		 	document.getElementById('product_image_small').style.backgroundColor ="#CCCCCC";
		 	document.getElementById('product_image_small_1').style.backgroundColor ="#FFFFFF";
		 	document.getElementById('product_image_small_2').style.backgroundColor ="#FFFFFF";
		 	document.getElementById('product_image_small_3').style.backgroundColor ="#FFFFFF";
	 	}else if(small_image == 'product_image_small_1'){
	 		document.getElementById('product_image_small').style.backgroundColor ="#FFFFFF";
		 	document.getElementById('product_image_small_1').style.backgroundColor ="#CCCCCC";
		 	document.getElementById('product_image_small_2').style.backgroundColor ="#FFFFFF";
		 	document.getElementById('product_image_small_3').style.backgroundColor ="#FFFFFF";
	 	}else if(small_image == 'product_image_small_2'){
	 		document.getElementById('product_image_small').style.backgroundColor ="#FFFFFF";
		 	document.getElementById('product_image_small_1').style.backgroundColor ="#FFFFFF";
		 	document.getElementById('product_image_small_2').style.backgroundColor ="#CCCCCC";
		 	document.getElementById('product_image_small_3').style.backgroundColor ="#FFFFFF";
	 	}else if(small_image == 'product_image_small_3'){
	 		document.getElementById('product_image_small').style.backgroundColor ="#FFFFFF";
		 	document.getElementById('product_image_small_1').style.backgroundColor ="#FFFFFF";
		 	document.getElementById('product_image_small_2').style.backgroundColor ="#FFFFFF";
		 	document.getElementById('product_image_small_3').style.backgroundColor ="#CCCCCC";
	 	}
	
}

// to reset the fields
// takes the form name
function reset_address(form_name)
{
	form_name.elements['firstname'].value = '';
	form_name.elements['lastname'].value = '';
	form_name.elements['phone'].value = '';
	form_name.elements['address'].value = '';
	form_name.elements['city'].value = '';
	form_name.elements['zipcode'].value = '';
}

// function to reset the fields on the shipping info page
function reset_shipping_address()
{
	document.checkout_shipping.firstname.value = '';
	document.checkout_shipping.lastname.value = '';
	document.checkout_shipping.phone.value = '';
	document.checkout_shipping.address.value = '';
	document.checkout_shipping.city.value = '';
	document.checkout_shipping.state.value = '';
	document.checkout_shipping.country.value = '';
	document.checkout_shipping.zipcode.value = '';
}
		
function validate_checkout_shipping_page()
{
	error = false;
	error_message = "";
	var firstname = document.checkout_shipping.firstname.value;
	var lastname = document.checkout_shipping.lastname.value;
	var phone = document.checkout_shipping.phone.value;
	var address = document.checkout_shipping.address.value;
	var city = document.checkout_shipping.city.value;
	var state = document.checkout_shipping.state.value;
	var country = document.checkout_shipping.country.value;
	var zipcode = document.checkout_shipping.zipcode.value;
	
	if (firstname == '')
	{
		error = true;
		error_message += "* Please enter first name." + "\n";
	}
	
	if (lastname == '')
	{
		error = true;
		error_message += "* Please enter last name." + "\n";
	}
	
	if (phone == '')
	{
		error = true;
		error_message += "* Please enter phone number." + "\n";
	}
	
	if (address == '')
	{
		error = true;
		error_message += "* Please enter address." + "\n";
	}
	
	if (city == '')
	{
		error = true;
		error_message += "* Please enter city." + "\n";
	}
	
	if (state == '')
	{
		error = true;
		error_message += "* Please enter state." + "\n";
	}
	
	if (country == '')
	{
		error = true;
		error_message += "* Please enter country." + "\n";
	}
	
	if (zipcode == '')
	{
		error = true;
		error_message += "* Please enter zipcode." + "\n";
	}
	
	
	if (error)
	{
		alert(error_message);
		return false;
	}
}

var selected;
function selectRowEffect(object, buttonSelect) 
{
	if (!selected) 
	{
		if (document.getElementById) 
		{
			selected = document.getElementById('defaultSelected');
		} 
		else 
		{
			selected = document.all['defaultSelected'];
		}
	}
	
	if (selected)
	{
		selected.className = 'moduleRow';
	}
		
	object.className = 'moduleRowSelected';
	selected = object;
	
	// one button is not an array
	if (document.checkout_shipping.shipping[0]) 
	{
		document.checkout_shipping.shipping[buttonSelect].checked=true;
	} 
	else 
	{
		document.checkout_shipping.shipping.checked=true;
	}
}

function rowOverEffect(object) 
{
	if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) 
{
	if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

// function to validate shipping option
function validate_shipping_option()
{
	elements = document.getElementsByTagName('input');
	
	for (var i=0; i<elements.length; i++ )
	{
		var currentElement = elements[i];
		
		if (currentElement.type == 'radio' && currentElement.checked)
		{
			return true;
		}
	}
	
	return false;
}

// validate the billing information
// takes form name
function validate_billing_information(form_name)
{
	var error_message = '';
	
	error_message += util_check_input(form_name, 'firstname', "* Please enter first name.\n");
	error_message += util_check_input(form_name, 'lastname', "* Please enter last name.\n");
	error_message += util_check_input(form_name, 'city', "* Please enter city.\n");
	error_message += util_check_input(form_name, 'phone', "* Please enter phone.\n");
	error_message += util_check_input(form_name, 'address', "* Please enter address.\n");
	error_message += util_check_input(form_name, 'country', "* Please enter country.\n");
	error_message += util_check_input(form_name, 'state', "* Please enter state.\n");
	error_message += util_check_input(form_name, 'zipcode', "* Please enter zip code.\n");
	
	if (error_message)
	{
		alert(error_message);
		return false;
	}
	
	return true;
}

// to validate the credit card information
// takes form name, year & month to validate the credit card expiry date
function validate_credit_card_information(form_name, year, month)
{
	error_message = '';
	error_message += util_check_input(form_name, "beanstream_usd_cc_owner", "* Please enter credit card owner.\n");
	error_message += util_check_input(form_name, "beanstream_usd_cc_number", "* Please enter credit card number.\n");
	error_message += util_check_input(form_name, "beanstream_usd_cvv_number", "* Please enter credit card cvv number.\n");
	error_message += util_check_input(form_name, "beanstream_usd_cc_expires_year", "* Please enter credit card expiry year.\n");
	
	if( ( '20'+form_name.elements['beanstream_usd_cc_expires_year'].value == year ) && ( form_name.elements['beanstream_usd_cc_expires_month'].value < month ) )
	{
		error_message += '* Please select valid credit card expiry date' + "\n";
	}
	
	if (error_message)
	{
		alert(error_message);
		return false;
	}
	
	document.getElementById("confirm_order").disabled = true;
	return true;
}

// an util function to validate the input fileds
function util_check_input(form_name, field_name, message)
{
	if ( ( form_name.elements[field_name] ) && ( form_name.elements[field_name].type != "hidden" ) && ( form_name.elements[field_name].value=='' ) )
	{
		return message;
	}
	
	return '';
}


// loads the states on the basis of the country
// if country is US or Canada then loads the states in a select box other simply put a text box to key in the state
// if get_order_information is true then
// * if the country is Canada then sets a js function call on on changeevent of the states selection box
// * if country is not canada then it simply calls get_order_information_markup() to chnage the order total markup
function changeStateField(base_path, get_order_information, order_total)
{
	var country_id = document.getElementById('country').value;
	var make_ajax_call_for_order_info = true;
		
	if(window.XMLHttpRequest)
	{
        	var http = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		var http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var LOADSTATE = base_path + "load_states_for_country.php?country_id=" + country_id;
	
	// get_oder_information decides whehter we need to do the processing for the oreder information or not
	// if country_id is 38 i.e canada then we need to load the order information on the basis of the state
	// so if the country id is 38 then ajax call will put an another js method call at onchange event of the state select box
	if ( get_order_information && country_id == 38 )
	{
		make_ajax_call_for_order_info = false;
		LOADSTATE += "&get_order_information=1&order_total=" + order_total;
	}
	
	http.open("GET", LOADSTATE, true); 
      
	http.onreadystatechange = function()
    	{
    		if( http.readyState == 4 ) 
    		{
			if( http.status == 200 ) 
			{ 
				document.getElementById('state_block').innerHTML = "";
				document.getElementById('state_block').innerHTML = http.responseText;
			}
			else
			{
				alert(http.status + "===" + http.statusText + " There seems some problem in this process, Please try again or contact to support.");
			}
    		}
    	 }
    	 
    	 http.send(null);
    	 
    	 // if order information is required and make_ajax_call_for_order_info is true then call get_order_information_markup()
    	 if ( get_order_information && make_ajax_call_for_order_info )
    	 {
    	 	get_order_information_markup(base_path, order_total);
    	 }
}


// to set the order information markup
function get_order_information_markup(base_path, total)
{
	var request_url = '';
	var country_id = '';
	var state_name = '';
	
	country_id = document.getElementById('country').value;
	state_name = document.getElementById('state').value;
	request_url = base_path + "get_order_information_markup.php?country_id=" + country_id + "&state_name=" + state_name + "&total=" + total;
	
	if(window.XMLHttpRequest)
	{
        	var http = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		var http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	http.open("GET", request_url, true); 
      
	http.onreadystatechange = function()
    	{
    		if( http.readyState == 4 ) 
    		{
			if( http.status == 200 ) 
			{ 
				document.getElementById('order_information').innerHTML = "";
				document.getElementById('order_information').innerHTML = http.responseText;
			}
			else
			{
				alert(http.status + "===" + http.statusText + " There seems some problem in this process, Please try again or contact to support.");
			}
    		}
    	 }
    	 
    	 http.send(null);
}

function mouse_handler(obj) {
	img = (obj.src).split(".");
			
	var image_path = "";
	for (var j = 0; j < img.length - 1; j++) {
		image_path += img[j];
		
		if (j < img.length - 2) {
			image_path += ".";
		}
	}
	
	/*
	 * Preload images
	 */
	preload_image_object = new Image();
	// set image url
	image_url = new Array();
	image_url[0] = image_path + '_p.' + img[img.length - 1];
	image_url[1] = image_path + '.' + img[img.length - 1];
	
	var i = 0;
	for(i=0; i<=image_url.length; i++) {
           if ( image_url[i] != null)
		preload_image_object.src = image_url[i];
	// End of preloading
	}
	obj.onmouseover = function(){
		this.src = image_path + '_p.' + img[img.length - 1];
	};
	
	obj.onmouseout = function(){
		this.src = image_path + '.' + img[img.length - 1];
	};
	
	/*
obj.onmousedown = function(){
		this.src = image_path + '.' + img[img.length - 1];
	};
	
	obj.onmouseup = function(){
		this.src = image_path + '_p.' + img[img.length - 1];
	};
*/
}

window.onload = function()
{
	var imgs = document.getElementsByTagName("img");

	for (var i = 0; i < imgs.length; i++) {
		if (imgs[i].className.match("button") != null) {
			mouse_handler(imgs[i]);
		}
	}
};

