
/********************************************************************* 
* validation.js.                                                     *
* Form validation functions for validating entire forms (onSubmit)   *
* or on-the-fly validation. (onBlur / onChange)                      *
* Copyright 2003 Sunarc technologies pvt ltd, bikaner.              *
*********************************************************************/

// Functions for onSubmit form validation.

function isAlpha(val, len, req) {
// Check for "a-z", "A-Z", " ", and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      return false;
   }
   if (val.value.length != 0) {
      for (i = 0; i < val.value.length; i++) {
         var ch = val.value.charAt(i);
         if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch == " ")) {
            continue;
         } else {
            return false;
         }
      }
      /*if (len != "0") {
         if (val.value.length != len) {
            return false;
         }
      }*/
   }
   return true;
}

function isDate(val, req) {
// Check for a properly formatted ANSI short date or a date shortcut.
   if ((val.value.length == 0) && (req == "R")) {
      return false;
   }
   if (val.value.length != 0) {
      var longform =  /^[0-9]{2}-[0-1][0-9]-[0-9]{4}$/;
         if (longform.test(val.value)) {
            return true;
         }
         return false;
   }
   return true;
}

function isTime(val) {
// Check for a properly formatted time.
//   if ((val.value.length == 0) && (req == "R")) {
  //    return false;
   //}
   if (val.value.length != 0) {
    //  var timeformat = /^[0-9]{2}:[0-9]{2}:[0-9]{2}$/;
      var timeformat = /^[0-9]{2}:[0-9]{2}$/;
      if (!timeformat.test(val.value)) {
         alert("Please enter a time in [hh:mm] format.");
         val.focus();
         val.select();
         return false;
      }
   }
   return true;
}

function isEmail(val, req) {
// Check for a properly formatted email address.
   if ((val.value.length == 0) && (req == "R")) {
      return false;
   }
   if (val.value.length != 0) {
     // var emailformat = /^.+@.+\..{2,3}$/;
      var emailformat = /^[^@\s]+@([-a-z0-9]+\.)+([a-z]{2}|com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum)$/;
      if (!emailformat.test(val.value)) {
         return false;
      }
   }
   return true;
}

function isNum(val, len, req) {
// Check for "0-9", ".", "-", and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      return false;
   }
   if (val.value.length != 0) {
      for (i = 0; i < val.value.length; i++) {
         var ch = val.value.charAt(i);
         if ((ch >= "0" && ch <= "9") || (ch == ".") || (ch == "-")) {
            continue;
         } else {
            return false;
         }
      }
      /*if (len != "0") {
         if (val.value.length != len) {
            return false;
         }
      }*/
   }
   return true;
}

function isPhone(val, req) {
// Check for a North American style phone number (555-555-5555).
   if ((val.value.length == 0) && (req == "R")) {
      return false;
   }
   if (val.value.length != 0) {
      var phoneformat = /^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/;
      if (!phoneformat.test(val.value)) {
         return false;
      }
   }
   return true;
}

function isText(val, len, req) {
// Check that field is not empty and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      return false;
   }
    if (val.value.length > len) {
     if (len != "0") {
         if (val.value.length != len) {
            return false;
         }
      }
   }
   return true;
}

// The same functions modified for onBlur / onChange form validation

function BisAlpha(val, len, req) {
// Check for "a-z", "A-Z", " ", and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      for (i = 0; i < val.value.length; i++) {
         var ch = val.value.charAt(i);
         if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch == " ") || (ch >= "0" && ch <= "9")) {
            continue;
         } else {
            alert("Please enter only alphabets.");
            val.focus();
            val.select();
            return false;
         }
      }
      /*if (len != "0") {
         if (val.value.length != len) {
            alert("This field must contain " + len + " ALPHA characters.");
            val.focus();
            val.select();
            return false;
         }
      }*/
   }
   return true;
}
///////////FUNCTION TO CHECK TEH SPECIAL CHARACTER ARE NOT ALLOWED
function BisSpecail(val) {
   if (val.value.length != 0) {
  
   for (i = 0; i < val.value.length; i++) {
         var ch = val.value.charAt(i);
          if ((ch == "^") || (ch == "#") || (ch == "$") || (ch == "\"") || (ch == "'") || (ch == "£") || (ch == "@") ) {
            alert("You cannot use £, #,$, @ or '  in a text message. Please remove these before sending.");
            val.focus();
            val.select();
            return false;           
         } else {

          continue;}
      }
      /*if (len != "0") {
         if (val.value.length != len) {
            alert("This field must contain " + len + " ALPHA characters.");
            val.focus();
            val.select();
            return false;
         }
      }*/
   }
   return true;
}

