//--------------------------------------------------------------------------------
//    Javascript 共通関数
//--------------------------------------------------------------------------------
//----------------------------------------------------------------------
//    JAVASCRIPTｸﾞﾛｰﾊﾞﾙ変数
//----------------------------------------------------------------------


//--------------------------------------------------------------------------------
//    エラーメッセージ関数
//--------------------------------------------------------------------------------
//    Input : iTitle   タイトル
//            iMsg     メッセージ
//    Output: Nothing
//    Return: Nothing
//--------------------------------------------------------------------------------
function addOnLoadEvent(i_Element,i_Function){
    try {
        i_Element.addEventListener('load',i_Function,false);
    } catch(e) {
        i_Element.attachEvent('onload',i_Function);
    }
}


//--------------------------------------------------------------------------------
//    時間取得 0000-00-00 00:00:00形式
//--------------------------------------------------------------------------------
function jGetNowtime() {
    
    var now       = new Date();
    var nowyear   = now.getYear();
    var nowmonth  = now.getMonth();
    var nowdate   = now.getDate();
    var nowHours  = now.getHours();
    var nowMinute = now.getMinutes();
    var nowSecond = now.getSeconds();
    
    var nowtime   = nowyear + '-' + nowmonth + '-' + nowdate + ' ' + nowHours + ':' + nowMinute + ':' + nowSecond;
    
    return nowtime;
    
}


//--------------------------------------------------------------------------------
//    現在年月 yyyy/mm0形式
//--------------------------------------------------------------------------------
function jGetNowYerMon() {
    
    
    var now       = new Date();
    var nowyear   = now.getFullYear();
    var nowmonth  = now.getMonth() + 1;
    
    if  (nowmonth < 10 ) {
        nowmonth = "0" + nowmonth ;
    }
    
    var nowYerMon   = nowyear + '/' + nowmonth;
    
    return nowYerMon;
    
}

//--------------------------------------------------------------------------------
//    HttpRequest関数
//--------------------------------------------------------------------------------
//    Input : iMenthod POST/GET
//            iUrl     PHP FileName
//            iParam   Parameter
//            iTiming  False:同期/True:非同期
//    Output: Nothing
//    Return: Respons
//--------------------------------------------------------------------------------
function jHttpRequest(iMenthod ,iUrl , iParam , iTiming ) {
    


    
    //宣言
    var request = new jHttpRequestCreate();
    
    if  (iTiming == true) {
        //非同期通信の場合
        request.onreadystatechange  = function(){
            if  (request.readyState == 4 ) {
                //受信完了
                if  (request.status == 200 ) {
                    //ﾘｸｴｽﾄ正常処理
                    //alert(request.readyState+':'+request.status+';'+request.responseText);
                    return request.responseText;
                    
                    //変数の開放
                    request = null;
                    
                } else {
                    //ﾘｸｴｽﾄ異常処理
                    //alert('通信エラー','ステータス:'+request.status);
                }
            }
        }
    }

//    timerId = setInterval(function(){
//  //タイマーを終了する
//  clearInterval(timerId);
//  //通信を中断する
//  request.abort();
//    }
//    , 5000);
//    
    
    //送信処理の実行
    try{
        
        if  (iMenthod == 'GET'){
            //GETの場合
            request.open(iMenthod,iUrl+'?'+iParam,iTiming);
        } else {
            //POSTの場合
            request.open(iMenthod,iUrl,iTiming);
        }
        
//        //IEの場合 request.timeout が使用できるのはIE8以降
//        request.timeout = 5000;
        
        //リクエスト送信時にヘッダーを追加しておかないとブラウザによってはデータ送信が出来ない 
        request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        
        if  (iMenthod == 'GET'){
            //GETの場合
            request.send(null);
        } else {
            //POSTの場合
            //alert('bef');
            request.send(iParam);
            //alert('aft');
        }
        
        if  (iTiming == true) {
            //非同期通信の場合
        } else {
            //同期通信の場合
            return request.responseText;
        }
        
    }catch(e){
        return 'NG:通信エラー';
    }
    
}


function jHttpRequest2(iMenthod ,iUrl , iParam , iTiming , iId , callBack) {
    
    //宣言
    var request = new jHttpRequestCreate();
    
    //非同期通信の場合
    request.onreadystatechange  = function(){
        if  (request.readyState == 4 ) {
            //受信完了
            if  (request.status == 200 ) {
                //ﾘｸｴｽﾄ正常処理
                
                //非同期通信の場合
                if  (iId==''){
                    callBack(request);
                }else{
                    document.getElementById(iId).innerHTML = request.responseText;
                }
                
                //変数の開放
                request = null;
                
            } else {
                //ﾘｸｴｽﾄ異常処理
                //alert('通信エラー','ステータス:'+request.statusText);
            }
        }
    }
    
    
    if  (iMenthod == 'GET'){
        //GETの場合
        request.open(iMenthod,iUrl+'?'+iParam,iTiming);
    } else {
        //POSTの場合
        request.open(iMenthod,iUrl,iTiming);
    }
    
    
    //リクエスト送信時にヘッダーを追加しておかないとブラウザによってはデータ送信が出来ない 
    request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    
    if  (iMenthod == 'GET'){
        //GETの場合
        request.send(null);
    } else {
        //POSTの場合
        request.send(iParam);
    }
    
}




//--------------------------------------------------------------------------------
//    HTTPリクエストオブジェクト作成
//--------------------------------------------------------------------------------
function jHttpRequestCreate() {
    var x = null;
    
    //IE7,Firefox, Safari
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    
    //IE6
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        // IE5
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            x = null;
        }
    }
    return x;
}

//--------------------------------------------------------------------------------
//    テロップ表示関数
//--------------------------------------------------------------------------------
function terop(msgs_str){
    
    var msgs = '<nobr>' + msgs_str + '</nobr>';
    
    var speed = 30;   //移動スピード
    //var speed = 1;   //移動スピード
    
    var base = document.getElementById("terop");
    
    //var windowWidth;
    //if (window.document.body.clientWidth) {
    //    windowWidth = window.document.body.clientWidth;
    //else
    //    windowWidth = 0;
    //}
    windowWidth = window.document.body.clientWidth;
    
    //表示文字のposition属性が空もしくはstaticの時はrelativeにする
    //後で位置調整するため
    if(base.style.position == "" || base.style.position == "static"){
      base.style.position = "relative";
    }
    
    base.style.overflow = "hidden";
    
    var msg = document.createElement("div");
    
    base.appendChild(msg);
    
    msg.style.position = "relative";
    msg.style.left = (base.offsetWidth + 1) + "px";
    var i = 0;
    var move = function(){
        if(parseInt(msg.style.left) > base.offsetWidth){
            msg.innerHTML = msgs;
        }
        msg.style.left = (parseInt(msg.style.left) - 1) + "px";
        
        //ウィンドウ左まで到達した場合
        if(parseInt(msg.style.left) < -(msg.offsetWidth)){
            //msg.style.left = (base.offsetWidth + 1) + "px";
            if (windowWidth > 0) {
                msg.style.left = windowWidth + "px";
            } else {
                msg.style.left = (base.offsetWidth + 1) + "px";
            }
        }
        
        //文字上の間隔
        msg.style.top = '5px';
        i++;
        if(i >= msgs.length) i = 0;
        setTimeout(move, speed);
    };
    setTimeout(move, speed);
}


