function validateZIP(field) {
		var valid = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
		
		if (field.length < 5) {
			alert("Please enter a US or Canada zip code.\nExample: 94111 or H1H0A3");
			return false;
		}
		for (var i=0; i < field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in your zip code");
				return false;
			}
		}
		return true;
}
	
function validateState(field) {
		var valid = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
		
		if (field.length!=2) {
			alert("Please enter a 2 letter US or Canada state code: CA");
			return false;
		}
		for (var i=0; i < field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in the US State code field."+'\n'+"Please enter a 2 letter US State code."+'\n'+"Example: CA");
				return false;
			}
		}
		return true;
}

function validateCity(field) {
		var valid = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm. ";
		
		if (field.length<2) {
			alert("City name is too short, please enter a full city name.\nExample: Los Angeles");
			return false;
		}
		for (var i=0; i < field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Please enter a full city name: Los Angeles");
				return false;
			}
		}
		return true;
}

function validateUsername(field) {
		var valid = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789";
		
		if (field.length<6) {
			alert("Username field must contain at least 6 characters, letters and digits only");
			return false;
		}
		if (field.length>16) {
			alert("Username field can not be bigger than 16 characters, letters and digits only");
			return false;
		}
		for (var i=0; i < field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Username field must contain at least 6 characters, letters and digits only");
				return false;
			}
		}
		return true;
}
	
function validatePhone(field) {
		var valid = "1234567890-";
		
		if (field.length<10)  {
			alert("Please enter a valid phone number: 999-999-9999");
			return false;
		}
		for (var i=0; i < field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in the phone number field, phone number must be entered in following format: 999-999-9999");
				return false;
			}
		}
		return true;
}
	
function validateEMail(field) {
		var valid = "~1234567890-_QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm@.;";
		
		if (field.length<7)  { // smalest email is bigger than 7 chars a@b.com
			alert("Please enter a valid email address"+'\n'+"Example: your_account@domain.com");
			return false;
		}
		for (var i=0; i < field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in the email field."+'\n'+"Please enter a valid email address."+'\n'+"Example: your_account@domain.com;another@website.com");
				return false;
			}
		}
		return true;
}

function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );
    while ( idx > -1 ) {
        str = str.replace( from, to ); 
        idx = str.indexOf( from );
    }
    return str;
}

function makeNormalString(field) {
		return replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(field,"'","`"),"~",""),"$",""),"\"","`"),"<","-"),">","-");
}

function validateLWH(field) { // length or width or height or weight or any other numeric only INT value
		var valid = "1234567890";
		
		if (field.length<1)  { 
			alert("Please enter numeric value");
			return false;
		}
		for (var i=0; i < field.length; i++) {
			temp = "" + field.substring(i, i+1);
			if (valid.indexOf(temp) == "-1") {
				alert("Invalid characters in the numeric value field.\nNo spaces, comas or dots (periods).");
				return false;
			}
		}
		if (parseInt(field) == 0) {
			alert("This numeric value Quantity, Weight, Height, Length can`t be zero");
			return false;
		}
		return true;
}
