// ===================================================================
// Author: SkyMark Corporation Development Team
// WWW: http://www.skymark.com/
// Common routines used by all project developed by SkyMark Corporation
// ===================================================================

//---------------String Section--------------------

function initTextFields(n)
{
	for(var i=0;i<n;i++)
	{
		if (document.forms[0].elements[i].value=="null")
		{
			document.forms[0].elements[i].value="";
		}
	}
}

// Check if the string is empty
// Param: value		the parameter to be evaluated
function isEmpty(value)
{
	var bEmpty = false;

	if (trimString(value) == "")
		bEmpty = true;

	return bEmpty;
}

// This function is used to trim String
// Param: 	str		the string to trim
function trimString (str)
{
  	str = this != window? this : str;
  	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

// Return text with the specified length
// Param: 	s			the text
//			maxlimit	the max limit
function truncate(s, maxlimit)
{
	if (s.value.length > maxlimit)
	{
		s.value = s.value.substring(0, maxlimit);
	}
}

// In String str, replace all the oldStrs with newStrs
// Param: str		the string
// 		  oldStr	the string to be replaced
// 		  newStr	the string to replace
function replaceString(str, oldStr, newStr)
{
	var sRetVal = "";
	var index = str.indexOf(oldStr);

	while(index != -1)
	{
		sRetVal = sRetVal + str.substring(0, index) + newStr;
		str = str.substring(index + oldStr.length);
		index = str.indexOf(oldStr);
	}

	sRetVal = sRetVal + str;

	return sRetVal;
}

//---------------Conversion Section--------------------//

// Convert string to an int
// Param: vStr	string to be converted
function strToInt(vStr)
{
	vStr = trimString(vStr);
	if (vStr == "")
		return 0;
	else
		return parseInt(vStr, 10);
}

// Convert string to a float
// Param: vStr	string to be converted
function strToFloat(vStr)
{
	if (vStr == "")
		return 0;
	else
		return parseFloat(vStr);
}

// Convert string to an array
// Param: vStr	string to be converted
function strToArray(vStr, vDelimiter)
{
	var aRet = new Array();
	var nPos = vStr.indexOf(vDelimiter);

	while (nPos != -1)
	{
		if (nPos != 0)
			aRet[aRet.length] = vStr.substring(0, nPos);
		else
			aRet[aRet.length] = "";

		if (vStr.length > nPos+1)
		{
			vStr = vStr.substring(nPos+1);
			nPos = vStr.indexOf(vDelimiter);
		}
		else
		{
			vStr = "";
			nPos = -1;
		}

	}

	if (vStr.length > 0)
		aRet[aRet.length] = vStr;

	return aRet;
}


// Convert int to a fix length string
// Param: vInt			The int value
//		  vLen			The length of result string
//		  vFillChar		The char that will be filled into string
function intToStr(vInt, vLen, vFillChar)
{
	var vRetVal = "" + vInt;

	if (vLen > 0)
	{
		while (vRetVal.length < vLen)
		{
			vRetVal = vFillChar + vRetVal;
		}
	}

	return vRetVal;
}


//-----------------Validation Section------------------------

// Validate if the given parameter is valid number ,it return true if the parameter is empty
// Param: s		the number
function isValidNumber(s)
{
	var i;
	var hasDecimalPoint = false;

	if (isEmpty(s))
	{
		return true;
	}
	if(parseFloat(s)<0 || parseFloat(s)>99999999 || s.indexOf(".")!=-1)
	{
		return false;
	}

	for (i = 0; i < s.length; i++)
	{
	  var c = s.charAt(i);

	  if(!isDigit(c) && !(c=="." && !hasDecimalPoint) && !(i==0 && c=="-"))
	  	return false;

	  if (c==".")
	  	hasDecimalPoint = true;
	}

	return true;
}

function isValidFloat(s)
{
	var i;
	var hasDecimalPoint = false;

	if (isEmpty(s))
	{
		return true;
	}
	if(parseFloat(s)<0 || parseFloat(s)>99999999 || s==".")
	{
		return false;
	}

	for (i = 0; i < s.length; i++)
	{
	  var c = s.charAt(i);

	  if(!isDigit(c) && !(c=="." && !hasDecimalPoint) && !(i==0 && c=="-"))
	  	return false;

	  if (c==".")
	  	hasDecimalPoint = true;
	}

	return true;
}

// Validate Quote given the string
// Param: s		the input parameter to be validated
function validateQuotes(s)
{
	if (s.value.indexOf('"') != -1 || s.value.indexOf("'") != -1)
	{
		alert("Invalid entry! Double quote and single quote characters are not allowed.");
		s.disabled = false;

		while (s.value.indexOf("'") != -1)
		{
			s.value = s.value.replace("'", "");
		}

		while (s.value.indexOf('"') != -1)
		{
			s.value = s.value.replace('"', "");
		}

		s.focus();
	}
}

// Validate if The User ID has valid format or not. Valid User ID has A-Z, a-z, 0-9, "-", "_" and "." only.
// Param: 	s		the input parameter to be validated
function validateUserIdFormat(s)
{
	var ok = true;
	var c;
	for (var i=0; i<s.value.length; i++)
	{
		c = "" + s.value.substring(i, i+1);
		if(!isLetter(c) && !isDigit(c) && (c != "-") && (c != "_") && (c != "."))
		{
			ok = false;
			break;
		}
	}

	if (!ok)
	{
		alert("Invalid entry! Only letters, digits, and special characters '-', '_' and '.' are allowed.");

		var temp = "";
		for (var i=0; i<s.value.length; i++)
		{
			c = "" + s.value.substring(i, i+1);
			if(isLetter(c) || isDigit(c) || (c == "-") || (c == "_") || (c == "."))
			{
				temp = temp + c;
			}
		}
		s.value = temp;

		s.focus();
	}
}

// Validate if input is valid or not. Valid input has 0-9 only.
// Param: s		the input parameter to be validated
function validateDigitFormat(s)
{
	if(isEmpty(s))
	{
		return false;
	}

	var i;
	for (i = 0; i < s.length; i++)
	{
	  var c = s.charAt(i);

	  if(!isDigit(c))
	  	return false;
	}
	return true;
}

// Returns true if character c is an English letter (A .. Z, a..z).
// Param: c		the input parameter to be evaluated
function isLetter (c)
{
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}

// Returns true if character c is a digit between 0 to 9.
// Param: c		the input parameter to be evaluated
function isDigit (c)
{
	return ((c >= "0") && (c <= "9"));
}

// Validate if the given parameter is positive number
// Param: c		the input parameter to be evaluated
function isPositiveNumber(c)
{
	var num = parseFloat(c);

	return (num != null && num > 0);
}

// Validate if the given parameter is a positive integer
// Param: s		the input parameter to be evaluated
function isPositiveInteger(s)
{
	var i;

	if (!isPositiveNumber(s))
	{
		return false;
	}

	for (i = 0; i < s.length; i++)
	{
	  var c = s.charAt(i);

	  if(!isDigit(c))
	  	return false;
	}

	return true;
}

// Validate if the given parameter is number
// Param: c		the input parameter to be evaluated
function isNumber(c)
{
	var num = parseFloat(c);

	return (num != null);
}

// Validate the double quotes
// Param: s		the input parameter to be validated
function validateDoubleQuote(s)
{
	if (s.value.indexOf('"') != -1)
	{
		alert("Invalid entry! Double quotes are not allowed.");
		s.disabled = false;

		while (s.value.indexOf('"') != -1)
		{
			s.value = s.value.replace('"', "");
		}

		s.focus();
	}
}

// Valid if the given string is in a valid email format
// Param: str		the input parameter to be evaluated
function isValidEmail(str)
{
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp)
	{
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr))
			supported = 1;
	}

	if (!supported)
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");

	return (!r1.test(str) && r2.test(str));
}


