/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
 var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function stripCharsInBag(s, bag){
 var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function daysInFebruary (year){
 // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
 for (var i = 1; i <= n; i++) {
  this[i] = 31
  if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
  if (i==2) {this[i] = 29}
   } 
   return this
}
function isDate(dtStr){
 var daysInMonth = DaysArray(12)
 var pos1=dtStr.indexOf(dtCh)
 var pos2=dtStr.indexOf(dtCh,pos1+3)
 var strYear=dtStr.substring(0,pos1)
 var strMonth=dtStr.substring(pos1+1,pos2)
 var strDay=dtStr.substring(pos2+1)
/*
 var pos2=dtStr.indexOf(dtCh,pos1+1)
 var strMonth=dtStr.substring(0,pos1)
 var strDay=dtStr.substring(pos1+1,pos2)
 var strYear=dtStr.substring(pos2+1)
 */
 strYr=strYear
 if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
 if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
 for (var i = 1; i <= 3; i++) {
  if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
 }
 month=parseInt(strMonth)
 day=parseInt(strDay)
 year=parseInt(strYr)
 if (pos1==-1 || pos2==-1){
  alert("The date format should be : yyyy/mm/dd")
  return false
 }
 if (strMonth.length<1 || month<1 || month>12){
  alert("Please enter a valid month")
  return false
 }
 if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
  alert("Please enter a valid day")
  return false
 }
 if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
  alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
  return false
 }
 if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
  alert("Please enter a valid date")
  return false
 }
