// ===========================
// Common JavaScript Functions 
// ===========================
//
// File Name		: prop_common.js
// Version		: 1.10
// Last Updated		: 1st Dec 2000
// Dependents		: common.js
// Remark		: These functions are from Main Javascript Library
//			  --> common.js.
//			  Put most used functions into this INCLUDE reduce 
//			  Time-to-Load for Browser rather than include everthing
//			  from "common.js".
//
// Method to include this common file into ASP / HTML file:
// --------------------------------------------------------
//
//    <Script language="JavaScript" src="prop_common.js"></Script>


// Global Variables and constants defined

//Get ROOT URL path for the web application
var ROOTURLPATH;
ROOTURLPATH = GetCookie("ROOTURL");

var PROPPATH		= ROOTURLPATH + "PropertySales/";
var CUSTPATH		= ROOTURLPATH + "CustomerService/";
var CONTPATH		= ROOTURLPATH + "ContratorSupplier/";
var PROCPATH		= ROOTURLPATH + "Procurement/";
var COMMONPATH		= ROOTURLPATH + "CommonFiles/";
var CALPATH 		= "../zoomwindows/calendar.asp";

var PROPZMWINPATH 	= PROPPATH + "ZoomWindows/";
var CUSTZMWINPATH	= CUSTPATH + "ZoomWindows/";
var PROCZMWINPATH	= PROCPATH + "ZoomWindows/";
var CONTZMWINPATH	= CONTPATH + "ZoomWindows/";
var COMMONZMWINPATH	= ROOTURLPATH + "ZoomWindows/";



//-----------------------------------------
//Name: Tan Ing Yeong
//Date: 01/11/2000
//Function: Go back a page
//-----------------------------------------

function GoBack(){

	history.back();
	
}

//-----------------------------------------
//Name: Tan Ing Yeong
//Date: 01/11/2000
//Function: Go back a page
//-----------------------------------------

function GoForward(){

	history.forward();
	
}	


//-----------------------------------------
//Name: sam Lee Kha WOh
//Date: 13/11/2000
//Function: Number checking routine
//-----------------------------------------
function checknum(frmElement, eleName)
{
	var errInfo, num;
	
	errInfo = "";
	
	num = parseFloat(frmElement.value);
	
	if (isNaN(num))
	{
			errInfo+= "___________________________________________\n\n";
			errInfo+="    "+ eleName +" must contain number !\n";
			errInfo+= "___________________________________________\n\n";
			alert(errInfo);
			frmElement.select();
			
			return false;
	}
	
	return true;
}



//---------------------------------------------------------
//Name: sam Lee Kha WOh
//Date: 21/12/2000
//Function: checking numeric value and return true / false
//----------------------------------------------------------
function isNum(strpass, eleName)
{
	var errInfo, num;
	var strLen = 0;
	var strTemp;
	
	errInfo = "";

	//Calculate the length of String passed-In	
	strLen = strpass.length;

	num = parseFloat(strpass);

	//convert to String again
	strTemp = new String(num);
	
	//Compare the length difference.
	//This is to avoid the truncation happens after "parseFloat" of the string
	//if string contains Number & Characters
	if ((isNaN(num)) || (strTemp.length != strLen))
	{
			errInfo+= "___________________________________________\n\n";
			errInfo+="    "+ eleName +" must contain number !\n";
			errInfo+= "___________________________________________\n\n";
			alert(errInfo);
			
			return false;
	}
	
	return true;
}



