$(document).ready(function() {	

	//select all the a tag with name equal to modal
	$('a[name="modal"]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn("fast");	
		$('#mask').fadeTo("fast",0.5);	
	
	
	
		//Get the window height and width
		
		var winW = $(window).width();
		              
		//Set the popup window to center of parent container
		$(id).css({top: $(this).parents('#shipvantageWrap').offset().top - 10});
		$(id).css('left', winW/2-$(id).width()/2);

		//transition effect
		$('body').append($(id).show(""));
		
		
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask').hide();
		$('.window').hide();
	});		
	
		$('.window .cancel').click(function(){
      $(this).replaceWith("");
    });
	
});
function fnRefereshDiv()
{
		  var mainDiv= document.getElementById("shipvantageInfo");
		  var subDiv1=document.getElementById("cancelORpurchaseBtn");
          var subDiv2=document.getElementById("SVMemStatus");
          mainDiv.removeChild(subDiv1);
          mainDiv.removeChild(subDiv2);
           	
          var refundStatus='';
          var svMembershipUsed =document.getElementById("svMembershipUsed").value;
          var newDiv = document.createElement('div');
          newDiv.setAttribute('id','SVMemStatus');
          newDiv.innerHTML = '<p>You have turned off autorenew. When your membership expires, you can purchase a new ShipVantage membership that will renew automatically every 12 months.</p>';
          mainDiv.appendChild(newDiv);
           	
          var newDivButton=document.createElement('div');
          newDivButton.setAttribute('id','cancelORpurchaseBtn');
          
          
          
          if(svMembershipUsed == "true")
          {
            
          	refundStatus = 'no';
          	newDivButton.innerHTML = "<div id=\"cancelMembershipBtn\"><a href=\"#cancelNoRefund\" name=\"modal\" onclick=\"enableCancelPopUp('"+refundStatus+"');\"></a></div>";
		  }
		  else 
		  {
		 
		  	refundStatus =  'yes';
		  	newDivButton.innerHTML = "<div id=\"cancelMembershipBtn\"><a href=\"#cancellWithRefund\" name=\"modal\" onclick=\"enableCancelPopUp('"+refundStatus+"');\"></a></div>";
		  }
		 
          mainDiv.appendChild(newDivButton);


}
function enableCancelPopUp(refundStatus)
{
document.getElementById("mask").style.display="block";
	if(refundStatus == 'yes' || refundStatus == 'Yes')
	{
		document.getElementById("cancellWithRefund").style.display="block";
		
	}
	else
	{
		document.getElementById("cancelNoRefund").style.display="block";
	}
	
}
function cancelAutoRenewal()
{
	document.getElementById("AutoRenewOff").style.display="none";
	url='SVActions?storeId='+storeId+'&catalogId='+catalogId+'&langId='+langId+'&action=RENEWAL_FLAGOFF';
	
	$.ajax({
		url: url,
		 success: function(value){
         	
         	     document.getElementById("AutoRenewalResponse").innerHTML=value;   
           		 var code=document.getElementById("Code").value;
        
            if(code == "Success")
            {
         
            	
            	document.getElementById("cancelAutoRenew").style.display="block";
            	document.getElementById("AutoRenewOff").style.display="none";
            	
         
            }
            else 
            {
           
            	document.getElementById("cancelAutoRenewError").style.display="block";
            	document.getElementById("AutoRenewOff").style.display="none";
            	
            }
            
                }
		});
	
}

