jQuery.fn.extend({
  autoTab: function(destination) {
    $(this).keyup(function(event) {
      var maxLen = $(this).attr('maxlength');
      if ($(this).val().length >= maxLen) {
        $(destination).val("");
        $(destination).focus();
      }
    });
    return false;
  },
  secureLoad: function(url, params, callback) {
    var off = url.indexOf(" ");
    if (off >= 0) {
      var selector = url.slice(off, url.length);
      url = url.slice(0, off);
    }

    callback = callback || function() {};

    var self = this;

    secureAjax(url, params, {
      type: "POST",
      dataType: "html",
      complete: function(res, status) {
        if ( status == "success" || status == "notmodified" ) {
          self.html( selector ?
            jQuery("<div/>")
              .append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))
              .find(selector) :
            res.responseText );
        }

        self.each( callback, [res.responseText, status, res] );

      }
    });

    return this;
  }
});

function postLink(url, params, fire) {
  var formHtml = "<form id='postLinkForm' name='postLinkForm' method='POST' action='"+url+"'>";
  if ( params.constructor == Array || params.jquery ) {
    jQuery.each(params, function() {
      formHtml += "<input type='hidden' name='"+this.name+"' value='"+this.value+"'>";
    });
  } else {
    for (var i in params) {
      if (params[i] && params[i].constructor == Array) {
        jQuery.each( params[i], function(){
          formHtml += "<input type='hidden' name='"+i+"' value='"+this+"'>";
        });
      } else {
        formHtml += "<input type='hidden' name='"+i+"' value='"+params[i]+"'>";
      }
    }
  }

  formHtml += "</form>";
  jQuery("body").append(formHtml);
  //XecureSubmit(document.postLinkForm);
  document.postLinkForm.fireSubmit(fire);
}

function secureGetJSON(url, data, callback) {
  return secureGet(url, data, callback, "json");
}


function secureGet(url, data, callback, type) {
  if (jQuery.isFunction(data)) {
    callback = data;
    data = null;
  }

  return secureAjax(url, data, {
    success: callback,
    dataType: type
  });
}

function securePost(url, data, callback, type) {
  if ( jQuery.isFunction(data)) {
    callback = data;
    data = {};
  }

  return secureAjax(url, data, {
    type: "POST",
    success: callback,
    dataType: type
  });
}

function secureAjax(url, params, options) {
  if (params) {
    if (typeof params != "string") {
      params = jQuery.param(params);
    }
    params = decodeURIComponent(params).replace(/\+/g, "%20");
    try {
      params = "q=" + escape_url(XecureAjax(params));
    } catch(e) {
      alert('¾Ј£ȭ¿¡ ½ȆчϿ´½4ϴ׮ °�¡°Ҡ¹®G ȏ½ñ࠹ٶ�.');
    }
  }

  var options = $.extend({
    url:  url,// + "?" + params
    data: params
  }, options || {});

  return jQuery.ajax(options);
}

$.ajaxSetup({
  type: "POST",
  dataType: "json",
  timeout: 480000,
  contentType: "application/x-www-form-urlencoded;charset=UTF-8"
});

/**
 * ajax ƫ½ƀʠO¾¸砷ε�̹́�º¸¿©°렳¡³ª¸砻緳¶°ҠȔ
 *
 */
//$(this).ajaxStart(function() {
//	defaultLoading();
//}).ajaxStop(function() {
// 	defaultUnloading();
//}).ajaxError(function() {
// 	alert("¼܇մϴ׮ ½ýºƛ ¿7�߻�4ϴ׮\n»󵣽ƃ»; ³²°ڠ½ø砽żԈ�º¯; µ帮°ڽ4ϴ׮");
//});

//helper
function objToString(o) {
    var s = '{\n';
    for (var p in o)
        s += '    ' + p + ': ' + o[p] + '\n';
    return s + '}';
}

function objToStringObject(o) {
    var s = '{';
    for (var p in o)
        s += '    "' + p + '": "' + o[p] + '",';
      s = s.substring(0, s.length - 1);
    return s + '}';
}

