//-----------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (C) Copyright IBM Corp. 2009 All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
//-----------------------------------------------------------------

/*
ajax Helper 
* need jquery.js , jquery.form.js ,jquery.blockUI.js
*/
 
 /**
* JSP_AJAX_DIV
* URL:URL
* DIV:DIV ID
*/

function toPage(url,div){
$.blockUI(
{ css: {
 	width:'10%',
    top: '40%',
    left: '45%', 
    border: 'none', 
    backgroundColor: '#fff', 
    opacity: .5, 
    color: '#fff' 
  },
  overlayCSS: { 
  backgroundColor: '#fff',
  opacity: .5
  },
  message: '<img src="/wcsstore/ConsumerDirectStorefrontAssetStore/images/images_sn/ajax_loading.gif" />' 
  }
); 
	$.ajax({
	  url:url,
	  cache: false,
	  async: false,
	  complete: function() { 
        // unblock when remote call returns 
        $.unblockUI(); 
        $(".blockUI").fadeOut("slow");
	    },
	  success: function(html){
	    $("#"+div).html(html);
	  }
	}); 
}


function toPageNoBlock(url,div){

	$.ajax({
	  url:url,
	  cache: false,
	  async: false,
	  success: function(html){
	  $("#"+div).html(html);
	  }
	}); 
}


/**
* form to ajax 
* div : div Id
* formId: form's Id
*/
function ajaxForm(form,div){
 var options = { 
        target: '#'+div,// target element(s) to be updated with server response
        cache: false,
        async : false,
        type: "POST"
    };
    $('#'+form).submit(function() { 
        $(this).ajaxSubmit(options); 
        return false; 
    });
    $('#'+form).submit();
}



function ajaxForm3(form,div,functionName){
 var options = { 
        target: '#'+div,// target element(s) to be updated with server response
        cache: false,
        async : false,
        type: "POST",
        success:functionName
    };
    $('#'+form).submit(function() { 
        $(this).ajaxSubmit(options); 
        return false; 
    });
    $('#'+form).submit();
}


/*
split Url
*/
function splitUrlForSearch(url,formId){
	var str=url.split("&");
	 for(var i=0;i<str.length;i++){
		  var temp = str[i].split("=");
		  var name = temp[0];
		  var value = temp[1];
		  $("form[id="+formId+"] input[name="+name+"]").val(value);
	 }   
}

function formToUrl(formId){
	return  $('#'+formId).attr('action')+'?'+$('#'+formId).serialize(); 
}
/*
url是传入参数的,格式形如pageNum=1&pageSize=40这样的字符串,在splitUrlForSearch(url)这个方法中,会修改页面上ID为pageNum的input框的value为1,以此类推.
formId就是放我们存参数的那个form 的ID
div就是存放取出的页面DIV的ID
*/
function showPage(url,formId,div){
	splitUrlForSearch(url,formId);
	var formUrl= formToUrl(formId);
	toPage(formUrl,div);
}

function showPageNoBlock(url,formId,div){
	splitUrlForSearch(url,formId);
	var formUrl= formToUrl(formId);
	toPageNoBlock(formUrl,div);
}

function ajaxForm2(formId,divId){
	var formUrl= formToUrl(formId);
//	alert("formURL:"+formUrl);
	toPageNoBlock(formUrl,divId);
}




function toggleOneClass(doc,docSelect,class1,class2){
   $(docSelect).each(function(i){
	   $(this).attr('class',class1);
	});
   $(doc).attr('class',class2); 
}

function ajaxForm4(formId,divId,count){
	var formUrl= formToUrl(formId);
	toPageNoBlock4(formUrl,divId,count);
}

function toPageNoBlock4(url,div,count){
url=url+'&count='+count;
	$.ajax({
	  url:url,
	  cache: false,
	  async: false,
	  success: function(html){
	    $("#"+div).html(html);
	  }
	}); 
}


function ajaxForm5(formId,divId,id){
	var formUrl= formToUrl(formId);
	
	toPageNoBlocking1(formUrl,divId,id);
	//mytest(formUrl,divId,id);
}


//add by zhouyunfei 
//说明：
//url:局部刷新指向的url;   div:页面被刷部分div的Id;   id:进度条所在div的Id
  
function toPageNoBlocking(url,div,id)
{

AddcardOut1(id,'javascript:void(0)');
$.post(url,function(data){
 $("#"+div).html(data); 
 hidecardOut1(id);

 });
}


function toPageNoBlocking1(url,div,id)
{
 hidecardOut('cardUserOut');
 AddcardOut1(id,'javascript:void(0)');
$.post(url,function(data){
 $("#"+div).html(data); 
 hidecardOut1(id); 
 if($('#couponerror').text() != "")
 {
 	AddscardOut('cardUserOut','javascript:void(0)')
 }
 });
}


