var error_fn;
var errormsg;

// ---- RESET 
function reset_error()
{
	error_fn=false;
	errormsg='Following Errors Occured ::\n_____________________________\n\n';
}

// ---- VALIDATE STRING - target, error, length, required
function validate_string(target,errstr,flength,required)
// ---- Contains zero or more of: A-Z, a-z and spaces
{
 	if( (required) && (target.value == "") ) {
   		errormsg+="Please enter " + errstr + ".\n";
   		error_fn=true;
	}
	else {
		if(target.value.length > flength) {
			errormsg+= errstr + " should be < " + flength + " .\n";
	   		error_fn=true;
		}		
		var charpos = target.value.search("[^A-Za-z *]"); 
		if(charpos >= 0) {
			errormsg+=errstr + " should be alpha.\n";
			error_fn=true;
		}
	}
}

// ---- VALIDATE email - target, error, length, required
function validate_email(target,errstr,flength,required)
{
 	if( (required) && (target.value == "") ) {
   		errormsg+="Please enter " + errstr + ".\n";
   		error_fn=true;
	}
	else {
		if(target.value.length < 5 ||
		target.value.indexOf("@") == -1 ||
		target.value.lastIndexOf(".") <target.value.lastIndexOf("@") ||
		target.value.indexOf(".") == -1 ||
		target.value.indexOf("\\") > -1 ||
		target.value.indexOf("/") > -1 ||
		target.value.indexOf("elm.co.ab") > -1 ||
		target.value.indexOf("!") > -1 ||
		target.value.indexOf(",") > -1 ||
		target.value.indexOf(";") > -1 ||
		target.value.indexOf("?subject") > -1) {    
		errormsg+= errstr + " entry invalid.\n";
	   		error_fn=true;
		}		
		
	}
}

// ---- VALIDATE STRING - target, error, length, required
function validate_arabic(target,errstr,flength,required)
{
 	if( (required) && (target.value == "") ) {
   		errormsg+= "Please enter " + errstr + ".\n";
   		error_fn=true;
	}
	else {
		if(target.value.length > flength) {
			errormsg+= errstr + "should be arabic < " + flength + " .\n";
	   		error_fn=true;
		}
	}
}

// ---- VALIDATE STRING - target, error, length, required
// ---- Contains zero or more of: A-Z, 0-9, @-_ and spaces
function validate_alphanum(target,errstr,flength,required)
{
 	if( (required) && (target.value == "") ) {
   		errormsg+="Please enter " + errstr + ".\n";
   		error_fn=true;
	}
	else {
		if(target.value.length > flength) {
			errormsg+= errstr + " should be < " + flength + " .\n";
	   		error_fn=true;
		}		
		var charpos = target.value.search("[^A-Za-z0-9.@_ *]"); 
		if(charpos >= 0) {
			errormsg+=errstr + " should be alpha numeric.\n";
			error_fn=true;
		}
	}
}


// ---- VALIDATE LIST - fieldname, error
function validate_list(fieldname,errstr)
{
	if((fieldname.selectedIndex == "0") && (fieldname.value == "") ) {
		errormsg+="Please Select " + errstr + ".\n";
		error_fn=true;
	}
}

// ---- VALIDATE NUMBER - filename, error, lower, upper bound
function validate_num(fieldname,errstr,upperlimit,required)
{
	if( (required) && (fieldname.value == "") ) {
 		errormsg+="Please enter " + errstr +".\n";
 		error_fn=true;
  	}
 	else {
 		var charpos = fieldname.value.search("[^0-9]"); 
		if(charpos >= 0) {
			errormsg+= errstr + " should be number.\n";
 			error_fn=true;
		}
		else {
			if (fieldname.value > upperlimit) {
				errormsg+= errstr + " should not be greater than " + upperlimit + ".\n";
 				error_fn=true;
			}
		}
	}
}

// ---- VALIDATE NUMBER - filename, error, lower, upper bound
function validate_num_range_id(fieldname,errstr,lowerlimit,upperlimit,required)
{
	if( (required) && (fieldname.value=="")) {
 		errormsg+="رجاء أدخل " + errstr +".\n";
 		error_fn=true;
  	}
 	else {
 		var charpos = fieldname.value.search("[^0-9]"); 
		if(charpos >= 0) {
			errormsg+= errstr + " يجب أن يكون عدد.\n";
 			error_fn=true;
		}
		else {
			if (fieldname.value < lowerlimit) {
				errormsg+= errstr + " يجب أن يكون عشر خانات" + ".\n";
 				error_fn=true;
			}			
			if (fieldname.value > upperlimit) {
				errormsg+= errstr + " يجب أن يكون عشر خانات" + ".\n";
 				error_fn=true;
			}
		}			
	}
}

