// ===================================================================
// Author: SkyMark Corporation Development Team
// WWW: http://www.skymark.com/
// Common routines used by all project developed by SkyMark Corporation
// ===================================================================

//---------------String Section--------------------

// 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(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();
}

// Disable the right-click menu
function disableRightClickMenu()
{
	document.oncontextmenu=new Function("return false");
}

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 onOpenChart(url)
{
  window.open(url,'_blank','top=100,left=100,width=800,height=500,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes')
}

// Handles collapse/expand folder event
function folderClicked(current, vIndex)
{
	//var foldercontent = current.parentNode.parentNode.nextSibling;
	var foldercontent = document.getElementById("subFolder"+vIndex);

	if (foldercontent.style.display=="none")
	{
		foldercontent.style.display="";
		current.src="image/Minus.gif";
	}
	else
	{
		foldercontent.style.display="none";
		current.src="image/Plus.gif";
	}
}