﻿
function Cart_Calculate(sLocal, sPageId)
{
	sMsg = "";
	aInputs = jQuery("input:text");
	
	for (i = 0; i < aInputs.length; i++)
	{
		sName = jQuery(aInputs[i]).attr("name");
		
		aName = sName.split("_");
		
		if (aName.length == 3 && aName[1] == "QTY")
		{
			sValue = jQuery(aInputs[i]).attr("value");
			
			sMsg += aName[0] + "," + sValue + ";";
		}
	}

	sURL = G_BASEURL+sLocal+"/"+sPageId+"?items=" + sMsg;
	
	//alert(sURL);
	
	document.location.href = sURL;

}

function Cart_RemoveItems(sLocal, sPageId)
{
	sMsg = "";
	aChecks = jQuery("input:checked");

	for (i = 0; i < aChecks.length; i++)
	{
		sName = jQuery(aChecks[i]).attr("name");
		
		aName = sName.split("_");
		sMsg += aName[0] + ",";
	}
	
	sURL = G_BASEURL+sLocal+"/"+sPageId+"?items=" + sMsg;
	
	//alert(sURL);
	
	document.location.href = sURL;
}

function setHiddenTextSelection(sValue, sName)
{
	jQuery(":input[name="+sName+"]").val(sValue);
}

function displayOther(sValue, sName)
{
	if (sValue != 'CA' && sValue != 'US')
	{
		jQuery(":input[name="+sName+"]").css("display","inline");
	}
	else
	{
		jQuery(":input[name="+sName+"]").css("display","none");
	}
}


var G_SAB_ON = false;
var G_TIMER;

function validationTypeClient(iLang)
{
	var aMsgs = ["Please select the kind of customer you are.\n", "Veuillez choisir le type de clientèle que vous êtes.\n"];

	var sMsg = "";
	
	if (jQuery("input[type=radio]:checked").length == 0)
	{
		sMsg += aMsgs[iLang];
	}

	if (sMsg.length > 0)
	{
		alert(sMsg);
	}
	else
	{
		oForm = jQuery("#form-cart-type");
	
		oForm.submit();
	}
}

function backCartXSRF(sFormId, sURL)
{
	oForm = jQuery("#"+sFormId);
	
	oDir = jQuery("#"+sFormId+" input[name=DIRECTION]");
	
	jQuery(oForm).attr("action", sURL);
	
	jQuery(oDir).attr("value", "BACK");
	
	oForm.submit();
}

function copyInfo()
{
	var aInputs = jQuery("#form-cart-info :input[type=text]");

	for (i = 0; i < aInputs.length; i++)
	{
		sName = jQuery(aInputs).eq(i).attr("name");
		
		aName = sName.split("_");
		ssName= sName.substring(2, sName.length);
		
		if (aName[0] == "F")
		{
			switch (aName[1])
			{
				case "EMAIL1":
				case "EMAIL2":
				case "PHONE":
					break;
				default:
					jQuery("#form-cart-info :input[name=L_"+ssName+"]").val(jQuery("#form-cart-info :input[name=F_"+ssName+"]").val());
			}
		}
	}

	var aInputs = jQuery("#form-cart-info :input[type=hidden]");

	for (i = 0; i < aInputs.length; i++)
	{
		sName = jQuery(aInputs).eq(i).attr("name");
		
		aName = sName.split("_");
		ssName= sName.substring(2, sName.length);
		
		if (aName[0] == "F")
		{
			switch (aName[1])
			{
				case "EMAIL1":
				case "EMAIL2":
				case "PHONE":
					break;
				default:
					jQuery("#form-cart-info :input[name=L_"+ssName+"]").val(jQuery("#form-cart-info :input[name=F_"+ssName+"]").val());
			}
		}
	}

	aSelects = jQuery("#form-cart-info select");
	
	sFCountryCode = jQuery("#form-cart-info select[name=F_SEL_COUNTRY_CODE] option:selected").val();
	sFCountryText = jQuery("#form-cart-info select[name=F_SEL_COUNTRY_CODE] option:selected").text();
	
	handlerLCountrySelection2(sFCountryCode, sFCountryText);
	
	for (i = 0; i < aSelects.length; i++)
	{
		sName = jQuery(aSelects).eq(i).attr("name");
		
		aName = sName.split("_");
		ssName= sName.substring(2, sName.length);

		if (aName[0] == "F")
		{			
			jQuery("#form-cart-info select[name=L_"+ssName+"]").val(jQuery("#form-cart-info :select[name=F_"+ssName+"]").val());
		}

		//alert(jQuery("#form-cart-info select[name=L_"+ssName+"]").val());
	}
}

