/*****************************************************************************
Copyright (C) 2008  Uður HAMZADAYI
*/
function displayResults(str, src) {
	// convert the string of characters into various formats and send to UI
	// str: string, the string of characters
	// src: string, the id of the UI location that originated the request
	var preserve = 'none';
	var pad = true;
	var showinvisibles;
	var bidimarkup;
	var cstyle = false;
	if (src != 'chars') { chars.value = str; }
	if (src != 'codePoints') { 
		preserve = 'none';
		if (document.getElementById('hexcplatin1').checked) { preserve = 'latin1'; }
		else if (document.getElementById('hexcpascii').checked) { preserve = 'ascii'; } 
		codePoints.value = convertCharStr2CP(str, preserve, true, 'hex'); 
		}
	if (src != 'decCodePoints') { 
		preserve = 'none';
		if (document.getElementById('deccplatin1').checked) { preserve = 'latin1'; }
		else if (document.getElementById('deccpascii').checked) { preserve = 'ascii'; } 
		decCodePoints.value = convertCharStr2CP(str, preserve, true, 'dec'); 
		}
	if (src != 'XML') { 
		if (document.getElementById('xmlinvisibles').checked) { showinvisibles = true; } else { showinvisibles = false; }
		if (document.getElementById('xmlbidimarkup').checked) { bidimarkup = true; } else { bidimarkup = false; }
		XML.value = convertCharStr2XML(str, showinvisibles, bidimarkup); 
		}
	if (src != 'UTF8') { 
		UTF8.value = convertCharStr2UTF8( str );
		}
	if (src != 'UTF16') { 
		UTF16.value = convertCharStr2UTF16( str );
		}
	if (src != 'hexNCRs') { 
		preserve = 'none';
		if (document.getElementById('hexncrlatin1').checked) { preserve = 'latin1'; }
		else if (document.getElementById('hexncrascii').checked) { preserve = 'ascii'; } 
		hexNCRs.value = convertCharStr2SelectiveCPs( str, preserve, true, '&#x', ';', 'hex' );
		}
	if (src != 'decNCRs') { 
		preserve = 'none';
		if (document.getElementById('decncrlatin1').checked) { preserve = 'latin1'; }
		else if (document.getElementById('decncrascii').checked) { preserve = 'ascii'; } 
		decNCRs.value = convertCharStr2SelectiveCPs( str, preserve, false, '&#', ';', 'dec' );
		}
	if (src != 'pEsc') { 
		pEsc.value = convertCharStr2pEsc( str );
		}
	if (src != 'jEsc') { 
		if (document.getElementById('cstyleSC').checked) { cstyle = true; }
		else { cstyle = false; }
		jEsc.value = convertCharStr2jEsc( str, cstyle );
		}
	if (src != 'Unicode') { 
		preserve = 'none';
		if (document.getElementById('unicodelatin1').checked) { preserve = 'latin1'; }
		else if (document.getElementById('unicodeascii').checked) { preserve = 'ascii'; } 
		Unicode.value = convertCharStr2CP(str, preserve, true, 'unicode');
		//Unicode.value = convertCharStr2Unicode( str, 'none', pad );
		//Unicode.value = convertCP2Unicode( convertCharStr2CP( str, preserve, pad ) ); 
		}
	if (src != 'zeroX') { 
		preserve = 'none';
		if (document.getElementById('zeroXlatin1').checked) { preserve = 'latin1'; }
		else if (document.getElementById('zeroXascii').checked) { preserve = 'ascii'; } 
		zeroX.value = convertCharStr2CP(str, preserve, false, 'zerox');
		}
	if (src != 'CSS') { 
		CSS.value = convertCharStr2CSS( str );
		}
	}


function hex2char ( hex ) {
	// converts a single hex number to a character
	// note that no checking is performed to ensure that this is just a hex number, eg. no spaces etc
	// hex: string, the hex codepoint to be converted
	var result = '';
	var n = parseInt(hex, 16);
    if (n <= 0xFFFF) { result += String.fromCharCode(n); } 
	else if (n <= 0x10FFFF) {
		n -= 0x10000
		result += String.fromCharCode(0xD800 | (n >> 10)) + String.fromCharCode(0xDC00 | (n & 0x3FF));
    	} 
	else { result += 'hex2Char error: Code point out of range: '+dec2hex(n); }
	return result;
	}

