


var bRequestSent = false;

function validatePayment() {
	var objForm = document.forms["pay_form"];
	var b = true;
	if (objForm != null) {
		
		if (!validateCardNum(objForm.cardnumber1)) {
			b = false;
		}

		if (!validateCardNum(objForm.cardnumber2)) {
			b = false;
		}
	
		if (!validateCardNum(objForm.cardnumber3)) {
			b = false;
		}

		if (!validateCardNum(objForm.cardnumber4)) {
			b = false;
		}

		if (!validateExpMonth(objForm.cexpMonth)) {
			b = false;
		}

		if (!validateExpYear(objForm.cexpYear)) {
			b = false;
		}

		if (!validateCVD(objForm.ccvd)) {
			b = false;
		}

		if (b) {
			objForm.cardnumber.value = objForm.cardnumber1.value + objForm.cardnumber2.value + objForm.cardnumber3.value + objForm.cardnumber4.value;
			objForm.expMonth.value = objForm.cexpMonth.value;
			objForm.expYear.value = objForm.cexpYear.value;
			objForm.cvd.value = objForm.ccvd.value;
		}
	}
	else {
		b = false;
	}

	return b;
}

function doValidatePayment() {
	if (validatePayment()) {
		if (!bRequestSent) {
			bRequestSent = true;
			document.forms["pay_form"].submit();
		}
	}
	return false;
}

function focusCardField(obj) {
	if (obj != null) {
		obj.focus();
		obj.select();
	}
}

function validateCardNum(obj) {
	obj.value = obj.value.trim();
	var rx = /^\d{4}$/;
	var s = obj.value;
	var b = true;
	if (!s.match(rx)) {
		b = false;
		$(obj).addClass("error");
	}
	else {
		$(obj).removeClass("error");
	}
	return b;
}

function validateExpMonth(obj) {
	obj.value = obj.value.trim();
	var rx = /^\d{1,2}$/;
	var s = obj.value;
	var b = true;
	if (!s.match(rx)) {
		s = "mm";
		b = false;
	}
	else {
		s = parseInt(s, 10);
		if ((s > 12) || (s < 1)) {
			s = "mm";
			b = false;
		}
		else if (s < 10) {
			s = "0"+ s;
		}
	}
	obj.value = s;
	if (!b) {
		$(obj).addClass("error");
	}
	else {
		$(obj).removeClass("error");
	}
	return b;
}

function validateExpYear(obj) {
	obj.value = obj.value.trim();
	var rx = /^\d{1,2}$/;
	var s = obj.value;
	var b = true;
	if (!s.match(rx)) {
		b = false;
		s = "åå";
	}
	else {
		s = parseInt(s, 10);
		if (s < 10) {
			s = "0"+ s;
		}
	}
	obj.value = s;
	if (!b) {
		$(obj).addClass("error");	
	}
	else {
		$(obj).removeClass("error");	
	}
	
	return b;
}

function validateCVD(obj) {
	obj.value = obj.value.trim();
	var rx = /^\d{3}$/;
	var b = true;
	if (!obj.value.match(rx)) {
		b = false;
		$(obj).addClass("error");
	}
	else {
		$(obj).removeClass("error");	
	}
	return b;

}

function initCardInfo() {
	clearCardInfo();

	
	$("#cardnumber1, #cardnumber2, #cardnumber3, #cardnumber4, #cexpMonth, #cexpYear").attr("autocomplete", "off");

	$("#cardnumber1, #cardnumber2, #cardnumber3, #cardnumber4").blur(function() {
		validateCardNum($(this).get(0));
	});

	$("#cardnumber1, #cardnumber2, #cardnumber3, #cardnumber4, #cexpMonth, #cexpYear").keyup(function(e) {
		var objForm = $(this).attr("form");

		if ($(this).attr("value").length == $(this).attr("maxLength")) {
			if (e.keyCode != 9 && e.keyCode != 16) {
				switch ($(this).attr("name")) {
					case "cardnumber1" :
						focusCardField(objForm.cardnumber2);
						break;
					case "cardnumber2" :
						focusCardField(objForm.cardnumber3);
						break;
					case "cardnumber3" :
						focusCardField(objForm.cardnumber4);
						break;
					case "cardnumber4" :
						focusCardField(objForm.cexpMonth);
						break;
					case "cexpMonth" :
						focusCardField(objForm.cexpYear);
						break;
					case "cexpYear" :
						focusCardField(objForm.ccvd);
						break;					
				}
			}
		}
	});
}

