/* JavaScript Document*/
var xcPopUp = function(el){ 
 	$(el).unbind('click').click(function(e){
			// Remove any previous instances of the layer
			$('#tempWrap').remove();
			$('#tempContent').remove();
			// This is the layer wrap
			var templayer = '<div id="tempWrap" class="layerWrap"></div> \
							 <div id="tempContent" class="layerContent"> \
								<p class="layerBody"></p> \
							 </div>';
			$('body').append(templayer);
			// Load in the dynamic page for the popup
			$('#tempContent p.layerBody').load('MyGoferAffiliate', null, function(){
				// Set the wrapper height
				var preAnimateHeight = $('.layerContent').height();
				//$('.layerWrap').height(preAnimateHeight + 10);
				$('.layerWrap').css('height', (preAnimateHeight + 10) + "px");
				// Show the layer
				$('#tempWrap').show().css('top', e.pageY).css('left', (e.pageX - 310));
				$('#tempContent').show().css('top', (e.pageY + 5)).css('left', (e.pageX - 305));			
				$('a.closeWin').click(function(){
					$('#tempWrap').remove();
					$('#tempContent').remove();
				});
			});
		});

}


$(document).ready(function(){
						   
//dashboard preffered stores			
$("#dashboard .pod #preferred_stores_content .preferred_stores_box:last-child").addClass('last');
//dashboard gofer affiliation popup
xcPopUp('#goferCheck')

/*affiliations*/
$("#affiliations .affiliation_box:nth-child(5)").addClass('clearleft');
 

/*opens and closes the motto*/
    $("div.mottotext").click(function(){
	$("#motto div.expanded").slideToggle("fast");
	});
/*expands the left nav*/
    $("div.menu_universal_profile div > h3 span.openclose").click(function(){
                    var selectedNum=0;
                    //$(this).toggleClass("open");
                    $(this.parentNode).toggleClass("open");
                    $(this.parentNode.parentNode).children("ul:eq(0)").slideToggle("fast");
                    $(this.parentNode.parentNode).children("span.displayAll").toggle();
                    if($(this.parentNode.parentNode).children("ul:eq(0)").children().length<=10){ $(this.parentNode.parentNode).children("span.displayAll").hide() }
                    return false;
 	 });			 
 
/* open orders table removes the last right white border from the header row*/
 $("table.order_center_table td:last-child").css("border-right", "0"); 
 $("table.order_center_table th:last-child").css("border-right", "0");
/* open orders table removes the last row bottom border*/
/* $("table.hideLastCelBrdr tr:last-child td").css("border-bottom", "0");
*//* calendar styles*/
 $("table#month td:first-child").css("border-left", "#ffffff"); 
  $("a.printPage").click(function(){window.print();});
 
  

/*  tabs var tabContainers = $('div.tabs > div');
    
    $('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.hide().filter(this.hash).show();
        
    $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        
        return false;
    }).filter(':first').click();*/


/*styles the previous and next tabs*/

$("#tabs_wrapper ul.profile_tabs li:first-child").css("border-left", "0");
/*$("#tabs_wrapper ul.profile_tabs li:last-child").addClass('last'); 
$('#tabs_wrapper ul.profile_tabs li.selected').prev().addClass('prev');         
$('#tabs_wrapper ul.profile_tabs li.selected').next().addClass('next'); */
/* tabs function, also tab styles	
var tabContainers = $('#tabs_wrapper > div.tab_contents_container');	
$('#tabs_wrapper ul.profile_tabs li a').click(function () {
	 tabContainers.hide().filter(this.hash).show();
        $('#tabs_wrapper ul.profile_tabs li').removeClass('selected').removeClass('prev').removeClass('next');
        $(this.parentNode).addClass('selected');
        $(this.parentNode).prev().addClass('prev'); 
		$(this.parentNode).next().addClass('next');
        return false;
    });
  */	
  
  //account info
  	$('a.changePass').toggle(function(){
	  		$(this).addClass('open');
	  		$('#standardButtons').hide();
	  		$('.section.password').show();
	},function(){
		$(this).removeClass('open');
		$('#standardButtons').show();
		$('.section.password').hide();
	});  
	
  //address book	
	/*$('#addressOptions input').click(function(){
		$('#addressOptions strong').html('Make Primary');
		$(this).parent().find('strong').html('Primary Address');
	});*/ 
	});
			
			

 



