// JavaScript.js




function IsNumeric(strString,theForm, errorMsg)
  
   { 
 var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;
 if ((strString.length > 9)|| (strString.length == 0))
	{  
		blnResult= false;
        theForm.ErrorMessage.value=errorMsg;
		theForm.ErrorMessage.style.visibility='visible';  	
}
  


   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
           theForm.ErrorMessage.value=errorMsg;
		   theForm.ErrorMessage.style.visibility='visible';
         }
      }
 
   return blnResult;
   }

function check(fullname,form,emptyField,invalid) {

var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
var digits =/[0123456789]/;

 

	if(fullname.value.length < 1) {
		 form.ErrorMessage.value=emptyField;
		 form.ErrorMessage.style.visibility='visible';
		 fullname.focus();
		return false;
	}

  if((fullname.value.search(digits) != -1) || (fullname.value.search(mikExp) != -1) ) {
		form.ErrorMessage.value=invalid;
		form.ErrorMessage.style.visibility='visible';
		
		fullname.select();
		fullname.focus();
		return false;

	}
    var result = trim(fullname.value);

	if ((result.length==0) || (result==null)) {
		form.ErrorMessage.value=emptyField;
		form.ErrorMessage.style.visibility='visible';
	    return false;
  	 }
  	fullname.value=result;

	return true;
}