function clearCardInfo() {
	$("#cardnumber").attr("value", "");
	$("#expMonth").attr("value", "");
	$("#expYear").attr("value", "");
	$("#cvd").attr("value", "");

	$("#cardnumber1").attr("value", "");
	$("#cardnumber2").attr("value", "");
	$("#cardnumber3").attr("value", "");
	$("#cardnumber4").attr("value", "");
	$("#cexpMonth").attr("value", "mm");
	$("#cexpYear").attr("value", "åå");
	$("#ccvd").attr("value", "");
}






function validateAddressField(obj) {
	if (obj != null) {
		var rx = /^eshp_d.*$/;
		obj.value = obj.value.trim();

		switch(obj.name) {
			case "eshp_iphone1":
			case "eshp_dphone1":
				obj.value = obj.value.replace(/\D/g, "");				
				break;
		}

		
		if (obj.name.match(rx)) {
			refreshDeliveryAddressFields(obj.form);
		}
		else {
			refreshInvoiceAddressFields(obj.form);
		}
	}
}


function refreshDeliveryAddressFields(objForm) {
	var rx = /^eshp_d.*$/;

	var b = someDeliveryAddressFieldSet(objForm);
	for (var i = 0; i < objForm.elements.length; i++) {
		obj = objForm.elements.item(i);
		if (obj.type == "text") {
			if (obj.name.match(rx)) {
				if (b) {
					switch (obj.name) {
						case "eshp_dcompany" :
							break;
						default :
							if (obj.value != "") {
								$(obj).removeClass("error");	
							}
							else {
								$(obj).addClass("error");
							}
							break;
					}
				}
				else {
					$(obj).removeClass("error");	
				}
			}
		}
	}
}


function validateInvoiceAddress(objForm) {
	var b = true;
	var rx = /^eshp_i.*$/;

	for (var i = 0; i < objForm.elements.length; i++) {
		obj = objForm.elements.item(i);
		if (obj.type == "text") {
			if (obj.name.match(rx)) {
				switch (obj.name) {
					case "eshp_icompany" :
						break;
					case "eshp_iphone1" :
						obj.value = obj.value.replace(/\D/g, "");
						if (obj.value == "") {
							b = false;
							$(obj).addClass("error");
						}
						else {
							$(obj).removeClass("error");						
						}
						break;

					case "eshp_iemail" :
						if (!isEmail(obj.value)) {
							b = false;
							$(obj).addClass("error");
						}
						else {
							$(obj).removeClass("error");
						}
						break;
					default :
						if (obj.value == "") {
							b = false;
							$(obj).addClass("error");
						}
						else {
							$(obj).removeClass("error");
						}
						break;
				}
			}
		}
	}

	return b;
}

function validateDeliveryAddress(objForm) {
	var b = true;
	var rx = /^eshp_d.*$/;

	if (someDeliveryAddressFieldSet(objForm)) {
		for (var i = 0; i < objForm.elements.length; i++) {
			obj = objForm.elements.item(i);
			if (obj.type == "text") {
				if (obj.name.match(rx)) {
					switch (obj.name) {
						case "eshp_dcompany" :
							break;
						case "eshp_dphone1" :
							obj.value = obj.value.replace(/\D/g, "");
							if (obj.value == "") {
								b = false;
								$(obj).addClass("error");
							}
							else {
								$(obj).removeClass("error");						
							}
							break;

						default :
							if (obj.value == "") {
								b = false;
								$(obj).addClass("error");
							}
							else {
								$(obj).removeClass("error");
							}
							break;
					}
				}
			}
		}		
	}

	return b;
}



function validatePasswordFields(objForm) {
	var b = true;
	if (objForm != null) {
		var obj1 = objForm.elements.namedItem("nc_pwd1");
		var obj2 = objForm.elements.namedItem("nc_pwd2");

		if ((obj1 != null) && (obj2 != null)) {
			obj1.value = trimWS(obj1.value);
			obj2.value = trimWS(obj2.value);

			if ((obj1.value.length > 0) || (obj2.value.length > 0)) {

				if ((obj1.value == obj2.value) && (obj1.value.length >= 8)) {
					$(obj1).removeClass("error");
					$(obj2).removeClass("error");
				}
				else {
					if (obj1.value.length < 8) {
						$(obj1).addClass("error");
						b = false;
					}
					else {
						$(obj1).removeClass("error");
					}
					if (obj2.value.length < 8) {
						$(obj2).addClass("error");
						b = false;
					}
					if (obj2.value != obj1.value) {
						$(obj2).addClass("error");
						b = false;
					}
				}
			}
			else {
				$(obj1).removeClass("error");
				$(obj2).removeClass("error");
			}
		}
	}
	return b;
}


