// Uniblue Currency Script
// For info on how to use the script please open http://www.liutilities.loc/assets/documentation/currency_script.txt
// For a list of currencies please open http://www.liutilities.loc/assets/documentation/currencies.html
// CREATED BY    : Jonathan Vella 
// CREATED ON    : 11/2008
// LATEST UPDATE : 15/12/2008
// UPDATES		 : added amount_save_total which returns the total saved price on a whole mvt


// Main entry function 
function currency(thisPage, mainP, freeP, MVTlinks) {
	if (mainP==null||MVTlinks=="") {return;}
	
	// Return Language + Currency + Symbol and set initial values to 0
	var lang 		= getLang();
	var curr	 	= getCurrency(lang);
	var symbol 		= getSymbol(curr);
	
	amount_orig_bundle = 0
	amount_discounted_bundle = 0
	amount_main_orig_bundle = 0
	amount_main_discounted_bundle = 0
	amount_offer_orig_bundle = 0
	amount_offer_discounted_bundle = 0	
	
	// Store MAIN PRODCUTS prices + FREE PRODUCTS prices + MVT links in an array (sepearted with a ::)
	var mainProds = mainP.split("::");
	var freeProds = freeP.split("::");
	var MVTLinks = MVTlinks.split("::");
	var totalMVTs = MVTLinks.length
	
	// Return if this is a bundle offer and set variables
		bundle_offer = false
		if ((totalMVTs < 2 && mainProds.length > 1) || (totalMVTs < 2 && freeProds.length > 1)){ bundle_offer = true }
	
	for ( i=0, totalMVTs; i < totalMVTs; ++i ){

		// Returns the price of all the package at its original pricing
			amount_orig 	= symbol + (getOriginalPrice(mainProds[i], curr) + getOriginalPrice(freeProds[i], curr)).toFixed(2);
			
		// Returns the price of all the package at its discounted pricing
			amount_discounted = symbol + (getDiscountedPrice(mainProds[i], curr) + getDiscountedPrice(freeProds[i], curr)).toFixed(2);
			
		// Returns the prices for the main product
			amount_mainp_orig = getOriginalPrice(mainProds[i], curr).toFixed(2);
			amount_mainp_discounted = getDiscountedPrice(mainProds[i], curr).toFixed(2);

		// Returns the prices for the promo product
			amount_extrap_orig = getOriginalPrice(freeProds[i], curr).toFixed(2);
			amount_extrap_discounted = getDiscountedPrice(freeProds[i], curr).toFixed(2);
			
		// Returns the amount of money saving from the main product
			amount_save	= symbol + (amount_mainp_orig - amount_mainp_discounted).toFixed(0);
			amount_save_total= symbol + ((getOriginalPrice(mainProds[i], curr) + getOriginalPrice(freeProds[i], curr)) - (getDiscountedPrice(mainProds[i], curr) + getDiscountedPrice(freeProds[i], curr))).toFixed(0);	

		// Returns the prices for the main and promo products together with the curr symbol
			amount_mainp_orig =  symbol + amount_mainp_orig;
			amount_mainp_discounted =  symbol + amount_mainp_discounted;	
			amount_extrap_orig = symbol + amount_extrap_orig;
			amount_extrap_discounted = symbol + amount_extrap_discounted;			
			if (amount_extrap_discounted == symbol + "0.00"){amount_extrap_discounted = "FREE"};
		
		//	Execute this code if this is a bundle ONLY ON THE FIRST LOOP
			if ( bundle_offer == true && i == 0 ){
	
				// Output the pricing of all main products and sum them up
					for ( x=0, mainProds.length; x < mainProds.length; ++x ){
						setInnerHTMLWithChecking("price-main-bundle-old"+[x+1], symbol + getOriginalPrice(mainProds[x], curr).toFixed(2));
						setInnerHTMLWithChecking("price-main-bundle-new"+[x+1], symbol + getDiscountedPrice(mainProds[x], curr).toFixed(2));
						setInnerHTMLWithChecking("price-main-bundle-save"+[x+1], symbol + (getOriginalPrice(mainProds[x], curr) - getDiscountedPrice(mainProds[x], curr)).toFixed(0));
						
						amount_main_orig_bundle = amount_main_orig_bundle + getOriginalPrice(mainProds[x], curr);		
						amount_main_discounted_bundle = amount_main_discounted_bundle + getDiscountedPrice(mainProds[x], curr);	
					}	
				
				// Output the pricing of all extra products and sum them up
					for ( x=0, freeProds.length; x < freeProds.length; ++x ){
						thisprice = symbol + getOriginalPrice(freeProds[x], curr).toFixed(2)
						if (thisprice == symbol + "0.00"){thisprice = "FREE"};
						setInnerHTMLWithChecking("price-promo-bundle-old"+[x+1], thisprice);
						
						thisprice = symbol + getDiscountedPrice(freeProds[x], curr).toFixed(2)
						if (thisprice == symbol + "0.00"){thisprice = "FREE"};						
						setInnerHTMLWithChecking("price-promo-bundle-new"+[x+1], thisprice);
											
						amount_offer_orig_bundle = amount_offer_orig_bundle + getOriginalPrice(freeProds[x], curr);		
						amount_offer_discounted_bundle = amount_offer_discounted_bundle + getDiscountedPrice(freeProds[x], curr);
						
					}
				
				// Returns the saved money on this bundle
					bundle_save = symbol + ((amount_main_orig_bundle + amount_offer_orig_bundle) - (amount_main_discounted_bundle + amount_offer_discounted_bundle)).toFixed(0);;
				
				// Returns the totals
					
					amount_bundle_total_old = symbol + (amount_main_orig_bundle + amount_offer_orig_bundle).toFixed(2);
					amount_bundle_total_new = symbol + (amount_main_discounted_bundle + amount_offer_discounted_bundle).toFixed(2);
					amount_main_orig_bundle = symbol + (amount_main_orig_bundle).toFixed(2);
					amount_main_discounted_bundle = symbol + (amount_main_discounted_bundle).toFixed(2)	;
					amount_offer_orig_bundle = symbol + (amount_offer_orig_bundle).toFixed(2);
					amount_offer_discounted_bundle =  symbol + (amount_offer_discounted_bundle).toFixed(2);
					if (amount_offer_discounted_bundle == symbol + "0.00"){amount_offer_discounted_bundle = "FREE"}
				
				// Output the totals
					setInnerHTMLWithChecking("price-bundle-save", bundle_save);
					setInnerHTMLWithChecking("price-main-bundle-total-old", amount_main_orig_bundle);
					setInnerHTMLWithChecking("price-main-bundle-total-new", amount_main_discounted_bundle);		
					setInnerHTMLWithChecking("price-promo-bundle-total-old", amount_offer_orig_bundle);
					setInnerHTMLWithChecking("price-promo-bundle-total-new", amount_offer_discounted_bundle);	
					setInnerHTMLWithChecking("price-bundle-total-old", amount_bundle_total_old);
					setInnerHTMLWithChecking("price-bundle-total-new", amount_bundle_total_new);					
			
			}			
			
		// Write content if this is a purchase page and its not a bundle
			if ((thisPage == "store" || thisPage == "purchase") && bundle_offer == false) {
					setInnerHTMLWithChecking("price-topline"+[i+1], amount_discounted);
					setInnerHTMLWithChecking("price-product"+[i+1], amount_mainp_orig);
					setInnerHTMLWithChecking("price-save"+[i+1], amount_save);
					setInnerHTMLWithChecking("price-save-total"+[i+1], amount_save_total);
					setInnerHTMLWithChecking("price-promo"+[i+1], amount_extrap_orig);
					setInnerHTMLWithChecking("price-promo-new"+[i+1], amount_extrap_discounted);
					setInnerHTMLWithChecking("price-old"+[i+1], amount_orig);
					setInnerHTMLWithChecking("price-new"+[i+1], amount_discounted);
			}
		// Update Links
		for (x=0 ; x < 8 ; x++){
			setHrefWithChecking("mvtlink"+[i+1]+[x+1], getLink(MVTLinks[i], curr))			
		}
				
	}
	

	
	// Write content if this is a product page
		if (thisPage == "site" || thisPage == "product") {
				setInnerHTMLWithChecking("right-orig", amount_orig);
				setInnerHTMLWithChecking("right-sell", amount_discounted);
				setInnerHTMLWithChecking("right-save", amount_save);
		}
	
}