function convertjEsc2Char ( str, shortEscapes ) { 
	// converts a string containing JavaScript or Java escapes to a string of characters
	// str: string, the input
	// shortEscapes: boolean, if true the function will convert \b etc to characters
	
	// convert \U and 6 digit escapes to characters
	str = str.replace(/\\U([A-Fa-f0-9]{8})/g, 
					function(matchstr, parens) {
						return hex2char(parens);
						}
						);
	// convert \u and 6 digit escapes to characters
	str = str.replace(/\\u([A-Fa-f0-9]{4})/g, 
					function(matchstr, parens) {
						return hex2char(parens);
						}
						);
	// convert \b etc to characters, if flag set
	if (shortEscapes) {
		//str = str.replace(/\\0/g, '\0'); 
		str = str.replace(/\\b/g, '\b'); 
		str = str.replace(/\\t/g, '\t'); 
		str = str.replace(/\\n/g, '\n'); 
		str = str.replace(/\\v/g, '\v'); 
		str = str.replace(/\\f/g, '\f'); 
		str = str.replace(/\\r/g, '\r'); 
		str = str.replace(/\\\'/g, '\''); 
		str = str.replace(/\\\"/g, '\"'); 
		str = str.replace(/\\\\/g, '\\'); 
		}
	return str;
	}


function UMessageBox(VALUE , TYPE){
  //alert( VALUE );
  //if( convertjEsc2Char( VALUE, false ) == VALUE )
  //   alert( 11 );
  
  //alert( "\u60a8\u8f93\u5165\u7684\u5b57\u7b26\u5728\u56fe\u50cf\u4e0a\u770b\u5230\u5de6\u8fb9\u3002" );
	
  //VALUE = displayResults(convertjEsc2Char( VALUE, false ) , "" );
										  
	/*
  var newDiv = document.createElement(newDiv);
  newDiv.innerHTML = VALUE;
  if( TYPE == "YESNO" ) {
     return confirm(newDiv.innerHTML);
  }
  return alert(newDiv.innerHTML);
  */
  if( TYPE == "YESNO" ) {
     return confirm(VALUE);
  }
  return alert(VALUE);
  
}

function _FlashActiveObjects() {
  theObjects = document.getElementsByTagName("object");
  for (var i = 0; i < theObjects.length; i++) {
    theObjects[i].outerHTML = theObjects[i].outerHTML;
  }
}


function addslashes(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\0/g,'\\0');
	return str;
}

function GetObjectTopPos(obj) 
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;	
   return curtop;
};



function GetObjectLeftPos(obj) 
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
};


function AttachEventHandler(obj, eventName, eventHandler) {
  if( obj ) {
     if(eventName.substring(0, 2) == 'on') {
 	    eventName = eventName.substring(2,eventName.length);
	 }
     if(obj.attachEvent){
        obj.attachEvent('on'+ eventName, eventHandler);
     } 
     else 
     if( obj.addEventListener){
        obj.addEventListener(eventName, eventHandler, false);  
     }
  }
};


function Edit_ValidateInteger(thi)
{
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }
   
   if( event.keyCode == 45 && thi.value.indexOf('-') < 0  ) {
      thi.value = '-' + thi.value;
	  event.returnValue = false;
   }
   else 
   if( event.keyCode == 43 && thi.value.indexOf('-') >= 0  ) {	   
	  thi.value = thi.value.replace(/-/,"");
	  event.returnValue = false;
   }
   if ((event.keyCode < 48) || (event.keyCode > 57))
      event.returnValue = false;
}

function Edit_ValidateUInteger(thi)
{ 
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }

   if ((event.keyCode < 48) || (event.keyCode > 57))
      event.returnValue = false;
}

