/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */

var ie=document.all?1:0; 
var ns=document.layers?1:0; 
var dom=document.getElementById?1:0; 


function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}


function ReplaceStr(strOriginal,strFind,strChange)
{
	return strOriginal.split(strFind).join(strChange);
}



function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";

	if (str.length == 0) 
		resultStr = "";
	else {	
  		// Loop through string starting at the beginning as long as there
  		// are spaces.
//	  	len = str.length - 1;
		len = str.length;
		
  		while ((i <= len) && (str.charAt(i) == " "))
			i++;

   	// When the loop is done, we're sitting at the first non-space char,
 		// so return that char plus the remaining chars of the string.
  		resultStr = str.substring(i, len);
  	}

  	return resultStr;
} // end TrimLeft

function TrimRight( str ) {
	var resultStr = "";
	var i = 0;

	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)	
		return null;

	// Make sure the argument is a string
	str += "";
	
	if (str.length == 0) 
		resultStr = "";
	else {
  		// Loop through string starting at the end as long as there
  		// are spaces.
  		i = str.length - 1;
  		while ((i >= 0) && (str.charAt(i) == " "))
 			i--;
 			
 		// When the loop is done, we're sitting at the last non-space char,
 		// so return that char plus all previous chars of the string.
  		resultStr = str.substring(0, i + 1);
  	}
  	
  	return resultStr;  	
} // end TrimRight



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 encodeURL(str){
 var s0, i, s, u;
 s0 = "";                // encoded str
 for (i = 0; i < str.length; i++){   // scan the source
	 s = str.charAt(i);
	 u = str.charCodeAt(i);          // get unicode of the char
	 if (s == " "){s0 += "+";}       // SP should be converted to "+"
	 else {
		 if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){       // check for escape
			 s0 = s0 + s;            // don't escape
		 }
		 else {                  // escape
			 if ((u >= 0x0) && (u <= 0x7f)){     // single byte format
				 s = "0"+u.toString(16);
				 s0 += "%"+ s.substr(s.length-2);
			 }
			 else if (u > 0x1fffff){     // quaternary byte format (extended)
				 s0 += "%" + (oxf0 + ((u & 0x1c0000) >> 18)).toString(16);
				 s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
				 s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
				 s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
			 }
			 else if (u > 0x7ff){        // triple byte format
				 s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
				 s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
				 s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
			 }
			 else {                      // double byte format
				 s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
				 s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
			 }
		 }
	 }
 }
 return s0;
}
 

 function decodeURL(str){
     var s0, i, j, s, ss, u, n, f;
     s0 = "";                // decoded str
     for (i = 0; i < str.length; i++){   // scan the source str
         s = str.charAt(i);
         if (s == "+"){s0 += " ";}       // "+" should be changed to SP
         else {
             if (s != "%"){s0 += s;}     // add an unescaped char
             else{               // escape sequence decoding
                 u = 0;          // unicode of the character
                 f = 1;          // escape flag, zero means end of this sequence
                 while (true) {
                     ss = "";        // local str to parse as int
                         for (j = 0; j < 2; j++ ) {  // get two maximum hex characters for parse
                             sss = str.charAt(++i);
                             if (((sss >= "0") && (sss <= "9")) || ((sss >= "a") && (sss <= "f"))  || ((sss >= "A") && (sss <= "F"))) {
                                 ss += sss;      // if hex, add the hex character
                             } else {--i; break;}    // not a hex char., exit the loop
                         }
                     n = parseInt(ss, 16);           // parse the hex str as byte
                     if (n <= 0x7f){u = n; f = 1;}   // single byte format
                     if ((n >= 0xc0) && (n <= 0xdf)){u = n & 0x1f; f = 2;}   // double byte format
                     if ((n >= 0xe0) && (n <= 0xef)){u = n & 0x0f; f = 3;}   // triple byte format
                     if ((n >= 0xf0) && (n <= 0xf7)){u = n & 0x07; f = 4;}   // quaternary byte format (extended)
                     if ((n >= 0x80) && (n <= 0xbf)){u = (u << 6) + (n & 0x3f); --f;}         // not a first, shift and add 6 lower bits
                     if (f <= 1){break;}         // end of the utf byte sequence
                     if (str.charAt(i + 1) == "%"){ i++ ;}                   // test for the next shift byte
                     else {break;}                   // abnormal, format error
                 }
             s0 += String.fromCharCode(u);           // add the escaped character
             }
         }
     }
     return s0;
 }


 
 function setCookie( cookieName, cookieValue, expireDate )
{
 var today = new Date();
 today.setDate( today.getDate() + parseInt( expireDate ) );
 document.cookie = cookieName + "=" + escape( cookieValue ) + "; path=/; expires=" + today.toGMTString() + ";"
}