function sameAsBilling()
{
	if (!G_SAB_ON)
	{
		G_SAB_ON = true;
		G_TIMER = window.setInterval("copyInfo()", 250);
		//copyInfo();
	}
	else // G_SAB_OFF
	{
		G_SAB_ON = false;
		window.clearInterval(G_TIMER);
	}
}

function verification(oForm,iLang)
{
	var sMsg = "";

	var aFields = { "F_LNAME":		["- Please enter your Billing Last Name.\n","- Veuillez entrer votre Nom de famille de Facturation.\n"],
			"F_FNAME":		["- Please enter your Billing First Name.\n","- Veuillez entrer votre Prénom de Facturation.\n"],
			"F_COMPANY":		["- Please enter your Billing Company Name\n","- Veuillez entrer le Nom de l'entreprise de Facturation\n"],
			"F_ADDRESS1":		["- Please enter your Billing Address.\n","- Veuillez entrer votre Adresse de Facturation.\n"],
			"F_ADDRESS2":		["",""],
			"F_CITY":		["- Please enter your Billing City.\n","- Veuillez entrer votre Ville de Facturation.\n"],
			"F_SEL_REGION_CODE_CA":	["- Please select a Billing Province.\n","- Veuillez choisir une Province de Facturation.\n"],
			"F_SEL_REGION_CODE_US":	["- Please select a Billing State.\n","- Veuillez choisir un État pour la Facturation.\n"],
			"F_SEL_REGION_CODE_FR":	["- Please select a Billing Region.\n","- Veuillez choisir une Région de Facturation.\n"],
			"F_REGION_OTHER":	["- Please enter your Billing Province, State or Region.\n","- Veuillez entrer votre Province, État ou Région de Facturation.\n"],
			"F_POSTAL_CODE":	["- Please enter your Billing Postal Code.\n","- Veuillez entrer votre Code Postal de Facturation.\n"],
			"F_SEL_COUNTRY_CODE":	["- Please select a Billing Country.\n","- Veuillez choisir un Pays de Facturation.\n"],
			"F_COUNTRY_OTHER":	["- Please enter your Billing Country.\n","- Veuillez entrer votre Pays de Facturation.\n"],
			"F_EMAIL1":		["- Please enter your Email.\n","- Veuillez entrer votre Courriel.\n"],
			"F_EMAIL2":		["- Please enter your Confirmation Email.\n","- Veuillez entrer votre Courriel de confirmation.\n"],
			"F_PHONE":		["- Please enter your Phone Number.\n","- Veuillez entrer votre Numéro de Téléphone.\n"],

			"L_LNAME":		["- Please enter your Shipping Last Name.\n","- Veuillez entrer votre Nom de famille de Livraison.\n"],
			"L_FNAME":		["- Please enter your Shipping First Name.\n","- Veuillez entrer votre Prénom de Livraison.\n"],
			"L_COMPANY":		["- Please enter your Shipping Company Name\n","- Veuillez entrer le Nom de l'entreprise de Livraison\n"],
			"L_ADDRESS1":		["- Please enter your Shipping Address.\n","- Veuillez entrer votre Adresse de Livraison.\n"],
			"L_ADDRESS2":		["",""],
			"L_CITY":		["- Please enter your Shipping City.\n","- Veuillez entrer votre Ville de Livraison.\n"],
			"L_SEL_REGION_CODE_CA":	["- Please select a Shipping Province.\n","- Veuillez choisir une Province de Livraison.\n"],
			"L_SEL_REGION_CODE_US":	["- Please select a Shipping State.\n","- Veuillez choisir un État de Livraison.\n"],
			"L_SEL_REGION_CODE_FR":	["- Please select a Shipping Region.\n","- Veuillez choisir une Région de Livraison.\n"],
			"L_REGION_OTHER":	["- Please enter your Shipping Province, State or Region.\n","- Veuillez entrer votre Province, État ou Région de Livraison.\n"],
			"L_POSTAL_CODE":	["- Please enter your Shipping Postal Code.\n","- Veuillez entrer votre Code Postal de Livraison.\n"],
			"L_SEL_COUNTRY_CODE":	["- Please select a Shipping Country.\n","- Veuillez choisir un Pays de Livraison.\n"],
			"L_COUNTRY_OTHER":	["- Please enter your Shipping Country.\n","- Veuillez entrer votre Pays de Livraison.\n"]
			};
			
	var aEmail = ["- The Email fields must be identical.\n","- Les champs Courriel doivent être identiques.\n"];

	//alert( jQuery("#form-cart-info :input[type=text]").length );
	
	aSelects = jQuery("#form-cart-info select");

	aTextInputs = jQuery("#form-cart-info :input[type=text]");

	sF_SelCountCode = jQuery("#form-cart-info select[name=F_SEL_COUNTRY_CODE] option:selected").val();
	sL_SelCountCode = jQuery("#form-cart-info select[name=L_SEL_COUNTRY_CODE] option:selected").val();

	for (i = 0; i < aTextInputs.length; i++)
	{
		//alert(jQuery(aTextInputs).eq(i).attr("name"));
		
		if
		(
			jQuery(aTextInputs).eq(i).val() == ""
			&&
			jQuery(aTextInputs).eq(i).attr("name") != undefined
		)
		{
			switch (jQuery(aTextInputs).eq(i).attr("name"))
			{
				case "F_REGION_OTHER":
					if
					(
						sF_SelCountCode != 'CA'
						&&
						sF_SelCountCode != 'US'
						&&
						sF_SelCountCode != 'FR'
					)
					{
						sMsg += eval("aFields."+jQuery(aTextInputs).eq(i).attr("name")+"["+iLang+"]");
					}
					break;
				case "L_REGION_OTHER":
					if
					(
						sL_SelCountCode != 'CA'
						&&
						sL_SelCountCode != 'US'
						&&
						sL_SelCountCode != 'FR'
					)
					{
						sMsg += eval("aFields."+jQuery(aTextInputs).eq(i).attr("name")+"["+iLang+"]");
					}
					break;
				default:
					sMsg += eval("aFields."+jQuery(aTextInputs).eq(i).attr("name")+"["+iLang+"]");
			}
		}
	}
	
	if (jQuery("#form-cart-info :input[name=F_EMAIL1]").val() != jQuery("#form-cart-info :input[name=F_EMAIL2]").val())
	{
		sMsg += aEmail[iLang];
	}
	
	for (j = 0; j < aSelects.length; j++)
	{
		sSelVal = jQuery("#form-cart-info select[name="+jQuery(aSelects).eq(j).attr("name")+"] option:selected").val();
	
		switch (jQuery(aSelects).eq(j).attr("name"))
		{
			case "F_SEL_REGION_CODE_CA":
				if (sF_SelCountCode == 'CA' && sSelVal == 'NULL')
				{
					sMsg += eval("aFields."+jQuery(aSelects).eq(j).attr("name")+"["+iLang+"]");
				}
				break;
			case "F_SEL_REGION_CODE_US":
				if (sF_SelCountCode == 'US' && sSelVal == 'NULL')
				{
					sMsg += eval("aFields."+jQuery(aSelects).eq(j).attr("name")+"["+iLang+"]");
				}
				break;
			case "F_SEL_REGION_CODE_FR":
				if (sF_SelCountCode == 'FR' && sSelVal == 'NULL')
				{
					sMsg += eval("aFields."+jQuery(aSelects).eq(j).attr("name")+"["+iLang+"]");
				}
				break;
			case "L_SEL_REGION_CODE_CA":
				if (sL_SelCountCode == 'CA' && sSelVal == 'NULL')
				{
					sMsg += eval("aFields."+jQuery(aSelects).eq(j).attr("name")+"["+iLang+"]");
				}
				break;
			case "L_SEL_REGION_CODE_US":
				if (sL_SelCountCode == 'US' && sSelVal == 'NULL')
				{
					sMsg += eval("aFields."+jQuery(aSelects).eq(j).attr("name")+"["+iLang+"]");
				}
				break;
			case "L_SEL_REGION_CODE_FR":
				if (sL_SelCountCode == 'FR' && sSelVal == 'NULL')
				{
					sMsg += eval("aFields."+jQuery(aSelects).eq(j).attr("name")+"["+iLang+"]");
				}
				break;
			case "F_NULL_SEL_STATE":
			case "L_NULL_SEL_STATE":
				break;
			default:
				if (sSelVal == "null")
				{
					sMsg += eval("aFields."+jQuery(aSelects).eq(j).attr("name")+"["+iLang+"]");
				}
		}
	
	}
	
	
	if (sMsg.length != 0)
	{
		alert(sMsg);
	}
	else
	{
		oForm = jQuery("#form-cart-info");
		oForm.submit();
		//alert("(c) Spytronic Inc.");
	}
}