function validateAddressForm() {
	var b = true;	
	var objForm = document.forms["eshp_address"];
	if (objForm != null) {

		if (!validateInvoiceAddress(objForm)) {
			b = false;

		}

		if (!validateDeliveryAddress(objForm)) {
			b = false;

		}

		if (!validatePasswordFields(objForm)) {
			b = false;
		}

		if (b) {
			objForm.submit();
		}

	}

	return false;
	
}



function someInvoiceAddressFieldSet(objForm) {
	var rx = /^eshp_i.*$/;
	var b = false;
	for (var i = 0; i < objForm.elements.length; i++) {
		obj = objForm.elements.item(i);
		if (obj.type == "text") {
			if (obj.name.match(rx)) {
				if (obj.value != "") {
					b = true;
					break;
				}
			}
		}
	}
	return b;
}

function refreshInvoiceAddressFields(objForm) {
	var rx = /^eshp_i.*$/;

	var b = true;//someInvoiceAddressFieldSet(objForm);
	for (var i = 0; i < objForm.elements.length; i++) {
		obj = objForm.elements.item(i);
		if (obj.type == "text") {
			if (obj.name.match(rx)) {
				if (b) {
					switch (obj.name) {
						case "eshp_icompany" :
							break;
						case "eshp_iphone1" :
							obj.value = obj.value.replace(/[\D]/g, "");
							if (obj.value == "") {
								$(obj).addClass("error");
							}
							else {
								$(obj).removeClass("error");
							}
							break;
						case "eshp_iemail" :
							if (!isEmail(obj.value)) {
								$(obj).addClass("error");
							}
							else {
								$(obj).removeClass("error");
							}
							break;
						default :
							if (obj.value != "") {
								$(obj).removeClass("error");	
							}
							else {
								$(obj).addClass("error");
							}
							break;
					}
				}
				else {
					$(obj).removeClass("error");	
				}
			}
		}
	}
}

function someDeliveryAddressFieldSet(objForm) {
	var rx = /^eshp_d.*$/;
	var b = false;
	for (var i = 0; i < objForm.elements.length; i++) {
		obj = objForm.elements.item(i);
		if (obj.type == "text") {
			if (obj.name.match(rx)) {
				if (obj.value != "") {
					b = true;
					break;
				}
			}
		}
	}
	return b;
}




var reqInvoiceOptions = null;

