/// <reference path="~/Include/jquery-1.4.1-vsdoc.js" />

function switchLayers(layerToHide, layerToShow) {
	layerToHide.style.display = 'none';
	layerToShow.style.display = 'inline';
}

function toggleLayer(layer) {
	layer.style.display = (layer.style.display == 'none') ? 'inline' : 'none';
}

function validKey(event) {
	bValid = true;
	code = event.keyCode;
	switch (code) {
		case 13: return false; // return
		case 191: return false; // single fnutt
		default: return true;  // all else is OK
	}
}

function vSubmitForm(frm) {
	return validateForm(frm)
}

function vSubmitIOForm(frm) {
	return validateNOForm(frm);
}

function vSubmitNOForm(frm) {
	return validateNOForm(frm);
}

function validateForm(frm) {
	var strMsg = '';
	for (i = 0; i < frm.elements.length; i++) {
		var elem = frm.elements[i];
		var bFirst = true;
		var strRet = '';
		if (elem.v == '1') {
			strRet = vElem(elem);
			if (strRet != '') {
				elem.style.background = '#FFA2A4';
				strMsg = strMsg + elem.vname + ':\n ' + strRet + '\n';
				if (bFirst) {
					//elem.focus();
					bFirst = false;
				}
			} else {
				elem.style.background = 'white';
			}
		}
		if (elem.v == '2') {
			strRet = vListElem(elem);
			if (strRet != '') {
				elem.style.background = '#FFA2A4';
				strMsg = strMsg + elem.vname + ':\n ' + strRet + '\n';
				if (bFirst) {
					//elem.focus();
					bFirst = false;
				}
			} else {
				elem.style.background = 'white';
			}
		}
	}
	if (strMsg != '') {
		//alert(strMsg);
		return false;
	} else {
		return true;
	}
}

function validateIOForm(frm) {
	var strMsg = '';
	for (i = 0; i < frm.elements.length; i++) {
		var elem = frm.elements[i];
		var bFirst = true;
		var strRet = '';
		if (elem.parentNode.parentNode.parentNode.style.display == 'inline' || elem.parentNode.parentNode.parentNode.style.display == '') {
			if (elem.v == '1') {
				strRet = vElem(elem);
				if (strRet != '') {
					elem.style.background = '#FFA2A4';
					strMsg = strMsg + elem.vname + ':\n ' + strRet + '\n';
					if (bFirst) {
						//elem.focus();
						bFirst = false;
					}
				} else {
					elem.style.background = 'white';
				}
			}
			if (elem.v == '2') {
				strRet = vListElem(elem);
				if (strRet != '') {
					elem.style.background = '#FFA2A4';
					strMsg = strMsg + elem.vname + ':\n ' + strRet + '\n';
					if (bFirst) {
						//elem.focus();
						bFirst = false;
					}
				} else {
					elem.style.background = 'white';
				}
			}
		}
	}
	if (strMsg != '') {
		//alert(strMsg);
		return false;
	} else {
		return true;
	}
}

function checkList(elem) {
	var bValid = true;
	if ($(elem).attr('vreq') == '1') {
		if ($(elem).val() == '' || $(elem).val() == '-1') {
			bValid = false;
		}
	}
	return bValid;
}

function checkLength(elem) {
	var bValid = true;
	var minLength = parseInt($(elem).attr('vmin'));
	if (minLength > 0) {
		if ($(elem).val().length < minLength) {
			return false;
		}
	}
	return bValid;
}

function validateNOForm(frm) {
	var bValid = true;

	$(frm).find(":input:visible:enabled[v]").each(
		function() {
			var thisValid = true;
			if ($(this).attr('v') == '1') {
				thisValid = checkLength(this);
			}
			else if ($(this).attr('v') == '2') {
				thisValid = checkList(this);
			}
			$(this).toggleClass('inputError', !thisValid);
			if (!thisValid) {
				bValid = false;
			}
		}
	);

	return bValid;
}

function vElem(e) {
	var strMsg = '';
	if (e.vmin != '0') {
		if (!e.value) {
			strMsg = strMsg + 'Is not allowed to be empty. ';
		}
	}
	if (e.vmin) {
		if (eval(e.value.length < e.vmin)) {
			strMsg = strMsg + 'Minimum length ' + e.vmin + '. ';
		}
	}
	return strMsg;
}

function vListElem(e) {
	var strMsg = '';
	if (e.vreq == '2') {
		if (!e.value || e.value == '-1') {
			strMsg = strMsg + 'At least one member must be added to the list of chosen members. ';
			return strMsg;
		}
	}
	if (e.vreq == '1') {
		if (!e.value || e.value == '-1') {
			strMsg = strMsg + 'Please choose from the list ' + e.vname;
		}
	}
	return strMsg;
}

function findIndex(name_arr, name) {
	for (var i = name_arr.length - 1; i >= 0; --i) {
		if (name_arr[i].value == name) {
			return i;
		}
	}

	return -1; // not found
}

function selectOption(form_id, num) {
	var selObj = document.getElementById(form_id);
	selObj.selectedIndex = num;
}

