var varName=new Array();// This array is used to save the parameter names passed to the JSP
var varVal=new Array();//This array is used to save the parameter values passed to the JSP
var userId;//This variable is used to save the value of the userId passed to the JSP
//and will be used in the function validate()
var caseNo;//This variable is used to save the value of the caseNo passed to the JSP
//and will be used in the function validate()

function getBranchs()// To submit the form if the user selects a specific  bank
//so that the right branches for the selected bank are showen in a list.
{
   document.MOJ.insert.value="no" ;
   document.MOJ.submit();
}




function userDate(userDate,privDateField,op)//To check that the passed user date and 
//the entered privilage date follow the required (That is  user date >  privilage date 
//for the start date and the end one)

{

userDateVal=new Date(formatDate(userDate));
privDateVal=new Date(formatDate(privDateField.value));

if(op=="less")
{

 if(Date.parse(userDateVal)>Date.parse(privDateVal))
  {
        alert("تاريخ البدء لابد وأن يكون أكبر من "+rev(userStartDate));
         return false;

     }else{return true;}

}



if(op=="more")
{

 if(Date.parse(userDateVal)<Date.parse(privDateVal))
  {
        alert("تاريخ الانتهاء أو البدء لابد وأن يكون أقل من "+rev(userEndDate));
         return false;

     }else{return true;}

}



}




function clear()//Called if an invalid userId or Case number is passed
//to the jsp.
{
    document.MOJ.represented.value="no";
    document.getElementById("taser").style.display='none';
    document.getElementById("tased").style.display='none';
    var elm=document.MOJ.ReprsentedFor
         for(j=0;j<elm.length;j++)
             {
                  eval("document.MOJ.ReprsentedFor"+"["+j+"]"+".checked=false"); 
             } 
}



function validate()// To validate the form fields
{

     var rep=document.MOJ.represented.value;
     var newPerNo=document.MOJ.userId.value;
     var newCaseNo=document.MOJ.caseNo.value;

//making sure that the  user did not change  values of userId and case number
//after he have selected the taser or the tased of the privilage because 
//if this check does not happen then the user may enterded inconsistant values

     if((rep=="yes")&&((newPerNo!=userId)||(newCaseNo!=caseNo)))
     {
      document.MOJ.represented.value="no"
      var elm=document.MOJ.ReprsentedFor
         for(j=0;j<elm.length;j++)
             {
                  eval("document.MOJ.ReprsentedFor"+"["+j+"]"+".checked=false"); 
             }      
      errorMeg=getError(12);
      alert(errorMeg);
      return true;
      }      
    else {
     

   if(!doSubmit(MOJ,true)||!chNumeric(document.MOJ.caseNo, 'رقم القضية هو رقم')||!chNumeric(document.MOJ.accountNo,'رقم الحساب هو رقم')||!checkDate(document.MOJ.startDate)||!checkDate(document.MOJ.endDate)||!before(document.MOJ.startDate,document.MOJ.endDate)||!userDate(userStartDate,document.MOJ.startDate,'less')||!userDate(userEndDate,document.MOJ.startDate,'more')||!userDate(userEndDate,document.MOJ.endDate,'more'))
   {
   return false;
       }

// if bank value entered then account should be entered too 
   if(document.MOJ.accountNo.value!=""&&document.MOJ.bankName.value=="no")
   { 
     errorMeg=getError(11);
     alert(errorMeg);
     document.MOJ.bankName.focus();
     return false;     
   }

// if account number  entered then bank should be selected too 
   if(document.MOJ.accountNo.value==""&&document.MOJ.bankName.value!="no")
   { 
     errorMeg=getError(10);
     alert(errorMeg);
     document.MOJ.accountNo.focus();
     return false;     
   }

if(document.MOJ.bankName.value!="no"&&document.MOJ.branch.value=="no")//check that  the user 
//must  select  a bank branch if he select a bank
   { 
     errorMeg=getError(15);
     alert(errorMeg);
     document.MOJ.branch.focus();
     return false;     
   }

   
if(document.MOJ.ReprsentedFor[0].checked)// To check that he must select a taser or a tased
//from the list  based on the value of the radio buttom named reprsentedFor.
    {
       if(document.MOJ.tased.value=="")
       {
         errorMeg=getError(9);
         alert(errorMeg);
         document.MOJ.tased.focus();
         return false;
         }
         
    }else {       

    if(document.MOJ.taser.value=="")
       {
         
         errorMeg=getError(8);
         alert(errorMeg);
         document.MOJ.taser.focus();
         return false;
         }

         }
   document.MOJ.insert.value="yes" ;//If every thing is right the value of the insert field
//must be changed to yes so that a forward in the jsp forwards the request to the 
//provideSuccess.jsp  so the actual editing is happened
   return true;

       }
}




function getVal()//To initialize the userId and the caseNo with the values
//of the passed userId and case number passed to  the JsP
{
 userId=document.MOJ.userId.value
 caseNo=document.MOJ.caseNo.value
}


function Show(src)
{

    pers=src.value;
if(document.MOJ.represented.value=="no")//if true then submit the form so that we
//can qurey the database to get the taser and the tased information based on the case number
// and then the user can select which to give the privilage to.
{   
   if(document.MOJ.userId.value=="")
     { 

       errorMeg=getError(13);
       alert(errorMeg);
       document.MOJ.userId.focus();
       src.checked=false;
       return false;
     }

        if(document.MOJ.caseNo.value=="")
     { 

       errorMeg=getError(14);
       alert(errorMeg);
       document.MOJ.caseNo.focus();
       src.checked=false;
       return false;
     }
     if(!chNumeric(document.MOJ.caseNo, 'رقم القضية هو رقم'))
     {
       src.checked=false;   
       return false;
       }

   document.MOJ.represented.value="yes"
   document.MOJ.submit();

}
else{
if(pers=="taser")
{
 document.getElementById("taser").style.display='';
document.getElementById("tased").style.display='none';
 }else{
 document.getElementById("taser").style.display='none';
document.getElementById("tased").style.display='';
 }

 }

}
