/* Window's name definition */
var WINDOW_INTERNAL = 'sf_internal';
var WINDOW_EXTERNAL = 'sf_external';
var WINDOW_TARIFF = 'sf_tariff';
var WINDOW_SUB_TARIFF = 'sf_sub_tariff';
var WINDOW_INSURANCE = 'sf_insurance';
var WINDOW_RELO = 'sf_relocation';
var WINDOW_COUNTRY = 'sf_countryinfo';
var WINDOW_PRINTFRIENDLY = 'sf_print_friendly';
var WINDOW_STORAGE = 'sf_storage';
var WINDOW_DESTCTYINFO = 'sf_destctyinfo';
var WINDOW_SHIPMENT = 'sf_shipment';
var WINDOW_INVOICE = 'sf_invoice';
var WINDOW_MULTIPLE = 'sf_multiple';

var LINK_LONELYPLANET = 'http://www.lonelyplanet.com';
var LINK_OMNI = 'http://www.omnimoving.com';
var LINK_MAPS = 'http://www.maps.com';
var LINK_ACROBATREADER = 'http://www.adobe.com';
var LINK_WINZIP = 'http://www.winzip.com';

var FINDAHOME_BKK = 'http://bangkok.asiaxpat.com/property/santafe/preferred.asp';
var FINDAHOME_BJG = 'http://beijing.asiaxpat.com/property/santafe/preferred.asp';
var FINDAHOME_CAN = 'http://guangzhou.asiaxpat.com/property/santafe/preferred.asp';
var FINDAHOME_HKG = 'http://www.asiaxpat.com.hk/property/santafe/preferred.asp';
var FINDAHOME_JKT = 'http://jakarta.asiaxpat.com/property/santafe/preferred.asp';
var FINDAHOME_KUL = 'http://kualalumpur.asiaxpat.com/property/santafe/preferred.asp';
var FINDAHOME_MNL = 'http://manila.asiaxpat.com/property/santafe/preferred.asp';
var FINDAHOME_SHA = 'http://shanghai.asiaxpat.com/property/santafe/preferred.asp';
var FINDAHOME_SIN = 'http://www.asiaxpat.com.sg/property/santafe/preferred.asp';
var FINDAHOME_TYO = 'http://tokyo.asiaxpat.com/property/santafe/preferred.asp';

var firstField;
var sHTMLPath="/ecs/html/";
var sHomePath="/ecs";
var sHomeURI="/ecs/index.jsp";
var sMyHomeURI="/ecs/jsp/myHome.jsp";
var sSantaFeWebsite = "http://www.santaferelo.com";
var sCalendarHTML = "common/Calendar.html";

var calWin = "";
self.isOpenCalendar = false;

function enlargeFields(oForm , bIsIE, sizeplus) {
	if (bIsIE) {
		for (var i=0; i<oForm.elements.length; i++) {
			if ((oForm.elements[i].type == 'text') || (oForm.elements[i].type == 'password')) {
				oForm.elements[i].size += sizeplus;
			} else if (oForm.elements[i].type == 'textarea') {
				oForm.elements[i].cols = 90;
			}
		}
	}
	return;
}	

function clearForm(sUrl) {
	if (confirm("Are you sure you want to reset the form?"))
		self.location = sUrl;
}

function resetForm(oForm) {
	if (confirm("Are you sure you want to reset the form?")) {
		oForm.reset();
	}
}


function resetForm() {
	if (confirm("Are you sure you want to reset the form?")) {
		window.location.reload();
	}
}

