﻿/* Copyright (c) 2006 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Thanks to Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 */
 
 var myWidth = 0
 var myHeight = 0
 
 function URLEncode(unencodedValue )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = unencodedValue;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode(encodedValue)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = encodedValue
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};
jQuery.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* @example value = $.getURLParam("paramName");
*/ 
 getURLParameter: function(strParamName){
	  var strReturn = "";
	  var strHref = window.location.href;
	  var bFound=false;
	  
	  var cmpstring = strParamName + "=";
	  var cmplen = cmpstring.length;

	  if ( strHref.indexOf("?") > -1 ){
	    var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	    var aQueryString = strQueryString.split("&");
	    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
	      if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
	        var aParam = aQueryString[iParam].split("=");
	        strReturn = URLDecode(aParam[1]);//should decode url string
	        bFound=true;
	        break;
	      }
	      
	    }
	  }
	  if (bFound==false) return null;
	  return strReturn;
	}
});
jQuery.extend({
	setURLParameter:function(url,parameterName,parameterValue){
		return url+"?"+URLEncode(parameterName)+"="+URLEncode(parameterValue);
	}
	
});
jQuery.extend({
	appendURLParameter:function(url,parameterName,parameterValue){
		return url+"&"+URLEncode(parameterName)+"="+URLEncode(parameterValue);
	}

});

function ClearCart() {
	var ClrCrt=confirm("Are you sure you wish to empty your cart?")
	if (ClrCrt) {
		$('#cart').load('/views/cart.asp?action=clear');
		//$('#cart').load('/views/mini_cart.asp');
		}
		
}

function DelFromCart(OrderItemID) {
	$('#cart').load('/views/cart.asp?action=remove&tiny=false&orderitemid='+OrderItemID);
	//$('#cart').load('/views/mini_cart.asp');
}

function CheckOut() {
	var protocol = 'http'
	url = location.href
	//var matchPos1 = url.indexOf('www');
	//alert(string1);
	//if (matchPos1 != -1) 
	//	{
 	//	protocol = 'https'
	//	}
	//else
	//	{
	//	protocol = 'http'
	//	}
	//alert(matchPos1+':'+protocol);
	location.href=protocol + '://' + location.hostname + '/checkout.asp'
}

function Currency() {
var billingCountry = "Australia" //default billing country is Australia

  	


			var USD_AUD = parseFloat('') * OrderTotal; 
			var EUR_AUD = parseFloat('') * OrderTotal;
			var GBP_AUD = parseFloat('') * OrderTotal; 
			var NZD_AUD = parseFloat('') * OrderTotal; 

			//alert(OrderTotal +'\n'+USD_AUD+'\n'+JPY_AUD+'\n'+KRW_AUD)	
			// other currency
			var Currency1str = '';

			// aussie dollars
			var Currency2str = ManageSet.MonetarySymbol + 'AUD ' + moneyFormat(OrderTotal)

	
			switch ("") {
				case "AUD":
				{
					Currency1str = '';
					break;
				}
				case "USD":
				{
					Currency1str = ' [USD ' + ManageSet.MonetarySymbol + moneyFormat(USD_AUD) + ']';
					break;
				}
				case "EUR":
				{
					Currency1str = ' [&#8364;' + moneyFormat(EUR_AUD) + ']';
					break;
				}
				case "GBP":
				{
					Currency1str = ' [&pound;' + moneyFormat(GBP_AUD) + ']';
					break;
				}
				case "NZD":
				{
					Currency1str = ' [NZD ' + ManageSet.MonetarySymbol + moneyFormat(NZD_AUD) + ']';
					break;
				}
			}

			//strOutput = "<TABLE CLASS=\"nopcart\" cellpadding=\"0\" cellspacing=\"0\" border=0 width=100%>"

			if (ManageSet.TotalBGColor!='') {
				TotalRowBGstr = ' bgcolor="' + ManageSet.TotalBGColor + '"'
			} else {
				TotalRowBGstr = ''
			}
				
			//TOTAL
			strOutput += '<TR><td colspan="6">';
			strOutput += '	<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber20">';
			strOutput += '	<tr><td bgcolor="#C40046" width="20" height="20" align="left">&nbsp;</td>';	
			strOutput += '	    <td bgcolor="#C40046" width="50%"><font color="#FFFFFF"><b>TOTAL</b></font></td>';
			strOutput += '	    <td bgcolor="#C40046" width="50%" align="right"><font color="#FFFFFF">' + Currency1str + '</font>&nbsp;&nbsp;<b><font color="#FFFFFF">' + Currency2str + '</font></b>&nbsp;&nbsp;</td>';
			strOutput += '	</tr></table>';	
			strOutput += '</td></TR></table>';
			document.write(strOutput)
}

