/**
 * TinyFrameWork Standard JavaScript-Library
 *
 * @Copyright (C)2007 SystemOcean Inc.
 */

/**
 * <form> Submit
 */
function cmd_submit( strMsg, objForm )
{
    if( strMsg == '' )
    {
        objForm.submit();
        return true;
    }
    else
    {
        if( confirm( strMsg ) )
        {
            objForm.submit();
            return true;
        }
        return false;
    }
}

/**
 * <form> Submit with PID
 */
function cmd_submitPID( strMsg, objForm , strPID )
{
    actionString = objForm.action;
    objForm.action = actionString + "?PID=" + strPID;
    returnValue = cmd_submit( strMsg, objForm );
    objForm.action = actionString;
    
    return returnValue;
}

/**
 * <form> Submit with PID and Option
 */
function cmd_submitPIDwithOpt( strMsg, objForm , strPID, strOpt )
{
    actionString = objForm.action;
    objForm.action = actionString + "?PID=" + strPID + "&" + strOpt;
    returnValue = cmd_submit( strMsg, objForm );
    objForm.action = actionString;
    
    return returnValue;
}

/**
 * Link with confirm message
 */
function cmd_confirm( strMsg, strURL )
{
    if( confirm( strMsg ) )
    {
        location.href= strURL;
        return true;
    }
    return false;
}

/**
 * Open Sub Window
 */
function openSubWindow( strWindowName, strURL,  lngWidth, lngHeight )
{
    subwindow = window.open(    strURL ,
                                strWindowName ,
                                "width=" + lngWidth + ",height=" + lngHeight + ",scrollbars=yes,status=no,menubar=no,alwaysRaised=yes,dependent=yes,resizable=no" );
    subwindow.focus();
    return true;
}

/**
 * Display Status Message
 */
function displayStatusMessage( strMessage )
{
    window.status = strMessage;
}

/**
 * create XMLHttpRequest Object
 **/
function createHttpRequest()
{
    if( window.ActiveXObject )
    {
        // for Windows Internet Explorer
        try
        {
            return new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch( e )
        {
            try
            {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch( e )
            {
                return null;
            }
        }
    }
    else if( window.XMLHttpRequest )
    {
        // for not Internet Explorer
        return new XMLHttpRequest();
    }
    else
    {
        // for other
        return null;
    }
}

/**
 * get Http Request Data
 *
 * @param idName        string  div tag id name
 * @param requestData   string  http request data
 */
function getHttpData( idName , requestData )
{
    // create Http Request Object
    req = createHttpRequest();
    // procedure on Read Data
    req.onreadystatechange = function()
    {
        if( req.readyState == 4 )
        {
            // on Read Data
            var readData = req.responseText;
            // output to div tag
            document.getElementById( idName ).innerHTML = decodeURIComponent( readData );
        }
    }
    // Initiate
    req.open( "POST", requestData );
    // Transite
    req.send("");
}

/**
 * URL encoding
 *
 * @param data      string  URL encoding source data
 */
function uriEncode(data)
{
    if(data != "" )
    {
        var encdata = '';
        var datas = data.split( '&' );

        for( i = 1 ; i < datas.length ; i++ )
        {
            var dataq = datas[i].split('=');
            if( encdata != "" )
            {
                encdata += '&';
            }
            encdata += encodeURIComponent( dataq[0] )
                     + '=' + encodeURIComponent( dataq[1] );
        }
    }
    else
    {
        encdata = "";
    }

    return encdata;
}


/**
 * お気に入りリスト取得
 *
 * @param idName   string  出力先DIVのID
 * @param dispNum  int     表示する商品数
 */
function getRecommendProducts( idName , dispNum )
{
    getHttpData( idName, "/?PID=RCMND&DISPNUM=" + String(dispNum) );
}