function openCalendar(sFieldName, oForm)
{	
	document.cookie = "calender=";
	if (self.isOpenCalendar == true)
	{
		if (calWin.closed == false) 
		{
			calWin.close();
		}
	}
				
	self.dateFieldY = oForm.elements[sFieldName + "Y"];
	self.dateFieldM = oForm.elements[sFieldName + "M"];
	self.dateFieldD = oForm.elements[sFieldName + "D"];
	//self.dateFormat = sFormat;
	self.currentDate = new Date();
	var winWidth = 180;
	var winHeight = 165;
	
//	var nPosX = Math.round((screen.availWidth-winWidth)/2);
//	var nPosY = Math.round((screen.availHeight-winHeight)/2);
//	var winStyle = 'status=no,resizable=no,height=' + winHeight.toString() + ',width=' + winWidth.toString() + ',scrollbars=no,titlebar=no,toolbar=no,left='+nPosX+',top='+nPosY;
//	calWin = window.open(sHTMLPath + sCalendarHTML, sFieldName, winStyle);
	calWin = openWin1(sHTMLPath + sCalendarHTML, winWidth, winHeight, sFieldName , 'no');
//	calWin.focus();
	self.isOpenCalendar = true;
}

function openWin1(url, width, height, name, scroll) {
	var posX = (screen.availWidth-width)/2;
	var posY = (screen.availHeight-height)/2;
		
	if (openWin1.arguments.length == 5 && scroll != "yes") 
		scroll = "no";
	else	scroll = "yes";
	oNewWin = window.open(url, name, 
			"toolbar=no,menubar=no,status=no,resizable=no,scrollbars=" + scroll
				+ ",screenX=" + posX + ",screenY=" + posY + ",innerWidth=" + width 
				+ ",width=" + width + ",height=" + height + ",top=" + posY + ",left=" + posX);	
	oNewWin.clientWidth = width;
	
	if (document.all) {
		document.onmousedown = focusPopup;
		document.onmouseup = focusPopup;
	
		
		if (parent.frames[0]) {			
			parent.frames[0].document.onmousedown = focusPopup;
			parent.frames[0].document.onmouseup = focusPopup;
		}
		if (top.frames[0]) {
			top.frames[0].document.onmousedown = focusPopup;
			top.frames[0].document.onmouseup = focusPopup;
		}
	} else {
		//self.onfocus = focusPopup;		
		window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP| Event.FOCUS);		
		window.onclick=focusPopup;
		window.onfocus=focusPopup;
		window.onblur = null;
	
		
		if (parent.frames[0]) {								
			parent.frames[0].captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP| Event.FOCUS);
			parent.frames[0].onclick=focusPopup;
			//parent.frames[0].document.onmousedown = focusPopup;
			//parent.frames[0].document.onmouseup = focusPopup;
		}
		
		if (top.frames[0]) {
			top.frames[0].captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP| Event.FOCUS);
			top.frames[0].onclick=focusPopup;
			//top.frames[0].document.onmousedown = focusPopup;
			//top.frames[0].document.onmouseup = focusPopup;
		}
	}
	return oNewWin;
}

function focusPopup() {	
	if (oNewWin != null && !oNewWin.closed) {			
		if (oNewWin && !oNewWin.closed)
			oNewWin.focus();
	} else if (document.layers) {		
		window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
		window.onclick=null;
		window.onfocus=null;
		if (parent.frames[0]) {
			parent.frames[0].releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
			parent.frames[0].onclick=null;
			parent.frames[0].onfocus=null;
		}
		if (top.frames[0]) {
			top.frames[0].releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS);
			top.frames[0].onclick=null;
			top.frames[0].onfocus=null;
		}
		return true;
	} else { // IE case
		document.onmousedown = null;
		document.onmouseup = null;
	
		if (parent.frames[0]) {			
			parent.frames[0].document.onmousedown = null;
			parent.frames[0].document.onmouseup = null;
		}
		if (top.frames[0]) {
			top.frames[0].document.onmousedown = null;
			top.frames[0].document.onmouseup = null;
		}	
	
		return true;		
	}
	return false;
}

function formInit(oForm) {
	focusField(oForm);
	initDates(oForm);	
}

