

/************************************************
Globle Variable Declarations
************************************************/

var str_SitePath = "";
str_SitePath = "http://talal/zalatimo/"



/**************************************************************
 LTrim: Returns a String containing a copy of a specified 
        string without leading spaces 

 Parameters:
      String = The required string argument is any valid 
               string expression. If string contains null, 
               false is returned

 Returns: String
***************************************************************/
function LTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for (i = 0; i < String.length; i++)
	{
		if (String.substr(i, 1) != ' ' &&
		    String.substr(i, 1) != '\t')
			break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}

/**************************************************************
 RTrim: Returns a String containing a copy of a specified 
        string without trailing spaces 

 Parameters:
      String = The required string argument is any valid 
               string expression. If string contains null, 
               false is returned

 Returns: String
***************************************************************/
function RTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for(j = String.length - 1; j >= 0; j--)
	{
		if (String.substr(j, 1) != ' ' &&
			String.substr(j, 1) != '\t')
		break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}

/**************************************************************
 RTrim: Returns a String containing a copy of a specified 
        string without both leading and trailing spaces 

 Parameters:
      String = The required string argument is any valid 
               string expression. If string contains null, 
               false is returned

 Returns: String
***************************************************************/
function Trim(String)
{
	if (String == null)
		return (false);

	return RTrim(LTrim(String));
}


/**************************************************************
 IsNumber:Return boolen value if the passed value is valid 
		  number or not

 Parameters:
      str_Value = String value that will be checked if its number
	  str_Alert = string value to be message alert
 Returns: Boolean
***************************************************************/

function IsNumber(str_Value,str_Alert)
{

str_val=Trim(str_Value)
test=true;

number=isNaN(str_val);
if (number == true)
	{
		test=false;
		alert(str_Alert);
	}

return(test);	
}	

/**************************************************************
 IsNigative:Return boolen value if the passed value is nigative 
			number or not

 Parameters:
      str_Value = String value that will be checked if its nigative number
	  str_Alert = string value to be message alert
 Returns: Boolean
***************************************************************/
function IsNegative(str_Value,str_Alert)
{

test=true;

if ((str_Value < 0 ) == true)
	{
	test=false;
	alert(str_Alert);
	}
return(test);	
}	
/**************************************************************
 IsEmpty:Return boolen value if the passed value is empty or null string

 Parameters:
      str_Value = String value that will be checked if its empty string or not
	  str_Alert = string value to be message alert
 Returns: Boolean
***************************************************************/
function IsEmpty(str_Value,str_Alert)
{
test = true;
valtrimed = Trim(str_Value);
test_len = valtrimed.length;

if (test_len == 0) 
	{
	   test = false;
	   alert(str_Alert);
	}
return(test);	
}	

/**************************************************************
 IsAlpha:Return boolen value if the passed value is Alphabet entries
		 or not

 Parameters:
      str_Value = String value that will be checked if its Alphabet 
				  entries
	  str_Alert = string value to be message alert
 Returns: Boolean
***************************************************************/
function IsAlpha(str_Value,str_Alert)
{

test=true;

var r;
var x;
	if (Trim(str_Value) != "")
	{
	x="[A-Za-z]+[A-Za-z ]*[A-Za-z ]*";
	r=str_Value.match(x);
	if (r!=str_Value)
	{
		test=false;
		alert(str_Alert);
	}
	}
	return(test);	
}	

/**************************************************************
 IsAlphaNum:Return boolen value if the passed value is Alphanumeric data

 Parameters:
      str_Value = String value that will be checked if its Alphanumeric data
	  str_Alert = string value to be message alert
 Returns: Boolean
***************************************************************/
function IsAlphaNum(str_Value,str_Alert)
{
test=true;
var r;
var x;
	if (Trim(str_Value) != "" )
	{
		x="[0-9A-Za-z]+[A-Za-z0-9_ ]*[a-zA-Z0-9]*";
		r=str_Value.match(x);
		if (r!=str_Value) 
		{
			test=false;
			alert(str_Alert);
		}
	}
	return(test);	
}	


