//---------------------------------------isDate(a)---------------------------------------
function isDate(a)
{
	var err=0;

	if (a.length != 10) err=1;

	b = a.substring(0, 2);  // day
	c = a.substring(2, 3);  // '/'
	d = a.substring(3, 5);  // month
	e = a.substring(5, 6);  // '/'
	f = a.substring(6, 10); // year

	//basic error checking
	if (b<1 || b>31) err = 1;
	if (c != '/') err = 1;
	if (d<1 || d>12) err = 1;
	if (e != '/') err = 1;
	if (f<0000 || f>9999) err = 1;
	if (isNaN(b)) err = 1;
	if (isNaN(d)) err = 1;
	if (isNaN(f)) err = 1;

	//advanced error checking
	// months with 30 days
	if (d==4 || d==6 || d==9 || d==11)
	{
		if (b==31) err=1;
	}

	// february, leap year
	if (d==2)
	{
		var g=parseInt(f/4);

		if (isNaN(g)) err=1;
		if (b>29) err=1;
		if (b==29 && ((f/4)!=parseInt(f/4))) err=1;
	}

	if (err==1)	return false;
	else return true;
}
//---------------------------------------------------------------------------------------


//--------------------compareDate(objDateFrom, objDateTo, objDate)-----------------------
function compareDate(objDateFrom, objDateTo, objDate){

	//if (checkDate(objDate.value)==false)
		//return;
		
	var strDateFrom=objDateFrom.value;
	var strDateTo=objDateTo.value;
	
	if (strDateFrom==null || strDateFrom ==""||strDateTo==null || strDateTo ==""){
		return;
	}

	var arrayDate;
	var datDateFrom;
	var datDateTo;
	var datTemp=new Date();
	
	arrayDate=strDateFrom.split("/");

	if (arrayDate.length != 3)
		return;

	datDateFrom= new Date(arrayDate[2], arrayDate[1]-1, arrayDate[0]);

	arrayDate=strDateTo.split("/");

	if (arrayDate.length != 3)
		return;

	datDateTo= new Date(arrayDate[2], arrayDate[1]-1, arrayDate[0]);

	if(datDateTo.getTime() - datDateFrom.getTime() <0 ){
		alert("Date From must not later than Date To")
		objDate.value="";
		objDate.focus();
	}
	
}
//---------------------------------------------------------------------------------------


//--------------------checkExpireDate(objToday, objExpDate)-----------------------
function checkExpireDate(objToday, objExpDate){
		
	var strDateFrom=objToday;
	var strDateTo=objExpDate;
	
	if (strDateFrom==null || strDateFrom ==""||strDateTo==null || strDateTo ==""){
		return false;
	}

	var arrayDate;
	var datDateFrom;
	var datDateTo;
	var datTemp=new Date();

	arrayDate=strDateFrom.split("/");

	if (arrayDate.length != 3)
		return;

	datDateFrom= new Date(arrayDate[2], arrayDate[1]-1, arrayDate[0]);

	arrayDate=strDateTo.split("/");

	if (arrayDate.length != 3)
		return;
		
	

	datDateTo= new Date(arrayDate[2], arrayDate[1]-1, arrayDate[0]);
	
	if(datDateTo.getTime() - datDateFrom.getTime() <0 ){
		return false;
	}
}
//---------------------------------------------------------------------------------------


//------------------isNumber(number,number_length,number_precision)----------------------
function isNumber(number,number_length,number_precision)
{
	var foundPosition = number.indexOf(".");
	var xArray = number.split(".");

	if (!(isNaN(number)))
	{
		if (foundPosition == -1)
		{
			if (number.length > number_length)
				return false;
			else
				return true;
		}
		else
		{
			if (xArray[0].length > number_length | xArray[1].length > number_precision)
				return false;
			else
				return true;
		}
	}
	else
		return false;
}
//---------------------------------------------------------------------------------------


//--------------------------------round_decimals(numChar, noOfDec)-----------------------

function round_decimals(numChar, noOfDec)
{
  if(numChar == ''){
  return '0.00';}
  
  if(!isInteger(noOfDec))  return Number.NaN;
  if(noOfDec>10 || noOfDec<0)  return Number.NaN;
  
  return parseFloat(numChar).toFixed(noOfDec);
}

//---------------------------------------------------------------------------------------

//---------------------------------trim(strString)---------------------------------------
function trim(strString)
{
	if(strString==null)
	  return (null);

	return rightTrim(leftTrim(strString));
}
//---------------------------------------------------------------------------------------


//--------------------------------leftTrim(strString)------------------------------------
function leftTrim(strString)
{

	if(strString==null)
	  return (null);

	var i;
	var intLength=strString.length;

	for (i=0; i<strString.length; i++)
	{
	  if(strString.charAt(i)!=" " && strString.charAt(i)!="\t" && strString.charAt(i)!="\r" && strString.charAt(i)!="\f" && strString.charAt(i)!="\n")
	    break;
	}

	return right(strString, intLength-i);
}
//---------------------------------------------------------------------------------------


