/*
 * getPage
 *   指定URLを読み込んで指定のidの要素としてセットする。
 *
 * 使用例
 *   getPage( { 'param' : "type=template&run=true" , 'url' : 'http://hoge.com/search.php' , 'method' : 'GET' , 'drawarea' : 'search_result' })；
 *
 *
 */
function getPage( vars )  //vars:url,method,param,drawarea
{
    jQuery.ajax({
      url : vars['url'] ,
      type : vars['method'] ? vars['method'] : 'POST',
      dataType : "plane/text",
	  data : vars['param'] ,
      success : function(responseText){ $('#'+vars['drawarea']).html(responseText) } ,
      error   : function(xml, status, e){ $('#'+vars['drawarea']).html("error:通信エラーが発生しました。"); }
    });

}

function setData( vars ){
    if( typeof vars['param'] == "object" )
        vars['param']['post'] = "set&js=true";
    else if(vars['param'])
        vars['param'] += "&post=set&js=true";
    else
        vars['param'] = "post=set&js=true";
    
    var data = vars['data'] ? vars['data'] : undefined;
    
    jQuery.ajax({
      url : vars['url'] ? vars['url'] : 'info_change.php' ,
      type : vars['method'] ? vars['method'] : 'POST',
      dataType : "json",
	  data : vars['param'] ,
      success : function(res){ responceAction( data , res ); } ,
      error   : function(xml, status, e){ $('#message_area').html("error:通信エラーが発生しました。"); }
    });
}

function getData( vars )
{
    if( typeof vars['param'] == "object" )
        vars['param']['post'] = "select&js=true";
    else if(vars['param'])
        vars['param'] += "&post=select&js=true";
    else
        vars['param'] = "post=select&js=true";
    
    var data = vars['data'] ? vars['data'] : undefined;
    
    jQuery.ajax({
      url : vars['url'] ? vars['url'] : 'info_change.php' ,
      type : vars['method'] ? vars['method'] : 'POST',
      dataType : "json",
	  data : vars['param'] ,
      success : function(res){ responceAction( data , res ); } ,
      error   : function(xml, status, e){ $('#message_area').html("error:通信エラーが発生しました。"); }
    });
}

function deleteData( vars )
{
    if( typeof vars['param'] == "object" )
        vars['param']['post'] = "delete&js=true";
    else if(vars['param'])
        vars['param'] += "&post=delete&js=true";
    else
        vars['param'] = "post=delete&js=true";
    
    var data = vars['data'] ? vars['data'] : undefined;
    
    jQuery.ajax({
      url : vars['url'] ? vars['url'] : 'info_change.php' ,
      type : vars['method'] ? vars['method'] : 'POST',
      dataType : "json",
	  data : vars['param'] ,
      success : function(res){ responceAction( data , res ); } ,
      error   : function(xml, status, e){$('#message_area').html("error:エラーが発生しました。:"+status); }
    });
}

function responceAction(data,res){
    if(!res['success']){
        data = data['error'];
    }else{
        data = data['success'];
    }
    
    switch(data['action']){
        default:
        case 'draw':
            if(data['msg'])
                $('#'+data['drawarea'] ).html( data['msg'] );
            else
                $('#'+data['drawarea'] ).html( res['msg'] );
            break;
        case 'alert':
            if(data['msg'])
                alert( data['msg'] );
            else
                alert( res['msg'] );
            break;
        case 'reload':
            window.location.reload(true);
            break;
        case 'location':
            window.location = data['url'];
            break;
        case 'func':
            data['callback']();
            break;
    }

}



function sortCheck ( id, comp, sort_pal ) {
   sortData( {
	  "param" : "&id="+id+"&comp="+comp+"&sort=" + sort_pal,
	  "data" : 
		{
		  "success" : { "action" : "reload" }
		 ,"error" : { "alert" : "draw" ,"msg" : "停止に失敗しました。" }
		}
   } );
}