$(function(){
	//Preferred Sotres Selection for the P.S. page in Profile...
	$('div.preferStore input[type="radio"]').click(function(){
		var _glob = $(this).parent().parent();			
		if($('span.Designated').text()=="Set Designated Store as Primary Preferred Store"){	
			$(this).parent().parent().parent().find('span').text('Select as My Primary Preferred Store');			
			$('span.Designated').text('Designated/Primary Preferred Store');			
			$(this).parent().parent().parent().find('div.preferStore').removeClass('primary');
			$(this).parent().parent().parent().find('div.preferStore').addClass('secondary');
			$(this).parent().parent().removeClass('secondary');
			$(this).parent().parent().addClass('primary');			
		}
		if($(this).parent().find('span').text()=="Select as My Primary Preferred Store"){
				$(this).parent().parent().parent().find('div.preferStore').removeClass('primary');
				$(this).parent().parent().parent().find('div.preferStore').addClass('secondary');
				$(this).parent().parent().removeClass('secondary');
				$(this).parent().parent().addClass('primary');	
				$(this).parent().parent().parent().find('span').text('Select as My Primary Preferred Store');										
				$(this).parent().find('span').text('Primary Preferred Store');					
				$('span.Designated').text('Set Designated Store as Primary Preferred Store');
		}
	});
	
	$('a.modStores').click(function(e){
		 
		
		var _global = $(this);
		//var newE = { pageX: e.pageX - 125, pageY: e.pageY - 75 };
		var newE = { pageX: _global.offset().left - 50, pageY: _global.offset().top - 75 };
		var settings = {filename:dynamicPopupPath2,contentname: '#delStorePop',jsonFlag: false, dynamicContent:true, windowWidth: '220px',windowHeight: 'auto'};
		
		dynpop.ini(newE, settings);
	});
	 
});
function removeStore(removedStoreUnitnumber,isPrimary){
	
	document.getElementById("removedStoreUnitNumber").value = removedStoreUnitnumber;	
	document.getElementById("isPrimaryStore").value = isPrimary;	
	document.getElementById("key").value='DELETE';
	
	document.getElementById("PreferredStoresForm").submit();
}
function changePrimaryStore(newPrimaryStoreId){
	  var profilePageOpt = {
			    method: 'GET',
			    asynchronous: true,
			    postBody: '',
		
			       //Handle successful response
			    onSuccess: function(t) {
					//alert("success");			    
			    },
		    	
		        // Handle other errors
		        onFailure: function(t) {
		        	//alert("failure");
		        }
  			  };
  	  var urlString=''; 
	  var key='UPDATE';
	  urlString=urlString+"&key"+"="+key;
	  urlString=urlString+"&newPrimaryStoreId"+"="+newPrimaryStoreId;
	  var url = "PreferredStoresCmd?"+urlString;
  	 new searsAjax(url, profilePageOpt);    
}
function savePickupInfo(){	
	var pickUpAddressId = document.getElementById("autoRefill").value;
	var address_Id = pickUpAddressId;
	var location = "";
	var deliveryType = "DeliveryPickup";	
	if(pickUpAddressId !="noSelection"){
	    if(pickUpAddressId=="Desk Delivery"){
		    var deskDeliveryId = document.getElementById("deskDeliveryId").value;
		    address_Id = deskDeliveryId;
		    location = document.getElementById("deskLocation").value;
		    if($.trim(location)==''){
		    	document.getElementById("expressResult").innerHTML = "Desk location field is empty.Please enter a valid value for desk location.";
		    	return false;
		    }
		    deliveryType = "DeskDelivery";
	    }
	    if(pickUpAddressId=="Home Delivery"){
	    	address_Id = document.getElementById("homeAddressId").value;
	    	deliveryType = "HomeDelivery";
	    }
	 }
	 else{
		address_Id = pickUpAddressId;
	 	deliveryType = pickUpAddressId;
	 }
		var URLstring = "MyInfoUpdate?storeId=10152&catalogId=10108&preferred=Y&URL=LogonStatusView&address_Id="+address_Id+"&deliveryType="+deliveryType+"&location="+location;
		ajaxCall(URLstring,"checkResult");
	
	return false;	
}
function checkResult(response){
	document.getElementById("expressResult").innerHTML = response;
	var result = document.getElementById("successEC").value;
	if(result=="true"){
		document.getElementById("expressResult").innerHTML = "Express checkout settings have been saved.";
	}else{
		document.getElementById("expressResult").innerHTML = "Error occured while saving express checkout settings.";
	}
	return true;
}
