// validates that the entry is a positive or negative number
function isNumber(str, signed, floating) 
{

    var oneDecimal = false;
    var oneChar = 0;
    
    // make sure value hasn't cast to a number data type
    str = str.toString( );
    
    if(str.length == 0) {
    	return false;
    }
    
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
        // OK for minus sign as first character
        if (signed == true && oneChar == 45) {
            if (i == 0) {
                continue;
            } else {
                return false;
            }
        }
        // OK for one decimal point
        if (floating == true && oneChar == 46) {
            if (!oneDecimal) {
                oneDecimal = true;
                continue;
            } else {
                return false;
            }
        }
        // characters outside of 0 through 9 not OK
        if (oneChar < 48 || oneChar > 57) {
            return false;
        }
    }

    return true;
}

function isValidLoginId(str) 
{
    var oneChar = 0;
    
    // make sure value hasn't cast to a number data type
    str = str.toString( );
    
    if(str.length < 4 || str.length > 12) {
    	return false;
    }

	str = str.toLowerCase();
    
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
		if(oneChar == 45 || oneChar == 46 || oneChar == 95) continue;
		if((oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) continue;
		return false;
	}

	return true;
}

function isValidLoginPwd(str) 
{
    var oneChar = 0;
    
    // make sure value hasn't cast to a number data type
    str = str.toString( );
    
    if(str.length < 4 || str.length > 12) {
    	return false;
    }

	str = str.toLowerCase();
    
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
		if(oneChar == 45 || oneChar == 46 || oneChar == 95) continue;
		if((oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) continue;
		return false;
	}

	return true;
}

function IsValidKoreanSocialId(idHigh, idLow)
{
	var socialId;
	var total = 0;

	if(isNumber(idHigh) == false || isNumber(idLow) == false) {
		return false;
	}

	socialId = idHigh + idLow;

	var total = 0;
	for(var i = 0; i < socialId.length; i++){
		if(i <= 7) {
			total += parseInt(socialId.charAt(i)) * (i + 2);
		} else if(i >= 8 && i <=11){
			total += parseInt(socialId.charAt(i)) * (i - 6);
		}
	}

	var check = (11 - (total % 11)) % 10;
	if(parseInt(check) == parseInt(socialId.charAt(12))){
		return true;
	} else {
		return false;
	}
}


function CartOrderCountUpDown(obj, deltaCount)
{
	var cnt = parseInt(obj.value, 10);
	cnt += deltaCount;
	if(cnt <= 0 || cnt >= 1000000) return;
	obj.value = cnt.toString();
}

function GetCartOrderCount(obj)
{
	var cnt = parseInt(obj.value, 10);
	if(cnt <= 0 || isNaN(cnt)) {
		alert("°³¼ö¸¦ È®ÀÎÇÏ¼¼¿ä.");
		obj.focus();
		return 0;
	}
	return cnt;
}


function setCookie(name, value, exp_y, exp_m, exp_d, path, domain, secure)
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

function deleteCookie(cookieName)
{
  // current date & time
  var cookieDate = new Date();
  cookieDate.setTime(cookieDate.getTime() - 1);
  document.cookie = cookieName += "=; expires=" + cookieDate.toGMTString();
}


function getCookie(cookieName)
{
  var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );

  if(results )
    return (unescape(results[2]));
  else
    return null;
}


function getCheckedRadio(radio)
{
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return radio[i].value;
        }
    }
    return "";
}

function checkRadio(radio, value)
{
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].value == value) {
            radio[i].checked = 1;
			return;
        }
    }
}


// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        }
    }
    return false;
}

function selectOption(obj, str)
{
	var i;	
	
	for(i=0; i<obj.options.length; i++) {
		if(obj.options[i].value == str) {
			obj.options.selectedIndex = i;
			return true;
		}
	}

	return false;
}

function getSelectedOption(obj)
{
	if(obj.selectedIndex >= 0 && obj.selectedIndex < obj.options.length) {
		return obj.options[obj.selectedIndex].value;
	} else {
		return "";
	}
}

function getBrowserHeight() 
//http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
{
  var browser_width = 0, browser_height = 0;
  if(typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    browser_width = window.innerWidth;
    browser_height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    browser_width = document.documentElement.clientWidth;
    browser_height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    browser_width = document.body.clientWidth;
    browser_height = document.body.clientHeight;
  }

  return browser_height;
}