// Return || ORIGINAL || pretty pricing for the particular prod/offer in the user's currency
function getOriginalPrice(product, curr) {
	
	switch (product){
		
		// Price for PS + DR (or any standard prod)
		case "PSbundle": case "PSplus10":
			switch (curr) {
				case "USD" : return 159.85;
				case "CAD" : return 159.85;
				case "AUD" : return 159.85;
				case "GBP" : return 106.52;
				case "EUR" : return 133.19;
			}
		break;
		
		// Price for Cross-sell PS + Any other standard prod)
		case "PScross": case "PSDRbundleoffer":
			switch (curr) {
				case "USD" : return 169.85;
				case "CAD" : return 169.85;
				case "AUD" : return 169.85;
				case "GBP" : return 109.85;
				case "EUR" : return 139.85;
			}
		break;		
		
		// Regular price for PS
		case "regPS": case "upsPS":
			switch (curr) {
				case "USD" : return 119.90;
				case "CAD" : return 119.90;
				case "AUD" : return 119.90;
				case "GBP" : return 79.90;
				case "EUR" : return 99.90;
			}
		break;
		
		
		// Regular prices for PP
		case "regPP":
			switch (curr) {
				case "USD" : return 49.95;
				case "CAD" : return 49.95;
				case "AUD" : return 49.95;
				case "GBP" : return 29.95;
				case "EUR" : return 39.95;
			}
		break;			
		
		// Regular price for RB, SP, DS, DR, ST and default
		case "reg": case "regRB": case "regSP": case "regDS": case "regDR": case "regST": case "offer1995": case "offer2495": case "offer2395": case "offer1495":
			switch (curr) {
				case "USD" : return 39.95;
				case "CAD" : return 39.95;
				case "AUD" : return 39.95;
				case "GBP" : return 26.62;
				case "EUR" : return 33.29;
			}
		break;
		
		// Price for main products together
		case "reg-reg": case "2prods":
		switch (curr) {
				case "USD" : return 79.90;
				case "CAD" : return 79.90;
				case "AUD" : return 79.90;
				case "GBP" : return 53.24;
				case "EUR" : return 66.58;
			}			
		break;
		
		default:
			return 0;
		break;
		
	}
}