function Edit_ValidatePlakaAlpha(thi)
{ 
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }

   if(event.keyCode < 65 || event.keyCode > 90 )
   {
	  if (event.keyCode < 97 || event.keyCode > 122 ) 
         event.returnValue = false;
   }
   
}



function Edit_ValidateTelephoneFormat(thi , iBlur )
{
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }
	
   if( iBlur == 0 )	{ //Blur
      thi.value = thi.value.replace(/ /g,"");
      if( thi.value.length > 3 ) {
        thi.value = thi.value.substring(0 ,3 ) + ' ' + thi.value.substring(3);
      }
      if( thi.value.length > 6 ) {
        thi.value = thi.value.substring(0 ,6 ) + ' ' + thi.value.substring(6);
      }
   }
   else { //Focus
	  thi.value = thi.value.replace(/ /g,""); 
   }
}


function Edit_ValidateTelephone(thi)
{
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }

   if (event.keyCode != 32 && (event.keyCode < 48) || (event.keyCode > 57) )
      event.returnValue = false;
}

function Textarea_ValidateLimit( thi , len ) {
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }
	
   if( thi.value.length >= len ) {
	  if( thi.value.length > len ) {
		 thi.value = thi.value.substring(0 ,len );
	  }
      event.returnValue = false;
   }
}


function Textarea_ValidateLimitNot( thi , len , idnotify ) {
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }
   
   if( thi.value.length >= len ) {
	  if( thi.value.length > len ) {
		 thi.value = thi.value.substring(0 ,len );
	  }
      if(event.keyCode >= 32 )
         event.returnValue = false;
   }
   if( idnotify != "" ) {
	   notctrl = document.getElementById( idnotify );
	   if( notctrl != null )
		  notctrl.innerHTML = len - ( thi.value.length );
   }
}


function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}


function Edit_ValidateFinishFormat_MoneyFocus(thi)
{
   thi.value = thi.value.replace(/\$|\,/g,'');
   thi.select();
}

function Edit_ValidateFinishFormat_Money(thi)
{
   thi.value = formatCurrency(thi.value);
}