function focusField(oForm) {
	if (oForm.elements[firstField] == null) {
		return;
	}
	if (oForm.elements[firstField].length) { // radio		
		if (oForm.elements[firstField].type
				&& oForm.elements[firstField].type.substring(0, 6) == 'select')
			oForm.elements[firstField].focus();
		else
			oForm.elements[firstField][0].focus();
	}
	else if (oForm.elements[firstField].type == 'checkbox') {
		if (oForm.elements[firstField].length) {
			oForm.elements[firstField][0].focus();
		} else {
			oForm.elements[firstField].focus();
		}
	} else {
		oForm.elements[firstField].focus();
	}	
}

function initDates(oForm) {		
	for (var i=0; i<asFields.length; i++) {
		if (asFields[i].dtype == TYPE_DATE) {
			if (!asFields[i].multiple) {
				with (oForm) {
					elements[asFields[i].field + 'Y'].onkeyup=enterDate;
					elements[asFields[i].field + 'M'].onkeyup=enterDate;
					elements[asFields[i].field + 'D'].onkeyup=enterDate;
				}
			} else {
				for (var j=asFields[i].lowIndex; j<asFields[i].upIndex; j++) {
					with (oForm) {						
						elements[asFields[i].field + 'Y' + j].onkeyup=enterDate;
						elements[asFields[i].field + 'M' + j].onkeyup=enterDate;
						elements[asFields[i].field + 'D' + j].onkeyup=enterDate;
					}
				}
			}
			
		}
	}	
}
function enterDate(e) {
	var asDate = ['Y', 'M', 'D'];
	var asDateMax = [4, 2, 2];
	var sname, sval;
	var oform;
	
	if (IE4) {		
		sname = event.srcElement.name;
		sval = event.srcElement.value;
		oform = event.srcElement.form;
		if (event.keyCode < 33 || event.keyCode>128 || 
			event.altKey || event.ctrlKey || event.shiftKey) {						
			return false;
		}
	} else {
		if (e.modifier == Event.ALT_MASK || e.modifier == Event.CONTROL_MASK ||
			e.modifier == Event.SHIFT_MASK || e.modifier == Event.META_MASK) {
			return true;		
		}
		sname = e.target.name;
		sval = e.target.value;
		oform = e.target.form;
	}
	
	for (var i=0; i<asDate.length; i++) {
		if (sname.charAt(sname.length-1) == asDate[i]) {
			handleDate(sval, asDateMax[i], 
					oform.elements[sname.substring(0, sname.length-1) + asDate[i+1]]);
			break;
		}
		if (i == 1) break;
	}
}

function handleDate(str, iMax, oNext) {
	if (str.length == iMax)
		oNext.focus();
}

function formatCurrency(n) {
	if (n.indexOf(",") != -1) n = n.replace(/,+/g, "");
  	var nAfter;
  
  	if (n.indexOf(".") != -1) {
		nAfter = n.substring(n.indexOf("."));
		n=n.substring(0, n.indexOf("."));
		for (var i=nAfter.length; i<3; i++) {
			nAfter += "0";
		}
	} else nAfter = ".00";
  
  	var arr=new Array('0'), i=0; 
  	while (n>0) 	
    	{arr[i]=''+n%1000; n=Math.floor(n/1000); i++;}
  	arr=arr.reverse();
  	for (var i in arr) if (i>0) //padding zeros
    while (arr[i].length<3) arr[i]='0'+arr[i];
  	return arr.join() + nAfter;
}

function checkCheckboxState(oChk, sName, sAction) {
	var iCount = 0, sVal;
	if (oChk.length) { // more than one checkbox
		for (var i=0; i<oChk.length; i++) {
			if (oChk[i].checked) {
				sVal = oChk[i].value;
				iCount++;
			}
		}
		if (iCount < 1) {
			alert("Please select a " + sName + " to " + sAction + ".");
			return "";
		} else if (iCount > 1) {
			alert("Please select only one " + sName + " to " + sAction + ".");
			return "";
		}		
	} else { // single checkbox
		if (oChk.checked) {
			sVal = oChk.value;
		} else {
			alert("Please select a " + sName + " to " + sAction + ".");
			return "";
		}
	}
	return sVal;
}

