/***********************************
	validate.js is written by Naveed Ahmed Mughal.
	This file includes javascript form validation that can be used in easy and simple way.
***********************************/

/**
	* This function validates Empty, more than one white spaces and special characters for form field. It returns true if wrong value other wise false.
	* @param Form-Element Field (e.g form.fname)
	* @param Msg // In case of  wrong value this message will be alerted
	* @author naveedmughal
**/
function isEmpty(Field, Msg){
	if(Field.value == ""){
		alert(Msg)
		Field.focus()
		return true
	}
	if(Field.value.indexOf("  ")!=-1){
		alert("Extra white spaces are not allowed");
		Field.focus();
		return true
	}
	
	var cnt = Field.value.length;
	//var iChars = "`!@#$%^&*()+=[]\\\';,.{}|\":<>?";
	var iChars = "'`~$%\"";   // These characters will be restriced.
	var str;
	for (var j = 0; j < cnt; j++){
		if (iChars.indexOf(Field.value.charAt(j)) != -1){
			alert ("Special character(s) are not allowed. Please try again!");
			Field.focus()
			return true;
		}
	}
	return false
}


/**
	* This function matches two form files values and alerts with provided message. It returns true if wrong value other wise false.
	* @param Form-Element Field (e.g form.password1)
	* @param Form-Element Field (e.g form.password2)
	* @param Msg // If both files will not match this message will be alert.
	* @author naveedmughal
**/

function isNotSame(Field1, Field2, Msg){
	if(Field1.value != Field2.value){
		alert(Msg)
		Field1.focus()
		return true
	}
	return false
}

/**
	* This function validates email address and alerts provided message in case of wrong email address. It returns true if wrong value other wise false.
	* @param Form-Element Email (e.g form.email)
	* @param Msg // It will alert this message in case of wrong email address.
	* @author naveedmughal
**/
function isNotValidEmail(Email, Msg){
	if(Email.value == ''){
		alert(Msg)
		Email.focus()
		return true;
	}
	//	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var reg = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;  // Regular Expression to match email address.
   	if(reg.test(Email.value) == false){
      	alert(Msg)
		Email.focus()
      	return true
	}
	var cnt = Email.value.length;
	var iChars = "'`~$%\""; // These characters all not allowed.
	var str;
	for (var j = 0; j < cnt; j++){
		if (iChars.indexOf(Email.value.charAt(j)) != -1){
			alert ("Special character(s) are not allowed in email");
			Email.focus()
			return true;
		}
	}
	
	return false
}

function isNotUsaZipCode(ZipCode, Msg){
	var reg =  /^\d{5}([\-]\d{4})?$/;  // Regular Expression to match email address.
   	if(reg.test(ZipCode.value) == false){
      	alert(Msg)
		ZipCode.focus()
      	return true
	}
}

/**
	* This function checkes if provided value is number or not. It returns true if wrong value other wise false.
	* @param Form-Element (e.g form.number)
	* @param Msg // It will alert this message in case of string.
	* @author naveedmughal
**/

function isNotNo(Field, Msg){
	if(isNaN(Field.value)){
		alert(Msg)
		Field.focus()
		return true
	}
	if(Field.value == '0' || Field.value == 0){
		alert('0 is not a valid number. Please provide valid number');
		Field.focus()
		return true
	}
	return false
}
/**
	* This function checks lenght of provided value. It returns true if wrong value other wise false.
	* @param Form-Element (e.g form.number)
	* @param Integer Length // Number to match length of field.
	* @param Msg // It will alert this message in case wrong length of value.
	* @author naveedmughal
**/
function isInvalidLength(Field, Length, Msg){
	if(eval(Field.value.length) != eval(Length) ){
			alert(Msg)
			Field.focus()
			return true
	}
	return false
}


/**
	* This function resets all fields of form.
	* @param Form (e.g form)
	* @author naveedmughal
**/
function Clear(Form){
	Form.reset
	return false
}

/**
	* This checkes extension of file. This is used for file element of form. 
	* @param Form-Element File (e.g form.file_doc)
	* @param Array ValidExts // Javascript array of allowed extensions (.pdf, .doc, .ppt, .docx)
	* @param Bool  isMust // If true then it will not allow to submit form without valid file. 
		If false then it will only validate extension if file is provided, if file is not provided then it will not validate extension.
	* @author naveedmughal
**/

function isNotValidExt(File, ValidExts, isMust){
	var FileName = File.value
	var Len = FileName.length
	var Ext = (FileName.substr(FileName.lastIndexOf("."), FileName.length)).toLowerCase()
	if(FileName == ""){
		if(isMust == true){
			alert("Please select "+ValidExts);
			return true
		}else{
			return false
		}
	}else{
		if(ValidExts.indexOf(Ext) == -1){
			alert("Select only "+ValidExts+" file");
			return true
		}
	}
	return false
}


function Confirm(){
	if(window.confirm('Are you sure you want to deltee this record?')){
		return true;
	}else{
		return false;
	}
}
