/*
 * utility functions
 */


/*
 * String Utility Functions
 */

// trim
function trim(input) {
	if (typeof input != "string") return input;
	
	var retValue = input;

	var ch = retValue.substring(0, 1);
	while (ch == " ") {
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	
	ch = retValue.substring(retValue.length - 1, retValue.length);
	while (ch == " ") {
		retValue = retValue.substring(0, retValue.length - 1);
		ch = retValue.substring(retValue.length - 1, retValue.length);
	}
	
	return retValue;
}

// replace string
function replace(str, source, copy) {
	while (str.indexOf(source) != -1) {
		str = str.substring(0, str.indexOf(source)) + copy + str.substring(str.indexOf(source) + source.length);
	}
	return str;
}


// windowsopen
function openWindow(loc, name, opt) {
	var win = window.open(loc, name, opt);
	if (win == null) {
		alert("ÇÑºû¿ÂÀ» ¿øÈ°ÇÏ°Ô ÀÌ¿ëÇÏ½Ã·Á¸é ¸ÕÀú ÆË¾÷Â÷´Ü¼³Á¤À» ²¨ÁÖ¼¼¿ä");
	}
	return win;
}

/*
 * cookie functions
 */

// get cookie expire day
function getexpirydate(mins){
  var Today = new Date();
  var nomilli = Date.parse(Today);
  Today.setTime(nomilli + mins * 60 * 1000);
  return Today.toUTCString();
}

// get cookie value
function getcookie(cookiename) {
  var cookiestring = "" + document.cookie;

  // cookie first
  var index1 = cookiestring.indexOf(cookiename);
  if (index1 == -1 || cookiename == "") {
    return "";
  }

  // cookie end
  var index2 = cookiestring.indexOf(';',index1);
  if (index2 == -1) {
    index2 = cookiestring.length; 
  }

  return unescape(cookiestring.substring(index1+cookiename.length+1, index2));
}

// set cookie
function setcookie(name, value, mins){
  // make cookie string
  cookiestring = name + "=" + escape(value) + ";expires=" + getexpirydate(mins) + ";path=/;";

  // write!
  document.cookie = cookiestring;
  if (!getcookie(name)){
    return false;
  } else {
    return true;
  }
}

/*
 * Multi Name-Value Cookie Container
 *
 * usage
 *
 * var cookie = new MCookie();
 * for (i = 0; i < 10; i++) {
 *   AppendMCookie(cookie, "name_" + i, "value_" + i);
 * }
 * SetMCookie(cookie, "sample_cookie", 10); // 10 minutes
 *
 *
 * var cookie = GetMCookie("sample_cookie");
 */

// multi name-value cookie container
function MCookie() {
  this.names = new Array();
  this.values = new Array();
  this.count = 0;
  
  return this;
}

// append multi name-value cookie
function AppendMCookie(cookie, name, value) {
  var index = cookie.count;
  // if exists cookie, update that
  for (i = 0; i < cookie.count; i++) {
    if (cookie.names[i] == name) {
      index = i;
      break;
    }
  }

  cookie.names[index] = name;
  cookie.values[index] = value;
  cookie.count++;
}

// delete one cookie in multi name-value cookie
function DeleteMCookie(cookie, index) {
  for (i = index + 1; i < cookie.count; i++) {
    cookie.names[i - 1] = cookie.names[i];
    cookie.values[i - 1] = cookie.values[i];
  }

  cookie.count--;
}

// multi name-value cookie seperator
var s1 = "||";
var s2 = "|||";

// set multi name-value cookie
function SetMCookie(cookie, name, mins) {
  var value = "";
  
  for (i = 0; i < cookie.count; i++) {
    value += escape(cookie.names[i]) + s1 + escape(cookie.values[i]);
    if (i + 1 < cookie.count) {
      value += s2;
    }
  }

  setcookie(name, value, mins);
}

// get multi name-value cookie
function GetMCookie(name) {
  var str = getcookie(name);
  var cookie = new MCookie();
  
  if (str != "") {
    var array = str.split(s2);
    
    for (i = 0; i < array.length; i++) {
      tmp = array[i].split(s1);
      AppendMCookie(cookie, tmp[0], tmp[1]);
    }
    
    cookie.count = array.length;

    return cookie;
  } else {
    return new MCookie();
  }
}

/*
 * Url Utility Functions
 *
 */

// add get variable
function AddGetVariable(url, name, value) {
	url = url + "";
	
	var index0 = url.indexOf("?" + name + "=");
	
	if (index0 == -1) {
	  var index1 = url.indexOf("&" + name + "=");
  	
	  if (index1 == -1) {
	    url = url + "&" + name + "=" + value;
	  } else {
      var index2 = url.substr(index1 + 1).indexOf("&");
      if (index2 == -1) {
        url = url.substr(0, index1) + "&" + name + "=" + value;
      } else {
        url = url.substr(0, index1) + url.substr(index1 + index2 + 1, url.length) + "&" + name + "=" + value;
      }
	  }
  } else {
    var index3 = url.substr(index0 + 1).indexOf("&");
    if (index3 == -1) {
      url = url.substr(0, index0) + "?" + name + "=" + value;
    } else {
      url = url.substr(0, index0) + "?" + name + "=" + value + url.substr(index0 + index3 + 1, url.length);
    }
  }	

	return url;
}

// Get GET Variable
function GetGetVariable(url, name) {
  url = url + "";
  
  var index0 = url.indexOf("?" + name + "=");
  
  if (index0 == -1) {
    var index1 = url.indexOf("&" + name + "=");
    
    if (index1 == -1) {
      return "";
    } else {
      var index2 = url.substr(index1 + name.length + 2).indexOf("&");
      if (index2 == -1) {
        return url.substr(index1 + name.length + 2);
      } else {
        return url.substr(index1 + name.length + 2, index2);
      }
    }
  } else {
    var index3 = url.substr(index0 + name.length + 2).indexOf("&");
    if (index3 == -1) {
      return url.substr(index0 + name.length + 2);
    } else {
      return url.substr(index0 + name.length + 2, index3);
    }
  }
}

// url encode - ASCII only
function URLEncode(plaintext) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "%20";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				// encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

// url decode
function URLDecode(encoded) {
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
}

// window action 
function ActionWindow(flag,UserID,Token) {
  flag = flag + "";
  if (flag.length == 4) {
    actionWindow(opener, flag.substr(0, 1),UserID,Token);
    actionWindow(top, flag.substr(1, 1),UserID,Token);
    actionWindow(parent, flag.substr(2, 1),UserID,Token);
    actionWindow(self, flag.substr(3, 1),UserID,Token);
  }
}

function actionWindow(o, action,UserID,Token) {
  if (o != null) {
    switch (action) {
      //case "1": o.location.href = "/loginlogic/login.asp?u="+UserID+"&t="+Token+"&r="+o.location.href; break;
      case "1": goActiveLogin(UserID,Token); break;
      case "2": o.close(); break;
    }
  }
}

function goActiveLogin(UserID,Token)
{
	var shtm = "<form name=uLogin action='/loginlogic/login.asp' method=post>";
		shtm+= "<input type=hidden name=u value='"+UserID+"'>";
		shtm+= "<input type=hidden name=t value='"+Token+"'>";
		shtm+= "<input type=hidden name=r value='"+window.location+"'>";				
		shtm+= "</form>";

	document.write(shtm);
	
	if(uLogin != null)
		uLogin.submit();
	else
		alert('script error');
}	