function checkAtLeastOneCheckboxState(oChk, sName, sAction) {
	var iCount = 0;
	var sIds = new Array();	
	if (oChk.length) { // more than one checkbox
		for (var i=0; i<oChk.length; i++) {
			if (oChk[i].checked) {				
				sIds[sIds.length] = oChk[i].value;
				iCount++;
			}
		}
		if (iCount < 1) {
			alert("Please select a " + sName + " to " + sAction + ".");		
			return "";
		} 	
	} else { // single checkbox
		if (!oChk.checked) { 			
			alert("Please select a " + sName + " to " + sAction + ".");	
			return "";
		}
		else {
			sIds[sIds.length] = oChk.value;			
		}
	}
	return sIds;
}

var oNewWin = null;
var popupWin = null;

function openStdWin(urlStr, winStr) {

	var w = 0, h = 0;
	var popW = 800, popH = 600;	

	if (NS4 || IE4) {
		w=screen.availWidth;
		h=screen.availHeight;
	}

	if (NS4) {
		popW = 750;
		popH = 500;
	}
   
	if (winStr == WINDOW_RELO || winStr == WINDOW_PRINTFRIENDLY || winStr == WINDOW_INTERNAL) {
		popW = 630;
		popH = 500;	   	
	} else if (winStr == WINDOW_STORAGE || winStr == WINDOW_DESTCTYINFO) {
	   	popW = 300;
	   	popH = 250;	   	
	} else if (winStr == WINDOW_INVOICE) {
	   	popW = 630;
	   	popH = 300;	   	
	} else if (winStr == WINDOW_SHIPMENT) {
		popW = 730;
		popH = 500;	   	
	} else if (winStr == WINDOW_EXTERNAL && NS4) {
		popH = 250;		
	}

	var leftPos = (w-popW)/2;
	var topPos = (h-popH)/2;
	
	// Close previously opened windows for showing in same popup
	if (winStr != WINDOW_MULTIPLE && popupWin && !popupWin.closed)
   		popupWin.close();
   		
   	if (winStr == WINDOW_COUNTRY) {
		popupWin = window.open(urlStr,winStr,'scrollbars=yes,toolbar=no,menubar=no,titlebar=no,location=no,personalbar=no,status=no,directories=no,resizable=yes,width='+popW+',height='+popH+',left='+leftPos+',top='+topPos);	   		   	
   	} else if (winStr == WINDOW_EXTERNAL) {
		popupWin = window.open(urlStr,winStr,'scrollbars=yes,toolbar=yes,menubar=yes,titlebar=yes,location=yes,personalbar=yes,status=yes,directories=yes,resizable=yes,width='+popW+',height='+popH+',left='+leftPos+',top='+topPos);	   	
   	} else if (winStr == WINDOW_MULTIPLE) {
		popupWin = window.open(urlStr,'_blank','scrollbars=yes,toolbar=no,menubar=no,titlebar=no,location=no,personalbar=no,status=no,directories=no,resizable=yes,width='+popW+',height='+popH+',left='+leftPos+',top='+topPos);	 
	} else if (winStr == WINDOW_SUB_TARIFF) {
		popupWin = window.open(urlStr,winStr,'scrollbars=yes,toolbar=no,menubar=no,titlebar=no,location=no,personalbar=no,status=no,directories=no,resizable=yes,width='+popW+',height='+popH+',left='+leftPos+',top='+topPos);		
	} else {
		popupWin = window.open(urlStr,winStr,'scrollbars=yes,toolbar=no,menubar=no,titlebar=no,location=no,personalbar=no,status=no,directories=no,resizable=no,width='+popW+',height='+popH+',left='+leftPos+',top='+topPos);
	}
		
	setTimeout("popupWin.focus()", 100);
}

