/*
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";
	}
	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")
}