// Return || DISCOUNTED || pretty pricing for the particular prod/offer in the user's currency
function getDiscountedPrice(product, curr) {
	switch (product){
		
		// Price for PS + DR (or any standard prod)
		case "PSbundle": case "PSplus10":
			switch (curr) {
				case "USD" : return 69.95;
				case "CAD" : return 69.95;
				case "AUD" : return 69.95;
				case "GBP" : return 46.61;
				case "EUR" : return 58.28;
			}
		break;	
		
		// Discounted price for PS
		case "regPS":
			switch (curr) {
				case "USD" : return 59.95;
				case "CAD" : return 59.95;
				case "AUD" : return 59.95;
				case "GBP" : return 39.95;
				case "EUR" : return 49.95;
			}
		break;
		
		// Discounted price for upsell PS
		case "upsPS": case "PScross": case "PSDRbundleoffer":
			switch (curr) {
				case "USD" : return 39.95;
				case "CAD" : return 39.95;
				case "AUD" : return 39.95;
				case "GBP" : return 29.95;
				case "EUR" : return 34.95;
			}
		break;
		
		// Discounted prices for PP, ST
		case "regPP": case "regST":
			switch (curr) {
				case "USD" : return 0;
				case "CAD" : return 0;
				case "AUD" : return 0;
				case "GBP" : return 0;
				case "EUR" : return 0;
			}
		break;
		
		// Discounted price for RB, SP, DS, DR
		case "reg": case "regRB": case "regSP": case "regDS": case "regDR":
			switch (curr) {
				case "USD" : return 29.95;
				case "CAD" : return 29.95;
				case "AUD" : return 29.95;
				case "GBP" : return 19.95;
				case "EUR" : return 24.95;
			}
		break;
		
		// Special Offer of $19.95
		case "offer1995":
			switch (curr) {
				case "USD" : return 19.95;
				case "CAD" : return 19.95;
				case "AUD" : return 19.95;
				case "GBP" : return 13.30;
				case "EUR" : return 16.62;		
			}
		break;
			
		// Special Offer of $24.95
		case "offer2495":
			switch (curr) {
				case "USD" : return 24.95;
				case "CAD" : return 24.95;
				case "AUD" : return 24.95;
				case "GBP" : return 16.62;
				case "EUR" : return 20.72;		
			}
		break;	
		
		// Special offer of $23.95
		case "offer2395":
			switch (curr) {
				case "USD" : return 23.95;
				case "CAD" : return 23.95;
				case "AUD" : return 23.95;
				case "GBP" : return 15.95;
				case "EUR" : return 19.95;
			}
		
		// Special offer of $23.95
		case "offer2396":
			switch (curr) {
				case "USD" : return 23.96;
				case "CAD" : return 23.96;
				case "AUD" : return 23.96;
				case "GBP" : return 15.96;
				case "EUR" : return 19.96;
			}
		break;		

		// Special offer of $14.95
		case "offer1495":
			switch (curr) {
				case "USD" : return 14.95;
				case "CAD" : return 14.95;
				case "AUD" : return 14.95;
				case "GBP" : return 9.96;
				case "EUR" : return 12.45;
			}
		break;	
		
		// Price for main products together (1 of them for free)
		case "reg-reg": case "2prods":
		switch (curr) {
				case "USD" : return 29.95;
				case "CAD" : return 29.95;
				case "AUD" : return 29.95;
				case "GBP" : return 19.95;
				case "EUR" : return 24.95;
			}			
		break;		
		
		default:
			return 0;
		break;
	}
	
}

