/* ---------------------------------------------------------------------
JavaScript for ASUMARU
FileName: common.js
--------------------------------------------------------------------- */


// *** OS
var UA = navigator.userAgent;
var osWin = (UA.indexOf("Win", 0) != -1);
var osMac = (UA.indexOf("Mac", 0) != -1);


// *** browser
var brwOpera = (UA.indexOf('Opera') != -1);
var brwIE = (UA.indexOf('MSIE') != -1) && !brwOpera;
var brwFirefox = (UA.indexOf('Firefox') != -1);
var brwSafari = (UA.indexOf('Safari') != -1);
var brwGecko = (UA.indexOf('Gecko') != -1) && !(UA.indexOf('like Gecko') != -1);
var brwNetscape = (UA.indexOf('Netscape') != -1);
UA = undefined;


// *** method
function DW(htmlSrc){document.write(htmlSrc);}
function DWL(htmlSrc){document.writeln(htmlSrc);}


// *** add preload object
var preLoadObj = new Array();

function addPreLoad(imgObj){
	preLoadObj = preLoadObj.concat(imgObj);
}


// *** image preload
function imgPreLoad(){
	preLoadImg = new Array();
	for(var i = 0; i < preLoadObj.length; i++){
		preLoadImg[i] = new Image();
		preLoadImg[i].src = preLoadObj[i];
	}
}
window.onload = function(){
	imgPreLoad();
}


// *** image over
function imgChange(imgObj){
	var imgSrc = imgObj.src;
	var imgStatus = imgSrc.indexOf("_on") != -1;
	if(!imgStatus){
		var P = imgSrc.lastIndexOf(".");
		imgSrc = imgSrc.substr(0, P) + '_on' + imgSrc.substring(P);
	} else {
		imgSrc = imgSrc.replace('_on', '');
	}
	imgObj.src = imgSrc;
}


// *** page top scroll
function pageTopScroll(){
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;
	if(document.documentElement){
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if(document.body){
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;
	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));
	window.scrollTo(Math.floor(x / 1.3), Math.floor(y / 1.3));
	if(x > 0 || y > 0){
		window.setTimeout("pageTopScroll()", 30);
	}
}


// *** keywrod search submit
function formSubmit(formObj){
	document.forms[formObj].submit();
	return true;
}

// *** pop up
function subWin(theURL,winName,features) {
  var winFocus;
  winFocus = window.open(theURL,winName,features);
  winFocus.focus();
}

/**
 * 多重送信制御
 */
var submit_once_flag = 1;
if("function"==typeof window.onload){
    var func = window.onload;
    window.onload = function (){
        func();
        submit_once_flag = 0;
    }
}
else{
    window.onload = function (){
        submit_once_flag = 0;
    }
}
function goForward_once(objForm)
{
    if(0!=submit_once_flag || !objForm){
        return;
    }
    submit_once_flag = 1;
    objForm.submit();
}

/**
 * httpRequestによる通信を行う
 */
var base_http_request = function () {
    this.httpRequest = null;

    /**
     * 初期処理
     * httpRequestオブジェクトの初期化などを行う
     *
     * @exception
     */
    this.init = function () {
        try{
            this.httpRequest = new XMLHttpRequest();
        }
        catch(e){
            try{ /* IE5, IE6 */
                this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); /* Windows XP */
            }
            catch(e1){
                try{ /* IE5, IE6 */
                    this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); /* Windows XP未満 */
                }
                catch(e2){
                    throw "オブジェクトの生成に失敗しました";
                }
            }
        }
    }

    /**
     * httpRequestによるリクエスト送信を行い、結果を返す
     * ※：通信結果はJSON形式であることを前提としています
     *
     * @param string url リクエスト送信先URL
     * @return array JSON形式の文字列の変換結果を格納した配列
     * @exception
     */
    this.request = function (url) {
        if(null==this.httpRequest){
            this.init();
        }

        this.httpRequest.open("GET", url, false);
        this.httpRequest.send(null);

        if(200!=this.httpRequest.status){
            throw "リクエストが失敗しました";
        }

        return eval("(" + this.httpRequest.responseText + ")");
    }
}