function setDeliveryFields(frm) {
	var id = document.OrderForm.order_customer.value;
	document.OrderForm.del_nr.value = delNr[id];
	document.OrderForm.del_name.value = delName[id];
	document.OrderForm.del_address.value = delAddress[id];
	document.OrderForm.del_zipcode.value = delZip[id];
	document.OrderForm.del_city.value = delCity[id];
	//document.OrderForm.del_contactname.value = delPerson[id];
	//document.OrderForm.del_contactphone.value = delPhone[id];
	selectOption('std_branch', findIndex(document.all.std_branch.options, delBranch[id]));
}

function getRegionBranchUrl(sPage, sRegionField, sBranchField, sExtra) {
	var rv = sPage;

	if (sRegionField != '') {
		var oRegion = document.getElementById(sRegionField);
		var sRegion = '';

		if (oRegion != null) {
			if (oRegion.type == 'hidden') {
				sRegion = oRegion.value;
			}
			else {
				sRegion = oRegion.options.value;
			}
		}
		rv += '?region=' + sRegion;

		if (sBranchField != '') {
			var sBranch = document.getElementById(sBranchField).options.value;

			if (sBranch != '') {
				rv += '&branch=' + sBranch;
			}
		}

		rv += sExtra
	}

	return rv;
}
function toggle(strIDelement) {
	var element = document.getElementById(strIDelement);
	if (element != null) {
		if (element.style.display == 'none') {
			element.style.display = 'inline';
		}
		else {
			element.style.display = 'none';
		}
	}
}

function ismaxlength(obj) {
	var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length > mlength) {
		obj.value = obj.value.substring(0, mlength)
	}
}

function switchToCust() {
	if ($('#deliverytype').val() == '2') {
		$('#deliverytype').val('1');

		$('#tabdeliverywh').removeClass('tabsel').addClass('tab');
		$('#tabdeliverycust').removeClass('tab').addClass('tabsel');

		$('#deliverywh').hide();
		$('#deliverycust').show();
	}
}

function switchToWh() {
	if ($('#deliverytype').val() == '1') {
		$('#deliverytype').val('2');

		$('#tabdeliverycust').removeClass('tabsel').addClass('tab');
		$('#tabdeliverywh').removeClass('tab').addClass('tabsel');

		$('#deliverycust').hide();
		$('#deliverywh').show();
	}
}

function startElementResizer(selector, iOffset) {
	$(window).load(function() { resizeDiv(selector, iOffset); });
	$(window).resize(function() { resizeDiv(selector, iOffset); });
}

function resizeDiv(selector, iOffset) {
	var height = $(window).height();
	$(selector).css('height', height - iOffset);
}

// AJAX FUNCTIONS FOLLOWS:

function runXML(strURL) {
	$.getScript(strURL);
}

function DoSearchCustomerName(Name) {
	if (Name.length > 2) {
		$.getScript("ajax_find_customer.asp?n=" + Name, function() { $('#osr').show(); });
	} else {
		$('#osr').hide();
		$('#osr').html('');
	}
}

function DoSearchCustomerNr(Nr) {
	if (Nr.length > 0) {
		$.getScript("ajax_find_customer.asp?nr=" + Nr);
	} else {
		$('#custnr_result').hide();
		$('#custnr_result').html('');
	}
}

function DoSearchCustomerAddressName(iCust,Name) {
	if (Name.length > 2) {
		$.getScript("ajax_find_customer.asp?c=" + iCust + "&an=" + Name,	function() { $('#osr').show(); });
	} else {
		$('#osr').hide();
		$('#osr').html('');
	}
}

function DoSelectCustomerAddress(iCust,Nr) {
	if (Nr.length > 0) {
		$('#osr').hide();
		$.getScript("ajax_find_customer.asp?c=" + iCust + "&a=" + Nr);
	}
}

function DoSelectCustomer(ID) {
	$('#osr').hide();
	$.getScript("ajax_find_customer.asp?i=" + ID);
}

function DoSelectCustomerNr(ID) {
	$('#custnr_result').hide();
	$.getScript("ajax_find_customer.asp?i=" + ID);
}

function PopupHelp(pageURL) {
	var w = 400;
	var h = 200;
	var left = (screen.width / 2) - (w / 2);
	var top = (screen.height / 2) - (h / 2);
	window.open(pageURL, 'Help', 'height=' + h + ',left=' + left + ',top=' + top + ',width=' + w + ',status = 1,directories=no,location=no,menubar=no,titlebar=no,toolbar=no,resizable=no,scrollbars=no,copyhistory=no,');
}

function attachHelpHandlers() {
	var elements = $('input');
	elements.focus(function() {
		var fldname = $(this).attr('name');
		var url = "help_checkout.asp?f=" + encodeURIComponent(fldname)
		var helpSection = $("div.ordersectionhelp");
		helpSection.load(url, function() {
			if (helpSection.text().length > 0) {
				helpSection.show();
			}
			else {
				helpSection.hide();
			}
		});
	});
	elements.blur(function() {
		var helpSection = $("div.ordersectionhelp");
		helpSection.text('').hide();
	});
}

