
  // ###################################### CHECK FORM FUNCTIONS ##################################################
  // checkUsername
  // Username - 3-15 chars, uc, lc, and underscore only.
  function checkUsername (strng , name) {
	  var error = "";
	  if (strng == "" || strng.length == 0) {
		  error = "Please enter the " + name + ".\n";
	  }
	  else{
		  var illegalChars = /\W/; // allow letters, numbers, and underscores
		  
		  if ((strng.length < 3) || (strng.length > 15)) {
			 error = "Username length must be between (3 - 15). \n";
		  }
		  else {
			  strng = strng.replace(/\./g,"");
			  if (illegalChars.test(strng)) {
				 error ="Username contains illegal characters. \n";
			  }
		  }
	  }
	  return error;
  }

  // checkPassword
  // Password - 4-15 chars, uc, lc, and underscore only.
  function checkPassword (pwd,pwd_conf) {
	  var error = "";
	  if (pwd == "" || pwd.length == 0) {
		  error = "Please enter the Password.\n";
	  }
	  else{
		  var illegalChars = /\W/; // allow letters, numbers, and underscores		  
		  if ((pwd.length < 4) || (pwd.length > 15)) {
		  error = "Password length must be between (4 - 15).\n";
		  }
		  else {
			  pwd = pwd.replace(/\./g,"");
			  if (illegalChars.test(pwd)) {
				error ="Password contains illegal characters. \n";
			  }
		  }
	  }
	  if(error=="" && pwd != pwd_conf){
		error ="Please reenter the Password. \n";
	  }

	  return error;
  }


  // checkName
  function checkName (strng , name) {
	  var error = "";
	  if (strng == "" || strng.length == 0) {
		  error = "Please enter the " + name + ".\n";
	  }
	  return error;
  }


  // checkNumber
  function checkNumber (strng , name) {
	  var error = "";
      if (strng == "" || strng.length == 0) {
		  error = "Please enter the " + name + ".\n";
	  }else if (isNaN(parseInt(strng))) {
		  error = name + " must be a number.\n";
	  }
	  return error;
  }
 

  // checkSelect
  function checkSelect (select , name) {
	  var error = "";
	  if ((select.options[0].selected == true) && (select.options[0].value == "")) {
		  error = "Please select the " + name + ".\n";
	  }
	  return error;
  }


  // checkEmail
  function checkEmail (strng) {
	  var error="";
	  if (strng == "") {
		 error = "";
	  }
	  else{
		  var emails=strng.split(",");
		  //var emailReg=/^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/;
		  var emailReg=/^[A-Za-z][A-Za-z0-9._-]*@[A-Za-z0-9][A-Za-z0-9._-]*\.[A-Za-z0-9][A-Za-z0-9._-]+$/;
		  for(i=0;i<emails.length;i++){
			if (!(emailReg.test(emails[i])))
			{
				error = "Email addres is invalid ("+emails[i]+"). \n";
			}
		  }
	  }
	  return error;
  }


  //checkPhone
  // phone number - strip out delimiters and check for 10 digits
  function checkPhone (strng) {  	  
	  var error = "";
	  if (strng == "") {
		 error = "";
	  }
	  else{	       
		  var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
		  if (isNaN(parseInt(stripped))) {
			 error ="Phone number contains illegal characters. \n";

		  }else if (stripped.length > 15) {
				error ="Phone number length must be less than 15 characters . \n";
		   }
		   else if (stripped.length < 4) {
				error ="Phone number length must be more than 3 characters . \n";
		   }
	  }
	 return error;
  }



  //checkDate
  function checkDate(theForm,name){	 
      var error = "";	  
      //check day
      error += checkDay(theForm.day.value,name);
      if(error ==""){
          //check month			
          error += checkDay(theForm.month.value,name);
          if(error == ""){
            //check year
            error += checkYear(theForm.year.value,name);
          }
      }
      return error;
  }


  //checkDay
  function checkDay(day,name){
     var error = "";
     if(day == "" || day == "0" || day.length > 2){		       	 
              error = "Please enter the " + name + ".\n";
      }
      else if (isNaN(parseInt(day))) {
              error = "Please enter the " + name + ".\n";
      }
      return error;
  }

  //checkYear
  function checkYear(year,msg){
     var error = "";
     if(year == "" || year == "0" || year.length != 4){
            error = "Please enter the " + name + ".\n";
                }
      else if (isNaN(parseInt(year))) {
          return("Date contains illegal characters.\n");
      }
      return error;
  }

  // checkCountry
  function checkCountry(value){
	var error = "";
	  if(value=="")
	  	 error = "Please enter the country.\n";
	  return error;
  }


	// isEmpty
	 function isEmpty (strng , name) {
	   var error = "";
	   if (strng == "" || strng.length == 0) {
		   error = "Please enter the " + name + ".\n";
	   }
	   return error;
	}

    function validateDate(str, name) {
        var error = "";
		var dateformat = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
			if ((str !='') && (!dateformat.test(str))) {
				  error = "Date format is invalid .\n";
			}
			return error;
	}



  // ########## Forms Validation Functions #############

    // ValidateUserForm
    function ValidateUserForm(theForm){
		var why = "";
        why += checkName(theForm.firstname.value , "First Name");   
        why += checkName(theForm.lastname.value ,"Last Name");
        // ADD 		
        if(theForm.action.value == 'addUser'){
	        why += checkUsername(theForm.username.value , "Username");
    	    why += checkPassword(theForm.password.value , theForm.pwd_conf.value);				
			
		}
        // UPDATE 		
        if(theForm.action.value == 'updateUser'){
	        if(theForm.password.value != "")
		        why += checkPassword(theForm.password.value , theForm.pwd_conf.value);	        
		}		
        why += checkEmail(theForm.email.value ,"E-mail");
        why += checkPhone(theForm.phone.value ,"Phone");  		
		//why += checkSelect(theForm.sys_role_name , "Role");			
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }

    // ValidateUserForm
    function ValidateClientForm(theForm){
		var why = "";
        why += checkName(theForm.firstname.value , "First Name");   
        why += checkName(theForm.lastname.value ,"Last Name");
	    why += checkUsername(theForm.username.value , "Username");	
	    why += checkName(theForm.email.value ,"Email");
        why += checkEmail(theForm.email.value ,"Email");
        why += checkSelect(theForm.country , "Country        ");	
        why += checkPhone(theForm.mobilephone.value ,"Mobile Phone");  		
        why += validateDate(theForm.birthdate.value ,"Birthdate  ");
				
				
				
        if (why != "") { 
           alert(why);
           return false;
        }
         return true;
     }

    // ValidateMAForm
    function ValidateMAForm(theForm){
		var why = "";
		why += checkSelect(theForm.type , "Activity Type");	
        why += checkName(theForm.purpose.value , "Purpose");   		
				
        if (why != "") { 
           alert(why);
           return false;
        }
         return true;
     }

    // validateAccountForm
    function validateAccountForm(theForm){
		var why = "";
        why += checkName(theForm.firstname.value , "First Name");   
        why += checkName(theForm.lastname.value ,"Last Name"); 		        
     

        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }


    // validateLifecylceMsgForm
    function validateLifecylceMsgForm(theForm){
		var why = "";
        why += checkSelect(theForm.type , "Message Type");   
        why += checkNumber(theForm.days.value ,"Days"); 		
        why += checkName(theForm.to.value ,"TO");		        		
		

        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }
     

    // validateSMSMsgForm
    function validateSMSMsgForm(theForm){
		var why = "";
        why += checkName(theForm.to.value ,"To");		
        why += checkName(theForm.msg.value ,"Message");
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }
     
    // validateLifecylceMsgForm
    function validateSchedulerForm(theForm){
		var why = "";
        why += checkNumber(theForm.start_hour.value ,"Start Hour");
        why += checkNumber(theForm.start_minutes.value ,"Start Minutes"); 		
        why += checkNumber(theForm.period_in_hours.value ,"Period In Hours");	
		

        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }     


    // validateLifecylceMsgForm
    function validateMailerMsgForm(theForm){
		var why = ""; 		
        why += checkName(theForm.to.value ,"TO");		        		
		

        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }
    // validateDomainItemForm
    function validateDomainItemForm(theForm){
		var why = "";
		why += checkSelect(theForm.sys_domain_id , "System Domain");		
        why += checkName(theForm.label.value , "Domain Item Label"); 		        

        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }



    // validateClassForm
    function validateClassForm(theForm){
		var why = "";	       
        why += checkName(theForm.ar_name.value , "");   
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }

    // validateCategoryForm
    function validateCategoryForm(theForm){
		var why = "";	       
        why += checkName(theForm.ar_name.value , "");   
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }


    // validateFeeTypeForm
    function validateFeeTypeForm(theForm){
		var why = "";	       
        why += checkName(theForm.ar_name.value , "");   
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }

    // validateDocTypeForm
    function validateDocTypeForm(theForm){
		var why = "";	       
        why += checkName(theForm.ar_name.value , "");  
        why += checkSelect(theForm.document_category_id , "");     
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }


    // validateSrvcStatisticForm
    function validateSrvcStatisticForm(theForm){
		var why = "";	       
        why += checkSelect(theForm.year , "السنة");
        why += checkNumber(theForm.applications_no.value , "عدد الطلبات السنوية");         
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }
     

    // validateSPForm
    function validateSPForm(theForm){
		var why = "";	       
        why += checkName(theForm.ar_name.value , "");       
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }

    // validateBSForm
    function validateBSForm(theForm){
		var why = "";	       
        why += checkName(theForm.ar_name.value , "");       
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }
     
    // validateSrvcForm
    function validateSrvcForm(theForm){
		var why = "";	       
        why += checkName(theForm.ar_name.value , "");       
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }     
     
     
    // validateSrvcFeeForm
    function validateSrvcFeeForm(theForm){
		var why = "";	       
        //////// other
        if(theForm.fee_type_id.value == '0'){
       		 why += checkName(theForm.other.value , "اسم الرسم الجديد");
        }		
        why += checkSelect(theForm.fee_type_id , "");        
        why += checkNumber(theForm.value.value , "");        
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }          
     
    // validateSrvcDocForm
    function validateSrvcDocForm(theForm){
		var why = "";
        //////// other
        if(theForm.document_type_id.value == '0'){
       		 why += checkName(theForm.other.value , "اسم الوثيقة الجديدة");
        }
        //////// 			       
        why += checkSelect(theForm.document_type_id , "");        
        why += checkSelect(theForm.status , "");       
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }        
     
     
    // validateFBForm
    function validateFBForm(theForm){
		var why = "";	       
		why += checkName(theForm.comment.value , "the message");            
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }   
     
    // validateMsgForm
    function validateMsgForm(theForm){
		var why = "";	       
		why += checkName(theForm.msg.value , "Message Body");            
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }         
     
     
    // validateConfigForm
    function validateConfigForm(theForm){
		var why = "";	       
		why += checkName(theForm.value.value , "Value");            
        if (why != "") {
           alert(why);
           return false;
        }
         return true;
     }      
     
  function confirmDelete(name,value, delUrl){
	  if (confirm("Are you sure you want to delete "+ name +" ("+ value +") ? ")) {
			document.location = '/CRM/delete?' + delUrl;
	  }
  }
