function Validate_int(fld, msg) {
	var val = fld.value;
	var ret = true;

	if (val == "")
		ret = false;

	if (isNaN(val))
		ret = false;

	if (parseInt(val) != val)
		ret = false;

	if (ret == false) {
		alert(msg + "\n- Must be a valid non decimal number");
		fld.value = "";
		fld.focus();
	}

	return ret;
}
function Validate_cat(fld, msg) {
	if (fld.value == 0){
		alert(msg + "\n- Please ensure to select an option");
		ret = false;
	} else {
		ret = true;
	}
	return ret;
}

function Validate_cur(fld, msg) {
	var ret = true;

	fld.value = fld.value.replace(/ $/, "");
	var val = fld.value.replace(/^\$/, "");

	if (parseFloat(val) != val)
		ret = false;

	if (parseInt(val * 100) != parseFloat(val * 100))
		ret = false;

	if (ret == false) {
		alert(msg + "\n- Must be a valid currency entry");
		fld.value = 0;
		fld.focus();
	}
	return ret;
}

function Validate_password(fld,fld2,msg){
	if (msg.length < 2)
		msg = "Please enter the password";

	if (fld.value.length < 6 || !fld.value.match(/\d+/gi) || !fld.value.match(/[a-z]+/gi)){
		alert(msg + "\n\nMinimum of 6 ALPHANUMERIC characters.\nMust be at least one letter and one numeral in the password.");
		fld.value = "";
		fld.focus();
		return false;
	}
	else if (!Validate_text(fld2, "Please enter your password in the verification box"))
		return false;
	else if(fld.value != fld2.value){
		alert("Please ensure that your passwords match");
		fld2.value = "";
		fld2.focus();
		return false;
	}
	else
		return true;
}

function Validate_phone(fld, msg) {
	var ret = true;
	var type = "phone";
	fld.value = fld.value.replace(/\D*/gi, "");

	if (fld.value.length < 5){
		if (fld.name == "Ecom_ShipTo_Telecom_Mobile_Number")
			type = "mobile";
		alert(msg + "\n- Must be a valid " + type + " number");
		fld.focus();
		ret = false;
	}
	return ret;
}

function Validate_text(fld, msg) {
	var val = fld.value.replace(/ */, "");
	var ret = true;

	if (val == "")
		ret = false;

	if (ret == false) {
		alert(msg);
		fld.value = "";
		fld.focus();
	}

	return ret;
}

function Validate_text_special_chars(fld, msg) {

	var iChars = "\!@#$%^*+=[]\\{}|\"<>?";

	for (var i = 0; i < fld.value.length; i++) {
		if (iChars.indexOf(fld.value.charAt(i)) != -1) {
			alert(msg);
			fld.value = "";
			fld.focus();
			return false;
		}
	}
	return true;
}

function Validate_memo(fld, msg) {
	return Validate_text(fld, msg);
}

function Validate_img(fld, msg) {
	return Validate_text(fld, msg);
}

function Validate_date(frmName,fldName, msg) {

	fld = eval("document." + frmName + "." + fldName + "_dsp"); // the display field
	realFld = eval("document." + frmName + "." + fldName); // the real field

	day = fld[0].value;
	month = fld[1].value;
	year = fld[2].value;

	if (ValidateDate(year, month, day) == false) {
		alert(msg + "\n- Must be a valid date");
		fld[0].value = 1;
		fld[0].focus();
		return false;
	}

	// Pad them out
	if (day.length < 2)
		day = "0" + day;
	if (month.length < 2)
		month = "0" + month;

	// transfer it to the hidden field, otherwise php gets its knickers in a not having an array of post fields
	realFld.value = year + "-" + month + "-" + day + " 00:00:00";

	// this affectively hides them from php
	/*
	fld[0].name = fld[0].name + "0_notapplicable";
	fld[0].value = "";
	fld[1].name = fld[1].name + "1_notapplicable";
	fld[1].value = "";
	fld[2].name = fld[2].name + "2_notapplicable";
	fld[2].value = "";
	*/
	return true;
}

function Validate_bit(fld, msg) {
	if (fld.value != 0 && fld.value != 1) {
		alert(msg + "\n- Must be a valid bit (either 1 or 0)");
		fld.value = 0;
		fld.focus();
		return false;
	}
	return true;
}

function Validate_email(fld) {
  var x = fld.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(x);
}

function Validate_IPAddress(fld) {
  var x = fld.value;
	var filter  = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
	return filter.test(x);
}