/**************************************************************
 IsAlphaNum:Return boolen value if the passed value is valid email format 
			or not

 Parameters:
      str_Value = String value that will be checked if its valid email format
	  str_Alert = string value to be message alert
 Returns: Boolean
***************************************************************/
function IsEmail(str_Value,str_Alert)
{
test=true;
var r;
var x;
	
	if (Trim(str_Value) != "")
	{
		x="[A-Za-z_]+[A-Za-z0-9_-]*[.]?[A-Za-z0-9_-]+[@][A-Za-z0-_-]+[.][A-Za-z0-9_]+[.]?[A-Za-z0-9_]+" ;
		r=str_Value.match(x);
		if (r!=str_Value)
		{
			test=false;
			alert(str_Alert);
		}
	}
	return(test);	
}	

/**************************************************************
 checkPasswordLength:Return boolen value if the passed value is
					 had length more than 6 charachter

 Parameters:
      str_Value = String value that will be checked 
	  str_Alert = string value to be message alert
 Returns: Boolean
***************************************************************/
function checkPasswordLength(str_Value,str_Alert)
{
test=1
valtrimed=Trim(str_Value)
test_len=valtrimed.length;
if ( test_len < 6 )
	{
		test=0
		alert(str_Alert);
	}
return(	test )
} 

/**************************************************************
 IsTelephone:Return boolen value if the passed value is valid phone format
			

 Parameters:
      str_Value = String value that will be checked 
	  str_Alert = string value to be message alert
 Returns: Boolean
***************************************************************/
function IsTelephone(str_Value,str_Alert)
{
var x,r
	test=true
	if (Trim(str_Value) != "" )
	{
	
		x="[[0-9]+[0-9-]*"
		r=str_Value.match(x)
		if (r!=str_Value) 
		{
		test=false;
		alert(str_Alert);
		}
	}
return(test);	
}

/**************************************************************
 ComboAdd: Add a new item to a SELECT HTML object at runtime.

 Parameters:
      Object = SELECT Object ID
      Value  = Value of the String ... <option VALUE="?????">....</option>
      String = String to add.

 Returns: None
***************************************************************/
function ComboAdd(Object, Value, String)
{
	Value = Trim(Value)
	String = Trim(String)

	if (Value.length < 1 || String.length < 1)
		return false

	Object[Object.length] = new Option(String, Value);
	Object.selectedIndex = Object.length;
}

/**************************************************************
 ComboDel: Delete the current/selected item from a SELECT 
           HTML object at runtime.

 Parameters:
      Object = SELECT Object ID

 Returns: None
***************************************************************/
function ComboDel(Object)
{
	var selected_index = Object.selectedIndex
	if (selected_index >= 0)
	{
		Object.options[Object.selectedIndex] = null;
		if (selected_index > 0)
			Object.selectedIndex = selected_index
		else
			Object.selectedIndex = 0;
	}
}
/**************************************************************
 InStr: Returns a Long specifying the position of the first 
        occurrence of one string within another. Is String1
        or String2 are null, false is returned.

 Parameters:
      String1 = String expression being searched.
      String2 = String expression sought

 Returns: Integer
***************************************************************/
function InStr(String1, String2)
{
	var a = 0;

	if (String1 == null || String2 == null)
		return (false);

	String1 = String1.toLowerCase();
	String2 = String2.toLowerCase();

	a = String1.indexOf(String2);
	if (a == -1)
		return 0;
	else
		return a + 1;
}
/**************************************************************
 IsSpecialChars: Returns a Boolean value indicating whether an 
         expression contains symbols such as
         @#$%^&|\_+-*="!?,/.:;'(){}<>[]

 Parameters:
    - Expression = Variant containing a numeric expression or 
                   string expression.

 Returns: Boolean
***************************************************************/
function IsSpecialChars(String1, String2)
{

	if (String1 != "")
	{
	Expression = String1.toLowerCase();
	if(String2 !="" || String2 == false)
		RefString  = String2;
	else
		RefString ="\"\'";

	if (Expression.length < 1) 
		return (false);

	for (var i = 0; i < Expression.length; i++) 
	{
		var ch = Expression.substr(i, 1)
		var a = RefString.indexOf(ch, 0)
		if (a != -1){
			alert("The following characters are not allowed " + RefString +" .");
			return (false);
			}
	}
	}
	return(true);

}


/**************************************************************
 IsMaxSize 
 Returns: Boolean
***************************************************************/
function IsMinSize(String1,Lenght,String2){
	var StringVal = Trim(String1);
	var StringLength = StringVal.length ;
	if (StringVal != "")
	{
	if ( parseInt(StringLength) < parseInt(Lenght) )
		{
			alert(String2);
			return false;
		}
	}
return true;
}