function isLaterThanToday(oForm, oField , bIncludeEqual) {
	today = new Date();
	year = today.getFullYear();
    month = today.getMonth()+1;		
    day = today.getDate();

	var bIEqual;
	if (isLaterThanToday.arguments.length < 3 ) 
		bIEqual = false;
	else
		bIEqual = bIncludeEqual;

	var asDateComponent = ["Y", "M", "D"];	
	var asTodayComponent = [year, month, day];
	var sVal1, sVal2;
	var cnt = 0;
	
	for (var i=0; i<asDateComponent.length; i++) {
		sVal1 = asTodayComponent[i];
		sVal2 = oForm.elements[oField.field + asDateComponent[i]].value;
		
		if (sVal1 != "" && sVal2 != "") {
			if (parseInt(sVal1, 10) > parseInt(sVal2, 10)) {
				alert("The " + oField.desc + " should not be earlier than today.\nPlease enter again.");
				oForm.elements[oField.field + asDateComponent[i]].focus();				
				return -1;
			} else if (parseInt(sVal1, 10) == parseInt(sVal2, 10) && bIEqual) {
				cnt++;
				if ( cnt == 3 ) {
					alert("The " + oField.desc + " should not be earlier than nor equal to today.\nPlease enter again.");
					oForm.elements[oField.field + asDateComponent[i]].focus();				
					return -1;
				}
			} else if (parseInt(sVal2, 10) > parseInt(sVal1, 10)) {
				return 1;
			}	
		} else return 1;	// if some fields are not filled in, default to be bigger than from		
	}
	return 0;
}

// bIncludeEqual 
function isEarlierThanToday(oForm, oField , bIncludeEqual) {
	today = new Date();
	year = today.getFullYear();
    month = today.getMonth()+1;		
    day = today.getDate();

	var bIEqual;
	if (isEarlierThanToday.arguments.length < 3 ) 
		bIEqual = false;
	else
		bIEqual = bIncludeEqual;

	var asDateComponent = ["Y", "M", "D"];	
	var asTodayComponent = [year, month, day];
	var sVal1, sVal2;
	var cnt = 0;
	for (var i=0; i<asDateComponent.length; i++) {
		sVal1 = asTodayComponent[i];
		sVal2 = oForm.elements[oField.field + asDateComponent[i]].value;
		if (sVal1 != "" && sVal2 != "") {
			if (parseInt(sVal1, 10) < parseInt(sVal2, 10)) {
				// SIT Bugfix (Ref=5948)
				alert("The " + oField.desc + " should not be later than today.\nPlease enter again.");
				oForm.elements[oField.field + asDateComponent[i]].focus();				
				return -1;
			} else if (parseInt(sVal1, 10) == parseInt(sVal2, 10) && bIEqual ) {
				cnt++;
				if ( cnt == 3 ) {
				// SIT Bugfix (Ref=5948)
					alert("The " + oField.desc + " should not be later than nor equal to today.\nPlease enter again.");
					oForm.elements[oField.field + asDateComponent[i]].focus();				
					return -1;
				}
			} else if (parseInt(sVal2, 10) < parseInt(sVal1, 10)) {
				return 1;
			}	
		} else return 1;	// if some fields are not filled in, default to be bigger than from		
	}
	return 0;
}

var INPUT_TYPE_TEXT = 1;
var INPUT_TYPE_CHECKBOX = 2;

function rowItem(field, type) {
	this.field = field
	this.type = type
	this.mandatory = false;
	this.name = field;
}

function checkAllRows(oForm, start, end) {
	
	for (var row=start; row<=end; row++) {
		if (!isRowEmpty(oForm, row)) {
			for (var i=0; i<asRowItems.length; i++) {
				if (asRowItems[i].mandatory == true && isColEmpty(oForm, row, i)) { 
					alert("[Line #" + row + "] Field " + asRowItems[i].name + " is mandatory.");
					oForm.elements[asRowItems[i].field+row].focus();
					return false; 
				}
			}
		}
	}
	return true;
}

