/**
 * 
 * 
 * @author  Keizo Miyata <miyata@able.ocn.ne.jp>
 * @create  2008/02/26
 * @copyright 2007 Sunrise Digital Corporation.
 * @version $id: v 1.0 2008/02/26 12:13:49 Miyata Exp $
 **/
 
var sdIsIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;

Array.prototype.sdShuffle = function() {
    var i = this.length;
    while(i){
        var j = Math.floor(Math.random()*i);
        var t = this[--i];
        this[i] = this[j];
        this[j] = t;
    }
    return this;
}

Array.prototype.sdSearch = function(value, lazy) {
	
	if(lazy){
		var reg = new RegExp(lazy, "igm");
		value = value.replace(reg, '');
	}
	
	for(var i=0; i<this.length; i++){
		var current = lazy ? this[i].replace(reg, '') : this[i];
		if(current == value) return i;
	}
	return false;
}

String.prototype.sdTrim = function(tirm_char) {
	if(!tirm_char) tirm_char = " 　";
	var pattern = "^["+tirm_char+"]+|["+tirm_char+"]+$";
	var reg = new RegExp(pattern, "g");
	
	return this.replace(reg, '');
}

String.prototype.sdWidth = function() {
	len = 0;
	str = escape(this);
	for (i = 0; i < str.length; i++, len++) {
		if (str.charAt(i) == "%") {
			if (str.charAt(++i) == "u") {
				i += 3;
				len++;
			}
		i++;
		}
	}
   return len;
}

//////////////////////////////////////////////////////////////////
var SdDom = new Object();

SdDom.getChildNodes = function(parent){
	
	var nodes = parent.childNodes;
	if(!nodes) return null;
	
	var list = new Array();
	var j = 0
	for (var i = 0; i < nodes.length; i++) {
		if(this._isNotTag(nodes[i])) list[j++] = nodes[i];
	}
	return list;
}

SdDom.getFirstChild = function(parent){
	var nodes = parent.childNodes;
	if(!nodes) return null;
	
	for (var i = 0; i < nodes.length; i ++) {
		if(this._isNotTag(nodes[i])) return nodes[i];
	}
	
	return null;
}

SdDom.getChildByClass = function(parent, class_name){
	var nodes = parent.childNodes;
	if(!nodes) return null;
	
	var node;
	for (var i = 0; i < nodes.length; i ++) {
		if(this._isNotTag(nodes[i]) && nodes[i].className == class_name){
			node = nodes[i];
			break;
		}
	}
	return node;
}

SdDom.getSelectedOption = function(dom_select){
	var node;
	for (var i = 0; i < dom_select.options.length; i ++) {
		if(dom_select.options[i].selected){
			node = dom_select.options[i];
			break;
		}
	}
	return node;
}

SdDom.getElemByName = function(form, name){
	var doms = Form.getElements(form);
	for(var ii=0; ii<doms.length; ii++){
		if(doms[ii].name == name) return doms[ii];
	}
	return
}

SdDom.onLoad = function(id, func){
	if(!this.interval_id)
		this.interval_id = new Object();
	
	if(this.interval_id[id])
		clearInterval(this.interval_id[id]);

	this.interval_id[id] = setInterval("SdDom._isLoadedDom('"+id+"',"+func+")", 300);
}

SdDom._isLoadedDom = function(id, func){
	//if(!sdIsIE) console.info(this.interval_id);
	if($(id)){
		func($(id));
		clearInterval(this.interval_id[id]);
	}
}

SdDom._isNotTag = function(node){
	return typeof node.tagName != "undefined";
}



/////////////////////////////////////////////////////////////////
var SdCookie = new Object();

SdCookie.set = function(name, value, second){
	var exp_date = new Date();
	exp_date.setTime(exp_date.getTime()+(second * 1000));
	
	var item = "@" + name + "=" + escape(value) + ";";
	item += "expires="+exp_date.toGMTString();
	document.cookie = item;
}

SdCookie.get = function(name){
   name = "@" + name + "=";
   var value = "";
   var cookie_string = document.cookie + ";" ;
   var pos_name = cookie_string.indexOf(name);
   
   if (pos_name != -1){
      var start = pos_name + name.length;
      var end   = cookie_string.indexOf(";" , start);
      value = unescape(cookie_string.substring(start,end));
   }
   return value;
}

SdCookie.del = function(name){
   this.set(name,"",0);
}

//////////////////////////////////////////////////////////////
var SdUrl = new Object();

SdUrl.encode = function(str){
	// Unicode to URL encoded UTF-8
	var i, encoded_str, char_code, padded_str;
	encoded_str = "";
	for (i = 0; i < str.length; i++){
		char_code = str.charCodeAt(i);
		if (char_code == 0x20){
			// space -> "+"
			encoded_str += "+";
		} else { // else 1
			if (((0x30 <= char_code) && (char_code <= 0x39)) || ((0x41 <= char_code) && (char_code <= 0x5a)) || ((0x61 <= char_code) && (char_code <= 0x7a))){
				// [0-9a-z-A-Z]
				// no escape
				encoded_str += str.charAt(i);
			} else if ((char_code == 0x2a) || (char_code == 0x2e) || (char_code == 0x2d) || (char_code == 0x5f)) {
				// [.-_]
				// no escape
				encoded_str += str.charAt(i);
			} else { // else 2
				// for internal unicode to UTF-8
				// Ref. http://homepage3.nifty.com/aokura/jscript/utf8.html
				// Ref. http://homepage1.nifty.com/nomenclator/unicode/ucs_utf.htm
				if ( char_code > 0xffff ) {
					encoded_str += "%" + ((char_code >> 18) | 0xf0).toString(16).toUpperCase();
					encoded_str += "%" + (((char_code >> 12) & 0x3f) | 0x80).toString(16).toUpperCase();
					encoded_str += "%" + (((char_code >> 6) & 0x3f) | 0x80).toString(16).toUpperCase();
					encoded_str += "%" + ((char_code & 0x3f) | 0x80).toString(16).toUpperCase();
				} else if ( char_code > 0x7ff ) {
					encoded_str += "%" + ((char_code >> 12) | 0xe0).toString(16).toUpperCase();
					encoded_str += "%" + (((char_code >> 6) & 0x3f) | 0x80).toString(16).toUpperCase();
					encoded_str += "%" + ((char_code & 0x3f) | 0x80).toString(16).toUpperCase();
				} else if ( char_code > 0x7f ) {
					encoded_str += "%" + (((char_code >> 6) & 0x1f) | 0xc0).toString(16).toUpperCase();
					encoded_str += "%" + ((char_code & 0x3f) | 0x80).toString(16).toUpperCase();
				} else {
					// for ascii
					padded_str = "0" + char_code.toString(16).toUpperCase();
					encoded_str += "%" + padded_str.substr(padded_str.length - 2, 2);
				}
			} // else 2
		} // else 1
	}
	return encoded_str;
}