/**************************************************************
 IsMinSize 
 Returns: Boolean
***************************************************************/
function IsMaxSize(String1,Lenght,String2){
	var StringVal = Trim(String1);
	var StringLength = StringVal.length ;
	if (StringVal != "")
	{
	if ( parseInt(StringLength) > parseInt(Lenght) )
		{
			alert(String2);
			return false;
		}
	}
return true;
}



/**************************************************************
 getForm: Returns a referance to a spcified form 

 Parameters:
      String = The name of the form 
               

 Returns: form object
***************************************************************/
function GetForm(String1){
	if (String1 == null)
		return null;
	else
		return eval("document.forms."+String1);
}

/**************************************************************
 submitForm: submits a form 

 Parameters:
      Object = form object
               

 Returns: none
***************************************************************/


function SubmitForm(formName){
var obj_form= GetForm(formName);
	if(obj_form){
		obj_form.submit();
	}
}

/**************************************************************
 resetForm: reset a form 

 Parameters:
      formName = form name string
               

 Returns: none
***************************************************************/


function ResetForm(formName){
var obj_form= GetForm(formName);
	if(obj_form){
		obj_form.reset();
	}
}

/**************************************************************
 GetElement: Gets an element refernce  

 Parameters:
	  String = element name	
      Object = form object
               

 Returns: object 
***************************************************************/

function GetElement(FormElement,Form){
	var form_length = Form.elements.length;
	var myform = Form;
	var Element = null;

	for (var i = 0; i < form_length; i++)
	{
		if(myform.elements[i].name == FormElement){
			Element=myform.elements[i];
			return 	Element;
		}
	}
	alert("Element not found");
	return Element;
}

/**************************************************************
 GetElementValue: Returns the element value

 Parameters:
	   Object = element object
               

 Returns: String 
***************************************************************/

function GetElementValue(CheckedElement){
		var ElementValue ="";
		if (CheckedElement == null)
		{
			alert("Null element");
			return false;
		}
		
		var ElementType = CheckedElement.type ;
		ElementType = ElementType.toLowerCase();
		if (
			ElementType == 'text' || ElementType == 'hidden' || 
			ElementType == 'password' || ElementType == 'textarea' || 
			ElementType == 'select-one' || ElementType == 'checkbox' || 
			ElementType == 'radio' || ElementType == 'select-multiple'
			)
		{
			
			if (
				ElementType == 'text' || ElementType == 'hidden' ||
				ElementType == 'password' || ElementType == 'textarea'
				)
				ElementValue = Trim(CheckedElement.value);
			
			/*else if (ElementType == 'checkbox' || ElementType == 'radio')
				ElementValue = Trim(CheckedElement.checked);
			*/
			
			else if (ElementType == 'select-one' || ElementType == 'select-multiple')
				ElementValue =  Trim(CheckedElement[CheckedElement.selectedIndex].value);
			
			
		}
		return ElementValue;
}

/**************************************************************
 selectElement: select and focus of an element 

 Parameters:
	   Object = element object
               

 Returns: none
***************************************************************/


function selectElement(CheckedElement)
{

		
		var ElementType = CheckedElement.type ;
		ElementType = ElementType.toLowerCase();
		
			if (ElementType == 'text'  || ElementType == 'password'  || ElementType == 'textarea'){
				CheckedElement.select();
				CheckedElement.focus();
			}
				
			else if (ElementType == 'checkbox' || ElementType == 'radio' || 
					 ElementType == 'select-one' || ElementType == 'select-multiple')
					 CheckedElement.focus();
					 CheckedElement.selectedIndex=0;
					 
						
						

}

/**************************************************************
 CheckElement: checks an element for the various validation conditions 

 Parameters:
	   String = element name
	   Object = form
	   The other parameters can be either false(is the validation condition is not required)
	   or an error message(string) if the validation condition is required.
	   
	   note :1- The last two parameters maxsize and minsize of char's in a field, in case 
			it's required the number of char's should be contcatenated with a $ and with 
			also with the error message.
			
			 2- When its needed to check for spcial characters the SpecialChars variable
			 should equal a string that contains the spcial characters
			
	   
	   Ex. you need to make shur the minimum of char's in a password field is 6.
	   The function should be called as follows
	   CheckElement(FormElement,Form,Empty,Alpha,Number,Negative,Alphanum,Email,Telephone,
	   "6$"+ ErrorMessage,MaxSize,SpecialChars)
	   Where ErrorMessage is a string that contains the actual error message.
	   
	   Ex.
	   CheckElement(FormElement,Form,Empty,Alpha,Number,Negative,Alphanum,Email,Telephone,
	   MinSize,MaxSize,"[]{}+-\\*\'=\"")
	   
               

 Returns: none
***************************************************************/
   
