﻿//Checking a complete form

function checkForm(theForm) {
	var rval="";
	
	rval += check_txt_stu_full_name_en(theForm.txt_stu_full_name_en);
	//rval += check_txt_stu_full_name_ar(theForm.txt_stu_full_name_ar);	
	rval += check_txt_stu_add(theForm.txt_stu_add);
	rval += check_txt_stu_mob(theForm.txt_stu_mob);
	//rval += check_cmb_home_ph_no(theForm.cmb_home_ph_no);
	rval += check_txt_stu_email (theForm.txt_stu_email);
	rval += check_txt_pp_no(theForm.txt_pp_no);
	rval += check_txt_pp_poi(theForm.txt_pp_poi);
	rval += check_txt_pp_pob(theForm.txt_pp_pob);
	rval += check_txt_fbno_nid(theForm.txt_fbno_nid);
	rval += check_txt_guardian_en(theForm.txt_guardian_en);
	//rval += check_txt_guardian_ar(theForm.txt_guardian_ar);	
	rval += check_txt_guardian_mob(theForm.txt_guardian_mob);
	rval += check_txt_stu_pic_path(theForm.txt_stu_pic_path);	
	//rval += check_cmb_pp_doe_yy(theForm.cmb_pp_doe_yy, theForm.cmb_pp_doi_yy);
	      
	if (rval == "") {
		return true;
	}
	else {
		//alert('Please correct following mistakes:\n\n' + rval);
		return false;
	}
}

//function to check on blur
function checkme(frm_ctrl) {

	if (frm_ctrl.name=="txt_stu_full_name_en") check_txt_stu_full_name_en(frm_ctrl);	
	//if (frm_ctrl.name=="txt_stu_full_name_ar") check_txt_stu_full_name_ar(frm_ctrl);
	if (frm_ctrl.name=="txt_stu_add") check_txt_stu_add(frm_ctrl);
	if (frm_ctrl.name=="txt_stu_mob") check_txt_stu_mob(frm_ctrl);
	//if (frm_ctrl.name=="cmb_home_ph_no") check_cmb_home_ph_no(frm_ctrl);
	if (frm_ctrl.name=="txt_stu_email") check_txt_stu_email(frm_ctrl);
	if (frm_ctrl.name=="txt_pp_no") check_txt_pp_no(frm_ctrl);
	if (frm_ctrl.name=="txt_pp_poi") check_txt_pp_poi(frm_ctrl);
	if (frm_ctrl.name=="txt_pp_pob") check_txt_pp_pob(frm_ctrl);
	if (frm_ctrl.name=="txt_fbno_nid") check_txt_fbno_nid(frm_ctrl);
	if (frm_ctrl.name=="txt_guardian_en") check_txt_guardian_en(frm_ctrl);
	//if (frm_ctrl.name=="txt_guardian_ar") check_txt_guardian_ar(frm_ctrl);
	if (frm_ctrl.name=="txt_guardian_mob") check_txt_guardian_mob(frm_ctrl);
	if (frm_ctrl.name=="txt_stu_pic_path") check_txt_stu_pic_path(frm_ctrl);
}

// Student Full Name validation

function check_txt_stu_full_name_en (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}



// Student Arabic Full Name validation

function check_txt_stu_full_name_ar (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';    
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}


// Student Address validation

function check_txt_stu_add (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
	
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}


// Student Mobile Number validation

function check_txt_stu_mob (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
     
}
else {
	if (frm_ctrl.value.length != 7) {
		error = "* must be 7 digit";
		frm_ctrl.style.background='#ffB0B0';
		document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
	}
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}


// Student Home Phone Number validation

function check_cmb_home_ph_no (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';    
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";     
}
else {
	if (frm_ctrl.value.length != 7) {
		error = "* must be 7 digit";
		frm_ctrl.style.background='#ffB0B0';    
		document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
	}
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}


// email - validating the email

function check_txt_stu_email (frm_ctrl) {
var error="";

	//removing preceeding and trailing spaces
	frm_ctrl.value = trim(frm_ctrl.value);
	
	if (frm_ctrl.value == "") {
		error = "* missing";
		frm_ctrl.style.background='#ffB0B0';    
		document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
	} 
	else {
		//test for valid email having something before @ and after @
		//Also after @something there must be something 2 or 3 character with period .
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(frm_ctrl.value))) { 
			error = "* invalid email";
			frm_ctrl.style.background='#ffB0B0';    
			document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    			
		}
		else {
		//test email for illegal characters
			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\!\#\$\%\^\&\*]/
			if (frm_ctrl.value.match(illegalChars)) {
				error = "* illegal char used";
				frm_ctrl.style.background='#ffB0B0';
				document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
		}
	}
	}
	
	//If no error then revert the backcolour from RED to WHITE
	if (error=="") {
		frm_ctrl.style.background='#fff';
		document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
	}

	return error;    
}


// passport number validation

function check_txt_pp_no (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0'; 
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";          
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}


// place of issue validation

function check_txt_pp_poi (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';    
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}


// place of birth validation

function check_txt_pp_pob (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';    
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}