function resetBG()
{
	jQuery("#table-accountable table").each( function () {
		jQuery(this).css("background-image","url('../images/gris-opacity50.png')");
	});
}

function chgBG(sId)
{
	resetBG();
	
	jQuery("#"+sId).css("background-image","url('../images/opacity50.png')");
}

function makePriceStr(iCur, iValue)
{
	fPrice = iValue.toFixed(2);

	switch (iCur)
	{
		case 1:
			return fPrice+"$ USD";
		case 0:
			return fPrice+"$ CAD";
	}
}

function calculateShippingMethod(sId, fSubTotal, iLang, iCur, sCountry, sRegion, iClientType)
{
	aMsg = new Array("- Your order must be equal or above 500$ to be eligible for the free shipping.\n", "Votre commande doit être égal ou suppérieur à 500$ pour profiter de la livraison gratuite.\n");
	
	aPrice = jQuery("#table-accountable #"+sId+" td[class=cell-price]").text().split("$");
	
	fPrice = new Number(aPrice[0]);
	
	tax1 = 0;
	tax2 = 0;

	if (sCountry == "CA" && fPrice == 0.00 && Math.floor(fSubTotal) < 500)
	{
		alert(aMsg[iLang]);
		
		resetBG();
		
		jQuery("input[type=radio]").each(function () { this.checked = false });
		
		return false;
	}
	else
	{
		fSubTotal += fPrice;
	}

	chgBG(sId);
		
	if (sCountry == "CA")
	{
		tax1 = fSubTotal * 0.05;

		if ((sRegion == "QC" && iClientType == G_CT_ENTER) || iClientType == G_CT_PARTI && sRegion != "ON" && sRegion != "BC")
		{
			tax2 = (fSubTotal + tax1) * 0.075;
			jQuery("#table-accountable #row-gst td[class=cell-price]").text(makePriceStr(iCur, tax1))
			jQuery("#table-accountable #row-qst td[class=cell-price]").text(makePriceStr(iCur, tax2))
	
			fTotal = tax1 + tax2 + fSubTotal;
			
			jQuery("#table-accountable #row-total td[class=cell-price]").text(makePriceStr(iCur, fTotal))
		}
		else if (sRegion == "ON")
		{
			tax1 = fSubTotal * 0.13;
			jQuery("#table-accountable #row-gst td[class=cell-price]").text(makePriceStr(iCur, tax1))
	
			fTotal = tax1 + fSubTotal;
			
			jQuery("#table-accountable #row-total td[class=cell-price]").text(makePriceStr(iCur, fTotal))
		}
		else if (sRegion == "BC")
		{
			tax1 = fSubTotal * 0.12;
			jQuery("#table-accountable #row-gst td[class=cell-price]").text(makePriceStr(iCur, tax1))
			
			fTotal = tax1 + fSubTotal;
			
			jQuery("#table-accountable #row-total td[class=cell-price]").text(makePriceStr(iCur, fTotal))
		}
	}
}

