﻿/////////////////////////////////////////
//Required ///////////////
/////////////////////////////////////////

function CheckRequired(TheObjName,errMsg)
{
  var TheObj;
  TheObj=document.getElementById(TheObjName);
  if (TheObj.value=='')
  {
    alert(errMsg);
    TheObj.focus();
    return false;
  }   
}

function CheckDrpRequired(TheObjName,errMsg)
{
  var TheObj;
  TheObj=document.getElementById(TheObjName);
  if (TheObj.selectedIndex == -1 || TheObj.options[TheObj.selectedIndex].text=="")
  {
    alert(errMsg);
    TheObj.focus();
    return false;
  }   
}

function Check_Rdb_Required(TheObjName,errMsg)
{
  var TheObj;
  TheObj=document.getElementById(TheObjName);
  if (TheObj.checked!=true)
  {
    alert(errMsg);
    TheObj.focus();
    return false;
  }   
}

function CheckDrpIsDifferentThan(TheObjName,TheDifferentThanText,errMsg)
{
  var TheObj;
  TheObj=document.getElementById(TheObjName);
  if (TheObj.value ==TheDifferentThanText)
  {
    alert(errMsg);
    TheObj.focus();
    return false;
  }   
}

/////////////////////////////////////////
//Date ///////////////
/////////////////////////////////////////

function CheckIsDate(TheObjName,errMsg)
{
  var TheObj;
  TheObj=document.getElementById(TheObjName);
  if (TheObj.value!="")
      {
      if (isValidDate(TheObj.value)!=true)
      {
        alert(errMsg);
        TheObj.focus();
        return false;
      } 
  } 
}

function isValidDate(dateStr) {

 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

 var matchArray = dateStr.match(datePat); 
 if (matchArray == null) {
  return false;
 }
 month = matchArray[3];
 day = matchArray[1];
 year = matchArray[4];
 
 if (month < 1 || month > 12) {
  return false;
 }
 
 if (day < 1 || day > 31) {
  return false;
 }
 
 if ((month==4 || month==6 || month==9 || month==11) && day==31) {
  return false
 }
 
 if (month == 2) { // check for february 29th
  var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
  if (day>29 || (day==29 && !isleap)) {
   return false;
  }
 }
 
 return true;  // date is valid
}

function CheckIsTime(TheObjName)
{

  var TheObj;
  TheObj=document.getElementById(TheObjName);
  var str=TheObj.value;
  var str1;
  var str2;
  var str3;
  try{
    str1=str.substr(0,2);
    str2=str.substr(2,1);
    str3=str.substr(3,2);
  }catch(e){}
  
  var IsError=false;
  
  if (str.length!=5){IsError=true;}
  if (str1.length!=2){IsError=true;}
  if (str2!=':'){IsError=true;}  
  if (str3.length!=2){IsError=true;}

  try{
    if (isNaN(str1)==true){IsError=true;}
    if (str1<0||str1>24){IsError=true;}
  }catch(e){IsError=true;}

  try{
    if (isNaN(str3)==true){IsError=true;}
    if (str3<0||str3>59){IsError=true;}
    if (str1==24&&str3!=0){IsError=true;}
  }catch(e){IsError=true;}
  
  if(IsError==true) { 
    alert("פורמט שעה לא תקין, דוגמא: 09:30"); 
    TheObj.focus();
    return false;    
  } 
} 

function getDateObject(dateString,dateSeperator)
{
    //This function return a date object after accepting 
    //a date string ans dateseparator as arguments
    var curValue=dateString;
    var sepChar=dateSeperator;
    var curPos=0;
    var cDate,cMonth,cYear;

    //extract day portion
    curPos=dateString.indexOf(sepChar);
    cDate=dateString.substring(0,curPos);
	
    //extract month portion				
    endPos=dateString.indexOf(sepChar,curPos+1);			
    cMonth=dateString.substring(curPos+1,endPos);

    //extract year portion				
    curPos=endPos;
    endPos=curPos+5;			
    cYear=curValue.substring(curPos+1,endPos);
    if (cYear.length==2){cYear="20"+cYear;}
	
    //Create Date Object
    dtObject=new Date(cYear,cMonth,cDate);	
    return dtObject;
}


/////////////////////////////////////////
//Mail and Url ///////////////
/////////////////////////////////////////

function CheckMail(TheObjName,errMsg)
{
  var TheObj;
  var TheMail;
  TheObj=document.getElementById(TheObjName);
  TheMail=TheObj.value;
  
  if (TheMail=="") {return true;}
  
  apos=TheMail.indexOf("@");
  dotpos=TheMail.lastIndexOf(".");
    if (apos<1||dotpos-apos<2) 
    {
      alert(errMsg);
      TheObj.focus();
      return false;
    }
}


function CheckSite(TheObjName,errMsg)
{
  var TheObj;
  var TheSite;
  TheObj=document.getElementById(TheObjName);
  TheSite=TheObj.value;
  
  if (TheSite=="") {return true;}

  var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
  if(RegExp.test(TheSite))
  { 
    return true; 
  }
  else
  { 
    alert(errMsg);
    TheObj.focus();
    return false;
  } 
}

/////////////////////////////////////////
//Length ///////////////
/////////////////////////////////////////

function CheckLength(TheObjName,MaxLength,errMsg)
{
  var TheObj;
  var TheString;
  TheObj=document.getElementById(TheObjName);
  TheString=TheObj.value;
    if (TheString.length > MaxLength) 
    {
      alert(errMsg);
      TheObj.focus();
      return false;
    }
}

function CheckMinLength(TheObjName,MinLength,errMsg)
{
  var TheObj;
  var TheString;
  TheObj=document.getElementById(TheObjName);
  TheString=TheObj.value;
    if (TheString.length < MinLength) 
    {
      alert(errMsg);
      TheObj.focus();
      return false;
    }
}

/////////////////////////////////////////
//Numeric ///////////////
/////////////////////////////////////////

function CheckNumeric(TheObjName,errMsg)
{
  var TheObj;
  var TheString;
  TheObj=document.getElementById(TheObjName);
    if (isNaN(TheObj.value))
    {
      alert(errMsg);
      TheObj.focus();
      return false;
    }
}

function CheckNumericRange(TheObjName,MinValue,MaxValue,errMsg)
{
  var TheObj;
  var TheString;
  TheObj=document.getElementById(TheObjName);

  if (TheObj.value!="")
  {
      if (CheckNumeric(TheObjName,errMsg)==false)
      {
        return false;
      }

      if (TheObj.value<MinValue || TheObj.value>MaxValue)
      {
        alert(errMsg);
        TheObj.focus();
        return false;
      }
  } 
}

/////////////////////////////////////////
//Is Hebrew ///////////////
/////////////////////////////////////////

function CheckHebrew(TheObjName,errMsg)
{
  var TheObj;
  TheObj=document.getElementById(TheObjName);
  var arry_Leeter=new Array ("א", "ב", "ג", "ד", "ה", "ו", "ז", "ח", "ט", "י", "כ", "ל", "מ", "נ", "ס", "ע", "פ", "צ", "ק", "ר", "ש", "ת");
  for (i=0;i<=21;i++)
  {
    if (0<=TheObj.value.indexOf(arry_Leeter[i]))
    {
       return true;
    }
  }  
  alert(errMsg);
  TheObj.focus();  
  return false;      
} 