// ---- VALIDATE STRING - target, error, length, required
function validate_date(target,errstr,flength,required)
{
 	if( (required) && (target.value == "") ) {
   		errormsg+="Please enter " + errstr + ".\n";
   		error_fn=true;
	}
	else {
		if(target.value.length > flength) {
			errormsg+= errstr + " should be < " + flength + " .\n";
	   		error_fn=true;
		}		
		var charpos = target.value.search("[^/0-9* ]"); 
		if(charpos >= 0) {
			errormsg+=errstr + " should be like: 2003/01/26.\n";
			error_fn=true;
		}
	}
}

// ---- VALIDATE STRING - target, error, length, required
function validate_filename(target,errstr)
{
 	if(target.value == "")  {
   		errormsg+="Please enter " + errstr + ".\n";
   		error_fn=true;
	}
}

// ---- VALIDATE STRING - target, error, length, required
// ---- Contains YYYY-MM-DD or YYYY/MM/DD
function check_date(target,errstr,flength,required)
{
 	if( (required) && (target.value == "") ) {
   		errormsg+="Please enter " + errstr + ".\n";
   		error_fn=true;
	}
	else {
		if(target.value.length > flength) {
			errormsg+= errstr + " should be < " + flength + " .\n";
	   		error_fn=true;
		}		
 		isdate = /^20[0-9][0-9][\/-][01][12456789][\/-][0123][1234567890]/.test(target.value);
		if(!isdate) {
			errormsg+=errstr + " should be like: 2003/01/26 or 2003-01-26.\n";
			error_fn=true;
		}
		else {
		// The format is good, so now check the semantics
		year = target.value.substring(0,4);
		month = target.value.substring(5,7);
		day = target.value.substring(8,10);
		if(day > 31) { 
			errormsg+=errstr + ": day should not be more than 31.\n";
			error_fn=true;
		}
		if(month > 12) { 
			errormsg+=errstr + ": month should not be more than 12.\n";
			error_fn=true;
		}
		if( (year%4) > 0) {
			if( (month == 2) && (day > 28)) {
				errormsg+=errstr + ": Cannot have more than 28 days in Feb.\n";
				error_fn=true;
			}
		}
		else {
			if( (month == 2) && (day > 29)) {
				errormsg+=errstr + " Cannot have more than 29 days in Feb.\n";
				error_fn=true;
			}
		}
		if( (month == 4) || (month == 6) || (month == 9) || (month == 11) ) {
			if(day > 30) {
				errormsg+=errstr + " Cannot have more than 30 days in this month.\n";
				error_fn=true;
			}
		}
		}
	}
}

/*	
	Function 	: show_message
	Author   	: Rayan Ali Alsaghier
	Date	 	: 10/08/2003
	Description : This function will display error message in screen
				  in case user forgot to complete required fields in 
				  form.				  
*/
function show_message(message)
{
	if(error_fn == true) 
		alert(message);

} 

/*	
	Function 	: openNewWindowConvertDate
	Author   	: Rayan Ali Alsaghier
	Date	 	: 23/08/2003
	Description : This function will be used for HELP.JSP to display
				  Help Screen 
*/
function openNewWindow(fileName) {
	window.open(fileName,"mainwindow","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width="+570+",height="+500);
}

/*	
	Function 	: RedirectToAddVehicle
	Author   	: Rayan Ali Alsaghier
	Date	 	: 07/03/2004
	Description : This function will be redirect user to add_vehicle.jsp
*/

function RedirectToAddVehicle() {
	location.href="add_vehicle.jsp";
}

/*	
	Function 	: openNewWindowCover
	Author   	: Rayan Ali Alsaghier
	Date	 	: 28/08/2003
	Description : This function will be used for COVERLIST.JSP to display
				  Cover List screen
*/
function openNewWindowCover(fileName) {
	window.open(fileName,"mainwindow","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width="+500+",height="+350);
}


/*	
	Function 	: openNewWindowConvertDate
	Author   	: Rayan Ali Alsaghier
	Date	 	: 18/10/2003
	Description : This function will be used for CONVERTDATE.JSP
				  to display convert date screen in small box.
*/
function openNewWindowConvertDate(fileName) {
	window.open(fileName,"mainwindow","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width="+300+",height="+250);
}

