/* 
	Copyright (c) 2010 Dovetail Services
	Catalogue Common JavaScript Goes Here
	
	Initial Code - Version 1 - Tindi June 2010
	
*/
function PopupCenter(pageURL, title, w, h)
{
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}

function PopupCenterPAF(sourcePage, actionPath, addressType)
{
	var pageURL = '/pafAddressPopUpWindow'+'?actionInput='+sourcePage+'&actionPath='+actionPath+'&addressType='+addressType;
	var title = 'PAF';
	var w = 470;
	var h = 300;
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}


function PopulateAddress(sourceWindow, sourceForm)
{
	var targetWindow = sourceWindow;
	var targetOpener = sourceWindow.opener;	
	var targetForm = sourceForm;	
	
	if (targetForm != null && targetWindow != null)
	{
		targetForm.submit();		
	}	
}


function SetPurchasePageDeliveryAddress(sourceWindow)
{	
	var targetOpener = sourceWindow.opener;	

	if (sourceWindow != null)
	{				
		targetOpener.document.addSubscriptionToBasketForm.deliveryCompany.value = sourceWindow.document.hiddenPopUpConfirmation.Company.value;
		targetOpener.document.addSubscriptionToBasketForm.deliveryLine1.value = sourceWindow.document.hiddenPopUpConfirmation.Line1.value;
		targetOpener.document.addSubscriptionToBasketForm.deliveryLine2.value = sourceWindow.document.hiddenPopUpConfirmation.Line2.value;
		targetOpener.document.addSubscriptionToBasketForm.deliveryLine3.value = sourceWindow.document.hiddenPopUpConfirmation.Line3.value;
		targetOpener.document.addSubscriptionToBasketForm.deliveryLine4.value = sourceWindow.document.hiddenPopUpConfirmation.Line4.value;
		targetOpener.document.addSubscriptionToBasketForm.deliveryLine5.value = sourceWindow.document.hiddenPopUpConfirmation.Line5.value;		
		targetOpener.document.addSubscriptionToBasketForm.deliveryPostalCode.value = sourceWindow.document.hiddenPopUpConfirmation.PostalCode.value;

		sourceWindow.close();		
	}
}


function SetCheckoutBillingAddress(sourceWindow)
{	
	var targetOpener = sourceWindow.opener;	
	

	if (sourceWindow != null)		
	{		
			
		targetOpener.document.checkoutDetailsValidatorForm.company.value = sourceWindow.document.hiddenPopUpConfirmation.Company.value;
		targetOpener.document.checkoutDetailsValidatorForm.line1.value = sourceWindow.document.hiddenPopUpConfirmation.Line1.value;		
		targetOpener.document.checkoutDetailsValidatorForm.line2.value = sourceWindow.document.hiddenPopUpConfirmation.Line2.value;
		targetOpener.document.checkoutDetailsValidatorForm.line3.value = sourceWindow.document.hiddenPopUpConfirmation.Line3.value;
		targetOpener.document.checkoutDetailsValidatorForm.line4.value = sourceWindow.document.hiddenPopUpConfirmation.Line4.value;
		targetOpener.document.checkoutDetailsValidatorForm.line5.value = sourceWindow.document.hiddenPopUpConfirmation.Line5.value;		
		targetOpener.document.checkoutDetailsValidatorForm.postalCode.value = sourceWindow.document.hiddenPopUpConfirmation.PostalCode.value;

		sourceWindow.close();		
	}
}

function SetRenewalBillingAddress(sourceWindow)
{	
	var targetOpener = sourceWindow.opener;		
     
	if (sourceWindow != null)		
	{		
			
		targetOpener.document.updateUserDetailsForm.company.value = sourceWindow.document.hiddenPopUpConfirmation.Company.value;
		targetOpener.document.updateUserDetailsForm.line1.value = sourceWindow.document.hiddenPopUpConfirmation.Line1.value;		
		targetOpener.document.updateUserDetailsForm.line2.value = sourceWindow.document.hiddenPopUpConfirmation.Line2.value;
		targetOpener.document.updateUserDetailsForm.line3.value = sourceWindow.document.hiddenPopUpConfirmation.Line3.value;
		targetOpener.document.updateUserDetailsForm.line4.value = sourceWindow.document.hiddenPopUpConfirmation.Line4.value;
		targetOpener.document.updateUserDetailsForm.line5.value = sourceWindow.document.hiddenPopUpConfirmation.Line5.value;		
		targetOpener.document.updateUserDetailsForm.postalCode.value = sourceWindow.document.hiddenPopUpConfirmation.PostalCode.value;

		sourceWindow.close();		
	}
}

