﻿/*
 * Browser type
 */
 
//document.domain = "hanbiton.com";

var isOpera = navigator.userAgent.indexOf("Opera") > -1; 
var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera; 
var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera; 

// ajax
// XMLHTTP object
var objHttp;

/*
	Title 및 상태바 링크 숨기기
*/
document.title = "紅香蕉, 通往歡樂的唯一入口";

if (document.layers) {
	document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
}

document.onmouseover = hidestatus;
document.onmouseout = hidestatus;

function hidestatus(){
	window.status = '紅香蕉, 通往歡樂的唯一入口';
	return true;
}
/*
 * document event
 */
/*
 * String object prototype functions
 */
// length
String.prototype.getAsciiLength = function() {
	var len = 0;
	for (i = 0; i < this.length; i++) {
		if (this.charCodeAt(i) < 255) {
			len++;
		} else {
			len+=2;
		}
	}
	return len;
}

// trim
String.prototype.trim = function() {
	var retValue = this;

	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;
}

/*
 * 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;
}




/*
 * utility functions
 */
//아스키코드값 173 입력 불가 처리 
function CheckNullChar(input)
{
  for (i=0; i<input.length; i++)
  {
    if(input.charCodeAt(i) == 173)
      return false;
  }
  return true;
} 
function CheckSpecialChar(input)
{
  var ret = false;
  ret = CheckNullChar(input);
  if(ret)
  {
    for (i=0; i<input.length; i++)
    {
      if(input.charCodeAt(i) == 8 || input.charCodeAt(i) == 92)
      {
        ret = false;
        break;
      }
    }
    ret = true;
  }
  return ret;
}

// 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;
}

// IsNumeric
function IsNumeric(str) {
	var r = new RegExp("^[0-9]*$");
	if (document.all) {
		return r.exec(str) != null;
	} else {
		return r(str) != null;
	}
}

// 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;
}

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;
}

// 이미지 미리 로딩
function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


// 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) + ";domain=redbana.tw;path=/;";

	// write!
	document.cookie = cookiestring;
	if (!getcookie(name)){
		return false;
	} else {
		return true;
	}
}

/*
 * 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();
	}
}

// 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) {
			if (url.indexOf("?") == -1) {
				url = url + "?" + name + "=" + value;
			} else {
				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;
}

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);
		}
	}
}

function DeleteGetVariable(url, name) {
	url = url + "";

	var index0 = url.indexOf("?" + name + "=");

	if (index0 == -1) {
		var index1 = url.indexOf("&" + name + "=");

		if (index1 != -1) {
			var index2 = url.substr(index1 + 1).indexOf("&");
			if (index2 == -1) {
				url = url.substr(0, index1);
			} else {
				url = url.substr(0, index1) + url.substr(index1 + index2 + 1, url.length);
			}
		}
	} else {
		var index3 = url.substr(index0 + 1).indexOf("&");
		if (index3 == -1) {
			url = url.substr(0, index0);
		} else {
			url = url.substr(0, index0) + "?" + url.substr(index0 + index3 + 2, url.length);
		}
	}	

	return url;
}

// is xp sp2?
function IsNaviXp2() {
	var strMatch = window.navigator.appVersion;

	if( strMatch.match("SV1") == "SV1" && strMatch.match("Windows NT 5.1") == "Windows NT 5.1" )
		return true;
	else
		return false;
}

// form submit with disabled viewstate
function FormSubmit(form, action) {
	for (i = 0; (e = document.getElementsByTagName("input")[i]); i++) {
		if (e.getAttribute("type").indexOf("hidden") != -1) {
			if (e.getAttribute("name") == "__VIEWSTATE") {
				e.disabled = true;
				break;
			}
		}
	}

	form.action = action;
	form.submit();
}


function ActionWindow(flag) {
	flag = flag + "";
	if (flag.length == 4) {
		actionWindow(opener, flag.substr(0, 1));
		actionWindow(top, flag.substr(1, 1));
		actionWindow(parent, flag.substr(2, 1));
		actionWindow(self, flag.substr(3, 1));
	}
}

function actionWindow(o, action) {
	if (o != null) {
		switch (action) {
			case "1": 
			    var ret = GetGetVariable(window.location, "ReturnUrl");
				if (ret != null && ret != "") {
					ret = unescape(ret);
					o.location = ret;
				} else {
					o.location.reload(); 
				}
				break;
			case "2": 
				o.close(); 
				break;
			case "3":
				var ret = GetGetVariable(window.location, "ReturnUrl");
				if (ret != null && ret != "") {
					ret = unescape(ret);
					o.location = ret;
				} else {
					o.location = "/home/Home.aspx";
				}
				break;
		}
	}
}

// select color pallete
function SelectColorInPallete(szFunction) {
	window.open("/popup/selectcolor.html?o=" + szFunction, "HanbitON_Pallete", "width=300,height=190,status=no,scrollbars=no");
}



// 창닫기
function closeWindow() {
	if (top != self) {
		top.closeWindow();
	} else {
		self.opener = self;
		self.close();
	}
}



//약관동의 창
/*
function openArticle() {
	window.open("http://www.hanbiton.com/h_article/marketarticle.aspx", "HanbitOn_Article", "width=800,height=550,status=no,scrollbars=no");
}
*/