// UAE National ID/Family book number validation

function check_txt_fbno_nid (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';    
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}



// Guardian name in English validation

function check_txt_guardian_en (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';    
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}



// Guardian name in Arabic validation

function check_txt_guardian_ar (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';    
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}



// Guardian mobile number validation

function check_txt_guardian_mob (frm_ctrl) {
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if (frm_ctrl.value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';    
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
}
else {
	if (frm_ctrl.value.length != 7) {
		error = "* must be 7 digit";
		frm_ctrl.style.background='#ffB0B0';    
		document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
	}
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}

// Student Image validation


function check_txt_stu_pic_path (frm_ctrl) {
var extArray = new Array(".gif", ".jpg", ".png");
var $filename = frm_ctrl.value;
var error="";

//removing preceeding and trailing spaces
frm_ctrl.value = trim(frm_ctrl.value);

if ($filename == "") {
	error = "";
}
else {
	//extract only file name with extention and remove the remaining path
	while ($filename.indexOf("\\") != -1)
		$filename = $filename.slice($filename.indexOf("\\") + 1);

	//extracting the file extension only
	ext = $filename.slice($filename.indexOf(".")).toLowerCase();

	//checking all extensions one by one with EXT
	error = "* allowed only:  " + (extArray.join("  "));
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";    
	for (var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) {
			error = ""
			break;
		}
	}
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "";
}

return error;
}


// Expiry date validation

function check_cmb_pp_doe_yy (frm_ctrl, frm_ctrl_doi) {
var error="";

if (frm_ctrl.options[frm_ctrl.selectedIndex].value == "") {
	error = "* missing";
	frm_ctrl.style.background='#ffB0B0';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";
}

if (frm_ctrl.options[frm_ctrl.selectedIndex].value <= frm_ctrl_doi.options[frm_ctrl_doi.selectedIndex].value) {
	error = "<=Issue Year";
	frm_ctrl.style.background='#ffB0B0';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "<span style='color:#ff0000'><b>" + error + "</b></span>";
}

//If no error then revert the backcolour from RED to WHITE
if (error=="") {
	frm_ctrl.style.background='#fff';
	document.getElementById('err_' + frm_ctrl.name).innerHTML = "*";
}

return error;
}


//=====================================================================================================================
// numbers only - Test given field against keypress event for allowed charachters

function numbersonly(myfield, e, dec)
{
var key;
var keychar;

//getting key code if browser is IE
if (window.event) {
   key = window.event.keyCode;
   //For debugging purpose only
   //alert ("Windows.Event.keycode: " + key);
}
else 
	//getting keycode if browser is Mozilla/Firefox
	if (e) {
		key = e.which;
	//For debugging purpose only
	//alert ("e.which: " + key);
	}
	else
	   return true;
   
//converting the keycode (unicode/ascii) character
keychar = String.fromCharCode(key);
//For debugging purpose only
//alert ("keychar: " + keychar);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;
else
   return false;
}


// Alphabatics only - Test given field against keypress event for allowed charachters

function alphaonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ").indexOf(keychar) > -1))
   return true;
else
   return false;
}


// Alphabatics only - Test given field against keypress event for allowed charachters

function arabiconly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
	//For Mozilla Firefox
   key = window.event.keyCode;
else if (e)
	//For IE
   key = e.which;
else
   return true;

keychar = String.fromCharCode(key);
//alert ("keychar: " + keychar);

/*var i=0, str="";

for(i=1536; i<=1775; i++) {
	str = str + String.fromCharCode(i) + " code:" + i + "  ";
}
alert (str);*/

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )   
	return true;
