// BROWSER DETECTION
function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}
var bw=new lib_bwcheck()

/* writes flash animation to screen 
 * PARAMS
 * 	src: the path of the flash file
 * 	width: width of the swf
 * 	height: height of the swf
 */
 
function writeFlash(src, width, height, alternateSrc, redirect) {
	if ((flashinstalled == 2) && (flashversion > 9) && src) {
		document.write('<object width="' + width + '" height="' + height + '" id="viewer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0">');
		document.write('<param name=movie value="' + src + '">');
		document.write('<param name="quality" value="high">');
		document.write('<param name="wmode" value="transparent">');
		document.write('<embed name="viewer" wmode="transparent" swliveconnect="true" width="' + width + '" height="' + height + '" src="' + src + '" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash"></embed>');
		document.write('</object>');
		return true;
	} else {
	 	if (redirect == 1)
	 		redirect	= 'http://www.macromedia.com/go/getflashplayer';
		if (redirect)
			document.write('<a href="' + redirect + '" target="_blank">');
		if (alternateSrc != "")
			document.write('<img src="' + alternateSrc + '" width="' + width + '" height="' + height + '" alt="" border="0" usemap="#nav">');
	 	if (redirect)
			document.write('</a>');
		return false;
	}
}

function clientThumb(swf_src, gif_src, jpg_src, width, height, url) {
	if (gif_src)
		alternateSrc	= gif_src;
	else
		alternateSrc	= jpg_src;
	
	writeFlash(swf_src, width, height, alternateSrc, url);
}

// IFRAME RESIZE
function resize() {
	if (parent.frames.length == 0) {
		urlArray	= new String(document.URL).split("/");
//		str = new String(urlArray[urlArray.length-1]).replace(/\?/, "%3F");
		str = escape(urlArray[urlArray.length-1]);
//		str = new String(urlArray[urlArray.length-1]).replace(/\&/, "%26");
//		alert(str);
		document.location = "index.php?redirect=" + str;
		return false;
	}
	parent.scrollTo(0,0);
	parent.document.getElementById("main_div").style.height = 1;
	parent.document.getElementById("main_div").style.height = window.document.body.scrollHeight + 5;
	return true;
}

// TEXTBOX FOCUS HANDLERS
function clearText(object, val) {
	if (object.value == val)
		object.value = "";
}

function restoreText(object, val) {
	if (object.value == "")
		object.value = val;
}

function setText(object, val) {
	object.value = val;
}

function addText(destination, addition, lowerLimit) {
	destination.value = Math.max(parseInt(destination.value) + parseInt(addition), lowerLimit);
}

// SELECT LIST SELECTOR

function checkSelected(formField, value) {
	for (i=0; i<formField.options.length; i++) {
		if (formField.options[i].value == value) {
			formField.selectedIndex = i;
			return i;
		}
	}
}

// IMAGE PRELOADER
var preloadArray = new Array();

function imagePreloader(key, src) {
	preloadArray[key] = new Image();
	preloadArray[key].src = src;
}

// IMAGE ROLLOVER
var rollArray	= new Array();
var normArray	= new Array();
var setArray	= new Array();

function rollerPics(key, normSrc, rollSrc) {
	normArray[key] = new Image();
	normArray[key].src = normSrc;
	rollArray[key] = new Image();
	rollArray[key].src = rollSrc;
}

function initSet(setName) {
	setArray[setName] = new Array();
}

function addToSet(imageID, setName) {
	setArray[setName].push(imageID);
}

function setOn(setName, key) {
	for (i=0; i<setArray[setName].length; i++) {
		setArray[setName][i].src = rollArray[key].src;
	}
}

function setOff(setName, key) {
	for (i=0; i<setArray[setName].length; i++) {
		setArray[setName][i].src = normArray[key].src;
	}
}

function rollOn(imageID, key) {
	imageID.src = rollArray[key].src;
}

function rollOff(imageID, key) {
	imageID.src = normArray[key].src;
}

function change(id, newClass) {
	id.className = newClass;
}

// POPUP WINDOW

function popupWin(url, name, width, height) {
	var left = (screen.width / 2) - width / 2;
	//var top = (screen.height / 2) - height.substr(0,3) / 2;
	var top = (screen.height / 2) - height / 2;
	if (arguments[4] != "")
		attributes = arguments[4];
	else
		attributes = "";
	var newPopup = window.open(url, name, "width=" + width + ", height=" + height + ", left=" + left + ", top=" + top + attributes);
	newPopup.focus();
}