// helper
function elementToString(n, useRefs) {
    var attr = "", nest = "", a = n.attributes;
    for (var i=0; a && i < a.length; i++)
        attr += ' ' + a[i].nodeName + '="' + a[i].nodeValue + '"';

    if (n.hasChildNodes == false)
        return "<" + n.nodeName + "\/>";

    for (var i=0; i < n.childNodes.length; i++) {
        var c = n.childNodes.item(i);
        if (c.nodeType == 1)       nest += elementToString(c);
        else if (c.nodeType == 2)  attr += " " + c.nodeName + "=\"" + c.nodeValue + "\" ";
        else if (c.nodeType == 3)  nest += c.nodeValue;
    }
    var s = "<" + n.nodeName + attr + ">" + nest + "<\/" + n.nodeName + ">";
    return useRefs ? s.replace(/</g,'&lt;').replace(/>/g,'&gt;') : s;
};

// convert string to json object

/*
function replaceAll(str, searchStr, replaceStr) {
	var temp_str = str;
	temp_str = temp_str.replace(eval("/" + searchStr + "/gi"), replaceStr);
	return temp_str;
}
*/

function replaceAll(str, searchStr, replaceStr) {
    while (str.indexOf(searchStr) != -1) {
        str = str.replace(searchStr, replaceStr);
    }
    return str;
}

function toJsonObject(jsonStr) {
/*
	if(re) {
		jsonStr = jsonStr.replace(eval("/<script language='javascript'>/gi"), '');
		jsonStr = jsonStr.replace(eval("/<script>/gi"), '');
		jsonStr = jsonStr.replace(eval("/<\\/script>/gi"), '');

		jsonStr = jsonStr.replace("var s = '';", '');
		jsonStr = replaceAll(jsonStr, "s += '", '');
		jsonStr = replaceAll(jsonStr, "'\;\n", '');
		
		//jsonStr = jsonStr.replace("/^d[a-zA-Z0-9_][^a-zA-Z0-9_].*;\n$/g", '');
		//jsonStr = jsonStr.replace(eval("/^\\s*|\\s*$/gi"), '');
		
		//jsonStr.replace(eval("/^do.+;\\r\\n$/"), "");
		var p = eval("/^do/");
		jsonStr.replace(p, '');
		
		if(p.test(jsonStr))
			alert('true');
		
		alert(jsonStr);
		$("#test").text('');
		$("#test").text(jsonStr);
	}
*/
	return eval('(' + BlockDec(jsonStr) + ')');
}

function getS(str) {
	str = str.replace(eval("/<script language='javascript'>/gi"), '');
	str = str.replace(eval("/<script>/gi"), '');
	str = str.replace(eval("/<\\/script>/gi"), '');

	str = str.replace("var s = '';", '');
	str = replaceAll(str, "s += '", '');
	str = replaceAll(str, "';", '');
	
	//str = str.replace(/[*\s]/g, '');
	//str = str.replace(/documen.+$/, '');
	
	$("#test").text(str);
	$("#test").show();
	return str;
}

function trim(text) {
	var str = text;
	if(text == "" || text == "undefined") {
		return "";
	}
	str = str.replace(/^\s*/, '');
	str = str.replace(/\s*$/, '');
	return str;
}

function format(text) {
    if(arguments.length <= 1) {
        return text;
    }
    
    var tokenCount = arguments.length - 2;
    for(var token = 0; token <= tokenCount; token++) {
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
    }
    return text;
}

function enc(string) {
	return string == null ?  null : encodeURIComponent(string); 
}


function toPrice(money, cipher) {
	var len, strb, revslice;

	strb = money.toString();
	strb = strb.replace(/,/g, '');
	strb = getOnlyNumeric(strb);
	strb = parseInt(strb, 10);
	
	if(isNaN(strb))
	 return '';
	
	strb = strb.toString();
	len = strb.length;
	
	if(len < 4)
	 return strb;
	
	if(cipher == undefined)
	 cipher = 3;
	
	count = len/cipher;
	slice = new Array();
	
	for(var i=0; i<count; ++i) {
		if(i*cipher >= len)
		break;
		slice[i] = strb.slice((i+1) * -cipher, len - (i*cipher));
	}
	
	revslice = slice.reverse();
	return revslice.join(',');
}

function getOnlyNumeric(str) {
	var chrTmp, strTmp;
	var len;
	
	len = str.length;
	strTmp = '';
	
	for(var i=0; i<len; ++i) {
		chrTmp = str.charCodeAt(i);
		if((chrTmp > 47 || chrTmp <= 31) && chrTmp < 58) {
			strTmp = strTmp + String.fromCharCode(chrTmp);
		}
	}

	return strTmp;
}


