/**
* common library of javascript functions
*/
function getInternetExplorerVersion() {    var rv = -1;
// Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {        var ua = navigator.userAgent;        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");        if (re.exec(ua) != null)            rv = parseFloat(RegExp.$1);    }    return rv;}
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
    } else if (obj.attachEvent)  {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}

/*
* fix alpha pngs on a page (turn on transparency for IE)
*/

//Commenting this out. Use the custom tag for applying IE transparency filter.

function fixAlphaPng() {
    var src     = '';
    var ua      = '';
    var width   = '';
   var height  = '';
//    var rpng    = new RegExp('\.(png|jpg)$');
	var rpng    = new RegExp('\.(png)$');
    var rmsie   = new RegExp('msie');
    var rmac    = new RegExp('mac');

    for (var i = 0; i < document.getElementsByTagName('IMG').length; i++) {
        src     = document.getElementsByTagName('IMG')[i].getAttribute('src');
        ua      = navigator.userAgent.toLowerCase();
		if (ver >= 8.0) { 
			width   = document.getElementsByTagName('IMG')[i].getAttribute('width').value;
			height  = document.getElementsByTagName('IMG')[i].getAttribute('height').value;
		}
		else {
			width   = document.getElementsByTagName('IMG')[i].getAttribute('width');
			 height  = document.getElementsByTagName('IMG')[i].getAttribute('height');
		} 
		var ver = getInternetExplorerVersion();
		
        if (src.match(rpng) && ua.match(rmsie) && !ua.match(rmac)) {
        	document.getElementsByTagName('IMG')[i].setAttribute('src', 'http://www.herbalessences.com.au/images/spacer.png');
            document.getElementsByTagName('IMG')[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\', sizingMethod=scale)';
			if (ver >= 8.0) { 
				
				document.getElementsByTagName('IMG')[i].style.width = width;					
				document.getElementsByTagName('IMG')[i].style.height = height;  
			}
			else {
				document.getElementsByTagName('IMG')[i].style.width = width + 'px';					
				document.getElementsByTagName('IMG')[i].style.height = height + 'px'; 
			}         	
			 	
			
        }
    }
}
addEvent(window, 'load', fixAlphaPng);

/*
* fix alpha pngs on a page (turn on transparency for IE)
*/

//Commenting this out. Use the custom tag for applying IE transparency filter.

function fixAlphaPngById(imageId) {
    var src     = '';
    var ua      = '';
    var width   = '';
   var height  = '';
    var rpng    = new RegExp('\.(png|jpg)$');
    var rmsie   = new RegExp('msie');
    var rmac    = new RegExp('mac');
	
	src     = document.getElementById(imageId).getAttribute('src');
	ua      = navigator.userAgent.toLowerCase();
	 var ver = getInternetExplorerVersion();
	if (ver >= 8.0) {			
			width   = document.getElementById(imageId).getAttribute('width').value;			
			height  = document.getElementById(imageId).getAttribute('height').value;
	}
	else {
			width   = document.getElementById(imageId).getAttribute('width');
			height  = document.getElementById(imageId).getAttribute('height');
		} 
    if (src.match(rpng) && ua.match(rmsie) && !ua.match(rmac)) {
    	document.getElementById(imageId).setAttribute('src', 'http://www.herbalessences.com.au/images/spacer.png');
        document.getElementById(imageId).style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\', sizingMethod=scale)';
		if (ver >= 8.0) {
			document.getElementById(imageId).style.width = width;		
			document.getElementById(imageId).style.height = height;}
	   else {
		document.getElementById(imageId).style.width = width + 'px';		
		document.getElementById(imageId).style.height = height + 'px';
	   }
       	
    }
}

/*
Nav Fix for IE hover
*/
navArray = Array('nav');
startList = function() {
	if (document.all&&document.getElementById) {
		//Top Navs
		for (m=0; m<navArray.length; m++) {
			if (document.getElementById(navArray[m])) {
				topNavRoot = document.getElementById(navArray[m]);
				for (i=0; i<topNavRoot.childNodes.length; i++) {
					node = topNavRoot.childNodes[i];
					//alert(node.nodeName);
					if (node.nodeName=="LI") {
						//Sub UL
						for (j=0; j<node.childNodes.length; j++) {
							subTopNavRoot = node.childNodes[j];
							if (subTopNavRoot.nodeName=="UL") {
								for (k=0; k<subTopNavRoot.childNodes.length; k++) {
									subnode = subTopNavRoot.childNodes[k];
									if (subnode.nodeName=="LI") {
										subnode.onmouseover=function() {
											this.className="over "+this.className;
										}
										subnode.onmouseout=function() {
											this.className=this.className.replace("over ", "");
										}
									}
								}
							}
						}
						//alert(" m = "+m+"\n i = "+i+"\n j = "+j+"\n k = "+k);
						var nav = i+1;
						node.filterroll = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://www.herbalessences.com.au/images/nav/btn'+nav+'_roll.png", sizingMethod="crop")';
						node.filterrest = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://www.herbalessences.com.au/images/nav/btn'+nav+'_rest.png", sizingMethod="crop")';
						node.filtersel = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://www.herbalessences.com.au/images/nav/btn'+nav+'_sel.png", sizingMethod="crop")';
						
						node.onmouseover=function() {
							this.className="over "+this.className;
							this.style.filter=this.filterroll;
						}
						node.onmouseout=function() {
							this.className=this.className.replace("over ", "");
							if (this.className=="on") {							
								this.style.filter=this.filtersel;
							} else {
								this.style.filter=this.filterrest;
							}
						}
					}
				}
			}
		}
	}
}
addEvent(window, 'load', startList);



/*
Redirect to no cookies page
*/
function noCookies(redirect) {
	var tmpcookie = new Date();
	chkcookie = (tmpcookie.getTime() + '');
	document.cookie = "chkcookie=" + chkcookie + "; path=/";
	if (document.cookie.indexOf(chkcookie,0) < 0) {
		window.location = redirect;
	} else {
		return("true");
	}
}

/*
Clear default value of text input
*/
function clearField(thefield) {
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
}


/* 
switch product pages within a collection
*/
function productSelect(url) {
	window.location = url;
}

/*
switch collection image on collection landing page
*/
function swapImage(id, imageName) {
	document.getElementById(id).setAttribute('src', imageName);
	fixAlphaPngById(id);
}

/*

*/
function preloadCollectionImages() {
	var collectionImg = document.getElementById('collection_img');

	// preload images
	collectionImg.body_envy = new Image();
	collectionImg.body_envy.src = "http://www.herbalessences.com.au/images/collections/bottles_body_envy.png";
	collectionImg.breaks_over = new Image();
	collectionImg.breaks_over.src = "http://www.herbalessences.com.au/images/collections/bottles_breaks_over.png";
	collectionImg.color_me_happy = new Image();
	collectionImg.color_me_happy.src = "http://www.herbalessences.com.au/images/collections/bottles_color_me_happy.png";
	collectionImg.dangerously_straight = new Image();
	collectionImg.dangerously_straight.src = "http://www.herbalessences.com.au/images/collections/bottles_dangerously_straigh.png";
	collectionImg.default_img = new Image();
	collectionImg.default_img.src = "http://www.herbalessences.com.au/images/collections/bottles_default.png";
	collectionImg.drama_clean = new Image();
	collectionImg.drama_clean.src = "http://www.herbalessences.com.au/images/collections/bottles_drama_clean.png";
	collectionImg.hello_hydration = new Image();
	collectionImg.hello_hydration.src = "http://www.herbalessences.com.au/images/collections/bottles_hello_hydration.png";
	collectionImg.none_of_your_frizzness = new Image();
	collectionImg.none_of_your_frizzness.src = "http://www.herbalessences.com.au/images/collections/bottles_none_of_your_frizzness.png";
	collectionImg.totally_twisted = new Image();
	collectionImg.totally_twisted.src = "http://www.herbalessences.com.au/images/collections/bottles_totally_twisted.png";
	collectionImg.long_term_relationship = new Image();
	collectionImg.long_term_relationship.src = "../images/collections/bottles_long_term_relationship.png";
}

/*
switch collection image on collection landing page
*/
function swapImageViaObject(id, imageObject) {
//	alert(imageObject.src);
	document.getElementById(id).setAttribute('src', imageObject.src);
	fixAlphaPngById(id);
}

var xmlHttp

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}



  function init() {
	
		// Fetch all the a elements in the document.
		var links = document.getElementsByTagName('a');

		// Loop through the a elements in reverse order
		// for speed.
		for (var i = links.length; i != 0; i--) {
			
			// Pull out the element for this iteration.
			var a = links[i-1];
			
			// If the element doesn't have an href, skip it.
			if (!a.href) continue;

			// If the element has a className that contains
			// 'external' attach the onclick handler.
			if (a.className && a.className.indexOf('external') != -1) a.onclick = PopWin;
		}
	}
	
	function PopWin(e) {
	
		// Accommodate IE's non-standard event handling.
		if (!e) var e = window.event;
		var a = e.target ? e.target : e.srcElement;

		// Open a new window with the link's href.
		var newwin = window.open(a.href);

		// The thought is that if the new window didn't
		// (popups blocked or whatever) we want to return
		// true so the link is follow normally. Not sure
		// if this works, but it doesn't seem to hinder.
		return !newwin;                               
	}
	
	onload = init;
	
	
	/* Include Fav Icon */
	document.write("<link rel='shortcut icon' href='http://www.herbalessences.com.au/favicon.ico'>");