////////////////////////////////////////////////////////////


function BisDate(val, req) {
// Check for a properly formatted ANSI short date or a date shortcut.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
	  val.value='';
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      var longform =  /^[0-9]{2}-[0-9]{2}-[0-9]{4}$/;
         if (longform.test(val.value)) {
            return true;
         }
         alert ("Please enter date as format (DD-MM-YYYY)");
		 val.value='';
         val.focus();
         val.select();
         return false;
   }
   return true;
}

function BisTime(val, req) {
// Check for a properly formatted time.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      var timeformat = /^[0-9]{2}:[0-9]{2}:[0-9]{2}$/;
      if (!timeformat.test(val.value)) {
         alert ("Please enter time as format (HH:MM:SS)");
         val.focus();
         val.select();
        return false;
      }
   }
   return true;
}


function BisEmail(val, req) {
// Check for a properly formatted email address.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   var email = val.value.toLowerCase();
   if (val.value.length != 0) 
   {
		  var emailformat = /^[^@\s]+@([-a-z0-9]+\.)+([a-z]{2}|com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum)$/;
		//var emailformat = /^.+@.+\..{2,3}$/;
	  	if (!emailformat.test(email)) {
         alert("Please enter a valid email Id.");
         val.focus();
         val.select();
         return false;
      }
   }
	val.value=email;
   return true;
}

function BisURL(val, req) {
// Check for a properly formatted url.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   var email = val.value.toLowerCase();
   var checkemail="";
    if (val.value.length >11) 
     {
	    	
		if(email.substring(0,11) == 'http://www.')
	   		checkemail = email.substring(11, email.length);
	

		var emailformat = /^(([-a-z0-9]+\.)+([a-z]{2}|com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum))$/;
		//var emailformat = /^.+\..{2,3}$/;
	  	if (!emailformat.test(checkemail)) {
         alert("Please enter a valid URL.");
         val.focus();
         val.select();
         return false;
      }
   }
	val.value=email;
   return true;
}

function BisNum(val, len, req) {
// Check for "0-9", ".", "-", and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      for (i = 0; i < val.value.length; i++) {
         var ch = val.value.charAt(i);
         if ((ch >= "0" && ch <= "9") || (ch == ".") || (ch == "-")) {
            continue;
         } else {
            alert("Please enter numeric characters only.");
            val.focus();
            val.select();
            return false;
         }
      }
      /*if (len != "0") {
         if (val.value.length != len) {
            alert("This field must contain " + len + " NUMERIC characters.");
            val.focus();
            val.select();
            return false;
         }
      }*/
   }
   return true;
}
// function for checking that field is number
function BisNumber(val,req) {
// Check for "0-9", ".", "-", and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      for (i = 0; i < val.value.length; i++) {
         var ch = val.value.charAt(i);
         if ((ch >= "0" && ch <= "9") || (ch == ".")) {
            continue;
         } else {
            alert("Please enter numeric characters only.");
            val.focus();
            val.select();
            return false;
         }
      }
      /*if (len != "0") {
         if (val.value.length != len) {
            alert("This field must contain " + len + " NUMERIC characters.");
            val.focus();
            val.select();
            return false;
         }
      }*/
   }
   return true;
}


// end is num
function BisPhone(val, req) {
// Check for a North American style phone number (555-555-5555).
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      var phoneformat = /^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/;
      if (!phoneformat.test(val.value)) {
         alert ("Please enter phone number with format (555-555-1212)");
         val.focus();
         val.select();
         return false;
      }
   }
   return true;
}
function BisMobile(val, req) {
// Check for a North American style phone number (555-555-5555).
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   if (val.value.length != 0) {
      //var phoneformat = /^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/;
	  var phoneformat = /[\+0-9]$/;
     
	  if (!phoneformat.test(val.value)) {
         alert ("Please enter phone number with format (+4464564646)");
         val.focus();
         val.select();
         return false;
      }
   }
   return true;
}
function BisText(val, len, req) {
// Check that field is not empty and optionally length.
   if ((val.value.length == 0) && (req == "R")) {
      alert("This field is required.");
      val.focus();
      val.select();
      return false;
   }
   /*if (val.value.length != 0) {
      if (len != "0") {
         if (val.value.length != len) {
            alert("This field must contain " + len + " ALPHA-NUMERIC characters.");
            val.focus();
            val.select();
            return false;
         }
      }
   }*/
   return true;
}