//--------------------------------rightTrim(strString)-----------------------------------
function rightTrim(strString)
{
	var i;
	if (strString==null)
	  return (null);

	for(i=strString.length-1; i>=0; i--)
	{
	  if(strString.charAt(i)!=" " && strString.charAt(i)!="\t" && strString.charAt(i)!="\r" && strString.charAt(i)!="\f" && strString.charAt(i)!="\n")
	    break;
	}
	return left(strString, i+1);
}
//---------------------------------------------------------------------------------------


//--------------------------------left(strString, intLength)-----------------------------
function left(strString, intLength)
{
	if(strString==null)
	  return (null);

	if(intLength>strString.length)
	  intLength=strString.length;

	return strString.substr(0, intLength);
}
//---------------------------------------------------------------------------------------


//--------------------------------Right(strString, intLength)----------------------------
function right(strString, intLength)
{
	if(strString==null)
	  return (null);

	if(intLength>strString.length)
	  intLength=strString.length;

	return strString.substr(strString.length-intLength);
}
//---------------------------------------------------------------------------------------


//-----------------------------------isEmpty(estr)---------------------------------------
function isEmpty(estr)
{
	estr = trim(estr);
	return ((estr == null) || (estr.length == 0));
}
//---------------------------------------------------------------------------------------


//------------------------------underline_Enable(obj)------------------------------------
function underline_Enable(obj)
{
	obj.style.textDecorationUnderline = true;
}
//---------------------------------------------------------------------------------------


//------------------------------underline_Disable(obj)-----------------------------------
function underline_Disable(obj)
{
	obj.style.textDecorationUnderline = false;
}
//---------------------------------------------------------------------------------------


//------------------------------------trap_key()-----------------------------------------
function trap_key()
{
	if (event.keyCode == 13) event.keyCode = 9;
}
//---------------------------------------------------------------------------------------


//------------------------------------trap_key()-----------------------------------------
function URLEncoding(inputValue)
{
	inputValue=escape(inputValue);
	re = /\+/gi;
	return inputValue.replace(re, "%2B");
}
//---------------------------------------------------------------------------------------


//---------------------------------HTMLEncoding(inputValue)------------------------------
function HTMLEncoding(inputValue)
{
	re = /\</gi;
	return inputValue.replace(re, "&lt;");
}
//---------------------------------------------------------------------------------------


//------------------------------------_dataChange()--------------------------------------
function _dataChange()
{

	var myObj=new Object();
	var initValue="";

	function captureData(){
	initValue=event.srcElement.value;
	}


	function isChange()
	{
		return initValue!=event.srcElement.value;
	}

	function getInitValue()
	{
		return initValue;
	}


	myObj.captureData=captureData;
	myObj.isChange=isChange;
	myObj.getInitValue=getInitValue;
	return myObj;

}
var dataChange = new _dataChange();

//---------------------------------------------------------------------------------------


//------------------------------------upperCase(theObj)----------------------------------
function upperCase(theObj)
{
	theObj.value=theObj.value.toUpperCase();
}
//---------------------------------------------------------------------------------------


//------------------------------------isValidEmail(strValue)-----------------------------
function isValidEmail(strValue) 
{ 
	var reg_exp =/\w[\w\.\-]*\w*@[\w\.\-]*\w\.[a-z]{2,3}/i;

	if (reg_exp.test(strValue))	return true; 
	else return false;
}
//---------------------------------------------------------------------------------------


//------------------------------------isInteger(strInteger)------------------------------
function isInteger(strInteger)
{
	strInteger="" + strInteger;
	if(strInteger==null || strInteger=="")
	  return false;

	strInteger=trim(strInteger);

	var strIntegerPat1=/^(-)?(\d+)(\.0+)?$/;
	var strIntegerPat2=/^(-)?(\d{1,3},)?(\d{3},)*(\d{3})(\.0+)?$/;
	return (strIntegerPat1.test(strInteger) || strIntegerPat2.test(strInteger));
}
//---------------------------------------------------------------------------------------


//----------------------------------formReset(theForm, theName)--------------------------
function formReset(theForm, theName){
	theForm.reset();
	eval("theForm." + theName + ".focus()");
}
//---------------------------------------------------------------------------------------

//--------------------------------------js_encode_single_quote(s)---------------------------------
function js_encode_single_quote(s)
{
	var x =	s.split("'");
	var r = '';		

	for (var i = 0; i < x.length; i++) {
		if ( i == (x.length - 1))
			r += x[i];
		else
			r += x[i] + "\\'";			
	}

	return r;	
}
//---------------------------------------------------------------------------------------

//----------------------------------validate new IC number-------------------------------