function doApprove() {
	var objForm = document.forms["eshp_relayredir"];
	var objOptionsForm = document.forms["eshp_options"];

	if ((objForm != null) && (objOptionsForm != null)) {

		if (reqInvoiceOptions != null) {
			reqInvoiceOptions.abort();
		}

		var params = {};

		var oNL = objOptionsForm.i_newsletter;
		var snl = "";

		$.each($(objOptionsForm.i_newsletter), function(i, n) {
			if ($(n).attr("checked")) {
				snl += (snl != "" ? "," : "") + $(n).val();
			}
		});

		params.nl = snl;
		params.pdf = (objOptionsForm.i_pdf.checked ? 1 : 0);


		reqInvoiceOptions = $.ajax({
			url : "/custom/site/xml/invoiceoptions.asp",
			dataType : "xml",
			type : "GET",
			data : params,
			cache : false,
			complete : function(XMLHttpRequest, textStatus) {
			
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {

			},
			success : function(data, textStatus) {
				if (data) {
					objForm.submit();
				}
			}

		});

	}

}

function approve() {
	doApprove();
	return false;
}









function initAddressForm() {
	var arrFields = ["eshp_icompany", "eshp_ifirstname", "eshp_ilastname", "eshp_iaddress1", "eshp_izipcode", "eshp_icity", "eshp_iphone1", "eshp_iemail", "eshp_dcompany", "eshp_dfirstname", "eshp_dlastname", "eshp_daddress1", "eshp_dzipcode", "eshp_dcity", "eshp_dphone1", "nc_pwd1", "nc_pwd2"];

	var str = "";
	for (var i = 0; i < arrFields.length; i++) {
		str += "#"+ arrFields[i] + (i < arrFields.length - 1 ? ", " : "");
	}

	var objForm = document.forms["eshp_address"];


	$("#eshp_iphone1, #eshp_dphone1").keydown(function(event) {
		var controlKeys = [8, 9, 13, 32, 35, 36, 37, 39];
		var isControlKey = controlKeys.join(",").match(new RegExp(event.which));


		if (!event.which || // Control keys in most browsers. e.g. Firefox tab is 0
			(48 <= event.which && event.which <= 57) || // Always 0 through 9
			(96 <= event.which && event.which <= 105) || // Always 0 through 9
			isControlKey) { // Opera assigns values for control keys.
				return;
		} else {
			event.preventDefault();
		}
	});


	$(str).blur(function() {
		validateAddressField($(this).get(0));
		validatePasswordFields(objForm);
	});
}






function trimWS(s) {
	return s.replace(/\s/gi, "");
}


/***********************/
/*** CREATE CUSTOMER ***/

function validateNewUsrName(obj) {
	var res = true;
	if (!isEmail($(obj).attr("value"))) {
		$(obj).addClass("error");		
		res = false;
	}
	else {
		$(obj).removeClass("error");
	}
	return res;
}

function validateNewPWD(obj) {
	var res = true;
	var obj1 = null;
	var obj2 = null;
	switch(obj.attr("name")) {
		case "nc_pwd1" :
			obj1 = obj.get(0);
			obj2 = obj1.form.elements.namedItem("nc_pwd2");
			break;
		case "nc_pwd2" :
			obj2 = obj.get(0);
			obj1 = obj2.form.elements.namedItem("nc_pwd1");
			break;
	}

	obj1.value = trimWS(obj1.value);
	obj2.value = trimWS(obj2.value);


	if ((obj1.value == obj2.value) && (obj1.value.length >= 8)) {
		$(obj1).removeClass("error");
		$(obj2).removeClass("error");
	}
	else {
		if (obj1.value.length < 8) {
			$(obj1).addClass("error");
			res = false;
		}
		else {
			$(obj1).removeClass("error");
		}
		if (obj2.value.length < 8) {
			$(obj2).addClass("error");
			res = false;
		}
		if (obj2.value != obj1.value) {
			$(obj2).addClass("error");
			res = false;
		}
	}
	return res;
}

function initCreateCustomerForm() {
	var objForm = document.forms["eshp_customer_create"];
	if (objForm != null) {

		
		$(objForm).find("input[type = 'text'], input[type = 'password']").each(function() {
			$(this).bind("blur", function() {
				v = $(this).attr("value");
				$(this).attr("value", trimWS(v));

				switch($(this).attr("name")) {
					case "nc_name" :
						validateNewUsrName($(this));
						break;
					case "nc_pwd1" :
					case "nc_pwd2" :
						validateNewPWD($(this));
						break;
				}
			});
		});
	}
}

function validateCreateCustomerForm() {
	var objForm = document.forms["eshp_customer_create"];
	if (
		validateNewUsrName($(objForm).find("input[name = 'nc_name']")) 
		&& validateNewPWD($(objForm).find("input[name = 'nc_pwd1']"))
		) {
		objForm.submit();
	}
	return false;
}

/*** CREATE CUSTOMER ***/
/***********************/





/****************************/
/*** MY ACCOUNT FUNCTIONS ***/

function initUpdatePasswordForm() {
	var objForm = document.forms["eshp_customer_pwd"];
	if (objForm != null) {
		$(objForm).find("input[type = 'password']").each(function() {
			$(this).bind("blur", function() {
				v = trimWS($(this).attr("value"));
				$(this).attr("value", v);
				if (v.length < 8) {
					$(this).addClass("error");
				}
				else {
					$(this).removeClass("error");
				}
			});
		});		
	}
}

function validateUpdatePasswordForm() {
	var objForm = document.forms["eshp_customer_pwd"];
	if (objForm != null) {
		var res = true;

		var objOldPwd = objForm.elements.namedItem("c_oldpwd");
		var objNewPwd1 = objForm.elements.namedItem("c_newpwd1");
		var objNewPwd2 = objForm.elements.namedItem("c_newpwd2");

		objOldPwd.value = trimWS(objOldPwd.value);
		objNewPwd1.value = trimWS(objNewPwd1.value);
		objNewPwd2.value = trimWS(objNewPwd2.value);
	

		if (objOldPwd.value.length < 8) {
			$(objOldPwd).addClass("error");
			res = false;
		}
		else {
			$(objOldPwd).removeClass("error");
		}

		if (objNewPwd1.value.length < 8) {
			$(objNewPwd1).addClass("error");
			res = false;
		}
		else {
			$(objNewPwd1).removeClass("error");
		}

		if (objNewPwd2.value.length < 8) {
			$(objNewPwd2).addClass("error");
			res = false;
		}
		else {
			$(objNewPwd2).removeClass("error");
		}


		if (objNewPwd1.value != objNewPwd2.value) {
			res = false;
			$(objNewPwd1).addClass("error");
			$(objNewPwd2).addClass("error");
		}


		if (res) {
			objForm.submit();
		}
	
	}
	return false;
}


/*** MY ACCOUNT FUNCTIONS ***/
/****************************/