var infowin = null;

function newPopWin(url,w,h)
{
	winLeft = (screen.width-w)/2;
	winTop = (screen.height-(h+110))/2; 

	infowin = window.open("","","toolbar=no,location=no,scrollbars=yes,resizable=yes,menubar=no,top=" + winTop + ",left=" + winLeft + ",width=" + w + ",height=" + h);
  	if (infowin != null)
  	{
    	infowin.location.href = url;
		// is the window.focus method supported by this verison of javascript
		// if so, then do it.
    	if (window.focus) {
       		infowin.focus();
    	}
  	}
// return true;
}

function newPrintFriendly(url,w,h)
{
	winLeft = (screen.width-w)/2;
	winTop = (screen.height-(h+110))/2; 

	infowin = window.open("","","toolbar=yes,location=yes,scrollbars=yes,resizable=yes,menubar=yes,top=" + winTop + ",left=" + winLeft + ",width=" + w + ",height=" + h);
  	if (infowin != null)
  	{
    	infowin.location.href = url;
		// is the window.focus method supported by this verison of javascript
		// if so, then do it.
    	if (window.focus) {
       		infowin.focus();
    	}
  	}
// return true;
}

function closePopWin()
{
  if (infowin != null && infowin.open) infowin.close();
// return true;
} 

//submit the checkout form verify if both the order type radio and the
//agreement checkbox were selected. Error message are from the languages
function submitCheckout(message_order_type, order_type, abb_price, cod_price, message_shipping)
{
	var show_message = '';
	//check if the order type was selected
	if (document.checkout.order_type[0].checked == false && 
		document.checkout.order_type[1].checked == false) {
		show_message = message_order_type;
	}
	//check if the shipping cost is 0
	if ( (order_type = 3 && !(abb_price > 0) ) ||
		 (order_type = 1 && !(cod_price > 0) ) ) {
		show_message = message_shipping; 
	}
	//validate submit
	if (show_message.length == 0) {
		//document.location.href="checkout_2.php";
		document.UpdateForm.move_to_checkout_2.value="yes";
		document.UpdateForm.submit();
	} else {
		alert(show_message);
	}
}
//submit the 2 step
function submitCheckoutUserRegistration(action_alternative, message, use_alt, valid_alt, shipping_message, isValidShippingCountry, isValidShippingAlternativeCountry)
{
	//address_country1 - normal registration
	//address_country2 - different shipping address
	alt       = document.alternative_address;
	check     = document.checkout_2;
	logged_in = document.logged_in_alternative;
	country1  = document.getElementById("address_country1");
	country2  = document.getElementById("address_country2");
	
	if (message.length > 0 && use_alt && !valid_alt) {
		//check if at least one field for alternative address is filled
		//alt.person_title3.value.length > 0
		if ( document.alternative_address && 
				( alt.address_city2.value.length > 0 			|| alt.person_name3.value.length > 0 		||
				  alt.person_prefix3.value.length > 0 			|| alt.person_surname3.value.length > 0 	||
				  alt.address_street2.value.length > 0 			|| alt.address_house_nr2.value.length > 0 	||
				  alt.address_house_nr_suffix2.value.length > 0 || alt.address_zip2.value.length > 0 		
				  
				) 
			) 
		{
			//if all mandatory fields are filled then checkout the form
			if ( alt.person_name3.value.length > 0 		&&  alt.person_surname3.value.length   > 0 && 
				 alt.address_street2.value.length > 0 	&&	alt.address_house_nr2.value.length > 0 &&
				 alt.address_zip2.value.length > 0 		&&  alt.address_city2.value.length     > 0
			) {
				//if the register form is valid then set the register form hidden fields
				if (check) {
					check.person_title3.value  			 = alt.person_title3.value;
					check.person_name3.value  			 = alt.person_name3.value;
					check.person_prefix3.value 			 = alt.person_prefix3.value;
					check.person_surname3.value  		 = alt.person_surname3.value;
					check.address_street2.value  		 = alt.address_street2.value;
					check.address_house_nr2.value 		 = alt.address_house_nr2.value;
					check.address_house_nr_suffix2.value = alt.address_house_nr_suffix2.value;
					check.address_zip2.value	 	 	 = alt.address_zip2.value;
					check.address_city2.value    		 = alt.address_city2.value;
					check.address_country2.value    	 = alt.address_country2.options[alt.address_country2.selectedIndex].text;
					check.alternative_complete.value     = "yes";
				//set the logged_in_alternative from hidden fiels
				} else {
					logged_in.person_title3.value  			 = alt.person_title3.value;
					logged_in.person_name3.value  			 = alt.person_name3.value;
					logged_in.person_prefix3.value 			 = alt.person_prefix3.value;
					logged_in.person_surname3.value  		 = alt.person_surname3.value;
					logged_in.address_street2.value  		 = alt.address_street2.value;
					logged_in.address_house_nr2.value 		 = alt.address_house_nr2.value;
					logged_in.address_house_nr_suffix2.value = alt.address_house_nr_suffix2.value;
					logged_in.address_zip2.value	 	 	 = alt.address_zip2.value;
					logged_in.address_city2.value    		 = alt.address_city2.value;
					logged_in.address_country2.value   		 = alt.address_country2.options[alt.address_country2.selectedIndex].text;
									
				}
			} else {
				alert (message);
				return;
			}
			if (logged_in ) {
				logged_in.logged_in_alternative_validate_form.value     = "yes";
			}
		}
		//alert("lululala");
	}
	if (check) {
		check.to_save.value = "yes";
		check.action_alternative.value = action_alternative;
		if ( (action_alternative.length == 0 && !use_alt && country1 && country1.options && country1.options[country1.selectedIndex].label == 0) ||
				(use_alt && country2 && country2.options && country2.options[country2.selectedIndex].label == 0)
		    ) {
			//alert(action_alternative.length);    action_alternative.length == 0 && 
			alert(shipping_message);
			//alert("lll");
			return;
		}
		//alert("lulu");
		check.submit();
	} else {
		//alert("lala");
		//submit the alternative address
		if (country2 && country2.options && country2.options[country2.selectedIndex].label == 0) {
			alert(shipping_message);
			return;
		}
		
		if (! isValidShippingCountry && !use_alt) {
			alert(shipping_message);
			return;
		}
		//document.location.href="checkout_3.php";
		logged_in.submit();
	}
}