// Alert the given text if it is not empty
// Param: vTxt		the text to display in 'Alert' message box
function displayText(vTxt)
{
	if(!isEmpty(vTxt))
		alert(vTxt);
}

// Reset the given field
// Param: vField	the field to be reset
function resetField(vField)
{
	vField.value = "";
	vField.focus();
}

//---------------Miscellaneous Section--------------------

// Display a new window dialog
// Param: 	url			the url
//			MyWindow	the window object
//			mwidth		the width of window
//			mheight 	the height of window
function doModal(url,MyWindow,mwidth,mheight)
{
	var newWindow;

	if (document.all&&window.print) //if ie5
		newWindow = window.showModelessDialog(url,MyWindow,"help:0;resizable:1;dialogWidth:"+mwidth+"px;dialogHeight:"+mheight+"px;status:no");
	else
		newWindow = window.open(url,"","width="+mwidth+"px,height="+mheight+"px,resizable=1,scrollbars=1");

	newWindow.name = "NewWindow";

	return newWindow;
}

// Display a new window dialog
// Param: 	url			the url
//			MyWindow	the window object
//			mwidth		the width of window
//			mheight 	the height of window
//			mLeft		the position of window from left
//			mTop		the position of window from top
function showModalLocation(url,MyWindow,mwidth,mheight,mLeft,mTop)
{
	var newWindow;

	if (document.all&&window.print) //if ie5
		newWindow = window.showModelessDialog(url,MyWindow,"help:0;resizable:1;dialogWidth:"+mwidth+"px;dialogHeight:"+mheight+"px;dialogLeft:"+mLeft+"px;dialogTop:"+mTop+"px;status:no");
	else
		newWindow = window.open(url,MyWindow,"width="+mwidth+"px,height="+mheight+"px,resizable=1,scrollbars=1,left="+mLeft+"px,top="+mTop+"px");

	newWindow.name = "NewWindow";

	return newWindow;
}