// Returns the current language selection from browser's user agent object
function getLang() {
	var languageinfo;
	
	languageinfo = navigator.userLanguage;
	if (languageinfo==null) {languageinfo=navigator.language;		}
	if (languageinfo==null) {languageinfo=navigator.browserLanguage;}
	if (languageinfo==null) {languageinfo=navigator.systemLanguage;	}
	if (languageinfo==null) {languageinfo="en-us";					}	
	
	return languageinfo.toLowerCase();
}

// Returns the currency symbol: $, €, etc
function getSymbol(curr) {
	switch (curr) {
		case "USD" : return "$";
		case "CAD" : return "$";
		case "AUD" : return "$";
		case "GBP" : return "&pound;";
		case "EUR" : return "&euro;";
	}
	return "$";
}

// Returns currency standard code based on localisation rules
function getCurrency(lang) {
	if (lang in {"en-uk":"", "gd":"", "en-ie":"", "gd-ie":"", "en-gb":""}) {
		return "GBP";
	} else {
		if (lang in {"en-au":"", "en-nz":""}) {
			return "AUD";
		} else {
			if (lang in {"en-ca":"", "fr-ca":""}) {
				return "CAD";
			} else {
				if (lang in {"eu":"", "hr":"", "da":"", "nl-be":"", "fr":"", "fi":"", "fr-be":"", "fr-ch":"", "fr-lu":"", "de":"", "de-at":"", "de-li":"", "de-ch":"", "de-lu":"", "el":"", "hu":"", "it":"", "it-ch":"", "lv":"", "mk":"", "mt":"", "no":"", "pt":"", "pl":"", "ro":"", "ro-mo":"", "ru":"", "ru-mo":"", "sr":"", "sl":"", "sk":"", "es":"", "sv":"", "sv-fi":"", "tr":"", "uk":"", "cs":"", "bg":"", "ca":"", "nl":"", "et":""}) {
					return "EUR";
				}		
			}		
		}		
	}
	return "USD";
}

// Check if HTML element exists, if yes write output
function setInnerHTMLWithChecking(id, value)
{
	//document.write(id + " - " + value + "<br /><br />");
	if (document.getElementById(id))
		document.getElementById(id).innerHTML = value;
}

// Check if HREF element exists, if yes write output
function setHrefWithChecking(id, value)
{
	//document.write(id + " - " + value + "<br /><br />");
	if (document.getElementById(id))
		document.getElementById(id).href = value;
}

// Returns a fully qualified CB Global Commerce URL
function getLink(linkMVT, curr) {
	return linkMVT+"&currency="+curr;
}