// DIV COMPONENTS

function popupDiv(div, left, top, content) {
	div.style.visibility = 'visible';
	div.style.left = left;
	div.style.top = top;
	div.innerHTML = content;
}

function hideDiv(div) {
	div.style.visibility = 'hidden';
}

function unhideDiv(div) {
	div.style.visibility = 'visible';
}

// FORM VALIDATION

function validateForm(url, name, type, width, height, form, message, fields) {
	var left = (screen.width / 2) - width / 2;
	var top = (screen.height / 2) - height / 2;
	if (arguments[8] != "")
		attributes = arguments[8];
	else
		attributes = "";

	if (type == "popup")
		breakTag = "<br><br>";
	else if (type == "alert")
		breakTag = "\n";
		
	var message;
	doSubmit = true;
	for (i=0;i<fields.length;i+=3) {
		switch (fields[i + 2])
		{ 
		   case "select" :
			 if (form.elements[fields[i]].selectedIndex == 0) {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
		     break; 
		   case "email" :
			 if (form.elements[fields[i]].value == "") {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 else {
				var emailCheck = validateEmailAddress(form.elements[fields[i]].value);
				if (emailCheck != "") {
					message += emailCheck;
					doSubmit = false;
				}
			  }
			  break;
		   case "text" :
			 if (form.elements[fields[i]].value == "") {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 break;
		   case "radio" :
			 if (form.elements[fields[i]].value == null) {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 break;
		   case "textarea" :
			 if (form.elements[fields[i]].value == "") {
				message += breakTag + fields[i + 1];
				doSubmit = false;
			 }
			 break;
		   default :
		}
	}
	if (doSubmit) {
		form.submit();
	} else {
		if (type == "popup") {
			url += '&message=' + message + '';
			var newPopup = window.open(url, name, "width=" + width + ", height=" + height + ", left=" + left + ", top=" + top + attributes);
			newPopup.focus();
		} else if (type == "alert")
			alert(message);
	}
}

function validateEmailAddress(field) {
//	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	var emailMessage = "";
	/*
	for (i=0; i<invalidChars.length; i++) {
		if (field.indexOf(invalidChars.charAt(i),0) > -1) {
			emailMessage = "\nEmail address contains invalid characters";
		}
	}
	for (i=0; i<field.length; i++) {
	   if (field.charCodeAt(i)>127) {
			emailMessage = "\nEmail address contains non ascii characters.";
		}
	}
	*/
	var atPos = field.indexOf('@',0);
	if (atPos == -1) {
			emailMessage = "\nEmail address must contain an @.";
	}
	if (atPos == 0) {
			emailMessage = "\nEmail address must not start with @.";
	}
	if (field.indexOf('@', atPos + 1) > - 1) {
			emailMessage = "\nEmail address must contain only one @.";
	}
	if (field.indexOf('.', atPos) == -1) {
			emailMessage = "\nEmail address is incomplete.";
	}
	if (field.indexOf('@.',0) != -1) {
			emailMessage = "\nInvalid email address.";
	}
	if (field.indexOf('.@',0) != -1){
			emailMessage = "\nInvalid email address.";
	}
	if (field.indexOf('..',0) != -1) {
			emailMessage = "\nInvalid email address.";
	}
	var suffix = field.substring(field.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
			emailMessage = "\nInvalid email address.";
	}
	return(emailMessage);
}

// LAYER SWITCHER

function divObject(thediv) {
	e = this;
	e.theDiv = thediv;
}

function showRemote(aDiv, timer) {
	if (window_main.showDiv && document.loaded)
		window_main.showDiv(aDiv, timer);
}

function showDiv(aDiv, timer) {
	if (timer) { closeTimer = setTimeout("closeDiv(" + aDiv + ")", timeout); }
	if (openDiv) {
		divArray[openDiv - 1].theDiv.visibility = "hidden";
	}
	divArray[(aDiv - 1)].theDiv.visibility = "visible";
	openDiv = aDiv;
	special = false;
}

function closeDiv(aDiv) {
	divArray[aDiv - 1].theDiv.visibility = "hidden";
	openDiv = 0;
}

function closeRemote() {
	if (window_main.closeAll && document.loaded)
		window_main.closeAll();
}

function closeAll() {
	if (openDiv) {
		divArray[openDiv - 1].theDiv.visibility = "hidden";
	}
	openDiv = 0;
}

function resetTimer() {
	clearTimeout(closeTimer);
}

function buildDiv(num) {
	temp = document.getElementById("popup"+num);
	tempDiv = temp.style;
	divArray[num-1] = new divObject(tempDiv);
}

function setLoaded(val) {
	parent.document.loaded = val;
}

function remote(channel) {
	switch (channel) {
		case 1:
			popupWin('popup_advert_2007.php?id=' + advertID + '&lang=' + advertLang, 'popup_advert', advertWidth, advertHeight);
			break;
		default:
			alert('invalid channel ' + channel + ' - please refer to owner\'s manual or contact technical support.');	
	}
}

// FS COMMAND

var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the the FSCommand messages in a Flash movie
function viewer_DoFSCommand(command, args) 
{
  var sampleFlaMovieObj = 
    InternetExplorer ? viewer : document.viewer;
  	
	eval(args);
	
}
// Hook for Internet Explorer
if (navigator.appName && 
  navigator.appName.indexOf("Microsoft") != -1 &&
  navigator.userAgent.indexOf("Windows") != -1 && 
  navigator.userAgent.indexOf("Windows 3.1") == -1) 
{
  document.write('<SCRIPT LANGUAGE=VBScript\> \n');
  document.write('on error resume next \n');
  document.write(
    'Sub viewer_FSCommand(ByVal command, ByVal args)\n');
  document.write(
    ' call viewer_DoFSCommand(command, args)\n');
  document.write('end sub\n');
  document.write('</SCRIPT\> \n');
}

// AJAX

var xmlHttp;
var xmlHttpArray = Array();

function getUrl(page, doThis) {
	var url= page;
	xmlHttp=GetXmlHttpObject(doThis);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}

function getUrlArray(page, doThis, objName) {
	var url = page;
	xmlHttpArray[objName] = GetXmlHttpObject(doThis);
	xmlHttpArray[objName].open("GET", url , true);
	xmlHttpArray[objName].send(null);
}

function postUrl(url, doThis, sendThis) {
	xmlHttp=GetXmlHttpObject(doThis);
	xmlHttp.open("POST", url , true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(sendThis);
}

function getFormData(form) {
	str = '';
	for (i=0; i<form.elements.length; i++) {
		if (i > 0)
			str += '&';
		
		name = form.elements[i].name + '';
		
		switch(form.elements[i].type) {
			case "checkbox":
			case "radio":
				if (!form.elements[i].checked) {
					value	= '';
					break;
				}
			default:
				value	= form.elements[i].value + '';
		}
		
		if (name.charAt(name.length - 1) == ']')
			name = name.substr(0, name.length - 2) + '%5B%5D';
		
		if (value != '')
			str += name + '=' + value;
	}
	return str;
}

function GetXmlHttpObject(handler) {
	var objXmlHttp = null;
	
	if (window.ActiveXObject) { // IE
		try {
			if (!objXmlHttp) {
				objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
		} catch (e) {
			try {
				objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				try {
					objXmlHttp = new XMLHttpRequest();
				} catch (e) {
					alert(e);
				}
			}
		}
		if (handler)
			objXmlHttp.onreadystatechange = handler;
		
	} else if (window.XMLHttpRequest) { // Mozilla, Safari,...
		objXmlHttp = new XMLHttpRequest();
		if (handler) {
			objXmlHttp.onload = handler;
			objXmlHttp.onerror = handler;
		}
	}
	
	return objXmlHttp;
}

// AJAX FUNCTIONS

function mainDiv() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("main_div").innerHTML = xmlHttp.responseText;
	} 
}

function displayMessage() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		popupDiv(document.getElementById("message_div"), (screen.availWidth / 2) - 110, 220, html_message(xmlHttp.responseText, 'message_div'));
	} 
}

function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("main_div").innerHTML = xmlHttp.responseText 
	} 
}

function updateCart() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		if (document.cartFrm.grand_total)
			document.cartFrm.grand_total.value = xmlHttp.responseText;
	} 
}

function addToCart() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		document.getElementById("message").innerHTML = xmlHttp.responseText 
	} 
}