/*	
	Function 	: limitText
	Author   	: Rayan Ali Alsaghier
	Date	 	: 23/09/2003
	Description : This function will be used for DELETE_POLICY.JSP to limit
				  number of characters to be added in textarea field
*/
function limitText(ob,mn){  
p =ob.value; l=p.length; if (l>mn)
	{ob.value=p.substring(0,l-1);}
}


/*	
	Function 	: ConvertToHijri
	Author   	: Rayan Ali Alsaghier
	Date	 	: 17/10/2003
	Description : This function will convert Gregorian date to Hijri
				  Convert To Hijry Calender 
*/
function IntPart(floatNum){
if (floatNum< -0.0000001)
	return Math.ceil(floatNum-0.0000001)
		else return Math.floor(floatNum+0.0000001)
	}

function ConvertToHijri(y,m,d,expire) {

 day = d
 month = m 
 year = y

 if ((year > 1582) || ((year == 1582) && (month>10)) || ((year == 1582) && (month==10) && (day > 14)) )
{
Temp = IntPart((1461*(year+4800+IntPart((month-14)/12)))/4)+
	           IntPart((367*(month-2-12*(IntPart((month-14)/12))))/12)-
	           IntPart((3*(IntPart((year+4900+IntPart( (month-14)/12))/100)))/4)+day-32075
	}

	else
	{
	Temp = 367*year-IntPart((7*(year+5001+IntPart((month-9)/7)))/4)+IntPart((275*month)/9)+day+1729777
	}
	 
	l=Temp-1948440+10632
	n=IntPart((l-1)/10631)
	l=l-10631*n+354
	j=(IntPart((10985-l)/5316))*(IntPart((50*l)/17719))+(IntPart(l/5670))*(IntPart((43*l)/15238))
	l=l-(IntPart((30-j)/15))*(IntPart((17719*j)/50))-(IntPart(j/16))*(IntPart((15238*j)/43))+29
	month=IntPart((24*l)/709)
	day=l-IntPart((709*month)/24)
	year=30*n+j-30
		if (expire == true) { year = year +1}
		if (month < 10) { month = "0" + month}
		if (day <10) { day = "0" + day }	
	return year+"/"+month+"/"+day
	
}

/*	
	Function 	: changeDate
	Author   	: Khaled Al-Shehri
	Date	 	: 14/03/2005
	Description : Whene a user change the date's DropDownList , the date's Text field will be changed.
*/
function changeDate(ddl)
{
	var str = document.all.ddlAccountValidYYYY.value + "-" + document.all.ddlAccountValidMM.value + "-" + document.all.ddlAccountValidDD.value ;
	var strArr = str.split('-');
	if(strArr[1].length == 1)
		strArr[1] = "0" + strArr[1];
	if(strArr[2].length == 1)
		strArr[2] = "0" + strArr[2];
	
	str = strArr[0] +"-" +  strArr[1] + "-" +  strArr[2];
	document.all.txtFullDate.value = str;
}
// end function changeDate

function datecheck(data,datetype) {
var a = data.value;
var b = /(\d{1,2})-(\d{1,2})-(\d{1,})/;var c = /[^\d-]/;var d = a.match(b);
if ((c.test(a)) || (d == null)) {errormsg += "Search "+ datetype  + " format should be : \"dd-mm-yyyy\".\n";error_fn=true;return false;}
if (d[1].length == 1) {a = "0" + a;}
var e = /29-0?2-(190[48]|19[2468][048]|19[13579][26]|20[02468][048]|20[13579][26])/;
var f = /31-(0?[13578]|10|12)-(\d{1,})/;
var g = /([012][1-9]|10|20|30)-((0?[13456789])|10|11|12)-(\d{1,})/;
var h = /([012][0-8]|09|19)-(0?2)-(\d{1,})/;
if (e.test(a) || f.test(a) || g.test(a) || h.test(a)) {return true;}
errormsg += "Search "+datetype+" is invalid. Either day, month or year is out of range.\n";error_fn=true;return false;}

/*	
	Function 	: popUp
	Author   	: Rayan Ali Alsaghier
	Date	 	: 11/05/2005
	Description : This function will be used for Alien Dependent Screen to display
				  Alien Dependant list. 
*/
function popUp(id) {
window.open('listAlienDependant.do?iqamaNum='+id,'Visitor','toolbar=0,scrollbars=1,location=0,status=0,menubar=0,resizable=0,width=600,height=360,left=100,top=100');
}