function submitCheckoutUnregisteredUser(shipping_message, select_radio_message)
{
	//address_country1 - normal registration
	//address_country2 - different shipping address
	check     = document.checkout_unregistered_user;
	country1  = document.getElementById("address_country1");

	
	check.to_save.value = "yes";
	if (!document.checkout_unregistered_user.delivery_same_as_shipping[0].checked && 
		!document.checkout_unregistered_user.delivery_same_as_shipping[1].checked) {
		alert(select_radio_message);
		return;
	}
	/*
	if ( (country1 && country1.options && country1.options[country1.selectedIndex].label == 0) && 
   	      document.checkout_unregistered_user.delivery_same_as_shipping[0].checked ) {
		alert(shipping_message);
		return;
	}
	*/
	//alert(country1.options[country1.selectedIndex].value.substring(0,1) == '0');
	//alert(country1);
	//alert(country1.options);
	//alert(document.checkout_unregistered_user.test_hidden.value);
	if ( (country1 && country1.options && country1.options[country1.selectedIndex].value.substring(0,1) == '0') && 
   	      document.checkout_unregistered_user.select_alternative_value.value == 'yes' ) {
		alert(shipping_message);
		return;
	}
	/*
	if (document.checkout_unregistered_user.delivery_same_as_shipping[1].checked) {
		document.checkout_unregistered_user.use_alternative_address.value = "yes";
	}
	*/
	if (document.checkout_unregistered_user.select_alternative_value.value == 'no') {
		document.checkout_unregistered_user.use_alternative_address.value = "yes";
	}
	check.submit();
}