//약관동의 창
function openArticle() {
	window.open("http://webservice.redbana.tw/Policy/MarketArticle.aspx", "Redbana_Article", "width=800,height=550,status=no,scrollbars=no");
}



// Membership Site

// 회원가입
function GoJoinMember() {
	//location.href = "http://webservice.redbana.tw/Register/MemberRegister.aspx?regsite=redbana"	
	location.href = "http://www.redbana.tw/Member/MemberRegister.aspx?regsite=redbana"	
}

// 회원수정
function GoEditMember() {
	location.href = "http://webservice.redbana.tw/Info/UserModifyCheck.aspx?regsite=redbana"
}

// 아이디/패스워드 찾기
function GoSearchIdPass() {
	window.open("http://webservice.redbana.tw/Info/SearchID.aspx?regsite=redbana", "RedbanaMemberShip", "left=10,top=10,width=345,height=210,status=no");
}

// 輸入認證碼
function GoCertification()
{
    location.href = "/Member/MemberCertification.aspx";
}

// 客服中心連結
function GoCustomerService() {
    location.href = "/Customer/ReportList.aspx";
}

// 캐시 충전
function GoChargeCash()
{
	window.open("http://billing.redbana.tw/cash_charge_pop.asp", "HanbitMemberCash", "left=10,top=10,width=345,height=380,status=no,scrollbars=yes");
}

/* 
 * popup functions
 */

// 이미지 팝업
function popview(url) {
	openWindow("/popup/ViewImage.aspx?url=" + url, "HanbitOnpopview", "width=100,height=100,status=no,scrollbars=yes");
}


// 바로가기
function goUrl(url) {
	if (url != "") {
		if (url.substring(0, 7) == 'http://') {
			openWindow(url, "", "");
		} else {
			location.href = url;
		}
	}
}

/*
 * frame resize functions
 */
// ifrmae 리사이즈
function resizeIframe(name) {
	if (name == null || name == "") {
		return;
	}
	
	try {
	  // iframe object
	  var oIFrame = document.getElementById(name);

	  // resize
		oIFrame.style.height = oIFrame.contentWindow.document.body.scrollHeight;
	} catch (e) {
	}
}

// 부모쪽 리사이즈 함수 호출
function parentResizeIframe(name) {
	if (parent && parent != this && parent.resizeIframe != null) {
		parent.resizeIframe(name);
	}
}


/*
 * hanbiton key shortcut
 */
function processKey(event) {
	var e;

	if (isIE) {
		e = window.event;
	} else {
		e = event;
	}

	// alt + number
	if (e.altKey && e.keyCode > 47 && e.keyCode < 54) {
		if (window == top && opener == null) {
			window.location = navi[e.keyCode - 48];
		}
	}
}

function setFocusing(obj)
{
	if(obj != null)
	{
		try {
			obj.focus();
			obj.style.imeMode ="active";
			obj.select();
		}
		catch (ex)
		{
			// not text box
		}
	}
}

function setSearchTxtBoxFocus()
{
	var objTxt = document.getElementById("mainquery");

	setFocusing(objTxt);
}

function setLoginTxtBoxFocus()
{
	var objTxt = document.getElementById("UserID");

	setFocusing(objTxt);
}