// Check that field is not empty and have the length.
function BisLen(val, len)
{
   if (val.value.length > len)
   {
      alert("This field can not have more than "+len+" characters.");
      val.focus();
      val.select();
      return false;
   }
   return true;
}

function isConfirmed(message) {
// Pop up a confirmation box. Return true for OK and false for Cancel.
   if (!confirm(message)) {
      return false;
   }
   return true;
}

//THIS IS FUNCTION CountLetter() TO COUNT HOW MANY LETTERS ARE REST TO ENTER IN THE CONTTROL
//var FieldName is name of the field, whose length is to be count
//var MaxLen is max letters, to be entered in the field
//var DisplayField is the control to display the message
//var NextFocus is the control to which focus is transered on limit over.
function CountLetter(FieldName, MaxLen, DisplayField)
{
	var CurLen = FieldName.value.length + 1;
	
	//if max letters has been entered, don't accept the character
		if(CurLen >= MaxLen)
		{	
			BisLen(FieldName,MaxLen);
			DisplayField.value = 'Limit over.';
			FieldName.value = FieldName.value.substring(0,MaxLen-1);
			return;
		}
		else
		{
			DisplayField.value = (MaxLen-CurLen+1) + ' characters more.';		
		}
		
}
//THIS IS FUNCTION CountLetter1() TO COUNT HOW MANY LETTERS ARE REST TO ENTER IN THE CONTTROL
//var FieldName is name of the field, whose length is to be count
//var MaxLen is max letters, to be entered in the field
//var DisplayField is the control to display the message
//var NextFocus is the control to which focus is transered on limit over.
function CountLetter1(FieldName, MaxLen, DisplayField)
{
	var CurLen = FieldName.value.length + 1;
	
	//if max letters has been entered, don't accept the character
		if(CurLen >= MaxLen)
		{	
			//BisLen(FieldName,MaxLen);
			DisplayField.value = 'Limit over.';
			FieldName.value = FieldName.value.substring(0,MaxLen-1);
			return;
		}
		else
		{
			DisplayField.value = (MaxLen-CurLen+1) + ' characters more.';		
		}
		
}