function validateShippingMethod(oForm, iLang)
{
	aMsg = new Array("- Please select a method of shipment.","- Veuillez choisir une méthode de livraison.");

	if (jQuery("input[type=radio]:checked").length == 0)
	{
		alert(aMsg[iLang]);
	}
	else
	{
		//alert("(c) Spytronic Inc.");
		oForm = jQuery("#form-cart-shipping-method");
		oForm.submit();
	}
}

function validateCC(iLang)
{
	oMsgs = {"CARD_TYPE":["- Please select a card type.\n","- Veuillez choisir un type de carte.\n"],
			"CARD_NUMBER":["- Please enter a valid card number.\n","- Veuillez entrer un numéro de carte valide.\n"],
			"CARD_CSV":["- Please enter a valid csv number.\n","- Veuillez entre un numéro de CSV valide.\n"],
			"CARD_EXP_MONTH":["- Please select an expiration month.\n","- Veuillez choisir un mois d'expiration.\n"],
			"CARD_EXP_YEAR":["- Please select an expiration year.\n","- Veuillez choisir une année d'expiration.\n"],
			"LU":["- Please read and check the box.\n","- Veuillez lire et cocher la case de condition d'achat.\n"]};

	sMsg = "";
	
	if (jQuery("#select-cc-type option:selected").val() == "null")
	{
		sMsg += oMsgs.CARD_TYPE[iLang];
	}
	
	iCCLength = jQuery("input[name=CARD_NUMBER]").val().length;
	
	if (jQuery("#select-cc-type option:selected").val() == "Mastercard")
	{
		if (iCCLength != 16)
		{
			sMsg += oMsgs.CARD_NUMBER[iLang];
		}
	}
	else if (jQuery("#select-cc-type option:selected").val() == "Visa")
	{
		if (iCCLength != 13 && iCCLength != 16)
		{
			sMsg += oMsgs.CARD_NUMBER[iLang];
		}
	}
	if (jQuery("input[name=CARD_CSV]").val().length != "3")
	{
		sMsg += oMsgs.CARD_CSV[iLang];
	}
	if (jQuery("#select-cc-exp-month option:selected").val() == "null")
	{
		sMsg += oMsgs.CARD_EXP_MONTH[iLang];
	}
	if (jQuery("#select-cc-exp-year option:selected").val() == "null")
	{
		sMsg += oMsgs.CARD_EXP_YEAR[iLang];
	}
	if (jQuery("input[name=LU]:checked").length == 0)
	{
		sMsg += oMsgs.LU[iLang];
	}
	
	if (sMsg.length != 0)
	{
		alert(sMsg);
	}
	else
	{
		oForm = jQuery("#form-cart-payment");
		
		oForm.submit();
		//alert("(c) Spytronic Inc.");
	}
}

