//LOSE IE6 FLICKER BUG
//--------------------

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

//CALL FUNCTIONS ONDOMREADY
//-------------------------

function onDomReady() {
	getElements();
	searchFrm();
};

//GET ELEMENTS
//------------

var homediv;
function getElements() {

	if (document.getElementById("homeimg")) {
		homediv = document.getElementById("homeimg");
		
		var preLoad = new Array();
		for (var j=0; j<imgsArray.length; j++) {
			preLoad[j] = new Image(559,399)
			preLoad[j].src = "/media/images/"+imgsArray[j];
		};
		setTimeout("homefade();", 3000);
	};

}

function searchFrm() {

	var search = document.getElementById("search").getElementsByTagName("input")[0];

	search.onfocus = function() {
		if (this.value == "Search products") {
			this.value = "";
		};
	};
	
	search.onblur = function() {
		if (this.value == "") {
			this.value = "Search products";
		};
	};
	
}

function popUp(URL, width, height) {

	var day = new Date();
	var id = day.getTime();
	
	var offsetWidth = (screen.width / 2) - (width / 2);
	var offsetHeight = (screen.height / 2) - (height / 2);

	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left = "+offsetWidth+",top = "+offsetHeight+"');");
	return false;
	
}

//DELIVERY COUNTRY ON BASKET PAGE
//-------------------------------

function toggleDeliveryCountry() {

	var div = document.getElementById("countrypop");
	
	//alert(div.innerHTML);
	
	if (div.style.display == "" || div.style.display == "none") {
		div.style.display = "block";
	} else {
		div.style.display = "none";
	};
	
	return false;

}

//MAKE SHIPPING ADDRESS SAME AS BILLING ADDRESS
//---------------------------------------------

function doAddress(checkbox) {

	if (checkbox.checked == true) {	
		document.getElementById("shipping_address1").value = document.getElementById("address1").value;
		document.getElementById("shipping_address2").value = document.getElementById("address2").value;
		document.getElementById("shipping_city").value = document.getElementById("city").value;
		document.getElementById("shipping_state").selectedIndex = document.getElementById("state").selectedIndex;
		document.getElementById("shipping_zip").value = document.getElementById("zip").value;
		document.getElementById("shipping_country").selectedIndex = document.getElementById("country").selectedIndex;		
	//} else {	
	//	document.getElementById("shipping_address").value = "";
	//	document.getElementById("shipping_address2").value = "";
	//	document.getElementById("shipping_city").value = "";
	//	document.getElementById("shipping_state").selectedIndex = 0
	//	document.getElementById("shipping_zip").value =  = "";
	//	document.getElementById("shipping_country_code").selectedIndex = 0
	};
	
}

//SUBMIT FORM FOR PRODUCT PAGE
//----------------------------

function addBasket() {

	var frmProduct = document.getElementById("frmProduct");
	frmProduct.submit();
	
	return false;
	
}

//QUANTITY PLUS AND MINUS
//-----------------------

function frmQty(direction) {

	var input = document.getElementById("frmQty");
	
	if (direction == 1 && parseInt(input.value) < 10) {
		input.value = parseInt(input.value)+1;
	} else if (direction == 2 && parseInt(input.value) > 1) {
		input.value = parseInt(input.value)-1;
	};

}

//HOMEPAGE SLIDESHOW
//------------------

var newimg;
var imgsArray = new Array();
imgsArray[0] = "home-2.jpg";
imgsArray[1] = "home-1.jpg";
imgsArray[2] = "home-4.jpg";
imgsArray[3] = "home-5.jpg";
var newimgInc = 1;
var timer = 0;
function homefade() {

	newimg = document.createElement("img");
	newimg.src = "/media/images/"+imgsArray[newimgInc];
	newimg.style.opacity = "0";
	newimg.style.filter = "alpha(opacity=0)";
	homediv.appendChild(newimg);
	if (newimgInc == (imgsArray.length-1)) {
		newimgInc = 0;
	} else {
		newimgInc++;
	};
	
	timer = setInterval("homefadeTrans();", 10);	

}

var trans = 0;
function homefadeTrans() {

	if (trans == 100) {
		clearTimeout(timer);
		timer = 0;
		trans = 0;
		
		homediv.removeChild(homediv.getElementsByTagName("img")[0]);		
		setTimeout("homefade();", 3000);
		return false;
	};

	newimg.style.opacity = trans/100;
	newimg.style.filter = "alpha(opacity="+trans+")";
	trans = trans+1;

}

//AJAX OBJECT CREATE
//------------------

var xmlHttp;
try {
	// Firefox, Opera 8.0+, Safari
	xmlHttp = new XMLHttpRequest();
} catch (e) {
	// Internet Explorer
	try	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e)	{
		try	{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e)	{
			alert("Your browser does not support AJAX!");
		};
	};
};	

//FORM VALIDATION
//---------------