//Below global variables and script is used for displaying free gifts
//TODO Convert Java script to JQuery 
var lastFreeGiftDiv = "";
var lastFreeGiftCounter = "";
function showSelectedFreeGift(divName, counter) {
	var giftRadio = 'priceCode' + counter;
	if(document.getElementById(giftRadio).checked){
		document.getElementById("giftId").value = divName;
	}
if(lastFreeGiftCounter == counter){
      if (lastFreeGiftDiv) {
                      document.getElementById(lastFreeGiftDiv).style.display = "none";
      } 
  }
  if (divName && document.getElementById(divName)) {
          var giftDivContent = 'giftDiv' + counter;
          var inputDivs = document.getElementById(giftDivContent).getElementsByTagName('div');
          for (var i = 0; i < inputDivs.length; i++) {
                          inputDivs[i].style.display = "none";
          }
          document.getElementById(divName).style.display = "block";
          lastFreeGiftDiv = divName;
          lastFreeGiftCounter = counter;
  }
}

function selectDefaultHiddenGiftValue(combo) {
   var val = combo.options[combo.selectedIndex].value;
   document.getElementById("giftId").value = val;
}




$(function() {

if (document.all && !window.opera && !window.XMLHttpRequest) {return;}
else {

    //find all form with class jqtransform and apply the plugin
    $("form.jqtransform").jqTransform();
	}
});




var promoTimer;
var promoId = 1;
var promoMax = 5

function promoRotate(id){
	
	clearInterval ( promoTimer );
	promoId = id;
	
	$("#promoOffers > div").hide();
	$("#promoOffer"+promoId+"").fadeIn();
	
	$("#promoNavLinks > li > a").removeClass();
	$("#promoNavLinks > li > a").eq(promoId - 1).addClass("promoLinkSelected");
	
	
	promoTimer = setInterval ( "promoRotate(promoId, '')", 4000 );
	if(promoId == promoMax){
		
		promoId = 1;
	}else{
	promoId++;
	}
}

$(function() {
  
  $("#promoNavLinks > li > a").click(function(){
	
	thisId = $(this).text();
	promoRotate(thisId);
	
   
  });
});
  
  promoTimer = setInterval ( "promoRotate(promoId)", 0 );
  


// Create the tooltips only on document load
$(document).ready(function()
{
 $('#upgradeHelp a[href]').qtip({
 position: {
 corner: {
 target: 'topRight',
 tooltip: 'bottomLeft'
 }
 },
 style: {
 name: 'dark',
 padding: '7px 13px',
 width: {
 max: 210,
 min: 0
 },
 border: {
           width: 7,
           radius: 5,
           color: '#A2D959'
       },
 tip: true
}
 });
}); 

/*

// JQuery Confirm Popup box using SimpleModal Confirm Modal Dialog
// http://www.ericmmartin.com/projects/simplemodal/
jQuery(function ($) {
	$('#confirm-dialog input.confirm, #confirm-dialog a.confirm').click(function (e) {
		e.preventDefault();
		// example of calling the confirm function
		var link = $('#confirm-dialog a.confirm').attr('href');
		
		
		// you must use a callback function to perform the "yes" action
		confirm(function () {
			window.location.href = link;
		});
	});
});

function confirm(callback) {
	$('#confirm').modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'confirm-overlay',
		containerId: 'confirm-container', 
		onShow: function (dialog) {
			var modal = this;

			$('.message', dialog.data[0]);

			// if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				modal.close(); // or $.modal.close();
			});
		}
	});
}


*/