function cancelSVMembership(refundRequired)
{
	document.getElementById("FreeTrialCancell").style.display="none";
	document.getElementById("cancelNoRefund").style.display="none";
	document.getElementById("cancellWithRefund").style.display="none";
	
	url='SVActions?storeId='+storeId+'&catalogId='+catalogId+'&langId='+langId+'&action=CANCEL_MEMBERSHIP';
	
	$.ajax({
		url: url,
		 success: function(value){
		          	
         	     document.getElementById("AutoRenewalResponse").innerHTML=value;   
            	 var code=document.getElementById("Code").value;
            	 var refundStatus = document.getElementById("refundStatus").value;
				 var newHTML = "";
            if(code == "Success")
            {
            	var membershipStatus = document.getElementById("svMembershipStatus").value;
            	var expiryDate = document.getElementById("svExpiryDate").value;
            	var memberShipType = document.getElementById("svMemberShipType").value;
            	
            	
            	if('no'==refundRequired)
            	{   
            		if(memberShipType != 'ANNUAL MEMBERSHIP'){
            		
	            		document.getElementById("FreeTrialCancell").style.display="none";
		            	document.getElementById("cancelNoRefund").style.display="none";
		            	document.getElementById("cancelShipVan").style.display="block";
		            	document.getElementById("svMembershipStatus").value = "Cancelled";    
		            }
		            else{
		            	document.getElementById("FreeTrialCancell").style.display="none"
		            	document.getElementById("cancelNoRefund").style.display="none";
		            	document.getElementById("cancelShipVanNoRefund").style.display="block";
		            	document.getElementById("svMembershipStatus").value = "Cancelled";    
		            }
	            }
	            else 
	            {
		           	document.getElementById("cancellWithRefund").style.display="none";
		            document.getElementById("cancelShipVan").style.display="block";
		            document.getElementById("svMembershipStatus").value = "Cancelled";   
	            }
	            
	            if(refundStatus=="No")
	            {          
	            	if(memberShipType == 'ANNUAL MEMBERSHIP')
	            	{            	
            		newHTML=newHTML+"<div id=\"logoWrap\">"+
							"<div id=\"shipvantageLogo\"></div><div id=\"shipvantageCancelled\"></div></div>"+
							"<div id=\"shipvantageInfo\">"+
							"<div><span>Type:</span><span> Annual Membership</span></div>"+
							"<div id=\"svExpire\">Expiration date:"+expiryDate+"</div>"+
							"<div id=\"SVMemStatus\"><p>"+
							"You've used your ShipVantage membership, you won't receive a refund of your annual membership"+
							" fee, but you can keep using your membership until it expires.</p></div>"+
							"</div>";      	
					document.getElementById("shipvantageWrap").innerHTML = newHTML;	           		
            		}
            		else{  	
            		newHTML=newHTML+"<div id=\"logoWrap\">"+
							"<div id=\"shipvantageLogo\"></div><div id=\"shipvantageCancelled\"></div></div>"+
							"<div id=\"shipvantageInfo\">"+
							"<div><span>Type:</span><span> 30-day free trial membership </span></div>"+
							"<div id=\"SVMemStatus\"><p>"+
							"To start saving on shipping again, please purchase a new ShipVantage annual membership.</p></div>"+
							"<div id=\"cancelORpurchaseBtn\"><div id=\"purchaselMembershipBtn\">"+
							"<a href=\"dap_"+storeId+"_"+catalogId+"_DAP_ShipVantage+Preview\""+
							" onclick=\"trackClickAction(this,'ShipVantage \> Membership Expired, Join Again from My Profile','ShipVantage \> Membership Expired, Join Again from My Profile');\">"+
							"</a></div></div>"+		
							"</div>"; 
					document.getElementById("shipvantageWrap").innerHTML = newHTML;	           		
            	
            		}
            	}
            	else if(memberShipType == 'ANNUAL MEMBERSHIP' && refundStatus=="Yes"){
            		newHTML=newHTML+"<div id=\"logoWrap\">"+
							"<div id=\"shipvantageLogo\"></div><div id=\"shipvantageCancelled\"></div></div>"+
							"<div id=\"shipvantageInfo\">"+
							"<div><span>Type:</span><span> Annual Membership </span></div>"+
							"<div id=\"SVMemStatus\"><p>"+
							"To start saving on shipping again, please purchase a new ShipVantage annual membership.</p></div>"+
							"<div id=\"cancelORpurchaseBtn\"><div id=\"purchaselMembershipBtn\">"+
							"<a href=\"dap_"+storeId+"_"+catalogId+"_DAP_ShipVantage+Preview\""+
							" onclick=\"trackClickAction(this,'ShipVantage \> Membership Expired, Join Again from My Profile','ShipVantage \> Membership Expired, Join Again from My Profile');\">"+
							"</a></div></div>"+									
							"</div>"; 
					document.getElementById("shipvantageWrap").innerHTML = newHTML;
            	}
            
            }
            else
            {
            	if('no'==refundRequired)
            	{         
            	document.getElementById("FreeTrialCancell").style.display="none"
            	document.getElementById("cancelNoRefund").style.display="none";
            	document.getElementById("cancelAutoRenewError").style.display="block";            	
	            }
	            else 
	            {
	           	document.getElementById("cancellWithRefund").style.display="none";
	            document.getElementById("cancelAutoRenewError").style.display="block";  
	            }
            }
            }
		});
	
}

function trackClickAction(obj, prop12, linkType) {
    if(typeof s != 'undefined') {
    s.linkTrackVars='prop12,prop10,channel,prop1,prop2,prop3,prop27,prop28';
    s.prop12=prop12;
    s.tl(true, 'o', linkType);
}
}