return true
}
function MM_validateForm() 
{ 
 var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
 j=0;
 // /^([-a-zA-Z0-9._]+@[-a-zA-Z0-9.]+(\.[-a-zA-Z0-9]+)+)$/;
 var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
 var regBlank = /[^\s]/;
 var regAlphaNum = /^([a-zA-Z0-9-+/_ :?;\n\r.,\']+)$/;
 var regZip = /^([a-zA-Z0-9]+)$/;
 var regcontact = /^([0-9-]+)$/;
 var regDate = /^([0-9_]+-[0-9][0-9]+-[0-9][0-9]+)$/;
 var regPhone=/^([0-9]{3}-[0-9]{3}-[0-9]{4})$/;
 var minLength = 5;
 var maxLength = 15;
 //alert (MM_validateForm.arguments[1].name);
 //alert("sss--->"+document.forms[""+args[0]].elements[""+args[0]].value);
 for(i=1; i<(args.length-2); i+=3) 
 { 
  mesg=args[i+1];
  test=args[i+2]; 
  val=document.forms[""+args[0]].elements[""+args[i]];
 
     if (val) 
  { nm=mesg; 
   
   val = val.value;
   //if ((val=val.value)!="") 
   if(regBlank.test(val))
   {
    if(test.indexOf('isEqual')!=-1)
    {
     result = trim(val);
    if(result.length==0){
    errors += '- '+nm+' is required.\n'; 
    }else{
     equal_obj_val = test.substring(8,test.indexOf(":"));
     mesg_string =test.substring((test.indexOf(":")+1));
     if(val != document.forms[""+args[0]].elements[""+equal_obj_val].value)
     {
      errors+='- '+nm+' must be same to '+mesg_string+'.\n';
     }
    }
    }
    else if(test.indexOf('isAlphaNum')!=-1)
    {
    var first_char;
     first_char= val.charAt(0);
     if(first_char==0||first_char==1||first_char==2||first_char==3||first_char==4||first_char==5||first_char==6||first_char==7||first_char==8||first_char==9){
      errors+='- '+nm+' must starts with  a char.\n';
     }
    result = trim(val);
    if(result.length==0){
    errors += '- '+nm+' is required.\n'; 
    }else{
     if(!regAlphaNum.test(val))
     {
      errors+='- '+nm+': Only Alpha Numeric and "_" Chars Allowed.\n';
     }
    }
    }
    else if(test.indexOf('isZip')!=-1)
    {
    
     if(!regZip.test(val))
     {
      errors+='- '+nm+' contains only alphanumeric values.\n';
     }
    
    }
    else if (test.indexOf('isDate')!=-1) 
    { 
     p=val.indexOf('-');
           
     if (p != 4 )
     {
      errors+='- '+nm+' must contain Valid Date YYYY-MM-DD.\n';
  
     }
     else if(!regDate.test(val))
     {
      errors+='- '+nm+' must contain Valid Date YYYY-MM-DD.\n';
     }
        }
     else if (test.indexOf('isExt')!=-1) 
    { 
     p=val.indexOf('.jpg');
     s=val.indexOf('.gif');
     q=val.indexOf('.jpeg');
     a=val.indexOf('.JPG');
     b=val.indexOf('.GIF');
     c=val.indexOf('.JPEG');
           if ((p<1 || p==(val.length-1)) && (s<1 || s==(val.length-1)) && (q<1 || q==(val.length-1)) 
     && (a<1 || a==(val.length-1)) && (b<1 || b==(val.length-1)) && (c<1 || c==(val.length-1)) 
     )
     {
      errors+='- '+nm+' File should be in .jpg, .jpeg, .gif format!.\n';
  
     }
    }
    else if (test.indexOf('isEmail')!=-1) 
    { 
     
     var first_char;
     first_char= val.charAt(0);
     if(first_char==0||first_char==1||first_char==2||first_char==3||first_char==4||first_char==5||first_char==6||first_char==7||first_char==8||first_char==9){
      errors+='- '+nm+'  can start only in characters.\n';
     }
     p=val.indexOf('@');
     s=val.indexOf('.');
           if (p<1 || p==(val.length-1))
     {
      errors+='- '+nm+' must contain a valid e-mail Address.\n';
  
     }
     //else if(s<p || s==(val.length-1))
     else if(!regEmail.test(val))
     {
      errors+='- '+nm+' must contain a valid e-mail Address.\n';
     }
        }
     else if (test.indexOf('isPhone')!=-1) 
    { 
     //p=val.indexOf('-');
           
      if(!regPhone.test(val))
     {
      errors+='- '+nm+' should be in the format of xxx-xxx-xxxx.\n';
     }
        }
      else if (test.indexOf('isContact')!=-1) 
    { 
     //p=val.indexOf('-');
           
      if(!regcontact.test(val))
     {
      errors+='- '+nm+' can contain numbers and "_" character.\n';
     }
     /*
     result = trim(val);
     if(result.length<minLength)
     {
      errors+='- '+nm+' must contain minimum 5 digits\n';
     }
      else if(result.length>maxLength)
     {
      errors+='- '+nm+' must contain maximum 15 digits\n';
     }
     */
        }
     else if (test.indexOf('isContactNo')!=-1) 
    { 
     //p=val.indexOf('-');
     
     if (isNaN(val)) errors+='- '+nm+' can contain only numbers.\n';
     result = trim(val);
       
      if(result.length<minLength)
     {
      errors+='- '+nm+' must contain minimum 5 digits\n';
     }
      else if(result.length>maxLength)
     {
      errors+='- '+nm+' must contain maximum 15 digits\n';
     }
        }
    else if (test.indexOf('isUrl')!=-1) 
    { 
     p=val.indexOf('http://');
     s=val.indexOf('.');
           if (p<0 || p==(val.length-1))
     {
      errors+='- '+nm+' must be valid URL e.g. http://www.abc.com\n';
  
     }
     else if(s<p || s==(val.length-1))
     {
      errors+='- '+nm+' must be valid URL e.g. http://www.abc.com\n';
     }

     /*
     function validateURL()
     {
     lengthValue = Trim(document.getElementById(txtURL).value);
     lengthValue = lengthValue.length;
     if(lengthValue != 0)
     {
     var j = new RegExp();
     j.compile("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+"); 
     lengthValue = Trim(document.getElementById('txtWeb').value);
     if (!j.test(lengthValue))
     { 
     alert(Please enter valid URL.");
     return false;
     }
     }
     */
        }
     else if (test.indexOf('isChar')!=-1) 
     { 
     var first_char;
     first_char= val.charAt(0);
     if(first_char==0||first_char==1||first_char==2||first_char==3||first_char==4||first_char==5||first_char==6||first_char==7||first_char==8||first_char==9){
      errors+='- '+nm+' must starts with  a char.\n';
     }
        }
        else if (test!='R') 
     {
     result = trim(val);
     if(result.length==0){
     errors += '- '+nm+' is required.\n'; 
     }
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
     if (test.indexOf('inRange') != -1) 
     { num = parseFloat(val);
      p=test.indexOf(':');
      min=test.substring(8,p); 
      max=test.substring(p+1);
      if (num<min || max<num) 
      errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
     } 
     if (val.indexOf('-') != -1) 
     { 
      errors+='- '+nm+' must contain a number without dashes sign.\n';
     } 
     if (val.indexOf('+') != -1) 
     { 
      errors+='- '+nm+' must contain a number without plus sign.\n';
     }
     
    }else if (test.charAt(0)=='R')
    {
    result = trim(val);
    if(result.length==0){
    errors += '- '+nm+' is required.\n'; 
    }
    } 
   }
   else if (test.charAt(0) == 'R'){
    errors += '- '+nm+' is required.\n'; 
   }
  }
  if(errors !="")
  { if(j<=0)
   {
    
    focusitem = document.forms[""+args[0]].elements[""+args[i]];
    j++;
   } 
   
  }
 } 
 
//return errors;
  
  if (errors)
  {
 alert('The following error(s) occurred:\n\n'+errors);
 
 focusitem.focus();
 return false;
   }
   else
 return true;
//  document.MM_returnValue = (errors == '');
 
}
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
//CODE FOR RECURRENCE STUFF

//For month and year
function year_validate(yr)
{
 if(!isNaN(yr.value))
   {
    var now = new Date();
 var year1= now.getYear();
 var monthnumber1 = now.getMonth();
 //monthnumber1=monthnumber1+1;
  if(yr.value.length !=4 || yr.value < year1)
  {
      alert('Expiry Year should be equal to or greater than Current Year  '+ year1);
   yr.focus();       
      return false;
      }
   }
   return true;
}
  
  //For Month
  function month_validate(mn)
  {  
  var now = new Date();
  
 //var year= now.getYear();
 var monthnumber = now.getMonth();
 monthnumber=monthnumber + 1;
  /***
       if(mn.value < monthnumber)
     {
      alert('Expiry Month should be more OR equal than Current Month  '+ monthnumber); 
  mn.focus();
        
         return true;
        }***/ 
  if(mn.value >12)
     {
      alert('Expiry Month should be equal to or less than 12'); 
     mn.focus();
        
         return false;
        } 
  return true;
   
 }
//
function valid_next_post_network1(formname)
{
 if(MM_validateForm(formname, 'txtFile', 'Photo', 'R'))
 {
  return true;
 } else{
  return false;
 }
}
function delete_confirm(form)
{
 if(form.delete1.value == "Delete")
 { 
  if(confirm("Are you sure you want to Delete? "))
  {
   form.submit;
  }
  else
  {
   return false;
  }
 }
}

function delete_confirm1(form)
{
 if(form.delete1.value == "Delete")
 { 
  if(confirm("Are you sure you want to Create Zip File? "))
  {
   form.submit;
  }
  else
  {
   return false;
  }
 }
}
function checkall(objForm){
 len = objForm.elements.length;
 var i=0;
 for( i=0 ; i<len ; i++) {
  if (objForm.elements[i].type=='checkbox') {
   objForm.elements[i].checked=objForm.check_all.checked;
  }
 }
}
function is_any_check_box_checked(fObj)
{
 found=false;
 for(i=0;i<fObj.length;i++)
 {
  if(fObj[i].type=="checkbox" && fObj[i].checked) 
  {
   found=true;
   break 
  }  
 }
 return found;
}
function checkCheckboxes(fObj)
{  
 ////////alert("aaaaaa");
 if(is_any_check_box_checked(fObj)==true)
 {
  if(confirm("Are you sure?"))
  {
   return true;  
  }
  else 
  {
   return false;  
  }
 }
 else if(is_any_check_box_checked(fObj)==false)
 {
  alert("Select at least one check box.");  
  return false;
 }
}

function chk_con_us_page(formname)
{
 if(MM_validateForm(formname,'dname','Name','R','demail','Email ID','RisEmail','comments','Commets','R'))
 {
  return true;
 } 
 
 else
  {
  return false;
}
}

function chk_fdbck_us_page(formname)
{
 if(MM_validateForm(formname,'from_name','Name','R','from_email','Email ID','RisEmail','comments','Commets','R'))
 {
  return true;
 } 
 
 else
  {
  return false;
}
}


function chk_subscrb_page(formname)
{
 if(MM_validateForm(formname,'email','Email ID','RisEmail'))
 {
  return true;
 } 
 
 else
  {
  return false;
}
}

function chk_regtn_page(formname)
{
if(MM_validateForm(formname,'name','Name','R','phone','Contact No','RisContact','email','Email ID','RisEmail','comments','Commets','R'))
 {
  return true;
 } 
 
 else
  {
  return false;
}
}
