
var emptyString = /^\s* $/;

// -----------------------------------------
//                  msg
// Display warn/error message in HTML element
// commonCheck routine must have previously been called
// -----------------------------------------

function msg(fld,    // id of element to display message in            
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  

};

/********************************** NUMERIC CHECK ********************************/
/* required to pass object and the id of next cell where you want to print error 
		it will check for only 0-9
*/ 


function check_Numeric(myfld, ifid)
{	
	if(myfld.value.search(/[^0-9]/) != -1) // only number checking
	{
		msg(ifid,"Only Numeric value allowed");
		myfld.value="";
		myfld.focus();
		return false;			
	}
	msg(ifid,"");
	return true;
}

// This function is used to validate phone number and fax
// By Paresh Kharsan
// Date : July 6, 2005

function check_phone(myfld, ifid)
{	
	if(myfld.value.search(/[^0-9+ ]/) != -1 || plus_Check(myfld.value) > 1) 
	{
		msg(ifid,"Invalid Number");
		myfld.value="";
		myfld.focus();
		return false;			
	}
	else if(plus_Check(myfld.value) == 1 && myfld.value.indexOf('+') != 0)
	{
		msg(ifid,"Invalid Number");
		myfld.value="";
		myfld.focus();
		return false;		
	}
	msg(ifid,"");
	return true;
}
 

function plus_Check(val)
{
	count = 0;
	for(pls=0; pls<val.length; pls++)
	{
		if(val.substring(pls,pls+1) == '+') 
			count++;
	}
	return count;
}

// End of telephone number validation




function isPrice(myfld, ifid, acceptLength)
{	
	 var input = myfld.value;
	 if (isNaN(input) || input == "") {
	 	msg(ifid,"Please insert valid value");
		myfld.value="";
		myfld.focus();
		return false;			
	 }
	 else {
	 // if the number is interger then add point after the interger number.
		 if (input.indexOf('.') == -1) {
		 	input += ".";
		 }
	 // using string.substring(from, to) function
	 // It returns a new string which contains a substring of string.
		 restText = input.substring(input.indexOf('.')+1, input.length);
	
		 if (restText.length > acceptLength)
		 {
			 msg(ifid,"Please insert valid amount");		
			 myfld.value="";
			 myfld.focus();
			 return false;		
		 }
		 else {
			 msg(ifid,"");
			 return true;
		 }
	 }
}



/********************************** CHECK FOR BLANK ********************************/
/* pass the object and id of the next cell	*/ 


function check_Blank(myfld, ifid)
{
	
	if(trim(myfld.value)=="")
	{
		msg(ifid,"Blank is not allowed");		
		return false;
	}
	msg(ifid,"");
	return true;
}


/******************************** TO REMOVE LEADING SPACES.******************************/
/* required to pass string value only */ 

function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

/******************************** TO REMOVE TRAILING SPACES.******************************/
/* required to pass string value only */ 

function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}


/******************************** TO REMOVE TRAILING & LEADING SPACES.******************************/
/* required to pass string value only */ 

function trim(str) {		
   return rtrim(ltrim(str));
}


/******************************** FUNCTION TO CHECK DATE ******************************/
/* required to pass date and format of that date
	DateToCheck : pass the that that u want to check, u can seperat year, month and
					date using ".", " ", "-", "/"
	FormatString : format of the date must be either "dd-mm-yyyy" or "dd-mmm-yyyy"
	ifid		: where u want to print error
		
*/

