﻿/**   
 String
*****************************/
String.prototype.trim = function () {
	var s = (this!=null) ? this : "";
	s = s.replace(/^\s+/g,"");
	s = s.replace(/\s+$/g,"");
	return s;
}

String.prototype.replaceQuot = function() {
	var s = (this!=null) ? this : "";
	s = s.replace(/\\/g,"\\\\");
	s = s.replace(/\"/g,"\\\"");
	s = s.replace(/\'/g,"\\\'");
	return s;
}

String.prototype.escapeXml = function() {
	var s = (this!=null) ? this : "";
	s = s.replace(/&/g,"&amp;");
	s = s.replace(/\'/g,"&#039;");
	s = s.replace(/\"/g,"&#34;");
	s = s.replace(/</g,"&lt;");
	s = s.replace(/>/g,"&gt;");
	return s;
}

String.prototype.removeTag = function() {
	var s = (this!=null) ? this : "";
	s = s.replace(/<\/?[^>]+>/gi,"");
	return s;
}

function getCookieVal (offset) 
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;

    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name)  {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)  
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal (j);
            
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}



function SetCookie (name, value)  {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie(name)
{
    var exp = new Date();
    exp.setTime (exp.getTime() - 1000000000);  
    SetCookie(name,'',exp);
}


/********************************************
 RequestValue function
 return url's parameter value
*********************************************/
function RequestValue(requestName)
{
	if(requestName == null) return "";
	
	if(location.search == null) return "";
	
	var fullParams	= location.search.replace('?','').split('&');
	
	if(fullParams == null) return "";
	
	var col;
	var returnValue;
	
	for(i=0; i < fullParams.length ; i++)
	{
		col		= fullParams[i].split('=');
		if(col != null && col[0].toLowerCase() == requestName.toLowerCase())
		{
			returnValue = col[1];
			break;
		}
	}
	return returnValue;
}

function fileHelper()
{
    this.download  =function(board_seq, seq, file_seq)
    {
	    var sUrl	= ROOT_PATH + "Handler/RequestFileHandler.ashx?board_seq=" + escape(board_seq) + 
					    "&seq=" + escape(seq) +
					    "&file_seq=" + escape(file_seq);
        var exceliframe2 = null;
        exceliframe2 = document.getElementById("exceliframe2");
        
        if( exceliframe2 == null)
        {
            exceliframe2 = document.createElement("IFRAME");
            exceliframe2.setAttribute("id","exceliframe2");
            exceliframe2.setAttribute("src","about:blank");
            exceliframe2.setAttribute("width","0px");
            exceliframe2.setAttribute("border","0px");
            exceliframe2.setAttribute("height","0px");
            exceliframe2.setAttribute("style","display:none;");
            
            if(document.all)
                document.insertBefore(exceliframe2,document.firstChild);
            else
                document.body.insertBefore(exceliframe2,document.body.firstChild);
        }  
	    exceliframe2.src = sUrl;
	    return false;
    }
}

var _file = new fileHelper(); 

function jsNumHandler(ctl_NM)
{			
	e = window.event;  
    if(e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105 || e.keyCode == 8 || e.keyCode == 46){
		if(e.keyCode == 48 || e.keyCode == 96)	 
		{
			if(typeof(ctl_NM)=="object")
			{
			if(ctl_NM != null){e.returnValue=false;}
			else{return};
			}
			else
			{
				return;													 
			}
		}	
		else if(e.keyCode == 45)
		{
			if(typeof(ctl_NM)=="object")
			{
			    if(ctl_NM != null && ctl_NM.value =='-'){e.returnValue=false;}
			    else{return};
			}
			else
			{
				return;													 
			}
			
		}else{	
			
			return;                             
		}
	}
	else if(e.keyCode == 37 || e.keyCode == 39 ||e.keyCode == 9)
	{ 
	    return;
	}
	else if(e.keyCode == 109 || e.keyCode == 189)
	{
		return;
	}
	else
	{                     
		e.returnValue=false;
	} 
}
//ModalDialog Open
function fn_ModalOpen(targetUrl,Width,Height,objType)
{
    var Attributes="dialogHeight:"+Height+"px;dialogWidth:"+Width+"px;status:no;";
    var Values=window.showModalDialog(targetUrl,'',Attributes);
    switch(objType)
    {
        case "POST" :
            if(Values !=null)
            {
                var Post=Values[0].split('-');
                document.all.txtzipcode1.value=Post[0];
                document.all.txtzipcode2.value=Post[1];
                document.all.txtadd1.value=Values[1]; 
            }

        default:
        break;
    }
    
}
function fn_CalenDar(szName)	
{	
    var rgRet = window.showModalDialog('../Common/Calendar.aspx?to='+szName,"",'dialogWidth:250px;dialogHeight:190px;scroll:no;status:no;help:no;');
		
    if(rgRet != null)
    {
	    document.all(szName).value = rgRet;
    }
}
function SelectHelper(obj)
{
    this.oSelect = obj;
    
    this.clear = function()
    {
        obj.options.length = 0;
    }
    
    this.add = function(text, value)
    {
        this.oSelect.options[this.oSelect.options.length] = new Option(text,value);
    }
    
    this.remove = function(index)
    {

    }
    
    this.removeSelectedItem = function()
    {
        var temp = new Array;
         for(var i = 0 ; i < this.oSelect.options.length ; i++)
        {
            if(this.oSelect.options[i].selected == false )
            {
                temp.push(new Array(this.oSelect.options[i].value, this.oSelect.options[i].text));
            }
        }
        
        this.clear();
        
        for(var j =0; j < temp.length ;j++)
        {
            this.add(temp[j][1],temp[j][0]);
        }
    }
    
    this.setSelectedValue = function(selectedValue)
    {
        for(var i = 0 ; i < this.oSelect.options.length ; i++)
        {
            if(this.oSelect.options[i].value == selectedValue )
            {
                this.oSelect.selectedIndex = i;
                return;
            }
        }
    }
    
    this.setSelectedText = function(selectedText)
    {
        for(var i = 0 ; i < this.oSelect.options.length ; i++)
        {
            if(this.oSelect.options[i].text == selectedText )
            {
                this.oSelect.selectedIndex = i;
                return;
            }
        }
    }
    
    this.getSelectedItem = function()
    {
        if(this.oSelect.selectedIndex == -1) return null;
        
        return this.oSelect.options[this.oSelect.selectedIndex];
    }
    
    this.getSelectedValue = function()
    {
        var item = this.getSelectedItem();
        if(item == null) return null;
        
        return this.getSelectedItem().value;
        //return this.oSelect.options[this.oSelect.options.selectedIndex].value;
    }
    
    this.getSelectedText = function()
    {
        var item = this.getSelectedItem();
        if(item == null) return null;
        return this.getSelectedItem().text;
        //return this.oSelect.options[this.oSelect.options.selectedIndex].text;
    }
}

function replace(target,oldTerm,newTerm,caseSens,wordOnly)
{
	var work = target;
	var ind = 0;
	var next = 0;
	
	if(!caseSens){
		oldTerm = oldTerm.toLowerCase();
		work = target.toLowerCase();
	}
	
	while((ind = work.indexOf(oldTerm,next)) >= 0)
	{
		if(wordOnly){
			var before = ind-1;
			var after = ind + oldTerm.length;
			if(!(space(work.charAt(before)) && space(work.charAt(after)))){
				next = ind + oldTerm.length;
				continue;				
			}
		}
		target = target.substring(0,ind) + newTerm + target.substring(ind + oldTerm.length,target.length);
		work = work.substring(0,ind) + newTerm + work.substring(ind + oldTerm.length,work.length);
		next = ind + newTerm.length;
		if(next >= work.length){break;}
	}
	return target;
}


function search(target,term,caseSens,wordOnly)
{
	var ind = 0;
	var next = 0;
	if(!caseSens){
		term = term.toLowerCase();
		target = target.toLowerCase();
	}

	while((ind = target.indexOf(term,next)) >=0){
		if(wordOnly){
			var before = ind -1;
			var after = ind + term.length;
			if (!(space(target.charAt(before)) && space(target.charAt(after)))){
				next = ind + term.length;
				continue;
			}
		}
		return true;
	}
	return false;
}
function space(check)
{
	var space = ".,/<>?!`';:@#$%^&*()=-[]{}" + '"' + "\\\n\t";
	for (var i=0;i<space.length;i++)
		if(check == space.charAt(i)){return true;}
	if (check == ""){return true;}
	if (check == null){return true;}
	
	return false;
}
function LTrim(i)
{
	var tmp;
	if (i.length != 0 ) {
		tmp = i.charAt(0);
		while ( tmp == " ") {
			i = i.substring(1, i.length);
			tmp = i.charAt(0);
		}
	}
	return i;
}
function RTrim(i)
{
	var tmp;
	if (i.length != 0 ) {
		tmp = i.charAt(i.length-1);
		while ( tmp == " ") {
			i = i.substring(0, i.length-2);
			tmp = i.charAt(i.length-1);
		}
	}
	return i;
}
function Trim(i)
{
	var tmp;
	if (i.length != 0 ) {

		tmp = i.charAt(0);
		while ( tmp == " ") {
			i = i.substring(1, i.length);
			tmp = i.charAt(0);
		}

		tmp = i.charAt(i.length-1);
		while ( tmp == " ") {
			i = i.substring(0, i.length-2);
			tmp = i.charAt(i.length-1);
		}
	}
	return i;
}
function isRate(check)
{
	var term = "0123456789.";
	var work = check;
	for (var j=0;j < work.length;j++){
		if (!search(term,work.charAt(j),true,false)){return false;}
	}
	return true;
}
function isEngNameCheck(check)
{
	var term = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,";
	var work = check;
	for (var j=0;j < work.length;j++){
		if (!search(term,work.charAt(j),true,false)){return false;}
	}
	return true;
}
function isNumeric(check)
{
	var term = '';
	var work = check;
//	document.write(check);
	for (var i=0;i < work.length;i++){
		term = work.charAt(i);
		if (!((term >= '0') && (term <= '9')))
		{
			return false;
		}
		//document.write(term + "<br>");
	}
	return true;	
}
function isAlpha(check)
{
	var term = '';
	var work = check;
	for (var j=0;j < work.length;j++){
		term = work.charAt(j);
		if (!(((term >= 'A') && (term <= 'Z')) || ((term >= 'a') && (term <= 'z')))){return false;}
	}
	return true;
}
function isNumAlpha(check)
{
	var term = '';
	var work = check;
	for (var j=0;j < work.length;j++){
		term = work.charAt(j);
		if (!(((term >= 'A') && (term <= 'Z')) || ((term >= 'a') && (term <= 'z')) || ((term >= '0') && (term <= '9')))){return false;}
	}
	return true;
}

// Validate with RegularExpression
function RegularTrim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}
function RegularIsValid(val,validationexpression) 
{
    var value = val;
    if (RegularTrim(value).length == 0)
        return true;  
    var rx = new RegExp(validationexpression);
    var matches = rx.exec(value);
    return (matches != null && value == matches[0]);
}

function GetByteLength(sValue)   
{
     len = sValue.length;
     for (var i=0; i< sValue.length ; i++)
     {
       character = sValue.substr(i,1).charCodeAt(0);
       if (character > 127) { len++; }
     }
     return len;
}

function alertmsg()
{
    this.alert = function(msg)
    {
        alert(msg);
    }
}
var ehr_msg = new alertmsg();


var NUMERICTYPE	= /\d+/
var EMAILTYPE	= /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
var DEFAULTTYPE	= null;
var ALPHATYPE	= /[a-zA-Z]+/;
var ENGLISHNAME	= "ENGLISHNAME";///\w+(\s*\w*)*\w+/;
var PERCENTAGETYPE = "PERCENTAGETYPE";
var PERCENTAGETYPE2 = "PERCENTAGETYPE2";
var NUMERICALPHA	= /[a-zA-Z]*\d*/;
var POINTNEMERICTYPE = "POINTNEMERICTYPE";// /^\d*\.?\d*/;

function assertValid(sValue,validType,allowNull,minLength,maxLength,headerMsg)
{
	if(headerMsg == null)
	{
		headerMsg = "";
	}
	
	if(sValue == null)
	{
		//alert('a');
		if(allowNull)
			return true;
		else 
		{			
			ehr_msg.alert(headerMsg + '필수 입력항목입니다.');
			return false;
		}
	}
	var inputValue	= Trim(sValue + '');
	
	if(inputValue == "")
	{
		//alert('b');
		if(allowNull) return true;
		else 
		{
			ehr_msg.alert(headerMsg + '필수 입력항목입니다.');
			return false;
		}
	}
	
	//alert(inputValue);
	// check length
	if(minLength != null && maxLength != null)
	{
		if(POINTNEMERICTYPE == validType)
		{			
			try
			{
				
				var cvtedNum	= parseInt(inputValue);						
				if(isNaN(cvtedNum))
				{
					ehr_msg.alert(headerMsg + '입력형식이 잘못되었습니다.');
					return false;
				}
				
				if(cvtedNum < minLength || cvtedNum >= maxLength)
				{
					ehr_msg.alert(headerMsg + minLength + '에서'  + maxLength + ' 미만 까지만 입력 가능합니다.');
					return false;
				}
			}
			catch(e)
			{			
				ehr_msg.alert(headerMsg + '입력형식이 잘못되었습니다.');
				return false;
			}
		}
		else
		{
			var totalByte	= GetByteLength(inputValue);
			
			//alert(totalByte);
			if(totalByte < minLength || totalByte > maxLength)
			{
				ehr_msg.alert(headerMsg + '최소 ' + minLength + ' 에서 최대' + maxLength + '(byte) 까지만 입력 가능합니다.');
				return false;
			}
		}
	}
		
	// check validation
	if(validType == null)
	{ return true;}
	
	if(ENGLISHNAME == validType)
	{
		if(isEngNameCheck(inputValue))
		{
			return true;
		}
		else
		{
			ehr_msg.alert(headerMsg + '입력형식이 잘못되었습니다.');
			return false;
		}
	}
	else if(validType == PERCENTAGETYPE)
	{
		if(!RegularIsValid(inputValue,NUMERICTYPE))
		{
			ehr_msg.alert(headerMsg + '입력형식이 잘못되었습니다.');
			return false;
		}
		
		if(parseInt(inputValue) > 100 || parseInt(inputValue) < 0)
		{
			ehr_msg.alert(headerMsg + '백분율은 0에서 100사이의 숫자만 가능합니다.');
			return false;
		}
	}
	else if(validType == PERCENTAGETYPE2)
	{		
		
		if(parseInt(inputValue) >= 100 || parseInt(inputValue) < 0)
		{
			ehr_msg.alert(headerMsg + '백분율은 0에서 100사이의 숫자만 가능합니다.');
			return false;
		}
	}
	else if(validType == POINTNEMERICTYPE)
	{
		return true;	
	}
	else
	{
		//if(validType == EMAILTYPE) alert('email');
		
		if(!RegularIsValid(inputValue,validType))
		{
			ehr_msg.alert(headerMsg + '입력형식이 잘못되었습니다.');
			return false;
		}
	}
	return true;
}

// ussage
// object
// predefined type : DEFAULT or etc..
// allow null : false, true
// min length ; byte minimum
// max length : byte maxmum
// header message : defined header
function assertValidFocus(oItem,validType,allowNull,minLength,maxLength,headerMsg)
{
	if(headerMsg == null)
	{
		headerMsg = "";
	}
	
	var isSuccess = assertValid(oItem.value,validType,allowNull,minLength,maxLength,headerMsg);
	
	if(!isSuccess)
	{
		
		oItem.focus();
		oItem.select();
	}
	return isSuccess;
}


function ProductSearch()
{
    this.v2005_before = 'N';
    this.v2006 = 'N';
    this.v2007 = 'N';
    this.vehicle_year = '';
    this.vehicle_make = '';
    this.vehicle_model = '';
    this.vehicle_options = '';

    this.find_by_style   = '';
    
    this.size_type = '';
    this.size_width = '';
    this.size_ratio = '';
    this.size_rim_size = '';
    
    this.name_tire_name = '';
    this.brand_tire_brand = '';
    
    this.category = '';

    
    this.searchParam = function()
    {
        var params = new Array();
        var action = 'searchproduct';
        
       
        // search by options
        var params = new Array
        (
            this.v2005_before
            ,this.v2006
            ,this.v2007
            
            ,this.vehicle_year
            ,this.vehicle_make
            ,this.vehicle_model
            ,this.vehicle_options
            
            ,this.find_by_style
    
            ,this.size_type
            ,this.size_width
            ,this.size_ratio
            ,this.size_rim_size
    
            ,this.name_tire_name
            ,this.brand_tire_brand

            ,this.category
        );
        
        
        return params;
    }
}