function isColEmpty(oForm, row, i) {
	if (asRowItems[i].type == INPUT_TYPE_TEXT) { 
		if (!isEmpty(trim(oForm.elements[asRowItems[i].field+row].value))) { return false;	}
	} 
	if (asRowItems[i].type == INPUT_TYPE_CHECKBOX) {
		if (oForm.elements[asRowItems[i].field+row].checked == true) {return false;}
	}
	return true;
}

function isRowEmpty(oForm, row) {
	for (var i=0; i<asRowItems.length; i++) {
		if (!isColEmpty(oForm, row, i)) { return false; }
	}
	return true;
}

function isAllRowsEmpty(oForm, start, end) {
	for (var k=start; k<=end; k++) {
		if (!isRowEmpty(oForm, k)) {return false;}
	}
	return true;
}

function addLines(oForm) {

	if(isEmpty(oForm.addLine.value) || !isInteger(oForm.addLine.value)) {
		alert("Please enter an integer.");
		oForm.addLine.focus();
		return;	
	}
	N1=parseInt(oForm.totalAddLine.value);
	if (isNaN(N1)){ N1=parseInt(0);}
	N2=parseInt(oForm.addLine.value);
	if (isNaN(N2)){ N2=parseInt(0);}
	// SIT debug (Ref=5962) check N2 to make sure the addLine value to be greater than 0
	if ( parseInt(N2) <= 0 ) {
		alert("Number should be greater than zero. Please enter again.");
		oForm.addLine.focus();
		return;
	}
	if (N1+N2 >iMaxRow) {
		// SIT debug (Ref=5962) 
		alert("Exceed maximum number of lines (" + iMaxRow + "). Please enter again.");
		oForm.addLine.focus();
		return;
	}

	oForm.totalAddLine.value = N1+N2;
	oForm.actionType.value = "html";
	oForm.target = "";
	oForm.action = sFormURL;
	oForm.submit();
}

function addLine(oForm) {

	N1=parseInt(oForm.totalAddLine.value);
	if (isNaN(N1)){ N1=parseInt(0);}

	if (N1+1 >iMaxRow) {
		// SIT debug (Ref=5962) 
		alert("Exceed maximum number of lines (" + iMaxRow + "). Please enter again.");
		return;
	}

	oForm.totalAddLine.value = N1+1;
	oForm.actionType.value = "html";
	oForm.target = "";
	oForm.action = sFormURL;
	oForm.submit();
}


function addChildLine(oForm) {
	n = parseInt(oForm.totalChildLine.value);
	if (isNaN(n)) {
		n = 0;
	}
	if (n + 1 > iMaxRow) {
		// SIT debug (Ref=5962) 
		alert("Maximum number of lines (" + iMaxRow + ") exceeded.");
		return;
	}
	oForm.totalChildLine.value = n + 1;
	oForm.actionType.value = "html";
	oForm.target = "";
	oForm.action = sFormURL;
	oForm.submit();
}

function deleteLines(oForm) {

	if(isEmpty(oForm.addLine.value) || !isInteger(oForm.addLine.value)) {
		alert("Please enter an integer.");
		oForm.addLine.focus();
		return;	
	}
	
	N1=parseInt(oForm.totalAddLine.value);
	if (isNaN(N1)){ N1=parseInt(0);}		
	N2=parseInt(oForm.addLine.value);
	if (isNaN(N2)){ N2=parseInt(0);}
	if ( parseInt(N2) <= 0 ) {
		alert("Number should be greater than zero. Please enter again.");
		oForm.addLine.focus();
		return;
	}
	if (N1-N2 <iMinRow) {
		// SIT debug (Ref=5962) 
		alert("Can't delete " + N2 + " lines (the table should contain at least " + iMinRow + " line(s)). Please enter again.");
		oForm.addLine.focus();
		return;
	} else if (!isAllRowsEmpty(oForm, N1-N2+1, N1)) {
		if (!confirm("Are you sure to delete non-empty row(s)?")) {
			return;
		}
	} 
	oForm.totalAddLine.value = N1-N2;
	oForm.actionType.value = "html";
	oForm.target = "";
	oForm.action = sFormURL;
	oForm.submit();
}