function checkRequired(formid) {

    //alert(formid);
    var form = document.getElementById(formid);
    var error = 0;
    var setfocus = 0;
    
    var inputs = form.getElementsByTagName("input");
    for (i=0; i<inputs.length; i++) {
        if (inputs[i].title == "required") {
            if (inputs[i].value == "") {
                //alert("Please complete all fields.");
                inputs[i].style.borderColor = "red";
                if (!setfocus) {
                    inputs[i].focus();
                    setfocus = 1;
                };
                //return false;
                error = 1;
            } else {
                //EMAIL ADDRESS FORMATTING ON INPUT WITH NAME 'frmEmail'

                var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                var filterResult = filter.test(inputs[i].value);

                if (inputs[i].name == "frmEmail" & filterResult == false) {

	                inputs[i].style.borderColor = "red";
                    if (!setfocus) {
                        inputs[i].focus();
                        setfocus = 1;
                    };
                    error = 2;

                } else {				
                    inputs[i].style.borderColor = "";
                };
            };
        };
    }
    
    var selects = form.getElementsByTagName("select");
    for (i=0; i<selects.length; i++) {
        if (selects[i].title == "required") {
            if (selects[i].value == "") {
                //alert("Please complete all fields.");
                selects[i].style.borderColor = "red";
                if (!setfocus) {
                    selects[i].focus();
                    setfocus = 1;
                };
                //return false;
                error = 1;
            } else {
				
                selects[i].style.borderColor = "";
            };
        };
    }
    
    var textareas = form.getElementsByTagName("textarea");
    for (i=0; i<textareas.length; i++) {
        if (textareas[i].title == "required") {
            if (textareas[i].value == "") {
                //alert("Please complete all fields.");
                textareas[i].style.borderColor = "red";
                if (!setfocus) {
                    textareas[i].focus();
                    setfocus = 1;
                };
                //return false;
                error = 1;
            } else {
                textareas[i].style.borderColor = "";
            };
        };
    }
    
    if (error == 1) {
		alert("Required fields have been left blank.");
		return false;
	} else if (error == 2) {
		alert("An email address is of incorrect format.");
		return false;
	} else {
	    return true;
	};
    
}

//JAVASCRIPT FLASH INSERTION
//--------------------------

function flash(url, img, width, height, flashversionuser) {

    if (!flashversionuser) {
        flashtest = 6;
    } else {
        flashtest = parseInt(flashversionuser);
    };
    
    //alert("Flash installed (2=yes): "+FlashDetect.installed);
    //alert("Flash version installed: "+FlashDetect.major);    
    //alert("Testing for: "+flashtest);        

    if (FlashDetect.installed == true && FlashDetect.major >= flashtest) {
        
        document.write('<object width="'+width+'" height="'+height+'" align="middle" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">');
        document.write('<param value="sameDomain" name="allowScriptAccess" />');
        document.write('<param value="'+url+'" name="movie" />');
        document.write('<param value="high" name="quality" />');
        //document.write('<param value="noscale" name="scale" />');
        document.write('<param value="transparent" name="wmode" />');
        document.write('<param value="lt" name="salign" />');
        document.write('<param value="#ffffff" name="bgcolor" /><embed width="'+width+'" height="'+height+'" align="middle" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="sameDomain" wmode="transparent" bgcolor="#ffffff" salign="lt" quality="high" src="'+url+'">');
        document.write('</embed>');
        document.write('</object>');
    } else {
        document.write('<img src="'+img+'" width="'+width+'" height="'+height+'" alt="" />');
    };
    
    flashtest == null;

}

//IMAGE HOVER FUNCTION
//--------------------

function imghover() {

    var hoverArray = new Array();
    var j = 0;
    var elements = document.getElementsByTagName("img");
    
    for (i=0; i<elements.length; i++) {
        if (elements[i].className.split(" ")[0]) {
            hoverArray[j] = elements[i];
            j++;
        };
    };

    for (i=0; i<hoverArray.length; i++) {
        
        var originalText = hoverArray[i].src;

        hoverArray[i].onmouseout = function() {
            this.src = originalText;
        };
        
        hoverArray[i].onmouseover = function() {
            this.src = this.className.split(" ")[1];
        };
        
        new Image().src = hoverArray[i].className.split(" ")[1];
        
    };
    
}

//ONDOM READY FUNCTION
//--------------------
function onDomReadyLoad() {

	//quit if this function has already been called
	if (arguments.callee.done) return;
	
	//flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	
	//kill the timer
	if (_timer) {
		clearInterval(_timer);
		_timer = null;
	};
	
    onDomReady();
    
};

//CALL ONDOMREADY FUNCTION
//for Mozilla
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", onDomReadyLoad, false);
};

/*for Internet Explorer*/
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			onDomReadyLoad(); // call the onload handler
		};
	};
/*@end @*/

//for Safari
if (/WebKit/i.test(navigator.userAgent)) {
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			onDomReadyLoad();
		};
	}, 10);
};

//for other browsers
window.onload = onDomReadyLoad;