function getCookie( cookieName ){
  var search = cookieName + "=";
  var cookie = document.cookie;
 
 if( cookie.length > 0 ){
  startIndex = cookie.indexOf( cookieName );
  if( startIndex != -1 ){
      startIndex += cookieName.length;

      endIndex = cookie.indexOf( ";", startIndex );

      if( endIndex == -1) endIndex = cookie.length;

      return unescape( cookie.substring( startIndex + 1, endIndex ) );
    } else{
        return false;
  }
 } else {  
  return false;
 }
}

function deleteCookie( cookieName )
{
 var expireDate = new Date();

 expireDate.setDate( expireDate.getDate() - 1 );
 document.cookie = cookieName + "= ; expires=" + expireDate.toGMTString() + "; path=/";
}

function checkByte(str){
	var passValue = true;
	for(var i=0; i< str.length; i++){
		if (str.charCodeAt(i) > 127){
				passValue = false;
				break;
		}
	}

	return passValue;
}


function OnlyNumber(e)
{
	if(ie){
		if(event.keyCode >=48 && event.keyCode <=57) //0~9
			return true;
		else if(event.keyCode == 8 || event.keyCode == 45 || event.keyCode == 40 || event.keyCode == 41 || event.keyCode == 32)	 //backspace,-,(,),sp
			return true;
		else
			return false;
	}else{
		if(e.which >=48 && e.which <=57) //0~9
			return true;
		else if(e.which == 8 || e.which == 45 || e.which == 40 || e.which == 41 || e.which == 32)	 //backspace,-,(,),sp
			return true;
		else
			return false;
	}
}




function OnlyString(e)
{
	if(ie){
		if(event.keyCode >=48 && event.keyCode <=57) //0~9
			return false;
		else if(event.keyCode == 59 || event.keyCode == 47) // ;,/
			return false;
		else if(event.keyCode == 8 || event.keyCode == 45 || event.keyCode == 40 || event.keyCode == 41 || event.keyCode == 32)	 //backspace,-,(,),sp
			return true;
		else
			return true;
	}else{
		if(e.which >=48 && e.which <=57) //0~9
			return false;
		else if(e.which == 59 || e.which == 47) // ;,/
			return false;
		else if(e.which == 8 || e.which == 45 || e.which == 40 || e.which == 41 || e.which == 32)	 //backspace,-,(,),sp
			return true;
		else
			return true;
	}
}
function OnlyAlphabet(e)
{
	if(ie){
		if(event.keyCode >=65 && event.keyCode <=90) //A~Z
			return true;
		else if(event.keyCode >=97 && event.keyCode <=122) //a~z
			return true;
		else if(event.keyCode ==32 || event.keyCode == 8 || event.keyCode == 46) //space,backspace,del
			return true;
		else
			return false;
	}else{
		if(e.which >=65 && e.which <=90) //A~Z
			return true;
		else if(e.which >= 97 && e.which <= 122) //a~z
			return true;
		else if(e.which == 32 || event.which == 8 || event.which == 46)	 //space,backspace,del
			return true;
		else
			return false;
	}
}

function xss(value) {

	var str;

	str = value;


	str.replace(/[\"\'][\s]*javascript:(.*)[\"\']/g, "\"\"");
	str = str.replace(/script(.*)/g, "");    
	str = str.replace(/eval\((.*)\)/g, "");
	
	return str;
} 

function OnlyStringNumber(e)
{
	if(ie){
		if(event.keyCode >=48 && event.keyCode <=57) //0~9
			return true;
		else if(event.keyCode == 8 || event.keyCode == 45 || event.keyCode == 40 || event.keyCode == 41 || event.keyCode == 32 ||  event.keyCode == 13)	 //backspace,-,(,),sp,enter
			return true;
		else if(event.keyCode >=65 && event.keyCode <=90 || event.keyCode >=97 && event.keyCode <=122)	 //a~z,A~Z
			return true;
		else
			return false;
	}else{
		if(e.which >=48 && e.which <=57) //0~9
			return true;
		else if(e.which == 8 || e.which == 45 || e.which == 40 || e.which == 41 || e.which == 32 ||  e.which == 13)	 //backspace,-,(,),sp,enter
			return true;
		else if(e.which >=65 && e.which <=90 || e.which >=97 && e.which <=122)	 //a~z,A~Z
			return true;
		else
			return false;
	}
}

String.prototype.trim = function() {
    return this.replace(/(^ *)|( *$)/g, "");
}