function submitCheckoutRegisteredUser(shipping_message, select_radio_message, isValidShippingCountry)
{
	if (!document.checkout_registered_user.delivery_same_as_shipping[0].checked && 
		!document.checkout_registered_user.delivery_same_as_shipping[1].checked) {
		alert(select_radio_message);
		return;
	}
	if (!isValidShippingCountry && document.checkout_registered_user.delivery_same_as_shipping[0].checked ) {
		alert(shipping_message);
		return;
	}
	if (document.checkout_registered_user.delivery_same_as_shipping[1].checked) {
		document.checkout_registered_user.use_alternative_address.value = "yes";
	}
	document.checkout_registered_user.submit();
}

function submitCheckoutAlternativeAddress(err_msg, use_alt, valid_alt)
{
	/*
	myForm = document.alternative_address;
	if (myForm.order_type) {
		if (myForm.order_type[0] && !use_alt && !myForm.order_type[0].checked && !myForm.order_type[1].checked) {
			alert(err_msg);
			return;
		} else if(myForm.order_type[0] && use_alt && valid_alt && !myForm.order_type[0].checked && !myForm.order_type[1].checked ) {
			alert(err_msg);
			return;
		}
		if (myForm.order_type[0] &&  myForm.order_type[1] && !myForm.order_type[0].checked || !myForm.order_type[1].checked) {
			document.alternative_address.alternative_to_save.value = "yes";	
		}
	}
	*/
	document.alternative_address.go_to_final_step.value = "yes";
	document.alternative_address.submit();
}

function changeCountry(valid_alt)
{
	//if (valid_alt) {
		//document.alternative_address.submit();
	//}
}

//change the order type
//when change the payment method also update the 
//product quantity
function changeOrderType(order_type)
{
	document.UpdateForm.change_payment.value="yes";
	document.UpdateForm.order_type.value=order_type;
	document.UpdateForm.submit();
	//submit the checkout form
	//document.checkout.submit();
}


function submitOrderSave(message)
{
	if (document.getElementById("your_refference_field")) {
		document.submit_order_save.your_refference.value = document.getElementById("your_refference_field").value;
		document.submit_order_save.PARAMPLUS.value = document.submit_order_save.PARAMPLUS.value + "&refference=" + document.getElementById("your_refference_field").value;
	}
	//if (!document.submit_order_save.agree.checked) {
	if (document.getElementById('agree') && !document.getElementById('agree').checked) {
		alert(message);
	} else {
		document.submit_order_save.submit();
	}
}

//submit the register form
function submitRegisterUser(cmbCountry, shippingMessage)
{
	document.register.to_save.value = "yes";
	if (cmbCountry.options[cmbCountry.selectedIndex].label == "0") {
		alert(shippingMessage);
	}
	//alert(cmbCountry.options[cmbCountry.selectedIndex].label);
	//normal user registration
	//document.register.submit();
	return true;
}

function changeAgreement()
{
	if (document.getElementById("your_refference_field")) {
		document.agree_form.your_refference.value = document.getElementById("your_refference_field").value;
	}
	document.agree_form.submit();
}

function submitCart(msg)
{
	type_1 = document.getElementById("order_type_1");
	type_3 = document.getElementById("order_type_3");
	type_6 = document.getElementById("order_type_6");
	type_7 = document.getElementById("order_type_7");
	if ( ( (type_1 && type_1.checked) || 
		   (type_3 && type_3.checked) ||
		   (type_6 && type_6.checked) ||
		   (type_7 && type_7.checked) ) && 
		  document.getElementById('country').options.length > 0) {
		document.UpdateForm.move_to_next_step.value="yes";
		document.UpdateForm.submit();
	} else {
		alert(msg);
	}
}

function validateShippingCosts(country)
{
	alert(document.getElementById(country).options[document.getElementById(country).selectedIndex].label);
	alert(document.getElementById(country).options[document.getElementById(country).selectedIndex].text);
	alert(document.getElementById(country).options[document.getElementById(country).selectedIndex].value);
	//alert(country);
}