///////////////////////////////////////////////////////////////////////
//                                                                   //
//     JavaScripts Function For Input Forms Validations				 //
//                                                                   //
///////////////////////////////////////////////////////////////////////
// This method trims white space off both ends of this string and returns the 
String.prototype.trim = function() 
{
  return( this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') ); 
}

// example: 
// var str = "  hello  world  ";
// str = str.trim();
// alert(str); 
///////////////////////////////////////////////////////////////////////

function IsNull(fldName,ErrMsg) {
// To check whether the field value is empty or not 
		var str = fldName.value;
		str = str.trim();
		if (str=="") {
			//SetColor()
			ErrorMsg(fldName,ErrMsg);
			return true;
		}
}
// is added by sharad
function isProper(string) {
   //if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()@&$#%1234567890;"
   
   for (var i = 0; i < string.value.length; i++) {
      if (iChars.indexOf(string.value.charAt(i)) != -1)
         return false;
   }
   return true;
}              
//function to check alphanumeric along with comma for the propertyid fld
 function isPropertyValue(fldName,ErrMsg) {
	var theString = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890,"
	var fldValue = fldName.value;
		var fldLength = fldValue.length;
		
		for (liCount = 0; liCount < fldLength; liCount++ ) {
			// Search through string's characters one by one.
			var fldChar = fldValue.charAt(liCount);
			
			if (theString.indexOf(fldChar) == -1 ) {
				ErrorMsg(fldName,ErrMsg);
				return true;
			}
		}
		counter = 0
		theString = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"
		for (liCount = 0; liCount < fldLength; liCount++ ) {
			// Search through string's characters one by one.
			var fldChar = fldValue.charAt(liCount);
			
			if (theString.indexOf(fldChar) == -1 ) {
				counter++;  
				//ErrorMsg(fldName,ErrMsg);
				//return true;
			}
			
		}
		//alert(counter)
		//alert((fldLength - 1))
		if ( counter == fldLength)
		{
			ErrorMsg(fldName,ErrMsg);
			return true;
		}
		counter = 0
		theString = "1234567890"
		for (liCount = 0; liCount < fldLength; liCount++ ) {
			// Search through string's characters one by one.
			var fldChar = fldValue.charAt(liCount);
			
			if (theString.indexOf(fldChar) == -1 ) {
				counter++
				//ErrorMsg(fldName,ErrMsg);
				//return true;
			}
		}
		//alert(counter)
		//alert(fldLength - 1)
		if ( counter == fldLength)
		{
			ErrorMsg(fldName,ErrMsg);
			return true;
		}
}             
        
function IsValidName(fldName,ErrMsg) {
// To check whether the field value is proper or not 
		if (isProper(fldName) == false) {
			ErrorMsg(fldName,ErrMsg);
			return true;
		}
}
//---------------------------------
function IsLength(fldName,chk_Min_Or_Max,chk_length,ErrMsg) {
// To check whether the length of the value, Minimum or Maximum 
// chk_Min_Or_Max = 0 means to check the value not greater than chk_length
// chk_Min_Or_Max = 1 means to check the value to be minimum chk_length

		var fldValue = fldName.value;
		var fldLength = fldValue.length;
		
		switch (chk_Min_Or_Max) {
			case 0:
				if (fldLength > chk_length ) {
					ErrorMsg(fldName,ErrMsg);
					return true;
				}
				break;
			case 1:
				if ( fldLength < chk_length ) {
					ErrorMsg(fldName,ErrMsg)
					return true;
				}
				break; 
		}
}

function IsNumeric(fldName,chk_Type,ErrMsg) {
// To check whether the field value is Numeric
// chk_Type : 0 means pure numeric 
//			  1 means numeric + dots and dash  

	var theString;
	
		switch (chk_Type) {
			case 0:
				theString = "1234567890";
				break;
			case 1:
				theString = "1234567890.-";		
				break;
		}
		
		var fldValue = fldName.value;
		var fldLength = fldValue.length;
		
		for (liCount = 0; liCount < fldLength; liCount ++ ) {
			// Search through string's characters one by one.
			var fldChar = fldValue.charAt(liCount);
			if (theString.indexOf(fldChar) == -1 ) {
				
				ErrorMsg(fldName,ErrMsg);
				return true;				
			}
		}
}

function IsName(fldName,ErrMsg) {
// To check whether the UserName or critical Fields are valid strings

	var theString = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890._-"
	
		var fldValue = fldName.value;
		var fldLength = fldValue.length;
		
		for (liCount = 0; liCount < fldLength; liCount ++ ) {
			// Search through string's characters one by one.
			var fldChar = fldValue.charAt(liCount);
			if (theString.indexOf(fldChar) == -1 ) {
				ErrorMsg(fldName,ErrMsg);
				return true;
			}
		}
}

function IsEmail(fldName,ErrMsg) {
// To check whether the value is a valid Email Address

	var theString = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890._-@"
	
		var fldValue = fldName.value;
		var fldLength = fldValue.length;
		var li_Check = 0
		var liCount;
		
		for (liCount = 0; liCount < fldLength; liCount ++ ) {
			// Search through string's characters one by one.
			var fldChar = fldValue.charAt(liCount);
			if (theString.indexOf(fldChar) == -1 ) {
				ErrorMsg(fldName,ErrMsg);
				return true;
			}
			else {
				// To check the number of times the @ or . is there in the value
				var s_fldChar = fldValue.substring(liCount,liCount+1);
				if ((s_fldChar == "@") || (s_fldChar == ".")) {
					li_Check = li_Check + 1;
				}
			}
		}
		if (li_Check < 2) {
			// If @ or . total count is greater than 2 
			ErrorMsg(fldName,ErrMsg);
			return true;
		}
}

function IsSelect(fldName,ErrMsg){
// To check whether the Index of the select options is not zerp

		var fldValue = fldName.selectedIndex;
		
		if (fldValue == 0) {
			ErrorMsg(fldName,ErrMsg)
			return true;
		}
}

function SetColor(fldName) {
// To set a different color to the form field when Error Found in validation

		if (fldName.style) {
			fldName.style.backgroundColor = "#FFCC99"
		} 	
}

function ErrorMsg(fldName,ErrMsg) {
// To show Error Messages Encountered in the form validation
	
		alert(ErrMsg);
		SetColor(fldName);
		fldName.focus();
}