// Return false if ctrl key and shiftkey is pressed
function doSelectStart()
{
	var oEvent = window.event;
	if (oEvent.ctrlKey || oEvent.shiftKey)
	    return false;
    else
    	return true;
}

// Use this function instead of "location.reload()"
// The reload() function causes problems when using Java 1.4.1
function reloadPage()
{
	window.top.Working.location.href=window.top.Working.location;
}

// Disable the right-click menu
function disableRightClickMenu()
{
	document.oncontextmenu=new Function("return false");
}

// Check if the browser is Netscape
function isNetscape()
{
	if(window.navigator.appName == "Netscape")
		return true;
	else
		return false;
}

// Get an Alphabet String from a number. If the number is larger than 26,
// it will return more than one characters, eg. "AA" or "AAA".
function getLetter(vNum)
{
	var sLetter = "";
	if(vNum >= 0)
	{
	     var sAlphabet  ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	   	 var nPos = vNum%26;
	   	 var sTemp = "";

	   	 if (nPos == 0)
	   	 	sTemp = "Z";
	   	 else
		   	 sTemp=sAlphabet.substr(nPos-1,1);

		 var nRound = parseInt(vNum/27)+1;

	   	 for (var i=0;i<nRound;i++)
	   	 	sLetter = sLetter+sTemp;
   	 }
     return sLetter;
}

// Print out
function doPrint()
{
	var controller = document.getElementById("controller");

	if(controller)
		controller.style.display = "none";

	window.print();

}

function round(number, nDigit)
{
	// rounds number to X decimal places, defaults to 2
    nDigit = (!nDigit ? 2 : nDigit);

    return Math.round(number*Math.pow(10,nDigit))/Math.pow(10,nDigit);
}

function onClickImg(url)
{
  window.open(url,'_blank','top=100,left=100,width=840,height=500,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes')
}

function onClickImgPie(url)
{
  window.open(url,'_blank','top=100,left=100,width=700,height=400,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes')
}

function validatePositiveNum(vField)
{
	var bRetVal = true;
	var sValue = vField.value;
	if(!isEmpty(sValue) && !isPositiveInteger(sValue))
	{
		alert("Please enter a positive whole number.");		
		vField.select();
		bRetVal = false;
	}
	return bRetVal;		
}	

function validateNoNegativeNum(vField)
{    
	var bRetVal = true;
    if (!isValidNumber(vField.value ))
    {
        alert('Please enter a positive whole number or zero.');
        vField.select();
        bRetVal = false;
	}
	return bRetVal;
}

function PrintPage()
{
	var oImgHeader = document.getElementsByName("imgHeader")[0];
	var oTblContainer = document.getElementsByName("tblContainer")[0];
		
	oImgHeader.style.width = "660";
	oTblContainer.style.width = "95%";
	
	print();
	
	oImgHeader.style.width = "";
	oTblContainer.style.width = "778";
}

function validateNoNegativeFloat(s)
{
	var bRetVal = isValidFloat(s);
	
	if(bRetVal)
	{
		var nTemp = strToFloat(s);
		bRetVal = (nTemp >= 0);	
	}	
	
	return bRetVal;
}	

function validateNoNegativeFloatByField(oField)
{
	var s = oField.value;
	var bRetVal = validateNoNegativeFloat(s);
	
	if(!bRetVal)
	{
		alert('Please do not enter dollar signs or commas. Just use positive numbers, and, optionally, a decimal point and up to 2 decimal places, e.g. 1111, or 1111.11.');
		oField.select();		
	}	
	return bRetVal;
}

function textLength(s, maxlimit)
{	
	  var result = true;
	  if (s.value.length > maxlimit)
	  {
	  	//alert("The maximum length is " + maxlimit);
	  	s.value = s.value.substring(0, maxlimit);
	    result = false;
	  }
	  
	  if (window.event)
	   window.event.returnValue = result;
	    
	  return result;
}