/*
Studio 2 Online (www.s2o.co.uk)
-----------------------------------------------------------
This file contains functions used in leisureshack.co.uk

Author: andrew mcmahon
Date: 2006-01-18
*/

function CheckNewsletterForm(){
	alertmessage = '';
	
	if (document.header_newsletter.name.value == '') {
		alertmessage += "Please enter your name.\n";
	}
	
	if (document.header_newsletter.email_address.value == '') {
		alertmessage += "Please enter your email address.\n";
	}else{
		if(!isEmailAddr(document.header_newsletter.email_address.value)){
			alertmessage += "Your email address is invalid.\n";
		}
	}

	if(alertmessage != ''){
		alert(alertmessage);
		return false;
	}
	return true;
}
		

function in_array(needle, haystack){
	for (var i in haystack){
		if(needle == haystack[i]){
			return true;
		}
	}
	return false;
}

function isNumeric(input){
	var allowed_chars = new Array(1,2,3,4,5,6,7,8,9,0);
	for(var i = 0; i < input.length; i++){
		if(!in_array(input.charAt(i), allowed_chars)){
			return false;
		}
	}
	return true;	
}

function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function empty(input){
	if(input == ""){
		return true;
	}
	
	if(typeof(input) == "boolean"){
		if(input != true && input != false){
			return true;
		}
	} 
	
	if(typeof(input) == "number"){
		if(input == 0){
			return true;
		}
	} 
	
	if(typeof(input) == "string"){
		if(input == ""){
			return true;
		}
	} 
	
	if(typeof(input) == "object"){
		if(input.length == 0){
			return true;
		}
	}
	return false;
}

function getErrorMessage(input){
	var output = "";
	for(i in input){
		output += input[i] + "\n";
	}
	//alert(output);
	return output;
}

function isEmailAddr(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
	return false;
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	return false;
	}
	
	if (str.indexOf(dot,(lat+2))==-1){
	return false;
	}
	
	if (str.indexOf(" ")!=-1){
	return false;
	}

	return true;					
}

//this functions opens a pop up window, provide path, window height and width
function popup_link(path,height,width) //used by info popups on OSB pages
{
		//We are opening a link to an external page with supplied path, height and width
		window.open(path, "myWindow", "status = 1, height ="+height+", width ="+width+", resizable = 0")
}

//this function sets the dimensions of an image, provide the image id and the proportion
function setImageDim(imageName,proportion)
{
	myImage = document.getElementById(imageName)
	//alert("image1 width : " + myImage.width + "px\nimage1 height : " + myImage.height + "px")
	
	if(myImage.width > myImage.height){
		
		var newHeight=0
		newHeight = (proportion / myImage.width) *  myImage.height
		
		myImage.width=proportion
		myImage.height=newHeight
		
	}
	else{
		
		var newWidth=0		
		newWidth = (proportion / myImage.height) *  myImage.width
		
		myImage.width=newWidth
		myImage.height=proportion
	}
	//debug - outputs the current height and width
	//alert("image1 width : " + myImage.width + "px\nimage1 height : " + myImage.height + "px")
}

function popup_link(path,height,width) //used by info popups on OSB pages
{
		//We are opening a link to an external page with supplied path, height and width
		window.open(path, "myWindow", "status = 1, height ="+height+", width ="+width+", resizable = 0, scrollbars = 1")
}

function createRequestObject() {
	var req;

	if(window.XMLHttpRequest){
		// Firefox, Safari, Opera...
		req = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		// Internet Explorer 5+
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		// There is an error creating the object,
		// just as an old browser is being used.
		alert('Problem creating the XMLHttpRequest object');
	}
	return req;

}

// Make the XMLHttpRequest object
var http = createRequestObject();

function sendRequest(id) {
	
	// Open PHP script for requests
	
	http.open('get', 'http://www.discountmobilityplus.co.uk/buildCompare.php?id='+id);
	http.onreadystatechange = handleResponse;
	
	http.send(null);
}
(function ($) {
$(function () {
	$('#how-to-decide-form input, #how-to-decide-form select').change(function () {
		$('#response').html('<p class="loading">Loading...</p>');	
		$.ajax({
			data: $('#how-to-decide-form').serialize(),
			error: function () {
				$('#response').html('<p class="error">An error has occured, please try again.</p>');
			},
			success: function (data) {
				$('#response').html(data);
			},
			url: '/showProducts.php'
		});
	});
	$('#how-to-decide-form input').trigger('change');
});

}(jQuery));

function handleResponse() {
	if(http.readyState == 4 && http.status == 200){

		// Text retu0rned FROM the PHP script
		var response = http.responseText;

		if(response) {
			// UPDATE ajaxTest content
			//alert(response);
			document.getElementById("compareResponse").innerHTML = response;
			refreshSrc('compare-frame');
			jQuery('#compare-popup').fadeIn().delay(2000).fadeOut();
		}
	}
}
function handleResponse2() {
	
	if(http.readyState == 4 && http.status == 200){

		// Text returned FROM the PHP script
		var response = http.responseText;
		
		//alert(response);
		
		if(response) {
			// UPDATE ajaxTest content
			//alert(response);
			document.getElementById("response").innerHTML = response;
		}
	}
}

function refreshSrc(element) {
	var f = document.getElementById(element);
	f.src = f.src;	
}