else {
	switch(String.fromCharCode(key)) {
/*		case "0":myfield.value += String.fromCharCode(1632);break;
		case "1":myfield.value += String.fromCharCode(1633);break;
		case "2":myfield.value += String.fromCharCode(1634);break;
		case "3":myfield.value += String.fromCharCode(1635);break;
		case "4":myfield.value += String.fromCharCode(1636);break;
		case "5":myfield.value += String.fromCharCode(1637);break;
		case "6":myfield.value += String.fromCharCode(1638);break;
		case "7":myfield.value += String.fromCharCode(1639);break;
		case "8":myfield.value += String.fromCharCode(1640);break;
		case "9":myfield.value += String.fromCharCode(1641);break;
*/		case "a":myfield.value += String.fromCharCode(0x0634);break;
		case "A":myfield.value += String.fromCharCode(0x0650);break;
		case "b":myfield.value += String.fromCharCode(0x0644)+String.fromCharCode(0x0627);break;
		case "B":myfield.value += String.fromCharCode(0x0644)+String.fromCharCode(0x0622);break;
		case "c":myfield.value += String.fromCharCode(0x0624);break;
		case "C":myfield.value += String.fromCharCode(0x007D);break;
		case "d":myfield.value += String.fromCharCode(0x064a);break;
		case "D":myfield.value += String.fromCharCode(0x005D);break;
		case "e":myfield.value += String.fromCharCode(0x062b);break;
		case "E":myfield.value += String.fromCharCode(0x064F);break;
		case "f":myfield.value += String.fromCharCode(1576);break;
		case "F":myfield.value += String.fromCharCode(0x005B);break;
		case "g":myfield.value += String.fromCharCode(0x0644);break;
		case "G":myfield.value += String.fromCharCode(0x0644)+String.fromCharCode(0x0623);break;
		case "h":myfield.value += String.fromCharCode(1575);break;
		case "H":myfield.value += String.fromCharCode(0x0623);break;
		case "i":myfield.value += String.fromCharCode(1607);break;
		case "I":myfield.value += String.fromCharCode(0x00F7);break;
		case "j":myfield.value += String.fromCharCode(1578);break;
		case "J":myfield.value += String.fromCharCode(0x0640);break;
		case "k":myfield.value += String.fromCharCode(0x0646);break;
		case "K":myfield.value += String.fromCharCode(0x060C);break;
		case "l":myfield.value += String.fromCharCode(0x0645);break;
		case "L":myfield.value += String.fromCharCode(0x002F);break;
		case "m":myfield.value += String.fromCharCode(0x0629);break;
		case "M":myfield.value += String.fromCharCode(0x2019);break;
		case "n":myfield.value += String.fromCharCode(0x0649);break;
		case "N":myfield.value += String.fromCharCode(0x0622);break;
		case "o":myfield.value += String.fromCharCode(1582);break;
		case "O":myfield.value += String.fromCharCode(0x00D7);break;
		case "p":myfield.value += String.fromCharCode(0x062D);break;
		case "P":myfield.value += String.fromCharCode(0x061B);break;
		case "q":myfield.value += String.fromCharCode(0x0636);break;
		case "Q":myfield.value += String.fromCharCode(0x064E);break;
		case "r":myfield.value += String.fromCharCode(0x0642);break;
		case "R":myfield.value += String.fromCharCode(0x064C);break;
		case "s":myfield.value += String.fromCharCode(0x0633);break;
		case "S":myfield.value += String.fromCharCode(0x064D);break;
		case "t":myfield.value += String.fromCharCode(0x0641);break;
		case "T":myfield.value += String.fromCharCode(0x0644)+String.fromCharCode(0x0625);break;
		case "u":myfield.value += String.fromCharCode(1593);break;
		case "U":myfield.value += String.fromCharCode(0x2018);break;
		case "v":myfield.value += String.fromCharCode(0x0631);break;
		case "V":myfield.value += String.fromCharCode(0x007B);break;
		case "w":myfield.value += String.fromCharCode(0x0635);break;
		case "W":myfield.value += String.fromCharCode(0x064B);break;
		case "x":myfield.value += String.fromCharCode(1569);break;
		case "X":myfield.value += String.fromCharCode(0x652);break;
		case "y":myfield.value += String.fromCharCode(1594);break;
		case "Y":myfield.value += String.fromCharCode(0x625);break;
		case "z":myfield.value += String.fromCharCode(0x0626);break;
		case "Z":myfield.value += String.fromCharCode(0x007E);break;
		case "[":myfield.value += String.fromCharCode(0x062c);break;
		case "]":myfield.value += String.fromCharCode(0x062F);break;
//		case "{":myfield.value += String.fromCharCode(0x003E);break;
//		case "}":myfield.value += String.fromCharCode(0x003C);break;
//		case "?":myfield.value += String.fromCharCode(0x061F);break;
		case "/":myfield.value += String.fromCharCode(0x0638);break;
		case ",":myfield.value += String.fromCharCode(0x0648);break;
//		case "<":myfield.value += String.fromCharCode(0x002C);break;
//		case ">":myfield.value += String.fromCharCode(0x002E);break;
		case ".":myfield.value += String.fromCharCode(0x0632);break;
		case ";":myfield.value += String.fromCharCode(0x0643);break;
//		case ":":myfield.value += String.fromCharCode(0x003A);break;
		case "'":myfield.value += String.fromCharCode(0x0637);break;
/*		case '"':myfield.value += String.fromCharCode(0x0022);break;
		case "!":myfield.value += "!";break;
		case "@":myfield.value += "@";break;
		case "#":myfield.value += "#";break;
		case "$":myfield.value += "$";break;
		case "%":myfield.value += "%";break;
		case "^":myfield.value += "^";break;
		case "&":myfield.value += "&";break;
		case "*":myfield.value += "*";break;
		case ")":myfield.value += "(";break;
		case "(":myfield.value += ")";break;
		case "-":myfield.value += "-";break;
		case "_":myfield.value += "_";break;
		case "=":myfield.value += "=";break;
		case "+":myfield.value += "+";break;
		case "\\":myfield.value += "\\";break;
		case "|":myfield.value += "|";break;
*/		case "~":myfield.value += String.fromCharCode(0x0651);break;
		case "`":myfield.value += String.fromCharCode(0x0630);break;
		case " ":myfield.value += " ";break;
	}
	return false;
}

/*
else if (key >= 1536 && key <=1775)
	return true;
else
	return false;*/
}


// non-empty textbox

function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The mandatory text area has not been filled in.\n"
  }
return error;	  
}

// Trim functions
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}



