var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";


 var bookmark = function(obj) {
 	var url = window.location.href;
 	var title = document.title;
	var e = window.event || arguments.callee.caller.arguments[0];
	var B = {
		IE : /MSIE/.test(window.navigator.userAgent) && !window.opera,
		FF : /Firefox/.test(window.navigator.userAgent),
		OP : !!window.opera
	};

	obj.onmousedown = null;

	if (B.IE) {
		obj.attachEvent("onmouseup", function () {
			try {
				window.external.AddFavorite(url, title);
				window.event.returnValue = false;
			} catch (exp) {
			}
		});
	} 
	else {
		if (B.FF || obj.nodeName.toLowerCase() == "a") {
			obj.setAttribute("rel", "sidebar"),
			obj.title = title,
			obj.href = url;
		} else if (B.OP) {
			var a = document.createElement("a");
			a.rel = "sidebar",
			a.title = title,
			a.href = url;
			obj.parentNode.insertBefore(a, obj);
			a.appendChild(obj);
			a = null;
		}
	}
};

// COOKIES

function writeCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	writeCookie(name,"",-1);
}

// VALIDATION
function checkPhone(o){
	if(!isValidUKPhoneNumber(o.val())){
		o.addClass( "ui-state-error" );
		updateTips(telNumberErrors[telNumberErrorNo]);
		return false;
	}else{
		return true;
	}
}
		
		
function isValidUKPhoneNumber (telephoneNumber) {
	// Convert into a string and check that we were provided with something
	var telnum = telephoneNumber + " ";
	if (telnum.length == 1)  {
		telNumberErrorNo = 1;
		return false
	 }
	telnum.length = telnum.length - 1;
  
 	 // Don't allow country codes to be included (assumes a leading "+")
	var exp = /^(\+)[\s]*(.*)$/;
	if (exp.test(telnum) == true) {
		 telNumberErrorNo = 2;
		 return false;
	}
  
  	// Remove spaces from the telephone number to help validation
	while (telnum.indexOf(" ")!= -1)  {
		telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  	}
  
  	// Remove hyphens from the telephone number to help validation
	while (telnum.indexOf("-")!= -1)  {
		telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
 	}  
  
 	// Now check that all the characters are digits
	exp = /^[0-9]{10,11}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 3;
		return false;
	}
  
	// Now check that the first digit is 0
	exp = /^0[0-9]{9,10}$/;
	if (exp.test(telnum) != true) {
	 	telNumberErrorNo = 4;
	return false;
	}

	// Disallow numbers allocated for dramas.
 
	// Array holds the regular expressions for the drama telephone numbers
	var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);

	for (var i=0; i<tnexp.length; i++) {
		if ( tnexp[i].test(telnum) ) {
			telNumberErrorNo = 5;
			return false;
		}	
	}

	// Finally check that the telephone number is appropriate.
	exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 5;
		return false;
	}
  
  	// Telephone number seems to be valid - return the stripped telehone number  
	return telnum;
}

function isValidEmail(email) {
	// SURELY THERE IS A CLEARER ONE THAN THIS
		
	if (!checkLength( email, "email", 6, 80 )) {
		return (false);
	}
		
	// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
	return (checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" ));
}

function checkLength( o, n, min, max ) {
	if ( o.val().length > max || o.val().length < min ) {
		o.addClass( "ui-state-error" );
		updateTips( "Length of " + n + " must be between " +min + " and " + max + "." );
		return false;
	} else {
		return true;
	}
}

function checkRegexp( o, regexp, n ) {
	if ( !( regexp.test(o.val() ) ) ) {
		o.addClass( "ui-state-error" );
		updateTips( n );
		return false;
	} else {
		return true;
	}
}
	
function updateTips( t ) {
	$(".validateTips")
		.text( t )
		.addClass( "ui-state-highlight" );
	setTimeout(function() {
		$(".validateTips").removeClass( "ui-state-highlight", 1500 );
	}, 500 );
}

function clearInputText(theText) {
     if (theText.value == theText.defaultValue) {
         theText.value = ""
     }
 }