//compare date
function DateComparison(date1, date2)
{
   var d1,d2,m1,m2,y1,y2;
   
   d1=eval(date1.substring(0,2));
   d2=eval(date2.substring(0,2));
   m1=eval(date1.substring(3,5));
   m2=eval(date2.substring(3,5));
   y1=eval(date1.substring(6,10));
   y2=eval(date2.substring(6,10));
  
   //return true if second is greater  or equal to first date
   	if (y2 > y1)
	{
   		return true;
	}
	else
	{
		if(y1 > y2)
		{
			return false;
		}
		else
		{
			if(m2>m1)
			{
				return true;
			}
			else
			{
				if(m1>m2)
				{
					return false;
				}
				else
				{
					if(d2>=d1)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
		}
	}   
 }
 
//compare date
function CheckNewDate(thedate)
{
   var d1,d2,m1,m2,y1,y2;
   var newdate=new Date();
   
   d1=eval(thedate.substring(0,2));
   d2=eval(newdate.getDate());
   m1=eval(thedate.substring(3,5));
   m2=eval(newdate.getMonth())+1;
   y1=eval(thedate.substring(6,10));
   y2=eval(newdate.getYear());
   
  
   //return true if second is greater  or equal to first date
   	if (y1 > y2)
	{
   		return true;
	}
	else
	{
		if(y1 < y2)
		{
			return false;
		}
		else
		{
			if(m1 > m2)
			{
				return true;
			}
			else
			{
				if(m2 > m1)
				{
					return false;
				}
				else
				{
					if(d1 >= d2)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
		}
	}   
 }
   

// The following functions are specific to esingapore.
//validate admin change password
function valid_admin_changepassword(form)
{
   var message="", error=0;
   
   if (form.oldpassword.value=='') { message+="Please enter old password.\n"; error=1; }
   if (form.newpassword.value=='') { message+="Please enter new password.\n"; error=1; }
   if (form.confirmpassword.value=='') { message+="Please enter confirm new password.\n"; error=1; }
	if (error==0)
	{
   		if (!(form.newpassword.value == form.confirmpassword.value)) { message+="Please re-enter confirm new password.\n"; error=1;
            form.confirmpassword.focus();
            form.confirmpassword.select();
		 }
	}
	if (error==1)
    { alert(message); return false; }
   return true;
}



//function to check record deletion
function DeleteRecord()
{
	var temp;

	temp=(confirm("On deletion of this record, all related records will be deleted from the database.\n Do you want to delete the record?"));
	if(temp)
	{
		return true;
	}
	else
	{ 
		return false;
	}
}

//function to check category deletion
function DeleteCategory(Category)
{
	if(Category.value == '')
	{
		alert ("Sorry, you can't delete without selecting any category. Please select category and then try.");
		Category.focus();
		return false;
	}
	else
	{
		var temp;
		temp=(confirm("On deletion of this record, all related records will be deleted from the database.\n Do you want to delete the record?"));
		if(temp)
		{
			return true;
		}
		else
		{ 
			return false;
		}
	}
}


//function to trim a string
function Trimmer(controlName) { 
	pVal = controlName.value;
    TRs=0; 
    for (i=0; i<pVal.length; i++) 
	{ 
        if (pVal.substr(i,1)==" ") 
		{
			TRs++;
		} 
		else 
			{break;} 
    } 

    TRe=pVal.length-1; 
    for (i=TRe; i>TRs-1;i--)
	{ 
        if (pVal.substr(i,1)==" ") 
		{
			TRe--;
		}
		else 
			{break;} 
    } 

    controlName.value=pVal.substr(TRs, TRe-TRs+1); 
} 
//end trim function


//VALIDATE ENTRY FORM PERSONAL INFO
function ValidatePersonalInfo()
 {
  var imgstr="";
  var error="";
  var message="";
  imgstr=document.article.article.value;
  
 /*
  if(document.article.details.value==""|| document.article.details.value.length<20 )
   {
     if(error==0)
	  document.article.details.focus();
	 error=1;
	 message=message + "Ad detail not be less then 20 characters.\n";
   }
   */
  if(document.article.name.value=="")
   {
     if(error==0)
	  document.article.name.focus();
	  error=1;
	  message=message + "Please enter your name.\n";
  }		  
 if(document.article.email.value=="")
  {
       if(error==0)
	    document.article.email.focus();
		error=1;
		message=message + "Please enter email address.\n";
  }
if(document.article.profession.value=="")
 {
    if(error==0)
	  document.article.profession.focus();
	   error=1;
	   message=message + "Please enter your profession .\n";
 }
 if(document.article.title.value=="")
  {
      if(error==0)
	     document.article.title.focus();
		error=1;
		message=message + "Please enter title of article.\n";
  }	
  if(imgstr=="")
   {
      if(error==0)
	     document.article.article.focus();
		error=1;
		message=message + "Please upload the article file.\n";
   }
 if(imgstr!="")
  {
     
     Ext=imgstr.substring(imgstr.lastIndexOf('.')+1);
	 Ext=Ext.toLowerCase();
     if(Ext!='doc' && Ext!='pdf' && Ext!='txt')
	  {
	    error=1;
		message=message + "Please upload only doc,txt,pdf format file.\n";
	  }
   }
 
 if(error==1)
  {
    alert(message);
    return false;
  }
 else
   return true;
   
}
//end function   
function TimeFormat(controlname)
{
  // alert("hiii");
   var hr,sec;
   var tim=controlname.value
   var com=tim.split(':');
       hr=eval(com[0]);
	   sec=eval(com[1]);
	   if(hr>23 || sec>60)
	   { alert("Please enter time in correct format as 22:30 for 10:30 pm");
	     controlname.focus();		 
	   }
	 	  
}
function checkTime(toVal)
{
  var fromVal=document.adwork.fromtime.value;
  var ToTime = toVal.split(':');
  toVal = eval(ToTime[0]+'.'+ToTime[1]);
  var FromTime = fromVal.split(':');
  fromVal = eval(FromTime[0]+'.'+FromTime[1]);

  //alert(toVal+"="+fromVal);
  if(eval(toVal)<=eval(fromVal))
   {
     alert("Please specify a valid time range.");
	 document.adwork.totime.focus();
   }
}
//=======================================================================\\
//=======CHECK THAT FIELD IS A VALID NUMBER FIELD========================\\
//=======================================================================\\
function isNumberField(theField)
{
  var numFlag=true;
  var countDot=0;
  var ch="";
  var newValue="";
  for(i=0;i<theField.value.length;i++)
   {
     ch=theField.value.charAt(i);
	 if(ch==":")
	  countDot+=1;
     if ((ch >= "0" && ch <= "9") || (ch == ":"))
	  {
		  if(countDot>1)
		  {
		   continue;
		  }
		  else
		  {
		   newValue+=ch;  //NEW VALUE OF THE NUMBER
		  }	
	  }
	 else
	 {
		continue;
	 } 
   } 
   //====for correct new value==\
   theField.value=newValue;
}