function check_Date(DateToCheck, FormatString, ifid) 
{

  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
	  	msg(ifid,"Date is not valid");
	    return false;
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i=0; i<strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }

  if (intDateSeparatorIdx != intFormatSeparatorIdx) {
  	msg(ifid,"Date is not valid");
    return false;
  }

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
	  	msg(ifid,"Date is not valid");	
		return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
	  	msg(ifid,"Date is not valid");		
      return false;
    }

    for (i=0; i<strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm') {
        strMonth = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'dd') {
        strDay = strDateToCheckArray[i];
      }

      if (strFormatArray[i] == 'yyyy') {
        strYear = strDateToCheckArray[i];
      }
    }
  } else {
    if (FormatString.length > 7) {
      if (strFormatString.indexOf('mmm') == -1) {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mm'), 2);
      } else {
        strMonth = strDateToCheck.substring(strFormatString.indexOf('mmm'), 3);
      }

      strDay = strDateToCheck.substring(strFormatString.indexOf('dd'), 2);
      strYear = strDateToCheck.substring(strFormatString.indexOf('yyyy'), 2);
    } else {
	  	msg(ifid,"Date is not valid");
      return false;
    }
  }

  if (strYear.length != 4) {
   	msg(ifid,"Date is not valid");
    return false;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) 
  {
   	msg(ifid,"Date is not valid");
    return false;
  }
  if (intday < 1)
  {
   	msg(ifid,"Date is not valid");
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) 
  {
    for (i=0; i<strMonthArray.length; i++) 
	{
      if (strMonth == strMonthArray[i]) 
	  {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) 
	{
	  	msg(ifid,"Date is not valid");
      return false;
    }
  }
  if (intMonth > 12 || intMonth < 1) 
  {
  	msg(ifid,"Date is not valid");	  
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) 
  {
   	msg(ifid,"Date is not valid");
    return false;
  }
  if (IsLeapYear(intYear) == true) 
  {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1])
  {
   	msg(ifid,"Date is not valid");
    return false;
  }
 	msg(ifid,"");  
  return true;
}



/******************************** TO CHECK LEAP YEAR *******************************/
/* Required to pass the value of the year */

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0)  {
	    return true;
		}
  } else {
    if ((intYear % 4) == 0) {
		return true;	
    }
  }
  return false
}


/******************************** FUNCTION TO CHECK VIDEO EXTATION	 ******************************/
/* required to pass object and the id of next cell where you want to print error 
*/


