function trimLeft(str){
  if (str==null){return null;}
  for(var i=0;str.charAt(i)==" ";i++);
  return str.substring(i,str.length);
}
function trimRight(str){
  if (str==null){return null;}
  for(var i=str.length-1;str.charAt(i)==" ";i--);
  return str.substring(0,i+1);
}
function trim(str){return trimLeft(trimRight(str));}
function trimLeftAll(str) {
  if (str==null){return str;}
  for (var i=0; str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t"; i++);
  return str.substring(i,str.length);
}
function trimRightAll(str) {
  if (str==null){return str;}
  for (var i=str.length-1; str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t"; i--);
  return str.substring(0,i+1);
}
function replaceAll(varb, replaceThis, replaceBy)
 {
      newvarbarray=varb.split(replaceThis);
      newvarb=newvarbarray.join(replaceBy);
      return newvarb;
}   
function trimAll(str) {
  return trimLeftAll(trimRightAll(str));
}
// isNull(value)
//   Returns true if value is null
function isNull(val){return(val=="");}

// isBlank(value)
//   Returns true if value only contains spaces
function isBlank(val){
  return isNull(trimAll(val));
}

// isInteger(value)
//   Returns true if value contains all digits
function isInteger(val){
  if (isBlank(val)){return false;}
  for(var i=0;i<val.length;i++){
    if(!isDigit(val.charAt(i))){return false;}
  }
  return true;
}

// isNumeric(value)
//   Returns true if value contains a positive float value
function isNumeric(val){return(parseFloat(val)==(val*1)&&parseFloat(val)>=0);}

// isArray(obj)
// Returns true if the object is an array, else false
function isArray(obj){return(typeof(obj.length)=="undefined")?false:true;}

// isDigit(value)
//   Returns true if value is a 1-character digit
function isDigit(num) {
  if (num.length>1){return false;}
  var string="1234567890";
  if (string.indexOf(num)!=-1){return true;}
  return false;
}

// setNullIfBlank(input_object)
//   Sets a form field to "" if it isBlank()
function setNullIfBlank(obj){if(isBlank(obj.value)){obj.value="";}}

//Basic function added
//return true if the string is valid email string
function isEmailChar(str){
  for (i=0; i<str.length; i++){
    c = str.charAt(i);	  
    if("~!#$%^&*(),\'`:\;?<>=+\n\t \\\"".indexOf(c,0) > 0)
      return false;	
  }
  return true;
}

//return true if the parts of email are valid
function isValidEmail(email){

var array = email.split("@");
  if(array.length != 2) return false; 
  var first, last;
  first = array[0]; last = array[1];
  if( first.charAt(0)=='.') return false;  
  if( first.charAt(first.length)=='.') return false;
  if( last.charAt(0)=='.') return false;
  if(first == "" || last == "") return false;
  first = trimLeftAll(first);
  last = trimRightAll(last);
  if(!isEmailChar(first) || !isEmailChar(last)) 
    return false;
  return true;
}

// return true if number is in range of number
function isInRangeOfNumber(number, from, to){
  if(from<=number&&number<=to)
    return true;
  return false;
}

// round by type
// type="L": lowerRound. Ex: roundEx(1.255, 2, "L") = 1.25
// type="U": upperRound. Ex: roundEx(1.254, 2, "U") = 1.26
// type=others:  Ex:roundEx(1.255, 2, "") = 1.26

var DEFAULT_ROUND_TYPE = "L";
function roundEx(val, digit, type) {
  var result = val;    
  var childs = String(result).split('.');  
  if(childs.length == 1 || (childs.length == 2 && childs[1].length <= digit)) 
		return result;	
  if (type == "L")
    result = lowerRound(val, digit);     
  else if (type == "U")
    result = upperRound(val, digit); 
  else if(type == "UU") 
		result = upperRoundFloat(val, digit); 
  else
    result = round(val, digit); 
  return result;
}

// return round number. Ex: lowerRound(1.255, 2) = 1.26
function round(number, digits) { 
  var num = Math.pow(10, digits);
  var result = Math.round(number*num);
  result = result / num;
  return result; 
}

// return round number. Ex: upperRound(1.254, 2) = 1.26
function upperRound(number, digits) {
  var num = Math.pow(10, digits);
  num = num;  
  var result = Math.round(number * num);    
  result = result / num;  
  return result;
}
function upperRoundFloat(number, digits) {
  var num = Math.pow(10, digits);
  var result = number * num;    
  temp = makeRoundUp(result / num);
  result = temp; 
  return result;
}
function makeRoundUp(val)
{
	if(val != "")
	{
		var index;
		index = String(val).indexOf('.');
		if(index >= 0)
		{			
			var length = String(val).length - index -1;
			if(length >=3)
			{
				if(String(val).substring(index + 3,index + 4) != '0')				
					val = val + 0.01;				
				val = Number(String(val).substring(0,index + 3));				
			}	
		}
	}	
	return val;	
}
// return round number. Ex: lowerRound(1.256, 2) = 1.25
function lowerRound(number, digits) {	
  var num = Math.pow(10, digits);  
  var result = parseInt(number * num);
  result = result / num;
  return result;
}
//return true if the ENTER key is pressed
function isEnterPressed(evt) {
  evt = (evt) ? evt : event;
  var charCode  = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
  if (charCode == 13 || charCode == 3)
    return true;
  return false;
}
function txt_copy(txtFrom, txtTo){ 
  txtTo.value = txtFrom.value;
}

function OpenFile(filename)
{	
	var ex = filename.substring(filename.length - 3, filename.length);
	if(ex == "xls")
	{		
		OpenExcelFile(filename);								
	}
	else if(ex == "doc")
	{
		OpenWordFile(filename);
	}
	else if(ex == "ppt")
	{
		OpenPowerPointFile(filename);
	}
	else if(ex == "pdf" || ex == "txt" || ex == "bmp" || ex == "jpg" || ex == "gif" || ex == "png" || ex == "xml"|| ex == "sql" || ex == "log")
	{
		//filename = replaceAll(filename, "/","\\");
		//filename = filename.replace("http:", "");
		if(ex == "pdf")
		{
			OpenPdfFile(filename);
		}
		if( ex == "bmp" || ex == "jpg" || ex == "gif" || ex == "png" )
		{
			OpenImageFile(filename);
		}
		if(ex == "txt" || ex == "xml"|| ex == "sql" || ex == "log")
		{
			OpenTxtFile(filename);
		}
	}
}
//
// Add Functions in here
//
function replaceString(str, sub, alter){
	if(str.indexOf(sub) == -1)
		return str;
	
	var beginOfSub = str.indexOf(sub);
	var beginStr = str.substring(0, beginOfSub);	
	var endOfSub = beginOfSub + sub.length;
	var endStr = str.substring(endOfSub, str.length);
	
	return beginStr + alter + endStr;
}
// The Javascript escape and unescape functions do not correspond
function URLEncode( string ){
	return escape(_URLEncode(string));  
}
function _URLEncode( string ){
         string = string.replace(/\r\n/g,"\n");  
         var utftext = "";  
   
         for (var n = 0; n < string.length; n++) {  
   
             var c = string.charCodeAt(n);  
   
             if (c < 128) {  
                 utftext += String.fromCharCode(c);  
             }  
             else if((c > 127) && (c < 2048)) {  
                 utftext += String.fromCharCode((c >> 6) | 192);  
                 utftext += String.fromCharCode((c & 63) | 128);  
             }  
             else {  
                 utftext += String.fromCharCode((c >> 12) | 224);  
                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);  
                 utftext += String.fromCharCode((c & 63) | 128);  
             }  
   
         }  
   
         return utftext;  

};

function URLDecode( string ){
	return _URLDecode(unescape(string));
}

function _URLDecode( utftext ){
	 var string = "";  
	 var i = 0;  
	 var c = c1 = c2 = 0;  

	 while ( i < utftext.length ) {  

		 c = utftext.charCodeAt(i);  

		 if (c < 128) {  
			 string += String.fromCharCode(c);  
			 i++;  
		 }  
		 else if((c > 191) && (c < 224)) {  
			 c2 = utftext.charCodeAt(i+1);  
			 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));  
			 i += 2; 
		 }  
		 else {  
			 c2 = utftext.charCodeAt(i+1);  
			 c3 = utftext.charCodeAt(i+2);  
			 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));  
			 i += 3;  
		 }  

	 }  

	 return string;  
}
// JScript File
	function addFlash(div_name,swf, width, height)
	{		
		var movieID = "main";
		var fo = new SWFObject(swf, movieID, width, height, "8", "#FFFFFF", true);         
		fo.addParam("scale", "noscale");
		fo.addParam("salign", "tl");
		fo.addParam("quality", "high" );
		fo.addParam("wmode", "transparent" );
		fo.write(div_name);
	}
	