function calcHeight(iframeId)
//http://th.atguy.com/mycode/iframe_size/
{
	var browser_height;

  //find the height of the internal page
  var the_height=
	document.getElementById(iframeId).contentWindow.
	  document.body.scrollHeight;

	browser_height = getBrowserHeight();

	if(the_height < browser_height-20) {
		the_height = browser_height-20;
	}

  //change the height of the iframe
  document.getElementById(iframeId).height= the_height;
}


// validates that the entry is formatted as an email address
function isValidEmailAddress(str,  ballowemany) {

    str = str.toLowerCase();
	var emailArray = str.split(";");
	if(emailArray.length > 1 && ballowemany == false) {
		return false;
	}

	for(var j = 0; j < emailArray.length; j++) {

		str = emailArray[j];
		if (str.indexOf("@") > 1) {

			var addr = str.substring(0, str.indexOf("@"));
			var domain = str.substring(str.indexOf("@") + 1, str.length);
			// at least one top level domain required
			if (domain.indexOf(".") == -1) {
				return false;
			}
			// parse address portion first, character by character
			for (var i = 0; i < addr.length; i++) {
				oneChar = addr.charAt(i).charCodeAt(0);
				// dot or hyphen not allowed in first position; dot in last
				if ((i == 0 && (oneChar == 45 || oneChar == 46))  || 
					(i == addr.length - 1 && oneChar == 46)) {
					return false;
				}
				// acceptable characters (- . _ 0-9 a-z)
				if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
					(oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
					continue;
				} else {
					return false;
				}
			}

			for (i = 0; i < domain.length; i++) {
				oneChar = domain.charAt(i).charCodeAt(0);
				if ((i == 0 && (oneChar == 45 || oneChar == 46)) || 
					((i == domain.length - 1  || i == domain.length - 2) && oneChar == 46)) {
					return false;
				}
				if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
					(oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
					continue;
				} else {
					return false;
				}
			}

		} else {
			return false;
		}
	}

	return true;
}



function formatDecNumber(num, decplaces) {
    // convert in case it arrives as a string value
    num = parseInt(num, 10);

	var str = num + "";

	// if needed for small values, pad zeros
	// to the left of the number
	while (str.length < decplaces) {
		str = "0" + str;
	}

	return str;
}

function isValidDateFormat(str)
{
	var a = str.split("-");
	var val;
	if(a.length != 3) {
		return false;
	}
	
	if(a[0].length != 4 || isNumber(a[0], false, false) == false) {
		return false;
	}
	if(a[1].length != 2 || isNumber(a[1], false, false) == false) {
		return false;
	}
	val = parseInt(a[1], 10);
	if(val <= 0 || val > 12) {
		return false;
	}
	if(a[2].length != 2 || isNumber(a[2], false, false) == false) {
		return false;
	}
	val = parseInt(a[2], 10);
	if(val <= 0 || val > 31) {
		return false;
	}
	
	return true;
}

function get_year(strDate)
{
	//yyyy-mm-dd
	if(isValidDateFormat(strDate) == false) {
		return 0;
	}
	strYear = strDate.substr(0, 4);
	if(isNumber(strYear, false, false) == false) {
		return 0;
	}
	return parseInt(strYear, 10);
}
function get_month(strDate)
{
	//yyyy-mm-dd
	if(isValidDateFormat(strDate) == false) {
		return 0;
	}
	strMonth = strDate.substr(5, 2);
	if(isNumber(strMonth, false, false) == false) {
		return 0;
	}
	return parseInt(strMonth, 10);
}

function get_day(strDate)
{
	//yyyy-mm-dd
	if(isValidDateFormat(strDate) == false) {
		return 0;
	}
	strDay = strDate.substr(8, 2);
	if(isNumber(strDay, false, false) == false) {
		return 0;
	}
	return parseInt(strDay, 10);
}


// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
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 += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				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 traceExpress(expressCode)
{
	var url;
	var windowFeatures = "height=540,width=530,scrollbars=1";
	url = "http://www.hanjinexpress.hanjin.net/customer/plsql/hddcw07.result?wbl_num=" + expressCode;
	window.open(url, "_express", windowFeatures);
}