function Checking_ICNO(obj,posi)
{
	var strError = /'|"|\\|#/

	if (strError.test(obj.value))
	{
		alert("Invalid IC number !");
		obj.focus();
		obj.select();
		return false;
	}

	if ((obj.value != '') && (!(isInteger(obj.value))))
	{
		alert('The I.C. No. text box should only contain numeric value.');
		obj.focus();
		obj.select();
		return false;
	}

	var StrIC1 = form.cnic1.value;
	var StrIC2 = form.cnic2.value;
	var StrIC3 = form.cnic3.value;

	if (!(isEmpty(obj.value)))
	{
		var intLength;
		var strValue;

		switch (posi)
		{
			case 1 :
				intLength = "6";
				strValue = 'first section';
			break;

			case 2 :
				intLength = "2";
				strValue = 'second section';
			break;

			case 3 :
				intLength = "4";
				strValue = 'last section';
			break;
		}

		if (obj.value.length < intLength )
		{
			alert("The " + strValue +  " of the I.C. No. text box should only contain numeric value and \n could not less than "+ intLength +" digits");
			obj.select();
			obj.focus();
			return false;
		}
	}

	if ((StrIC1.length == "6") && (StrIC1 != "000000")){
		var intyear = new Number(StrIC1.substring(0,2));
		var intmonth = (StrIC1.substring(2,4));
		var intday = (StrIC1.substring(4,6));
		if (intyear >= 20){
			intyear = 1900 + intyear;
		}else{
			intyear = 2000 + intyear;
		}

		if (intmonth == "00" || intday == "00")
		{
			alert("Invalid IC number !");
			form.cnic1.value="";
			form.cnic1.focus();
			return;
		}
		
		var MyDate1 = intday +'/'+ intmonth +'/'+ intyear;
		
		if (!(isDate(MyDate1))){
			alert("Invalid IC number !");
			form.cnic1.value="";
			form.cnic1.focus();
			return;
		}
	}
	
	
	if ((StrIC3.length == 4) && (StrIC3 != "0000")){
		if ((StrIC3 % 2) == 0){
			form.cgdr.value = "F";
		}else{
			form.cgdr.value = "M";
		}	
	}
}

//---------------------------------------------------------------------------------------

function TextChange(obj,intValue,numLength)
{
	if (obj.value.length == numLength)
	{
		if (intValue == "1") {
			form.cnic2.focus();}
		else form.cnic3.focus();
	}
}


function onClick_ShowDetail()
{
   if(document.getElementById("img_plus").getAttribute("alt")=="click here to view details"){
   	document.getElementById("img_plus").src="../img/SUBTRACT.GIF";
   	document.getElementById("img_plus").setAttribute("alt", "click here to close details");
   	row_dad1.style.display = '';
   	row_dsta.style.display = '';
   	row_dpos.style.display = '';
   	row_dtel.style.display = '';
   	row_dfax.style.display = '';
   }else{
   	document.getElementById("img_plus").src="../img/add.gif";
   	document.getElementById("img_plus").setAttribute("alt", "click here to view details");
	row_dad1.style.display = 'none';
	row_dsta.style.display = 'none';
	row_dpos.style.display = 'none';
	row_dtel.style.display = 'none';
   	row_dfax.style.display = 'none';
   }
}

function onChange_AirCondFlag()
{
  if(form.airc.value == '1'){
	id_imdl.innerText = 'Indoor Model Code :';
	id_isn1.innerText = 'Indoor S/N ';
	row_br1.style.display = '';
	row_br2.style.display = '';
	row_br3.style.display = '';
	row_indoor.style.display = '';
	row_isn2.style.display = '';
	row_isn3.style.display = '';
	row_isn4.style.display = '';
	row_isn5.style.display = '';
	row_isn6.style.display = '';
	row_out1.style.display = '';
	row_omdl.style.display = '';}
 else{
     	id_imdl.innerText = 'Model Code :';
	id_isn1.innerText = 'Serial Number :';
	row_br1.style.display = 'none';
	row_br2.style.display = 'none';
	row_br3.style.display = 'none';
	row_indoor.style.display = 'none';
	row_isn2.style.display = 'none';
	row_isn3.style.display = 'none';
	row_isn4.style.display = 'none';
	row_isn5.style.display = 'none';
	row_isn6.style.display = 'none';
	row_out1.style.display = 'none';
	row_omdl.style.display = 'none';}
}

function onChange_CorporateFlag()
{
  if(form.corp.value == '1'){
	row1.style.display = 'none';
	row2.style.display = 'none';
	row3.style.display = 'none';
	row4.style.display = 'none';
	row5.style.display = 'none';
	row6.style.display = 'none';
	row7.style.display = 'none';
	row8.style.display = 'none';
	row9.style.display = 'none';
	row10.style.display = 'none';
	row_cnic.style.display = 'none';
	id_coic.innerText = 'Company Reg. No.';
	form.img_coic.style.dsiplay = 'none';
} else{
     	row1.style.display = '';
	row2.style.display = '';
	row3.style.display = '';
	row4.style.display = '';
	row5.style.display = '';
	row6.style.display = '';
	row7.style.display = '';
	row8.style.display = '';
	row9.style.display = '';
	row10.style.display = '';
	row_cnic.style.display = '';
	id_coic.innerText = 'Old I/C or Passport No.';
	form.img_coic.style.dsiplay = '';
}
}


function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

function validate(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are accepted!");
field.focus();
field.select();
   }
}