function Cart_Add(sLocal, sPageCall, sProductId, iQuantity)
{
	sBaseURL = G_BASEURL;

	sURL = sBaseURL + sLocal + "/" + sPageCall + "?productid="+ sProductId + "&quantity=" + iQuantity;
	
	//alert(sURL);
	
	document.location.href = sURL;
}

function handlerFCountrySelection(oSelect)
{
	sSelVal = oSelect.options[oSelect.selectedIndex].value;

	jQuery("select[name='F_NULL_SEL_STATE']").hide();

	jQuery("input[name='F_REGION_OTHER']").hide();

	jQuery("select[name='F_SEL_REGION_CODE_CA']").hide();

	jQuery("select[name='F_SEL_REGION_CODE_US']").hide();

	jQuery("select[name='F_SEL_REGION_CODE_FR']").hide();

	if (sSelVal != 'NULL')
	{	
		if (sSelVal == 'CA')
		{
			jQuery("select[name='F_SEL_REGION_CODE_CA']").show();
			jQuery("input[name='F_REGION_OTHER']").val('');
		}
		else if (sSelVal == 'US')
		{
			jQuery("select[name='F_SEL_REGION_CODE_US']").show();
			jQuery("input[name='F_REGION_OTHER']").val('');
		}
		else if (sSelVal == 'FR')
		{
			jQuery("select[name='F_SEL_REGION_CODE_FR']").show();
			jQuery("input[name='F_REGION_OTHER']").val('');
		}
		else
		{
			jQuery("input[name='F_REGION_OTHER']").show();
			jQuery("input[name='F_SEL_REGION_CODE']").val('NULL');
			jQuery("input[name='F_SEL_REGION_TEXT']").val('NULL');
		}
		
		jQuery("input[name='F_SEL_COUNTRY_TEXT']").val(oSelect.options[oSelect.selectedIndex].innerHTML);
	}
	else
	{
		jQuery("select[name='F_NULL_SEL_STATE']").show();
	}
}

