function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {window.onload = func;}
	else {window.onload = function() {
		if (oldonload) {oldonload();}
		func();}}
}

function createObject(){

	var request_object;
	var browser = navigator.appName;
	
	if (browser == "Microsoft Internet Explorer"){
		request_object = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		request_object = new XMLHttpRequest();
	}
	
	return request_object;

}

function flagForm(element){
	$('#' + element).addClass("highlight");
	$('#' + element).blur(function(){
		flagFormRestore(element);
	});
	$('#' + element).focus();
}

function flagFormRestore(element){
	$('#' + element).removeClass("highlight");
}

function productDetails(id){
	$('#ie6background').css("display", "block");
	$('#productOverlay').css("display", "block");
	$('#productWindow').slideDown("slow");
	
	// reset any previous requests
	$('#productDetails').css('display', 'none');
	$('#loadingDetails').css('display', 'inline');
	
	// send the request
	req = createObject();
	req.onreadystatechange = function(){ productDetailsComplete() };
	req.open("GET", "/callers/productDetails.asp?id=" + id, true);
	req.send(null);
	
	return false;
}

function productDetailsComplete(){
	
	if (req.readyState == 4){
	
		// build response object
		var data = req.responseText;
		
		// swap around data
		$('#productDetails').html(data);
		$('#loadingDetails').css('display', 'none');
		$('#productDetails').css('display', 'inline');
	
	}

}

function productDetailsClose(){

	$('#productWindow').slideUp("slow", productDetailsCloseDone);
	return false;

}

function productDetailsCloseDone(){
	$('#ie6background').css("display", "none");
	$('#productOverlay').css("display", "none");
}

function scrollProducts(direction){

	// check if it is currently working already
	if (isWorking == true){
		return false;
	}
	
	// calculate current margin
	currentMargin = $('#products').css("marginLeft");
	currentMargin = currentMargin.replace("px", "");
	currentMargin = parseInt(currentMargin);

	// calculate new margin
	if (direction == 'left'){
		newMargin = (currentMargin + 744);
	} else {
		newMargin = (currentMargin - 744);
	}
	
	newMarginString = String(newMargin + "px");
	newPage = ((0 - (newMargin / 744)) + 1);
	
	// change page URL
	if (newMargin == 0){
		window.location.hash = '/pages/products/' + pageSlug;
	} else {
		window.location.hash = '/pages/products/' + pageSlug + ',' + newPage;
	}
	
	// move the content
	isWorking = true;
	
	$('#products').animate({
		marginLeft: newMarginString
	}, 1000, scrollProductsEnable );
	
	// check left arrow
	if (newMargin >= 0){
		$('#scrollLeft').css("display", "none");
	} else {
		$('#scrollLeft').css("display", "block");
		
		if (newPage > 2){
			$('#scrollLeft').attr("href", pageSlug + "," + (newPage - 1));
		} else {
			$('#scrollLeft').attr("href", pageSlug);
		}
	}
	
	// check right arrow
	if (newMargin <= -((totalPages - 1) * 744)){
		$('#scrollRight').css("display", "none");
	} else {
		$('#scrollRight').css("display", "block");
		$('#scrollRight').attr("href", pageSlug + "," + (newPage + 1));
	}
	
	// and return
	return false;
}

function scrollProductsEnable(){

	// we're finished animating
	isWorking = false;

}

function validateBrochureRequest(){

	// check for missing values
	if ($('#firstname').val() == ''){
		alert("You did not enter your first name.");
		flagForm("firstname");
		return false;
	} else if ($('#surname').val() == ''){
		alert("You did not enter your surname.");
		flagForm("surname");
		return false;
	} else if ($('#email').val() == ''){
		alert("You did not enter your email address.");
		flagForm("email");
		return false;
	} else if ($('#address1').val() == ''){
		alert("You did not enter your address.");
		flagForm("address1");
		return false;
	} else if ($('#town').val() == ''){
		alert("You did not enter your nearest town or city.");
		flagForm("town");
		return false;
	} else if ($('#postcode').val() == ''){
		alert("You did not enter your postcode.");
		flagForm("postcode");
		return false;
	} else if ($('#phone').val() == ''){
		alert("You did not enter your telephone number.");
		flagForm("phone");
		return false;
	}
	
	// else allow form submission
	return true;

}

function validateFindStockist(){

	// check for missing values
	if ($('#postcodeSearch').val() == ''){
		alert("You did not enter your postcode.");
		flagForm("postcodeSearch");
		return false;	
	}
	
	// else allow form submission
	return true;

}

function validateUserCreate(){

	// check for missing values
	if ($('#email').val() == ''){
		alert("You did not enter an email address.");
		flagForm("email");
		return false;	
	} else if ($('#password').val() == ''){
		alert("You did not enter a password.");
		flagForm("password");
		return false;	
	} else if ($('#name').val() == ''){
		alert("You did not enter a name.");
		flagForm("name");
		return false;	
	}
	
	// else allow the form to go through
	return true;

}

function validateUserEdit(){

	// check for missing values
	if ($('#email').val() == ''){
		alert("You did not enter an email address.");
		flagForm("email");
		return false;		
	} else if ($('#name').val() == ''){
		alert("You did not enter a name.");
		flagForm("name");
		return false;	
	}
	
	// else allow the form to go through
	return true;

}

function viewTech(){

	// work out what to do
	if (showingTech == true){
	
		$('div.techBox').css("display", "none");
		$('div.imgBox').css("display", "block");
		showingTech = false;
		$('a#techLink').html("View tech spec");
	
	} else {
	
		$('div.imgBox').css("display", "none");
		$('div.techBox').css("display", "block");
		showingTech = true;
		$('a#techLink').html("View product image");
	
	}
	
	// and return
	return false;

}