function sortData( vars )
{
    if( typeof vars['param'] == "object" )
        vars['param']['post'] = "sort&js=true";
    else if(vars['param'])
        vars['param'] += "&post=sort&js=true";
    else
        vars['param'] = "post=sort&js=true";
    
    var data = vars['data'] ? vars['data'] : undefined;
	
    jQuery.ajax({
      url : vars['url'] ? vars['url'] : 'info_change.php' ,
      type : vars['method'] ? vars['method'] : 'POST',
      dataType : "json",
	  data : vars['param'] ,
      success : function(res){ responceAction( data , res ); } ,
      error   : function(xml, status, e){$('#message_area').html("error:エラーが発生しました。:"+status); }
    });
}

function checkCancel ( flg, id ) 
{
	if(flg.checked)
	{
		var list =	document.getElementById(id).getElementsByTagName("input");
		for (var i = 0; i < list.length; i++) 
		{
			if (list[i].getAttribute("type") == "checkbox") { list[i].checked = false; }
		}
	}
}

// アラートのステータスを変更
function alertCheck ( type, id ) {
   alertData( {
	  "param" : "&type="+type+"&id="+id,
	  "data" : 
		{
		  "success" : { "action" : "reload" }
		 ,"error" : { "alert" : "draw" ,"msg" : "停止に失敗しました。" }
		}
   } );
}

function alertData( vars )
{
    if( typeof vars['param'] == "object" )
        vars['param']['post'] = "alert&js=true";
    else if(vars['param'])
        vars['param'] += "&post=alert&js=true";
    else
        vars['param'] = "post=alert&js=true";
    
    var data = vars['data'] ? vars['data'] : undefined;
	
    jQuery.ajax({
      url : vars['url'] ? vars['url'] : 'info_change.php' ,
      type : vars['method'] ? vars['method'] : 'POST',
      dataType : "json",
	  data : vars['param'] ,
      success : function(res){ responceAction( data , res ); } ,
      error   : function(xml, status, e){$('#message_area').html("error:エラーが発生しました。:"+status); }
    });
}






function attSortCheck ( user_id, att_id, area, sort_pal ) {
   attSortData( {
	  "param" : "&user_id="+user_id+"&att_id="+att_id+"&area="+area+"&sort=" + sort_pal,
	  "data" : 
		{
		  "success" : { "action" : "reload" }
		 ,"error" : { "alert" : "draw" ,"msg" : "停止に失敗しました。" }
		}
   } );
}

function attSortData( vars )
{
    if( typeof vars['param'] == "object" )
        vars['param']['post'] = "att_sort&js=true";
    else if(vars['param'])
        vars['param'] += "&post=att_sort&js=true";
    else
        vars['param'] = "post=att_sort&js=true";
    
    var data = vars['data'] ? vars['data'] : undefined;
	
    jQuery.ajax({
      url : vars['url'] ? vars['url'] : 'info_change.php' ,
      type : vars['method'] ? vars['method'] : 'POST',
      dataType : "json",
	  data : vars['param'] ,
      success : function(res){ responceAction( data , res ); } ,
      error   : function(xml, status, e){$('#message_area').html("error:エラーが発生しました。:"+status); }
    });
}

function careChange ( user_id, mode ) {
	jQuery.ajax({
      url : 'info_change.php' ,
      type : 'POST',
      dataType : "json",
	  data : "&post=care&js=true&user_id="+user_id+"&mode="+mode,
      success : function(res){ window.location.reload(true); } ,
      error   : function(xml, status, e){ $('#message_area').html("error:エラーが発生しました。:"+status); }
    });
}

function cookieDel () {
	rwAuthInfo.CookieWrite(rwAuthInfo.conf.kwordPass, "", rwAuthInfo.conf.savePeriod);
	rwAuthInfo.CookieWrite(rwAuthInfo.conf.kwordId, "", rwAuthInfo.conf.savePeriod);
}

/*
使用例

function deleteCheck ( type, id ) {
  var flag = confirm ( "この注目商品を削除します。");
  if(flag) {
	   deleteData( {
          "param" : "&type=" + type + "&id=" + id ,
          "data" : 
            {
              "success" : { "action" : "draw" ,"drawarea" : "message_area" , "msg" : "削除に成功しました。" }
             ,"error" : { "action" : "draw" ,"drawarea" : "message_area" , "msg" : "削除に失敗しました。" }
            }
       } );
  }
}

deleteCheck( 'template', 'T0001' );

*/