function Edit_ValidateReel(thi)
{
   if( thi.readOnly ) {	
      event.returnValue = false;
	  return;
   }
	
   if( event.keyCode == 45 && thi.value.indexOf('-') < 0  ) {
      thi.value = '-' + thi.value;
	  event.returnValue = false;
   }
   else 
   if( event.keyCode == 43 && thi.value.indexOf('-') >= 0  ) {	   
	  thi.value = thi.value.replace(/-/,"");
	  event.returnValue = false;
   }
	
   if (((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 46))
      event.returnValue = false;
	  
   if(event.keyCode == 46 && thi.value.indexOf('.') >=0 )
      event.returnValue = false;
}

function Edit_ValidateDate(thi)
{
  if(((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 46))
     event.returnValue = false;
}


function GetFileExt(VALUE){
  var w_space = '.';
  var v_length = VALUE.length;
  var strTemp = "";
  if(v_length <= 0){
    return"";
  }
  
  var iTemp = v_length - 1;
  while(iTemp > -1){
  
     if(VALUE.charAt(iTemp) == w_space)
        break;
     
     strTemp = VALUE.substring(iTemp ,v_length );
     iTemp = iTemp - 1;
  }

  return strTemp.toLowerCase();
}

function TrimRight(VALUE){

   var w_space = String.fromCharCode(32);
   var v_length = VALUE.length;
   var strTemp = "";
   if(v_length <= 0){
      return"";
   }
   var iTemp = v_length -1;

   while(iTemp > -1){
     if(VALUE.charAt(iTemp) == w_space){
     }
     else{
        strTemp = VALUE.substring(0,iTemp + 1 );
        break;
     }
     iTemp = iTemp - 1;

   } //End While
   
   return strTemp;

} //End Function


function TrimLeft(VALUE){

   var w_space = String.fromCharCode(32);
   var v_length = VALUE.length;
   var strTemp = "";
   if(v_length <= 0){
     return"";
   }
   var iTemp = 0;

   while(iTemp < v_length){
     if(VALUE.charAt(iTemp) == w_space){
     }
     else{
        strTemp = VALUE.substring(iTemp , v_length );
        break;
     }
     iTemp = iTemp + 1 ;

   } //End While
   
   return strTemp;

} //End Function

function CheckMailEnglishSting(VALUE){

   var v_length = VALUE.length;
   var iTemp = 0;
   var OK = 0;  
   while(iTemp < v_length) {
	  OK = 0;  

	  if( VALUE.charCodeAt( iTemp ) >= 65 && VALUE.charCodeAt( iTemp ) <= 90 )
	     OK = 1;  
	  else 	 
	  if( VALUE.charCodeAt( iTemp ) >= 97 && VALUE.charCodeAt( iTemp ) <= 122 )
	     OK = 1;  
	  else	 
	  if( VALUE.charCodeAt( iTemp ) >= 48 && VALUE.charCodeAt( iTemp ) <= 57 )
	     OK = 1;  
      else
	  if( VALUE.charCodeAt( iTemp ) == 46 )
	     OK = 1;  
	  else
	  if( VALUE.charCodeAt( iTemp ) == 95 )
	     OK = 1;  

      if( OK == 0 )
	     return false;
  
      iTemp = iTemp + 1;
   }
   return true;  	
}


function CheckMail(VALUE){
   
   var w_sep = '@';

   var iAdim = 0;
   var v_length = VALUE.length;
   
   if( v_length <= 0 ) {
      return  false;
   }
   
   var iTemp = 0;
   var iDolu = 0;
   while(iTemp < v_length){
     
     if( VALUE.charAt(iTemp) == '@' && iAdim > 0 )
        return false;
        
     if( VALUE.charAt(iTemp) == w_sep){
        if( iAdim == 0 ) {
           if( iDolu <= 0 )
              return false; 
              
           w_sep = '.';
           iDolu = 0;   
           iAdim = 1;
           iTemp = iTemp + 1;
           continue; 
        }
        else
        if( iAdim == 1 ) {
           if( iDolu <= 0 )
              return false; 
           iDolu = 0;   
           iAdim = 2;
           iTemp = iTemp + 1;
           continue; 
           
        }
        else
        if( iAdim == 2 ) {
           if( iDolu <= 0 )
              return false; 
           iDolu = 0;   
           iAdim = 2;
           iTemp = iTemp + 1;
           continue; 
           
        }
        
     }
     else
     if(VALUE.charAt(iTemp) == ' ')
        return false;
     

     iDolu = iDolu + 1;    

     
     iTemp = iTemp + 1;
   }
   
   if( iAdim == 2 && iDolu > 0 )
      return true;
      
      
   return false;
}

function CheckColorValue( VALUE ) {
   if (VALUE.search(/^#([\da-fA-F]{3}$)|([\da-fA-F ]{6}$)/) == -1)
     return false; 
   VALUE = VALUE.replace( "#","" );	 
   if( VALUE.length != 6 )
      return false;
   return true;
}



function CheckWebURL( VALUE ) {
   var v = new RegExp(); 
   v.compile("(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&amp;%_\./-~-]*)?");
   return v.test(VALUE);
}

function CheckDate( VALUE ){
   var w_seperator = String.fromCharCode(46);
   var v_length = VALUE.length;
   if(v_length <= 0){
     return false;
   }
   
   var strGun = "";
   var strAy = "";
   var strYil = "";
   
   var iTemp = 0 ;
   var iPart = 0 ;
   var iTotaSeperator = 0 ;
   while( iTemp < v_length ) {
        
     if( VALUE.charAt(iTemp) == w_seperator ) {
        iTotaSeperator++;
        iPart++;
        iTemp++;
        continue;    
     }   
        
     if( iPart == 0 ) {
        strGun += VALUE.charAt(iTemp);
     }

     
     if( iPart == 1 ) {
        strAy += VALUE.charAt(iTemp);
     }
     
     if( iPart == 2 ) {
        strYil += VALUE.charAt(iTemp);
     }
     iTemp++;
   }
   
   

   if( strGun.length < 1 || strGun.length > 2 )
      return false;
      
   if( strAy.length < 1 || strAy.length > 2 )
      return false;
      
   if( strYil.length != 4 )
      return false;

   if( iTotaSeperator != 2 )
      return false;


   iTemp= 0;
   v_length = strGun.length;
   while( iTemp < v_length ) {
      if( strGun.charAt(iTemp) < '0' || strGun.charAt(iTemp) > '9' )  
         return false;    
      iTemp++;   
   }

   iTemp= 0;
   v_length = strAy.length;
   while( iTemp < v_length ) {
      if( strAy.charAt(iTemp) < '0' || strAy.charAt(iTemp) > '9' )  
         return false;    
      iTemp++;      
   }

   iTemp= 0;
   v_length = strYil.length;
   while( iTemp < v_length ) {
      if( strYil.charAt(iTemp) < '0' || strYil.charAt(iTemp) > '9' )  
         return false;    
      iTemp++;      
   }

   
   var iGun = parseInt( strGun , 10 );
   var iAy  = parseInt( strAy , 10 ); 
   var iYil = parseInt( strYil , 10 );
   
   if( iYil < 1900 || iYil > 2100 )
      return false;   
 
   if( iAy < 1 || iAy > 12 )
      return false;   

   var iMaxGun = 31;
   switch( iAy ) {
      case 2:
        iMaxGun = 28;      
        if( (iYil % 4 ) == 0 )
           iMaxGun = 29;
           
      break;

      case 4:  
      case 6:
      case 9:
      case 11:
        iMaxGun = 30
      break;
   
   }
      
   if( iGun < 1 || iGun > iMaxGun )
      return false;   


   return true;


} //End Function

function SetFocus( idCtrl ) {
  document.getElementById(idCtrl).focus();
}

function Verify_TextField( idCtrl, msg ) {
   var str = document.getElementById(idCtrl).value;
   str = TrimRight( str );
   if( str.length <= 0 ) {
      UMessageBox( msg );      
      return false;
   }
   return true;
}

function Verify_IntField( idCtrl, msg ) {
   var str = document.getElementById(idCtrl).value;
   str = TrimRight( str );
   if( str.length <= 0 || str == "" ) {
      UMessageBox( msg );      
      return false;
   }
   else {
	  if( parseInt( str,10 ) <= 0 ) {
         UMessageBox( msg );      
         return false;
	  }
   }
   return true;
}


function Verify_PasswordField( idCtrl, msg ) {
   str = document.getElementById(idCtrl).value;
   str = TrimRight( str );
   if( str.length < 5 ) {
      UMessageBox( msg );      
      return false;
   }
   return true;
}

function Verify_CheckField( idCtrl, msg ) {
   if( document.getElementById(idCtrl).checked ) {
      return true;
   }
   else {
      UMessageBox( msg );      
      return false;   
   }
   
   return true;
}


function GetValue( idCtrl ) {
   return document.getElementById(idCtrl).value;
}


function Verify_MailField( idCtrl , msg ) {
   str = document.getElementById(idCtrl).value;
   str = TrimRight( str );
   if( str.length <= 0 ) {
      UMessageBox( msg );      
      return false;
   }
   else
   if( !CheckMail( str ) ) {
      UMessageBox( msg );      
      return false;
   }
        
   return true;
}

function Verify_WebField( idCtrl , msg ) {
   str = document.getElementById(idCtrl).value;
   str = TrimRight( str );
   if( str.length <= 0 ) {
      UMessageBox( msg );      
      return false;
   }
   else
   if( !CheckWebURL( str ) ) {
      UMessageBox( msg );      
      return false;
   }
   return true;
}


function Verify_DateField( idCtrl , msg ) {
   str = document.getElementById(idCtrl).value;
   str = TrimRight( str );
   if( str.length <= 0 ) {
      UMessageBox( msg );      
      return false;
   }
   else
   if( !CheckDate( str ) ) {
      UMessageBox( msg );      
      return false;
   }
   return true;
}