function CheckElement(FormElement,Form,Empty,Alpha,Number,Negative,Alphanum,Email,Telephone,MinSize,MaxSize,SpecialChars){
	var CheckedElement=GetElement(FormElement,Form);
	var ElementValue="";
	var status;
	if(CheckedElement==null){
		alert(messageArray[10]);
		return false;
	}
	else{
		ElementValue = GetElementValue(CheckedElement);
		
			
		if (Empty!=false){
			if (IsEmpty(ElementValue,Empty)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Alpha!=false){
			if (IsAlpha(ElementValue,Alpha)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Number!=false){
			if (IsNumber(ElementValue,Number)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		if (Negative!=false){
			if (IsNegative(ElementValue,Negative)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Alphanum!=false){
			if (IsAlphaNum(ElementValue,Alphanum)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Email!=false){
			if (IsEmail(ElementValue,Email)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Telephone!=false){
			if (IsTelephone(ElementValue,Telephone)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (MinSize!=false){
			var Message =MinSize.substr(MinSize.indexOf("$")+1);
			MinSize=MinSize.substr(0,MinSize.indexOf("$"));
				
			if (IsMinSize(ElementValue,MinSize,Message +" "+MinSize+".")==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (MaxSize!=false){
				var Message =MaxSize.substr(MaxSize.indexOf("$")+1);
				MaxSize=MaxSize.substr(0,MaxSize.indexOf("$"));
				
			if (IsMaxSize(ElementValue,MaxSize,Message +" "+MaxSize+"." )==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		if (SpecialChars!=false){
			if (IsSpecialChars(ElementValue,SpecialChars)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
				
		return true;
	}
}

function CheckElement2(FormElement,Empty,Alpha,Number,Negative,Alphanum,Email,Telephone,MinSize,MaxSize,SpecialChars){
	var CheckedElement=FormElement;
	var ElementValue="";
	var status;
	if(CheckedElement==null){
		alert(messageArray[10]);
		return false;
	}
	else{
		ElementValue = GetElementValue(CheckedElement);
		
			
		if (Empty!=false){
			if (IsEmpty(ElementValue,Empty)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Alpha!=false){
			if (IsAlpha(ElementValue,Alpha)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Number!=false){
			if (IsNumber(ElementValue,Number)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		if (Negative!=false){
			if (IsNegative(ElementValue,Negative)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Alphanum!=false){
			if (IsAlphaNum(ElementValue,Alphanum)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Email!=false){
			if (IsEmail(ElementValue,Email)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (Telephone!=false){
			if (IsTelephone(ElementValue,Telephone)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (MinSize!=false){
			var Message =MinSize.substr(MinSize.indexOf("$")+1);
			MinSize=MinSize.substr(0,MinSize.indexOf("$"));
				
			if (IsMinSize(ElementValue,MinSize,Message +" "+MinSize+".")==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		
		if (MaxSize!=false){
				var Message =MaxSize.substr(MaxSize.indexOf("$")+1);
				MaxSize=MaxSize.substr(0,MaxSize.indexOf("$"));
				
			if (IsMaxSize(ElementValue,MaxSize,Message +" "+MaxSize+"." )==false){
				selectElement(CheckedElement);
				return false;
				}
		}
		if (SpecialChars!=false){
			if (IsSpecialChars(ElementValue,SpecialChars)==false){
				selectElement(CheckedElement);
				return false;
				}
		}
				
		return true;
	}
}
/**************************************************************
 CheckPasswordsMatch: compare the value of two elemets 

 Parameters:
	   (String1) - Element name
	   (String2) - Element name
	   (String3) - Error Message
	   (Object)  - Form Object           

 Returns: Boolean
***************************************************************/
function CheckPasswordsMatch(String1,String2,String3,Object){
	
	if(GetElementValue(GetElement(String1,Object))!=GetElementValue(GetElement(String2,Object))){
		alert(String3);
		selectElement(GetElement(String1,Object));
		return false;
	}
	else
		return true;
}
/**************************************************************
 SelectedIndex: check if the first option is selected in a DDL 

 Parameters:
	   (String1) - Element name
	   (String2) - index value
	   (String3) - Error Message
	   (Object)  - Form Object
           

 Returns: Boolean
***************************************************************/
function SelectedIndex(String1,String2,String3,Object){
	var SelectElement= GetElement(String1,Object);
	if( SelectElement.selectedIndex <=0  ){
		alert(String3);
		selectElement(SelectElement);
		return false;
	}else
		return true;
}
/**************************************************************
 SelectedMultiple: check if the options is selected in a DDL 

 Parameters:
	   (String1) - Element name
	   (String2) - index value
	   (String3) - Error Message
	   (Object)  - Form Object
           

 Returns: Boolean
***************************************************************/
function SelectedMultiple(String1,String2,String3,Object){
	var SelectElement= GetElement(String1,Object);
	
	if(SelectElement.length > 1)
		for(i=1;i<SelectElement.length;i++){
			if(SelectElement.options[i].selected == true){
				return true;		
			}
		}	
	
	alert(String3);
	return false;
		
}

/**************************************************************
 SelectedMultiple: check if the options is selected in a DDL 

 Parameters:
	   (String1) - Element name
	   (String2) - index value
	   (String3) - Error Message
	   (Object)  - Form Object
           

 Returns: Boolean
***************************************************************/
function SelectedMultiple2(String1,String2,String3,Object){
	var SelectElement= GetElement(String1,Object);
	
	if(SelectElement.length > 0)
		for(i=1;i<SelectElement.length;i++){
			if(SelectElement.options[i].selected == true){
				return true;		
			}
		}	
	
	alert(String3);
	return false;
		
}
/**************************************************************
 SelectedLength: check the lenght of a DDL 

 Parameters:
	   (String1) - Element name
	   (Number) -   length value
	   (String3) - Error Message
	   (Object)  - Form Object
           

 Returns: Boolean
***************************************************************/
function SelectedLength(String1,Number1,String3,Object){

	var SelectElement= GetElement(String1,Object);
	if( SelectElement.length < parseInt(Number1)  ){
		alert(String3);
		selectElement(SelectElement);
		return false;
	}else{
		 return true;
		}
}

/**************************************************************
 CheckDropDown: check if the first option is selected in a DDL 

 Parameters:
	   (str_SelectName) - Select object Name
	   (obj_Form) - the From object
	   (str_Alert) - string to appear in the alert error message
	   (int_CheckFor)  - integer value to check the value of selected index equal it
           

 Returns: Boolean
***************************************************************/
function CheckDropDown(str_SelectName,obj_Form,str_Alert,int_CheckFor)
{
if (GetElementValue(GetElement(str_SelectName,obj_Form)) == int_CheckFor)
		{
			alert(str_Alert);
			selectElement(GetElement(str_SelectName,obj_Form));
			return false;
		}

return true;
		
}
function CheckDropDown2(str_SelectName,str_Alert,int_CheckFor)
{
if (GetElementValue(str_SelectName) == int_CheckFor)
		{
			alert(str_Alert);
			selectElement(str_SelectName);
			return false;
		}

return true;
		
}



/**************************************************************
 GetMonthNumber: return the month number

 Parameters:
	   (str_MonthName) - Month Name
	   
           

 Returns: Integer
***************************************************************/
function GetMonthNumber(str_MonthName)
{
	var int_Number = 0;
	
	switch (str_MonthName)
	{
		case "January":
			int_Number = 1;
			break;
		case "February":
			int_Number = 2;
			break;
		case "March":
			int_Number = 3;
			break;
		case "April":
			int_Number = 4;
			break;
		case "May":
			int_Number = 5;
			break;
		case "June":
			int_Number = 6;
			break;
		case "July":
			int_Number = 7;
			break;
		case "August":
			int_Number = 8;
			break;
		case "September":
			int_Number = 9;
			break;	
		case "October":
			int_Number = 10;
			break;
		case "November":
			int_Number = 11;
			break;
		case "December":
			int_Number = 12;
			break;
	}
	
	return int_Number;
		
}

function AddOther(sourceElement){

	ComboAdd(sourceElement,"-5","Other...");

	if (sourceElement.length == 2) 
			sourceElement[1].selected=true;
		else	
			sourceElement[0].selected=true;
			
}