function check_Video(myfld, ifid)
{
	file_name=myfld;	
	
	if (myfld.value=="")
	{
		msg(ifid,"Blank id not allowed");
		file_name.focus();
		return false;
	}
	
	myfld=myfld.value;	
	
	extArray = new Array(".mp2", ".mpeg", ".dat", ".mov", ".avi", ".ram", ".rm", ".3gp");
	
	allowSubmit = false;

	while (myfld.indexOf("\\") != -1)
	myfld = myfld.slice(myfld.indexOf("\\") + 1);
	ext = myfld.slice(myfld.lastIndexOf(".")).toLowerCase();	
	
	for (var i = 0; i < extArray.length; i++) 
	{
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	if (allowSubmit) {
		msg(ifid,"");
		return true;
	}
	else
	{
		msg(ifid,"Please upload valid video file.");
		file_name.focus();
		return false;
	}
}



/******************************** FUNCTION TO CHECK IMAGE EXTATION ******************************/
/* required to pass object and the id of next cell where you want to print error 
*/

function check_Image(myfld, ifid)
{
	file_name=myfld;	
	
	if (myfld.value=="")
	{
		
		msg(ifid,"Blank id not allowed");
				
		file_name.focus();
		return false;
	}
	
	myfld=myfld.value;	
	extArray = new Array(".gif", ".jpg", ".jpeg");
	
	allowSubmit = false;
	
	while (myfld.indexOf("\\") != -1)
	myfld = myfld.slice(myfld.indexOf("\\") + 1);
	ext = myfld.slice(myfld.lastIndexOf(".")).toLowerCase();	
	for (var i = 0; i < extArray.length; i++) 
	{
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	if (allowSubmit) {
		msg(ifid,"");
		return true;		
	}
	else
	{
		msg(ifid,"Please upload valid image file.");
		file_name.focus();
		return false;
	}
}


/******************************** FUNCTION TO CHECK AUDIO EXTATION	 ******************************/
/* required to pass object and the id of next cell where you want to print error 
*/

function check_Audio(myfld, ifid)
{
	file_name=myfld;	
	
	if (myfld.value=="")
	{
		msg(ifid,"Blank id not allowed");
		file_name.focus();
		return false;
	}
	
	myfld=myfld.value;	
	
	extArray = new Array(".mp3", ".wav", ".wma", ".au", ".sam", ".smp", ".mp2", ".ram", ".rm");
	
	allowSubmit = false;

	while (myfld.indexOf("\\") != -1)
	myfld = myfld.slice(myfld.indexOf("\\") + 1);
	ext = myfld.slice(myfld.lastIndexOf(".")).toLowerCase();	
	
	for (var i = 0; i < extArray.length; i++) 
	{
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	if (allowSubmit) {		
		msg(ifid,"");
		return true;
	}
	else
	{
		msg(ifid,"Please upload valid audio file. ");
		file_name.focus();
		return false;
	}
}


/********************************** CHECK FOR EMAIL ********************************/
/* pass the object and id of the next cell	*/ 


function check_Email(myfld, ifid)
{
	var flg = true;
	var index = myfld.value.indexOf("@");
	var spc = myfld.value.indexOf(" ");
	if(spc == -1)
	{
		if (index > 0)
		{
			var pindex = myfld.value.indexOf(".",index);
			if (!(pindex > index+1) && (myfld.value.length > pindex+1))
			{
				msg(ifid,"Please, enter valid email address");
			 	flg=false;
			}	
	  	}
	 	else
	  	{	
	  		msg(ifid,"Please, enter valid email address");
			flg=false;
	  	}
	}
	else
	{
		msg(ifid,"Please, enter valid email address");
		flg=false;
	}	
	if(flg)
	{
		SingleQuote = myfld.value.indexOf("'");
		if(SingleQuote!= -1)
		{
			msg(ifid,"Please, enter valid email address");
			flg=false;
		}
	}
	if (!check_SpecialcharForEmail(myfld,ifid)){
		msg(ifid,"Please, enter valid email address");
		flg=false;		
	}
	
	var hashcount;
	
	hashcount=0;
	
	for (var i = 0; i < myfld.value.length; i++) 
	{
		if (myfld.value.charAt(i)=="@") 
		{		 	 
			hashcount++;
		}
	}
	
	if (hashcount!=1)
	{
		msg(ifid,"Please, enter valid email address");
		flg=false;
	}
	
	if(!flg)
	{
		myfld.focus();
		return false;
	}
	msg(ifid,"");
    return true;
}


/********************************** RADIO BUTTON CHECKING ********************************/
/* required to pass object and the id of next cell where you want to print error 
*/ 


function check_Radio(myfld, ifid)
{ 
	// require at least one radio in this group to be checked
  if(!myfld.length) 
  {
	msg(ifid,"");
	return true; // invalid parameter
  }
  var visible= false, enabled= false;
  for(var i= 0; i < myfld.length; i++)
  {
    if(!enabled) enabled= !myfld[i].disabled;
    if(myfld[i].checked) 
	{
		msg(ifid,"");		
		return true;
	}
	else if(myfld[i].offsetWidth == undefined || myfld[i].offsetWidth > 0) 
	{	
		visible= true;
	}
  }
  if(!visible||!enabled)
  {
	 msg(ifid,"");
  	 return true; // no visible/enabled options in this group
  }
  
  msg(ifid,'You must select one of the options.');
  return false;  
}



/********************************** CHECK FOR ALPHANUMERIC ********************************/
/* pass the object and id of the next cell	

	it will check for only 0-9,A-Z,a-z
*/ 


function check_AlphaNumeric(myfld, ifid)
{	
	if(myfld.value.search(/[^0-9^a-z^A-Z]/) != -1)  
	{
		msg(ifid,"Only Alpha numeric is allowed");
		myfld.value="";
		myfld.focus();
		return false;			
	}
	msg(ifid,"");
	return true;
}

/********************************** CHECK FOR ALPHABETIC ********************************/
/* pass the object and id of the next cell	
	it will check for only a-z, A-Z

*/ 


function check_Alpha(myfld, ifid)
{	
	if(myfld.value.search(/[^a-z^A-Z]/) != -1)  
	{
		msg(ifid,"Only alphabatic is allowed.");
		myfld.value="";
		myfld.focus();
		return false;			
	}
	msg(ifid,"");
	return true;
}

/********************************** CHECK FOR CHECKBOX ********************************/
/* required to pass object and the id of next cell where you want to print error 
	minValue	:	number of minimum checkbox must be checked
	maxValue	:	number of maximum checkbox must be checked
*/ 

function check_Checkbox  (myfld,   // checkboxes to be validated
                            ifld,   // id of element to receive info/error msg
                            minValue,    
                            maxValue)                                    
{
	
  var count = 0;
  for (var j=0; j<myfld.length; j++)
     if (myfld[j].checked) count++;

  if (count>=minValue && count<=maxValue) 
  {
  		msg (ifld, "");
	  return true;
  }
  else 
  {	  
	   var errorMsg;
	
	  errorMsg = 'Minimum ' + minValue + ' and Maximum ' + maxValue + ' checkbox must be checked';
	  
	  msg (ifld, errorMsg);
	  myfld[0].focus();
	  return false;
  }
}

/********************************** CHECK FOR SPECIAL CHARACTER ********************************/
/* required to pass object and the id of next cell where you want to print error 	
*/ 

function check_Specialchar(myfld, ifld)
{
	var iChars = "-_!@#$%^&*()+=[]\\\';,./{}|\":<>?";

	  for (var i = 0; i < myfld.value.length; i++) 
	  {
		if (iChars.indexOf(myfld.value.charAt(i)) != -1) 
		{
		 msg (ifld, "Special characters are not allowed.");		 
		 return false;
		}
	  }
	  return true;
}

/********************************** CHECK FOR MIMINUM & MAXIMUM LENGHT ********************************/
/* required to pass object and the id of next cell where you want to print error 	
	minValue	:	number of minimum character
	maxValue	:	number of maximum character
*/ 

function check_length(myfld, ifld, minValue, maxValue)
{
	
	  if (myfld.value.length >= minValue && myfld.value.length <= maxValue)
	  {
		 msg (ifld, "");		 
		 return true;
	  } 
	  else 
	  {		  
	  	 msg (ifld,' should be ' + minValue + '-' + maxValue + ' characters.');
	  	return false;
	  }
}

function check_length1(myfld, ifld, maxValue)
{
	  if (myfld.value.length >= minValue && myfld.value.length <= maxValue)
	  {
		 msg (ifld, "");		 
		 return true;
	  } 
	  else 
	  {		  
	  	 msg (ifld,'should be ' + minValue + '-' + maxValue + ' characters.');
	  	return false;
	  }
}
// ADDED BY RANJANA PRAJAPATI...ON 18/06/2005
function check_length2(myfld, ifld, minValue, maxValue)
{
	  if (myfld >= minValue && myfld <= maxValue)
	  {
		 msg (ifld, "");		 
		 return true;
	  } 
	  else 
	  {		  
	  	 msg (ifld,'shoule bd ' + minValue + '-' + maxValue + ' characters.');
	  	return false;
	  }
}
/********************************** STRING COMPARISION ********************************/
/* required to pass object and the id of next cell where you want to print error 	
	minValue	:	number of minimum character
	maxValue	:	number of maximum character
*/ 

function string_compare(myfld, myfld2, ifld, err_mesg)
{

	  if (myfld.value==myfld2.value) 
	  {
		 msg (ifld, "");		 
		 return true;
	  } 
	  else 
	  {		  
	  	 msg (ifld,err_mesg);
	  	return false;
	  }
}

/********************************** CHECK SPACE ********************************/
/* required to pass object and the id of next cell where you want to print error 		
	
*/
function check_space(myfld, ifld)
	{
		var iChars = " !@#$%^&*()+=[]\\\';,./{}-_|\":<>?";

		for (var i = 0; i < myfld.value.length; i++) 
		{
			if (iChars.indexOf(myfld.value.charAt(i)) != -1) 
			{
				msg (ifld, "Special character are not allowed.");		 
				return false;
			}
		}
		return true;
	}	

// ADDED BY RANJANA PRAJAPATI...ON 18/06/2005
function check_space1(myfld, ifld)
	{
		var iChars = " !@#$%^&*()+=[]\\\'_-;./{}|\":<>?";

		for (var i = 0; i < myfld.value.length; i++) 
		{
			if (iChars.indexOf(myfld.value.charAt(i)) != -1) 
			{
				msg (ifld, "Special character are not allowed.");		 
				return false;
			}
		}
		return true;
	}	

// ADDED BY RANJANA PRAJAPATI...ON 18/06/2005	
/********************************** CHECK IP ADDRESS ********************************/
/* required to pass object and the id of next cell where you want to print error 		
	ip_address :	IP address that u want to check
*/
	function verifyIP (IPvalue,ifld) {
		errorString = "";
		theName = "IPaddress";
		
		var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
		var ipArray = IPvalue.match(ipPattern); 
		
		if (IPvalue == "0.0.0.0")
			errorString = errorString + theName + ': '+IPvalue+' is a special IP address.';
		else if (IPvalue == "255.255.255.255")
			errorString = errorString + theName + ': '+IPvalue+' is a special IP address.';
		if (ipArray == null)
			errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
		else {
			for (i = 0; i < 4; i++) {
				thisSegment = ipArray[i];
			if (thisSegment > 255) {
				errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
			i = 4;
			}
			if ((i == 0) && (thisSegment > 255)) {
				errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
			i = 4;
				  }
			   }
		}
		extensionLength = 3;
		if (errorString != "")
		{
			msg (ifld, errorString);		 
			return false;
		} 
	 return true;
	}

	function verifySUBNETIP (IPvalue,ifld) {
		errorString = "";
		theName = "IPaddress";
		
		var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
		var ipArray = IPvalue.match(ipPattern); 
		
		if (IPvalue == "0.0.0")
			errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
		else if (IPvalue == "255.255.255")
			errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
		if (ipArray == null)
			errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
		else {
			for (i = 0; i < 3; i++) {
				thisSegment = ipArray[i];
			if (thisSegment > 255) {
				errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
			i = 3;
			}
			if ((i == 0) && (thisSegment > 255)) {
				errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
			i = 3;
				  }
			   }
		}
		extensionLength = 2;
		if (errorString != "")
		{
			msg (ifld, errorString);		 
			return false;
		} 
	 return true;
	}
/********************************** COMBO BOX COMPARISION ********************************/
/* required to pass object and the id of next cell where you want to print error 		
	cmdvalue :	index of null selection
*/

function check_Combo(myfld, ifld, cmdvalue)
{
	 if(myfld.selectedIndex == eval(cmdvalue)) 
	 { 	
		  msg (ifld, "Please Select one option");
		  return false;                                   
	 } else {
		  msg (ifld, "");
		  return true;                                   
	 }
	 
}


function check_SpecialcharForEmail(myfld, ifld)
{
	
	var iChars = "~!#$%^&*()+=[]\\\;,/{}|\":<>?";
	
	  for (var i = 0; i < myfld.value.length; i++) 
	  {
		if (iChars.indexOf(myfld.value.charAt(i)) != -1) 
		{
		 msg (ifld, "Special characters are not allowed.");		 
		 return false;
		}
	  }
	  return true;
}



function check_withSpecialchar(myfld, ifld,specialchar)
{

	var tempstr = "@#$%&*!()+=[]\\\';,-_./{}|\":<>?^";
	
	for (var i = 0; i < specialchar.length; i++) 
	{
		  tempstr=tempstr.replace(specialchar.charAt(i),"");		  
	}
	
	iChars=tempstr;

	  for (var i = 0; i < myfld.value.length; i++) 
	  {
		if (iChars.indexOf(myfld.value.charAt(i)) != -1) 
		{
		 msg (ifld, "Special characters are not allowed.");		 
		 return false;
		}
	  }
	  return true;
}