function handlerLCountrySelection(oSelect)
{
	sSelVal = oSelect.options[oSelect.selectedIndex].value;

	jQuery("select[name='L_NULL_SEL_STATE']").hide();

	jQuery("input[name='L_REGION_OTHER']").hide();

	jQuery("select[name='L_SEL_REGION_CODE_CA']").hide();

	jQuery("select[name='L_SEL_REGION_CODE_US']").hide();

	jQuery("select[name='L_SEL_REGION_CODE_FR']").hide();

	if (sSelVal != 'NULL')
	{	
		if (sSelVal == 'CA')
		{
			jQuery("select[name='L_SEL_REGION_CODE_CA']").show();
			jQuery("input[name='L_REGION_OTHER']").val('');
		}
		else if (sSelVal == 'US')
		{
			jQuery("select[name='L_SEL_REGION_CODE_US']").show();
			jQuery("input[name='L_REGION_OTHER']").val('');
		}
		else if (sSelVal == 'FR')
		{
			jQuery("select[name='L_SEL_REGION_CODE_FR']").show();
			jQuery("input[name='L_REGION_OTHER']").val('');
		}
		else
		{
			jQuery("input[name='L_REGION_OTHER']").show();
			jQuery("input[name='L_SEL_REGION_CODE']").val('NULL');
			jQuery("input[name='L_SEL_REGION_TEXT']").val('NULL');
		}
		
		jQuery("input[name='L_SEL_COUNTRY_TEXT']").val(oSelect.options[oSelect.selectedIndex].innerHTML);
	}
	else
	{
		jQuery("select[name='L_NULL_SEL_STATE']").show();
	}
}

function handlerLCountrySelection2(sFCountryCode, sFCountryText)
{
	jQuery("select[name='L_NULL_SEL_STATE']").hide();

	jQuery("input[name='L_REGION_OTHER']").hide();

	jQuery("select[name='L_SEL_REGION_CODE_CA']").hide();

	jQuery("select[name='L_SEL_REGION_CODE_US']").hide();

	jQuery("select[name='L_SEL_REGION_CODE_FR']").hide();

	if (sFCountryCode != 'NULL')
	{	
		if (sFCountryCode == 'CA')
		{
			jQuery("select[name='L_SEL_REGION_CODE_CA']").show();
			jQuery("input[name='L_REGION_OTHER']").val('');
		}
		else if (sFCountryCode == 'US')
		{
			jQuery("select[name='L_SEL_REGION_CODE_US']").show();
			jQuery("input[name='L_REGION_OTHER']").val('');
		}
		else if (sFCountryCode == 'FR')
		{
			jQuery("select[name='L_SEL_REGION_CODE_FR']").show();
			jQuery("input[name='L_REGION_OTHER']").val('');
		}
		else
		{
			jQuery("input[name='L_REGION_OTHER']").show();
			jQuery("input[name='L_SEL_REGION_CODE']").val('NULL');
			jQuery("input[name='L_SEL_REGION_TEXT']").val('NULL');
		}
		
		jQuery("input[name='L_SEL_COUNTRY_TEXT']").val(sFCountryText);
	}
	else
	{
		jQuery("select[name='L_NULL_SEL_STATE']").show();
	}
}