// ---------------------------------------------------------------------------------------
// Format the form item value into currency format
// By		: Wilson Ten
// Date		: 19th Aug 2000
// Modified By 	: Sam Lee Kha Woh
// Modified Date: 19th Dec 2000
// Input	: Form Item Object
// Depends  : function removeChr(strVal, chr)
// Example	: when calling from the html form 
//			  <INPUT TYPE="TEXT" NAME="DownPayment" onKeyup=ToCurrency(form.DownPayment)>
// ---------------------------------------------------------------------------------------
function ToCurrency(formItem){
	var newValue = "";
	var k=1, n;
	var decimal="";
	
	//Modified since 19th Dec 2000
	//If the formItem is empty, just return false
	if (formItem.value.length == 0) return false;

	formItem.value = removeChr(formItem.value,",");
	n = formItem.value.length;
	
	if (formItem.value.indexOf(".") > 0){
		n = formItem.value.indexOf(".")
		decimal = formItem.value.substr(n,formItem.value.length-n);
	}
	else
	{
		decimal = ".00";
	}
		
	if (n > 3){
		for (i=n-1;i>-1;i--){
			newValue = formItem.value.substr(i,1) + newValue;
			if (k>1 && k % 3==0 && i>0)
			   newValue = "," + newValue;
			k++;
		}
	}else newValue = formItem.value;

	
	//Modified since 19th Dec 2000
	//if 0 is the value, make newValue = "0.00"
	if (newValue == 0) newValue = "0";
	

	formItem.value = newValue + decimal;
}


	// --------------------------------------------------------------------
	// Remove the specified char in a string variable and return new string
	// Except chars specified in the ignoreChr
	// By		: Wilson Ten
	// Date		: 19th Aug 2000
	// Modified	: Sam Lee Kha Woh
	// Modified Date: 9th Dec 2000
	// --------------------------------------------------------------------
	function removeChr(strVal, chr){
		var strTemp="";

		for (i=0;i<strVal.length;i++){
			if (strVal.substr(i,1) != chr)
			   strTemp += strVal.substr(i,1);
		}
		return strTemp;
	}


//--------------------------------------------------
//	 Zoom window for General Used
//--------------------------------------------------
function show_zoomwin(frmName, frmElement, zmwinpath,  strMoreEle)
{
		var str, strvalue;
		var arr, ele;

		str = window.showModalDialog(zmwinpath,0,'dialogHeight:400px;help:no');
		
		arr = str.split(";");
		
		if (str.length != 0)
		{

			//-- Modified Date : 7 March 20001
			//-- Modified By   : Sam Lee
			//-- Purpose	   : Check Pass in Element is object or string
			if (isNaN(frmElement.length))  frmElement.value = arr[0];
			else frmName.item(frmElement).value = arr[0];
			
			// If there are more form element to be returned and assign value,
			// follow the value will be assigned following the sequence of the passed in elements
			j = 1;
			if ((strMoreEle.length !=0) && (strMoreEle != 0))
			{
				ele = strMoreEle.split(";");
			
			   for (i = 0; i<= arr.length - 2; i++)
				{
					
					frmName.item(ele[i]).value = arr[j];
					j++;
				}//for 	
			}
		}
}


//-------------------------------------------------------------------
// Name		: Sam Lee Kha Woh
// Date		: 1 December 2000
// Modified By 	: Nil
// Modified Date: Nil
// Remark	: Calendar Zoom Window.
//		  value will be return in dd/mm/yyyy format 
//		  to the calling form element.
//		
//---------------------------------------------------------------------
function show_calendar(frmElement)
{
	var str, strValue;
	var arrDate;

	
	str = window.showModalDialog(CALPATH,0,'dialogWidth:270px;dialogHeight:325px;dialogTop:120px;dialogLeft:690px;help:no');
	
	if (str == '' )
		frmElement.value = frmElement.value;
	else
	{
	//Get Date delimited by ";"
	arrDate = str.split(";");
	
	//Format date into dd/mm/yyy
	strValue = arrDate[0] + "/" + arrDate[1] + "/" + arrDate[2];	
	
	frmElement.value = strValue;}
}



//--------------------------------------------------
//	 Cookies Manipulation Functions
//--------------------------------------------------


// ----------------------------------------------------------------------------------------
// Function to create or update a cookie.
// By		: Wilson Ten
// Date		: 1st Aug 2000
//
// Remarks:-  
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie. If omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid. If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission requires a secure channel (HTTPS).  
//
//    The first two parameters are required.  The others, if supplied, must
//    be passed in the order listed above.  To omit an unused optional field,
//    use null as a place holder.  For example, to call SetCookie using name,
//    value and path, you would code:
//
// Example:-
//    SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//    Note that trailing omitted parameters do not require a placeholder.
//
//    To set a secure cookie for path "/myPath", that expires after the
//    current session, you might code:
//
//    SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
// ----------------------------------------------------------------------------------------
	
function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}



// -----------------------------------------------------------------
// "Internal" function to return the decoded value of a cookie
// By	: Wilson Ten
// Date : 1st Aug 2000
// -----------------------------------------------------------------
	
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}



// ------------------------------------------------------------------
// Function to return the value of the cookie specified by "name".
// By		: Wilson Ten
// Date		: 1st Aug 2000
// Depends	: function getCookieVal (offset)
//
// Remarks:-
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, 
//              or null if the cookie does not exist.
//
// -----------------------------------------------------------------
	
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
		return null;
}
