/*
'*
'*# Overview
'**	File name							: phoenix_common.js
'*	Creation							: 01/09/2008
'*	Author								: Screen Pages
'*	Purpose								: standard javascript
'*	Related Files						: n/a
'*	Comments							: none
'*
'*	Version History
'*	{xx.xx} {yyyy/mm/dd}  {Author}      {Description}
'*	{xx.xx} {2008/10/09}  {Zb}      {Added trim functions}
'*
'*	 1.00   2009/09/01    Screen Pages  Initial Build
'*	 1.01   2009/09/07    PJR - Japan currency - change from YEN to JPY
'*	 1.02   2009/09/09    PJR - Allow an override for the minimum characters allowed for a search
'*
'*	LIST OF FUNCTIONS:
'*		trim(str, chars) - removes all leading and trailing occurrences of a SET OF CHARACTERS SPECIFIED
'*		ltrim(str, chars) - removes all leading occurrences of a SET OF CHARACTERS SPECIFIED
'*		rtrim(str, chars) - removes all trailing occurrences of a SET OF CHARACTERS SPECIFIED
*/

	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  FRAMES BUSTER														*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

//	if (top.location != location) {top.location.href = location.href;}


	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  GENERAL FUNCTIONS												*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

/*
	'*	trim - removes all leading and trailing occurrences of a SET OF CHARACTERS SPECIFIED
	'*		If no characters are specified it will trim whitespace characters
	'*		Without the second parameter, they will trim these characters:
	'*			" " (ASCII 32 (0x20)), an ordinary space.
	'*			"\t" (ASCII 9 (0x09)), a tab.
	'*			"\n" (ASCII 10 (0x0A)), a new line (line feed).
	'*			"\r" (ASCII 13 (0x0D)), a carriage return.
	'*			"\0" (ASCII 0 (0x00)), the NUL-byte.
	'*			"\x0B" (ASCII 11 (0x0B)), a vertical tab.
*/
	function trim(str, chars) {
	    return ltrim(rtrim(str, chars), chars);
	}


/*	'*	trim - removes all leading occurrences of a SET OF CHARACTERS SPECIFIED	(more info at trim() function) */
	function ltrim(str, chars) {
	    chars = chars || "\\s";
	    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}

/*	'*	trim - removes all trailing occurrences of a SET OF CHARACTERS SPECIFIED	(more info at trim() function) */
	function rtrim(str, chars) {
	    chars = chars || "\\s";
	    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}

	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  CLIENT DATE SETUP AND FUNCTIONS			*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

	theday		= new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") ;
	themonth	= new Array("January","February","March","April","May","June","July","August","September","October","November","December") ;
	thesuffix	= new Array("","st","nd","rd","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","st","nd","rd","th","th","th","th","th","th","th","st") ;
	thisday		= new Date() ;
	theyear		= (thisday.getYear()<1900?thisday.getYear()+1900:thisday.getYear()) ;
	thedate		= theday[thisday.getDay()]+' '+thisday.getDate()+thesuffix[thisday.getDate()]+' '+themonth[thisday.getMonth()]+' '+ theyear+'' ;

	function jsf_showyear() {document.write(theyear);}
	function jsf_showdate() {document.write(thedate);}

	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  NETSCAPE 4 RESIZE CONSSESSION			*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

	function jsf_reloadPage(init) { //reloads the window if Nav4 resized
		if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
		document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=jsf_reloadPage; }}
		else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
	}
	jsf_reloadPage(true);

	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  IE PAGE WIDTH CONSSESSION				*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

	function jsf_MinMaxWidth(a,b){ // pass min and max measured against window width
		var nw="auto",w=document.documentElement.clientWidth;
		if(w>=b){nw=b+"px";} if(w<=a){nw=a+"px";} return nw;
	}

	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  IE DROP DOWN MENU CONSSESSION			*/
	/*	Javascript for the CMS CSS Menu Module	*/
	/*  Copyright (c) 2005 Alexander Endresen	*/
	/*  IE DROP DOWN MENU CONSSESSION			*/
	/*  Released under General Public Licence	*/
	/*  This script will emulate the css :hover	*/
	/*  effect on the menu elements in IE		*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

	// The variables
	var cssid = "primary-nav"; // CSS ID for the menuwrapper
	var menuadd = "h";  // Character to be added to the specific classes upon hovering. So for example, if this is set to "h", class "menuparent" will become "menuparenth" when hovered over.
	var menuh = "menuh"; // Classname for hovering over all other menu items


	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */
	/*  commented out by ZB temporarily - until we rationalise naming conventions for navigation lists vert/horiz */
	/*  - variable cssid	*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */
//	if (window.attachEvent) window.attachEvent("onload", cssHover);

	function cssHover()
	{
		var sfEls = document.getElementById(cssid).getElementsByTagName("LI");

		for (var i=0; i<sfEls.length; i++)
		{
			sfEls[i].onmouseover=function()
			{
				if (this.className != "") {
					this.className = this.className + menuadd;
				}
				else {
					this.className = menuh;
				} // if
			} // onmouseover

			sfEls[i].onmouseout=function()
			{
				if (this.className == menuh)
				{
					this.className = "";
				}
				else
				{
					this.className = this.className.replace(new RegExp(menuadd + "$"), "");
				} // if
			} // onnmouseout
		} // for
	} // function


	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  GETOBJECT COMMON FUNCTION				*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

	function getObject(nameStr) {
		var ie  = (document.all);
		var ns4 = document.layers? true : false;
		var dom = document.getElementById && !document.all ? true : false;

		if (dom) {
		    return document.getElementById(nameStr);
		} else if (ie) {
		    return document.all[nameStr];
		} else if (ns4) {
		    return document.layers[nameStr];
		}
	}


	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  LEFT COMMON FUNCTION					*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

	function leftStr(str, n){
		if (n <= 0)
		    return "";
		else if (n > String(str).length)
		    return str;
		else
		    return String(str).substring(0,n);
	}


	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  RIGHT COMMON FUNCTION					*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

	function rightStr(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else {
			var iLen = String(str).length;
			return String(str).substring(iLen, iLen - n);
		}
	}


	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  JQUERY COMMON FUNCTIONS					*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */

	jQuery(document).ready(function()
	{

		// --- INI
		//automatically updates the mini basket totals
		updateBasketTotal();

		// -------------------------------------------- //
		// 	 EMAIL SIGNUP VALIDATION - START			//
		// -------------------------------------------- //

			jQuery("#frmSubscribe").validate(
			{
				errorLabelContainer: "#email-signup-errormsg",
			    submitHandler: function(thisform) {

			    	jQuery("#email-signup").fadeOut(1000, function() {
			    		jQuery("#email-signup-confirmation").fadeIn(500)
			    	});
					jQuery(thisform).ajaxSubmit();
					return false;
				}


			});

			jQuery("#up_gi_email_address").click(function()
			{
				jQuery(this).val('');
			});

			jQuery("#frmSubscribe_link").click(function()
			{
				jQuery("#frmSubscribe").submit();
			});

		// -------------------------------------------- //
		// 	 EMAIL SIGNUP VALIDATION - END				//
		// -------------------------------------------- //


		// -------------------------------------------- //
		// 	 SEARCH VALIDATION - START					//
		// -------------------------------------------- //

			//Start [1.02] - allow search minumum character override
			var searchMinLength;
			if(jQuery("#searchOverrideValue")) {
				searchMinLength = jQuery("#searchOverrideValue").val();
			} else {
				searchMinLength = 3;
			}

			jQuery("#frmSearch").validate({
					rules: {
						txtSearch: {
							minlength: searchMinLength
						}
					},
					messages: {
						txtSearch: {
							minlength: "Please enter a valid search term (minimum of " + searchMinLength + " characters)"
						}
					},
					errorLabelContainer: "#search-errormsg"
			});
			// End [1.02]


			jQuery("#txtSearch").click(function(){
				jQuery(this).val('');
			});

		// -------------------------------------------- //
		// 	 SEARCH VALIDATION - START					//
		// -------------------------------------------- //


		// -------------------------------------------- //
		// 	 CURRENCY SELECTOR - START					//
		// -------------------------------------------- //

			jQuery("#GBPCurrency").click(function(){
				jQuery("#up_gi_currency").val('GBP');
				jQuery("#frmCurrency").submit();
			});

			jQuery("#USDCurrency").click(function(){
				jQuery("#up_gi_currency").val('USD');
				jQuery("#frmCurrency").submit();
			});

			jQuery("#EURCurrency").click(function(){
				jQuery("#up_gi_currency").val('EUR');
				jQuery("#frmCurrency").submit();
			});

			// [1.01] - change YEN to JPN
			jQuery("#JPYCurrency").click(function(){
				jQuery("#up_gi_currency").val('JPY');
				jQuery("#frmCurrency").submit();
			});

		// -------------------------------------------- //
		// 	 CURRENCY SELECTOR - END					//
		// -------------------------------------------- //




	});


	// -------------------------------------------- //
	// 	 MINI-BASKET DATA RETRIEVAL - START			//
	// -------------------------------------------- //

	function updateBasketTotal() {
		jQuery.ajax({
			type: "POST",
			data: {},
			url: "/GeneralPages/minibasket_ajax.asp",
			success: function(xml){
				//This simple XPath XML function looks through the returned XML data
				//All tags can now be accessed within the loop
				var _itemcount = "";
				var _baskettotal = "";
				var _basketxratetotal = "";

				//This function will loop for each match on mini-basket
				jQuery("minibasket", xml).each(function(){
					_itemcount = jQuery("item_count", this).text();
					_baskettotal = jQuery("basket_total", this).text();
					_basketxratetotal = jQuery("basket_xrate_converted_total", this).text();
					jQuery("#ajax_itemcount").text(_itemcount);
					jQuery("#ajax_baskettotal").text(_baskettotal);

					if(_basketxratetotal.length > 0) {
						jQuery("#ajax_basket_xrate_total").text(" (" + _basketxratetotal + ")");
					}

				});


			}
		});
	}

	// -------------------------------------------- //
	// 	 MINI-BASKET DATA RETRIEVAL - END			//
	// -------------------------------------------- //



	// -------------------------------------------- //
	// 	 OPENSTANDARDCMPOPUP - START				//
	// -------------------------------------------- //

	function OpenStandardCMPopup(sCMPage, iWidth, iHeight){
	 	var cm_win;
	   	var sPage;
		sPage='/GeneralPages/cm_popup.asp?sCCPage=' + sCMPage;
		cm_win = window.open(sPage,sCMPage,'location=no,scrollbars=yes,resizable=yes,width=' + iWidth + ',height=' + iHeight + ',left=100,top=100');
	    cm_win.focus();
	}

	// -------------------------------------------- //
	// 	 OPENSTANDARDCMPOPUP - END					//
	// -------------------------------------------- //

	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	*/
	/*  END OF FILE								*/
	/*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  */