function hidecardOut1(o){
	var h=$("#"+o).height();
	var w=$("#"+o).width();
	$("#"+o).animate({height:0,width:0,left:$("#"+o).attr("l")+"px",top:$("#"+o).attr("t")+"px"},300,function(){
		$("#"+o).hide();																										  
		$("#"+o).height("auto");																								  
		$("#"+o).width(w);																							  
	});
	
	$("#"+o).parent().css('z-index',0);
	var addId1 = document.getElementById('bigdiv1');
	$('#bigdiv1').animate({opacity:0},300,function(){
		document.body.removeChild(addId1);										 
	});	
}

function hidecardOut(o){
	var h=$("#"+o).height();
	var w=$("#"+o).width();
	$("#"+o).animate({height:0,width:0,left:$("#"+o).attr("l")+"px",top:$("#"+o).attr("t")+"px"},300,function(){
		$("#"+o).hide();																										  
		$("#"+o).height("auto");																								  
		$("#"+o).width(w);																							  
	});
	$("#"+o).parent().css('z-index',0);
	var addId = document.getElementById('bigdiv');
	$('#bigdiv').animate({opacity:0},300,function(){
		document.body.removeChild(addId);											 
	});	
}

function AddcardOut(showId,o){
	var width = document.documentElement.clientWidth - 3;
	if( window.screen.availHeight < document.body.scrollHeight ){
		 height = document.body.scrollHeight;
	 }else{
		 height = window.screen.availHeight;
	 }
	var newobj = document.createElement('div');
	var p=$(o).offset();
	var top=p.top;
	var left=p.left;
	var h=$("#"+showId).height();
	var w=$("#"+showId).width();
	
	newobj.setAttribute('id','bigdiv');
	newobj.style.position = "absolute" ;
	newobj.style.left = "0px" ;	
	newobj.style.top = "0px" ;	
	newobj.style.background = "#666666" ;
	newobj.style.width = width + "px" ;
	newobj.style.height = height +"px" ;
	newobj.style.opacity = 0 ;
	newobj.style.filter="alpha(opacity=0)";
	newobj.style.zIndex = 10001; 
	/*newobj.innerHTML = '<iframe id="ss" name="ss" style="position:absolute;top:-5px;left:0;border:none;width:100%;height:100%;background:#666666;filter:alpha(opacity=0);" ></iframe>';
	newobj.onclick=function(){
		hidecardOut(showId);
	}
*/
	document.body.appendChild(newobj);	
	$("#"+showId).css({"top":top+"px","left":left+"px",height:0,width:0});
    $("#"+showId).attr("t",top);
	$("#"+showId).attr("l",left);
	$('#bigdiv').animate({opacity:0.65},300,function(){
		$("#"+showId).animate({height:h+"px",width:w+"px",top:(document.documentElement.clientHeight-h)/2+$(window).scrollTop()+"px",left:(width-w)/2+"px"},function(){
			$("#"+showId).height("auto");																								  
			$("#"+showId).width(w);																							  
		})	
	});	
	$("#"+showId).css('z-index','10002');
}


function AddcardOut1(showId,o){
	var width = document.documentElement.clientWidth - 3;
	if( window.screen.availHeight < document.body.scrollHeight ){
		 height = document.body.scrollHeight;
	 }else{
		 height = window.screen.availHeight;
	 }
	var newobj = document.createElement('div');
	var p=$(o).offset();
	var top=p.top;
	var left=p.left;
	var h=$("#"+showId).height();
	var w=$("#"+showId).width();
	
	newobj.setAttribute('id','bigdiv1');
	newobj.style.position = "absolute" ;
	newobj.style.left = "0px" ;	
	newobj.style.top = "0px" ;	
	newobj.style.background = "#666666" ;
	newobj.style.width = width + "px" ;
	newobj.style.height = height +"px" ;
	newobj.style.opacity = 0 ;
	newobj.style.filter="alpha(opacity=0)";
	newobj.style.zIndex = 10001; 
	//newobj.innerHTML = '<iframe src="test.html" id="ss" name="ss" style="position:absolute;top:-5px;left:0;border:none;width:100%;height:100%;background:#666666;filter:alpha(opacity=0);" ></iframe>';

	document.body.appendChild(newobj);	
	$("#"+showId).css({"top":top+"px","left":left+"px",height:0,width:0});
    $("#"+showId).attr("t",top);
	$("#"+showId).attr("l",left);
	$('#bigdiv1').animate({opacity:0.65},300,function(){
		$("#"+showId).animate({height:h+"px",width:w+"px",top:(document.documentElement.clientHeight-h)/2+$(window).scrollTop()+"px",left:(width-w)/2+"px"},function(){
			$("#"+showId).height("auto");																								  
			$("#"+showId).width(w);																							  
		})	
	});	
	$("#"+showId).css('z-index','10002');
}