function trim(sString)
{   
  

	while (sString.substring(0,1) == ' ')
	{   
        
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{ 
		sString = sString.substring(0,sString.length-1);
	}
   return sString;
}

function fill_select(f)

{
        for(x=0;x<12;x++) 
                
                document.writeln("<OPTION value=\""+x+"\">"+date_arr[x].text);

        ran = f.months[f.months.selectedIndex].value;
        selection=date_arr[ran].value;

}



function update_days(f)

{

        var newDay,oneDigit;
        var arabicDay;
        var dayString = new String()     
							
        temp=f.days.selectedIndex; 

        for(x=days_arr.length;x>0;x--)

        {

                days_arr[x]=null;

                f.days.options[x]=null;

         }
                  
       
   	   	    ran = f.months[f.months.selectedIndex].value;
 	        selection=parseInt(date_arr[ran].value);

          
        ret_val = 0;

        
        if (selection==28) 
        {
                
                year=parseInt(f.years.options[f.years.selectedIndex].value);

				if (year % 4 ==0) ret_val=1;
				
				if (year % 100 == 0) 
				   if (year % 400 == 0)
						ret_val=1;  
				  else ret_val=0;				          
     
        }

        selection = selection + ret_val;                
      

          if((navigator.appName.search("Microsoft")!=-1) && (navigator.userLanguage.search("en") != -1))// if it is english
           {   
	           for(x=1;x < selection+1;x++){         
    	         	days_arr[x-1]= new Option(x,x);		 								                                      
        	        f.days.options[x-1]= days_arr[x-1] ;
                }
           }       

      else          // START of conversion days to arabic        
        {
		 for(x=1;x < selection+1;x++)     
          { 
	            dayString = x.toString()	   //convert day to string
    	    	arabicDay= new String()          //contain the day after change each digit to arabic
    	    	
    	    	if(dayString.length == 2){
	    	    	for(var i=0;i<2;i++)
	    	    	{
	  	    	    	oneDigit = dayString.slice(i,i+1)   //take digit by digit to convert it to arabic
		    	    	newDay= parseInt(oneDigit)           //get the int value of each digit
		    	    	newDay= newDay+ 1632                 
		    	        arabicDay = arabicDay.concat(String.fromCharCode(newDay)); //get the charcter again from its unicode
		  			  	 //concatentate each digit till finish the 2 numbers
		  		   }
	  		   }
	  		   else //in case only 1 digit
	  		   {		  		   
	                newDay = x+1632;
	                arabicDay  = String.fromCharCode(newDay);
		  	   }
	  		   
                    days_arr[x-1]= new Option(arabicDay,x);		 								                                      
                    //days_arr[x-1].value=x;
        	        f.days.options[x-1]= days_arr[x-1];
        	        //f.days.options[x-1].text=  arabicDay;         	    
       	           
            }
          }
           // END of conversion
                     
       
        
	if (temp==-1) f.days.options[0].selected=true; 
	else
	{
	
	         if (temp>(selection-1))
	            f.days.options[0].selected=true;
	         else  f.days.options[temp].selected=true;  
	}

}       


function year_install(f)

{     

          
      var arabicYear
      var newVal
      var oneDigit
      var yearString = new String()
      var i=0
      var time = new Date();
     var currentYr = time.getFullYear();
	
if((navigator.appName.search("Microsoft")!=-1) && (navigator.userLanguage.search("en") != -1)) // if it is english, no conversion needed
           {   
             for(x=currentYr;x>1900;x--)      	
        	document.writeln("<OPTION value=\""+x+"\">"+x)
           } 

//START of conversion the year to arabic if system language is not english
	else
        { 
          for(x=currentYr;x>1900;x--) 
          { 
	            yearString = x.toString()	   //convert year to string
    	    	arabicYear= new String()          //contain the year after change each digit to arabic
    	    	
    	    	for(var i=0;i<4;i++)
    	    	{
  	    	    	oneDigit = yearString.slice(i,i+1)   //take digit by digit to convert it to arabic
	    	    	newVal= parseInt(oneDigit)           //get the int value of each digit
	    	    	newVal= newVal+ 1632                 
	    	    		    	    
	  	      arabicYear  = arabicYear + "&#" + newVal+ ";"; //concatentate each digit till finish the 4 numbers
	  		   }
              document.writeln("<OPTION value=\""+x+"\">" +  arabicYear);
        
            }
          }
       // END of conversion 
	
                      
        update_days(f)
}


function checkAllmandatoryFields(form,fullname,mothername,emptyFields) {

if((fullname.value.length < 1)&&(mothername.value.length < 1))
 {
 		 form.ErrorMessage.value=emptyFields;
		 form.ErrorMessage.style.visibility='visible';
		 return false;
 }
 		 return true;
 
}
function validate(f,errorMsg)
{
var flag= true;
var selectedYear= parseInt(f.years[f.years.selectedIndex].value);
var selectedDay =parseInt(f.days[f.days.selectedIndex].value);
var selectedMonth =parseInt(f.months[f.months.selectedIndex].value);

if (selectedYear == yr) {

  if (selectedMonth>currentMonth) flag=false;
   else if (selectedMonth==currentMonth) {
		if (selectedDay>currentDay) flag=false;
       }
    
 } if (flag==false) alert(errorMsg);
  return flag;
  }
  
  ///REGISTRATION
  
  
function checkform(form,email,empty,invalidFormat)
{
   
	var specialChars = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;

if (email.value == "") 
		{
			form.ErrorMessage.value=empty;
			form.ErrorMessage.style.visibility='visible';
			email.focus();
			return false ;
		
		}
		else if (!checkEmail(email.value))
		{
			form.ErrorMessage.value=invalidFormat;
			form.ErrorMessage.style.visibility='visible';
			email.focus();
			return false ;
		}	

		
		return true ;
	
}

function checkEditFormPassword(form,pw,invalidPassword,passMismatch, invalidPasswordLength, emptyPassword) 
{
var specialChars = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;	

	
	if ((pw.value!= null) && (pw.value == "")){
		
		if ((pw.value.length < 8)||(pw.value.length > 12)){
			form.ErrorMessage.value=invalidPasswordLength;
			form.ErrorMessage.style.visibility='visible';
			pw.focus();
		    return false ;
		}
		if (pw.value.search(specialChars)!= -1)
		{
			form.ErrorMessage.value=invalidPassword;
			form.ErrorMessage.style.visibility='visible';
			pw.focus();
		    return false ;
		}
		
		if (pw.value!=form.PasswordConfirmation.value)
		{
			form.ErrorMessage.value=passMismatch;
			form.ErrorMessage.style.visibility='visible';
			pw.focus();
		    return false ;
		}
		
		}

	return true;


}
function checkEditUserFormPassword(form,pw,invalidPassword,passMismatch, invalidPasswordLength, emptyPassword) 
{
var specialChars = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;	

	if ((pw.value!= null) && (pw.value != "")){
		
		if ((pw.value.length < 8)||(pw.value.length > 12)){
			
			form.ErrorMessage.value=invalidPasswordLength;
			form.ErrorMessage.style.visibility='visible';
			pw.focus();
		    return false ;
		}
		if (pw.value.search(specialChars)!= -1)
		{
			
			form.ErrorMessage.value=invalidPassword;
			form.ErrorMessage.style.visibility='visible';
			pw.focus();
		    return false ;
		}
		
		if (pw.value!=form.PasswordConfirmation.value)
		{
			
			form.ErrorMessage.value=passMismatch;
			form.ErrorMessage.style.visibility='visible';
			pw.focus();
		    return false ;
		}
		}
		else{
		
			if (pw.value!=form.PasswordConfirmation.value)
			{
			
			form.ErrorMessage.value=passMismatch;
			form.ErrorMessage.style.visibility='visible';
			pw.focus();
		    return false ;
			}
			return true;
		}
		return true;
		}

	




function checkPassword(form,pw,invalidPassword,passMismatch) {



var specialChars = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;	


if (pw!=null){
		
		if (pw.value.search(specialChars)!= -1)
		{
			form.ErrorMessage.value=invalidPassword;
			form.ErrorMessage.style.visibility='visible';
			pw.focus();
		    return false ;
		}
		
		if (pw.value!=form.PasswordConfirmation.value)
		{
			form.ErrorMessage.value=passMismatch;
			form.ErrorMessage.style.visibility='visible';
			pw.focus();
		    return false ;
		}
		
		}

}

function checkEmail(email) {
	var flag=true
	if((email.indexOf("@")==-1) || (email.indexOf(".")==-1) || (email.indexOf("@")!=email.lastIndexOf("@"))) {
		flag=false
	}
	return flag
}

function addOption(formObject,selectObject,optionText,optionValue,errorCompanyExists) {
    var optionObject = new Option(optionText,optionValue)
    var optionRank = selectObject.options.length
    var i=0
	var add= true
    for(i=0;i<selectObject.options.length;i++)
 	{
   
    if (selectObject.options[i].value == optionValue ) {
    formObject.ErrorMessage.value=errorCompanyExists;
	formObject.ErrorMessage.style.visibility='visible';
    add=false
	}
	}
   if (add!=false) { selectObject.options[optionRank]=optionObject}
    
}

function deleteOption(selectObject,optionRank) {
    if (selectObject.options.length!=0) { selectObject.options[optionRank]=null }
}

function testAdd(formObject,errorMsg1,errorMsg2,errorCompanyExists) {
   
	formObject.ErrorMessage.style.visibility='hidden';
	if (IsNumeric(formObject.optionText.value,formObject,errorMsg1) == true) {

        
        addOption(formObject,formObject.compList,formObject.optionText.value,formObject.optionText.value,errorCompanyExists)
		formObject.optionText.value=""
    } else {

        formObject.ErrorMessage.value=errorMsg2;
		formObject.ErrorMessage.style.visibility='visible';
    }
   
}

function testDelete(formObject,errorMsg) {
  //  var formObject = document.EditUser
        formObject.ErrorMessage.style.visibility='hidden';
    if (formObject.compList.selectedIndex!=-1) {
        deleteOption(formObject.compList,formObject.compList.selectedIndex)
    } else {
       
formObject.ErrorMessage.value=errorMsg;
formObject.ErrorMessage.style.visibility='visible';
    }
}
 

  function loadCompanies(theForm,id){ 


	var formObject = theForm
  	var optionObject = new Option(id,id)
    var optionRank = formObject.compList.options.length
    formObject.compList.options[optionRank]=optionObject
}


function disableGroup(formName,checkBox, groupName) {

 var disableIt = false;
  var checkIt = true; 
if (checkBox.checked == false) disableIt = true

  for (var i=0; i<formName.elements.length; i++) {
    if (formName.elements[i].name == groupName) {

      formName.elements[i].disabled = disableIt;
   
		if (checkIt == true) {
		formName.elements[i].checked = true;
        checkIt= false;
		}
    }
  }

}