function Trim(psString) {
	return psString.replace(/(^\s+)|(\s+$)/g,"");
}

function ValidateNumeric(poEditBox,psMsg,piLength) {
	var lbResult = true;
	var lsValue = poEditBox.value;
	if (!psMsg) psMsg = 'number'

	if (isNaN(lsValue)) {
		alert(psMsg + ' must be numeric.');
		lbResult = false;
	}

	if (piLength && lsValue.length > piLength) {
		alert(psMsg + ' cannot be more than ' + piLength + ' characters.');
		lbResult = false;
	}

	if (!lbResult) {
		poEditBox.select();
		poEditBox.focus();
	}

	return lbResult;
}

function ValidateMandatory(pForm) {
	for (i = 0; i <= pForm.elements.length-1; i++)
	{
		if(pForm.elements[i].mandatory == "1" && !pForm.elements[i].disabled)
		{
			if (Trim(pForm.elements[i].value)=="")
			{
				var lsName = (pForm.elements[i].name.substr(0,2)=="id") ? pForm.elements[i].name.substr(2) : pForm.elements[i].name;
				alert(lsName + " is mandatory.");
				pForm.elements[i].focus();
				return false;
			}
		}
	}
	return true;
}

function ValidateJDE(poEditBox) {
	var lbResult = true;
	var lsValue = poEditBox.value;

	if (isNaN(lsValue)) {
		alert('JDE Account must be numeric');
		lbResult = false;
	}

	if (lsValue.length != 8) {
		alert('JDE Account must be 8 characters in length');
		lbResult = false;
	}

	if (!lbResult) {
		poEditBox.select();
		poEditBox.focus();
	}

	return lbResult;
}

function ValidateEmail(poEditBox) {
	var lbResult = true;
	var lsValue = poEditBox.value;

	//var loRE = new RegExp( "^(-?[0-9]{1,})((-?[.]?[0-9]{1,})?)$", "ig" );
	//var loRE = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	var loRE = new RegExp("^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,})+$");
	lbResult = loRE.test(lsValue);

	if (!lbResult) {
		poEditBox.select();
		poEditBox.focus();
		alert('Invalid Email Address: '+lsValue);
	}

	return lbResult;
}

function ValidateTelCountry(poEditBox) {
	var lbResult = true;
	var lsValue = poEditBox.value;
	var loRE = new RegExp("^\\+[0-9]{1,3}$");
	lbResult = loRE.test(lsValue);

	if (!lbResult) {
		poEditBox.select();
		poEditBox.focus();
		alert('Invalid Telephone Country Code (example +27)');
	}

	return lbResult;
}

function ValidateTelCode(poEditBox) {
	var lbResult = true; //ValidateNumeric(poEditBox,'Telephone Code',10);
	var lsValue = poEditBox.value;
	var loRE = new RegExp("^(0)");
	lsValue = lsValue.replace(loRE, "($1)");
	poEditBox.value = lsValue;
	loRE = new RegExp("^\\(0\\)[0-9]{2,5}$");
	lbResult = loRE.test(lsValue);

	if (!lbResult) {
		poEditBox.select();
		poEditBox.focus();
		alert('Invalid Telephone Code: '+lsValue);
	}

	return lbResult;
}

function IsDate(psDate) {
	//if (psDate.length!=10)  return false;

	var liYMD=psDate.split("/");
	if (liYMD.length!=3)  return false;

	var y=parseInt(liYMD[0],10);
	var m=parseInt(liYMD[1],10);
	var d=parseInt(liYMD[2],10);
	if (isNaN(d)||isNaN(m)||isNaN(y)) return false;

	if ((m<1)||(m>12)) return false;

	var days=[31,28,31,30,31,30,31,31,30,31,30,31];
	if (((y%4)==0) && (y%100!=0) || (y%400==0)) days[1]=29;

	if ((d<1)||(d>days[m - 1])) return false;

	if ((y<1900)||(y>2100)) return false;

	return true;
}

function ValidateDate(poEditBox) {
	if (IsDate(poEditBox.value)) {
		return true;
	} else {
		poEditBox.select();
		poEditBox.focus();
		alert('Invalid '+poEditBox.id+': '+poEditBox.value);
		return false;
	}
}

function IsYYYYMM(psDate) {
	if (psDate.length!=7)  return false;

	var liYM=psDate.split("/");
	if (liYM.length!=2)  return false;

	var y=parseInt(liYM[0],10);
	var m=parseInt(liYM[1],10);
	if (isNaN(m)||isNaN(y)) return false;

	if ((m<1)||(m>12)) return false;

	if ((y<1900)||(y>2100)) return false;

	return true;
}

function ValidateMonth(poEditBox) {
	if (IsYYYYMM(poEditBox.value)) {
		return true;
	} else {
		poEditBox.select();
		poEditBox.focus();
		alert('Invalid '+poEditBox.id+': '+poEditBox.value);
		return false;
	}
}


function GetRadioValue(poForm,psRadioName) {
	for (i = 0; i <= poForm.elements.length-1; i++) {
		if (poForm.elements[i].type == "radio" && poForm.elements[i].id == psRadioName) {
			if (poForm.elements[i].checked)
				return poForm.elements[i].value;
		}
	}
}

function ValidateEndDateBiggerThanStartDate(poStartDate,poEndDate) {
	date1 = new Date(poStartDate.value);
	date2 = new Date(poEndDate.value);
	diff = new Date();
	diff.setTime(date2.getTime() - date1.getTime());
	if (diff.getTime() < 0) {
		alert("From Date must be after To Date.");
		poEndDate.select();
		return false;
	} else {
		return true;
	}
}