﻿// JScript File

function ShowMenuPopup(conID)
{    
    var popup = document.getElementById(conID);
    
    if (popup.style.display == "none")
        popup.style.display = "block";
    else
        popup.style.display = "none"
}


// lvls Bra/DD+ img swap
function SwapBraImg(ImgConID, newImg, LnkConID, NavUrl, SelectedContainer, DeselectContainer)
{
    var ImgCon = document.getElementById(ImgConID);
    var LnkCon = document.getElementById(LnkConID);
    ImgCon.setAttribute("src", "http://images.lasenza.co.uk/images/photos/new_medium/" + newImg + ".jpg");
    LnkCon.setAttribute("href", NavUrl);
    
    // Change bg image
    var s = document.getElementById(SelectedContainer);
    s.setAttribute("class", "braDDCon_s");
    s.setAttribute("className", "braDDCon_s");
    
    var d = document.getElementById(DeselectContainer);
    d.setAttribute("class", "braDDCon");
    d.setAttribute("className", "braDDCon");
}

// AffiliateID Cookie
var affid = queryString('affid');
if(affid != "false")
{
	// set cookie to store affid
	Set_Cookie("jsAffiliate", affid, 30, "/", "", "");
}
else
{
    affid = queryString('affID');
    
    if(affid != "false")
        Set_Cookie("jsAffiliate", affid, 30, "/", "", "");
}

// Network Cookie
var net = queryString('network');
if(net != "false")
{
	// set cookie to store network
	Set_Cookie("jsNetwork", net, 30, "/", "", "");
	
	// set the date
	var dt = new Date();
	var month = dt.getMonth() + 1;
	var created = dt.getDate() + '/' + month + '/' + dt.getFullYear();
	//Set_Cookie("jsAFCreated", created, 30, "", "", "");
}



function SetCheckoutControls(skudetails, total, tdUID, tdName, tdCreated, coremetrics, pjpromo)
{	
    document.getElementById("hpjPromoTrigger").value = pjpromo;


	// TradeDoubler
	var inputTD = document.getElementById("tduid");
	inputTD.value = tdUID;	
	
	// AffiliateID
	var inputAff = document.getElementById("affiliateID");
	var affid = Get_Cookie("jsAffiliate");
	if(affid != '')
		inputAff.value = affid;
	else
		inputAff.value = '';
		
    // Network
	var inputAFNet = document.getElementById("afNetwork");
	var network = Get_Cookie("jsNetwork");
    if(network != '')
	    inputAFNet.value = network;
	else
        inputAFNet.value = '';
	
	// Coremetrics
	var inputCoremetrics = document.getElementById("coremetricsTag");
	inputCoremetrics.value = coremetrics;
		
		
	document.forms[0].subtotal.value = total;
    document.forms[0].delcharge.value = "2.95";
    document.forms[0].ordertotal.value = total;    
    document.forms[0].skudetails.value = skudetails;
    document.forms[0].deltype.value = "1";
}



// ---------------------------------------------------------------------
function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return '';
	}
}






function CreateCookie(name,value) 
{
	var date = new Date();
	date.setTime(date.getTime()+(30*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();		
	document.cookie = name + "=" + value + expires //+ "; domain=lasenza.co.uk";
}

function GetCookie(CookieName)
{
	var cookies = document.cookie;

	if (cookies.indexOf(CookieName) != -1)
	{
		var startpos = cookies.indexOf(CookieName) + CookieName.length + 1;
		var endpos = cookies.indexOf(";",startpos);

		if (endpos == -2) 
			endpos = cookies.length;
			
		return unescape(cookies.substring(startpos,endpos));
	}
	else
		return false; // the cookie couldn't be found! it was never set before, or it expired.
}




function queryString(key)
{
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}

function PageQuery(q) 
{
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;

	this.keyValuePairs = new Array();
	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) 
	{
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	
	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}

function LoadCheckoutVariables(skudetails, subtotal, total)
{
	document.getElementById("skudetails").value = skudetails;
	document.getElementById("subtotal").value = subtotal;
	document.getElementById("ordertotal").value = total;
}

function addExpressDel(type, skudetails, subtotal, total, delcharge, TotalCon, DeliveryCon)
{
    var newDelCharge = delcharge;
    if(type == 1)
    {
        // [NOTE] free delivery period FREE
        if(subtotal > 34.99)
            newDelCharge = 0.00;
        else
            newDelCharge = 2.95;
        //newDelCharge = 0.00;
    }
    else
    {
        // [NOTE] free delivery period £5
        if(subtotal > 99.99)
            newDelCharge = 0.00;
        else
            newDelCharge = 6.95;
    }
    
    var newTotal = subtotal + newDelCharge;
    
    var strNewDelCharge = String(newDelCharge);
    var strNewTotal = String(newTotal);
    if(strNewDelCharge.indexOf('.') == -1)
        strNewDelCharge += ".00";
    if(strNewTotal.indexOf('.') == -1)
        strNewTotal += ".00";
        
    var objDeliveryCon = document.getElementById(DeliveryCon).innerHTML = "£" + strNewDelCharge;
    var objTotalCon = document.getElementById(TotalCon).innerHTML = "£" + strNewTotal;
    
    document.forms[0].subtotal.value = subtotal;
    document.forms[0].delcharge.value = newDelCharge;
    document.forms[0].ordertotal.value = newTotal;    
    document.forms[0].skudetails.value = skudetails;
    document.forms[0].deltype.value = type;
}