// object id, value
function SelectOptionInList( lstSelectList, intID )
  {
        try
        {
              var intIndex = 0;
              // Loop through all the options
              for( intIndex = 0; intIndex < lstSelectList.options.length; intIndex++ )
              {
                    // Is this the ID we are looking for?
                    if( lstSelectList.options[intIndex].value == intID ) 
                    {
                          // Select it
                          lstSelectList.selectedIndex = intIndex;
                          // Yes, so stop searching
                          break;
                    }
              }
        }
        catch( expError )
        {
              alert( "js::SelectOptionInList( ).\n" +
                          "Error:" + expError.number + ", " + expError.description );
        }
  } 

// view port
function getSize() {
	 if(typeof(window.innerWidth) == 'number') {
	  //Non-IE
	  myWidth = window.innerWidth;
	  myHeight = window.innerHeight;
	 }else if(document.documentElement &&
	  (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
	  //IE 6+ in 'standards compliant mode'
	  myWidth = document.documentElement.clientWidth;
	  myHeight = document.documentElement.clientHeight;
	 } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
	  //IE 4 compatible
	  myWidth = document.body.clientWidth;
	  myHeight = document.body.clientHeight;
	 }
	 //alert(myWidth+':'+myHeight);
	 //return [myWidth, myHeight];
}

function Infomaxim_sendView(type,id,campaign,opt2,opt3) {
	if (!document.all&&!document.layers&&!document.getElementById) //stop old browser breaking this?
	return
	getSize()
	//jQuery the data to analytics collector
	$('#infomaxim_analytics').load('/includes/hit.asp?t='+type+'&i='+id+'&w='+screen.width+'&h='+screen.height+'&c='+screen.colorDepth+'&aw='+myWidth+'&ah='+myHeight+'&ca='+campaign+'&ref='+URLEncode(document.referrer)+'&url='+URLEncode(location.href));
}

function Infomaxim_sendClick(url,type) {
	$('#infomaxim_analytics').load('/includes/hit.asp?click=1&url='+URLEncode(url)+'&t='+type);
}
 
$(document).ready(function(){
    //Requires jQuery 1.3
    //Explanation:-  http://permanentinkdesign.com/articles/google-analytics-jquery-and-file-downloads/
    //InfomaximAnalytics();

    // The slashes are to ensure the period is in the url, the $ is to make sure it is the end of the url, the i is to make it case insensitive.
	filetypes = /\.doc$|\.xls$|\.exe$|\.zip$|\.pdf$|\.mp3$|\.psd$/i;
	$("a").live("click", function(){
		// Track mailto links
		if ($(this).attr("href").match(/^mailto\:/i)) {
			var url = $(this).attr("href").replace(/^mailto\:/i, "")
			Infomaxim_sendClick(url,'mailto')
		}
		// Track external links
		else if (location.host != this.host.replace(/\:80$/i, "")) {
			var url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "")
			Infomaxim_sendClick(url,'ext')
		}
		// Track downloads (links with a given extension)
		else if ($(this).attr("href").match(filetypes)) {
			// The URL needs to be changed for each site this is applied to.
			var host = location.host.replace(/\./, "\\.")
			var pattern = new RegExp('^(http\:\/\/)*(www\.)*('+host+')*\/','i')
			var url = $(this).attr("href").replace(pattern, "")
			Infomaxim_sendClick(url,'download')
		}
	});
});

