//
//$Log: utils.js,v $
//Revision 1.10  2007/01/07 10:23:10  diginnet
//fix to currencyString()
//
//Revision 1.9  2006/12/31 10:48:45  imogen
//Add cvs $Log: utils.js,v $
//Add cvs Revision 1.10  2007/01/07 10:23:10  diginnet
//Add cvs fix to currencyString()
//Add cvs
//
//
function screenObject()
{
	this.x = 0;
	this.y = 0;
	this.dragging = false;
	this.message = "";
}


function strltrim() 
{
	return this.replace(/^\s+/,'');
}

function strrtrim() 
{
	return this.replace(/\s+$/,'');
}

function strtrim() 
{
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

function strxtrim()
{
	return this.replace(/^[^a-zA-Z0-9]+/, '').replace(/[^a-zA-Z0-9]+$/, '');
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;
String.prototype.xtrim = strxtrim;

function validate_booking_query()
{
	var surname = getObjValue("booking_surname");
	
	if (surname == null || surname == "")
	{
		alert("Please enter your surname");
		return false;
	}
	
	if (!checkEmail("booking_email"))
	{
		return false;
	}
	
	if (!checkPhone("booking_phone"))
	{
		return false;
	}
	
	
	return true;
}

function checkPhone(phoneId)
{
	var phoneObj = findObj(phoneId);
	if (phoneObj)
	{
		if (phoneObj.value.trim().length > 0)
		{
			return true;
		}
	}
	
	alert("Please check your phone number");
	
	return false;
}

function conditionalLink(question, url) {
  if (confirm(question)) 
  {
    document.location = url;
  }
}

function findObj(n) { 
  
  if(document.getElementById) 
  {
  	return document.getElementById(n); 
  }
  
 
  return null;
}

function isVisible(obj)
{
	var o = findObj(obj);
	
	if (!o)
	{
		return false;
	}
	
	return (o.style.visibility == "visible");
	
}

function swapImg(imgid, imgsrc)
{
	var img = findObj(imgid);
	
	if (!img)
	{
		alert("not found");
		return;
	}
	
	img.src = imgsrc;
	return;
	
}

function setObjectVisibility(objid, bool)
{
	if (bool)
	{
		showObject(objid);
	}
	else
	{
		hideObject(objid);
	}
	
	return;
}

function showObject(objid)
{
	var obj = findObj(objid);
	
	if (obj)
	{
		obj.style.visibility = "visible";
	}
	
	return;
	
}

function showObjectAt(objid, x, y)
{
	var obj = findObj(objid);
	
	moveObjectTo(objid, x, y);
	showObject(objid);
	
	return;
	
}

function moveObjectTo(objid, x, y)
{
	var obj = findObj(objid);
	
	if (obj)
	{
		obj.style.left = x;
		obj.style.top = y;
	}
	
	return;
	
}

function hideObject(objid)
{
	var obj = findObj(objid);
	
	if (obj)
	{
		obj.style.visibility = "hidden";
	}
	
	return;
	
}

function getObjValue(objid)
{
	var obj = findObj(objid);
	
	if (obj)
	{
		return obj.value;
	}
	
	return null;
	
}

function setObjValue(objid, val)
{
	var obj = findObj(objid);
	
	if (obj)
	{
		var old_val = obj.value;
		obj.value = val;
		return old_val;
	}
	
	return null;
	
}

function setCheckbox(objid, true_false)
{
	var obj = findObj(objid);
	
	if (obj)
	{
		var old_val = obj.checked;
		obj.checked = true_false;
		return old_val;
	}
	
	return null;
	
}

function getListSelectedIdx(objid)
{
	var obj = findObj(objid);
	var idx = -1;
	
	if (obj)
	{
		idx = obj.selectedIndex;
	}
	
	return idx;

}

function setSelectedIndex(objid, idx)
{
	var obj = findObj(objid);
	if (!obj)
	{
		return;
	}
	
	obj.selectedIndex = idx;
	
}



//	convert a decimal / int value to
//	a currency string with 2 dp
//	so    1 => 1.00
//	   10.5 => 10.50
function currencyString(val)
{
	var pence = parseInt(parseFloat(val) * 100.0) % 100;
	var pounds = parseInt(val);
	
	if (pence < 10)
	{
		return pounds + ".0" + pence;
	}
	else
	{
		return pounds + "." + pence;
	}
	
}

function syncXYCoords(objtosync, syncobj)
{
	var syncobj_coords = getScreenCoords(syncobj);
		
	moveObjectTo(objtosync, syncobj_coords.x, syncobj_coords.y)

	return;
}

function getScreenCoords(objid)
{
	var thing = new screenObject();
	thing.x = 0;
	thing.y = 0;
	thing.message = "Not Found";

	var objOfInterest = findObj(objid);
	
	if (!objOfInterest)
	{
		return thing;
	}
	
	thing.x = objOfInterest.offsetLeft;
	thing.y = objOfInterest.offsetTop;
	thing.message = "Found";
	
	var numParents = 0;
	
	while (objOfInterest = objOfInterest.offsetParent)
	{
		thing.x += objOfInterest.offsetLeft;
		thing.y += objOfInterest.offsetTop;
		++numParents;
	}

	return thing;
}

	/*
		validEmail
		==========
		(c) laccata 2003
		
		NOTE:
		This is a simple email validator
		
				
	*/
function checkEmail(emailId)
{
	var emailObj = findObj(emailId);
	if (emailObj)
	{
		return validEmail(emailObj);
	}
	
	return false;
}

function validEmail(formControl) {

	pattern = new RegExp("^[a-zA-Z0-9]+([\\-_\\.]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\\-_\\.]?[a-zA-Z0-9]+)*\\.[a-zA-Z]{2,4}$");
	if (!pattern.test(formControl.value))
	{
		alert("Please check your email address");
		return false;
	}
			
	return true;
	
}

	/*
		validName
		==========
		(c) laccata 2003
		
		Simple name validator.
		A string starting with a-z or A-Z.
		Any more rules, let me know ...
	*/
function validName(formControl) {

	pattern = new RegExp("^[a-zA-Z]");
	
	if (!pattern.test(formControl.value.trim()))
	{
		alert("You've forgotten to enter your name");
		return false;
	}
	
	return true;
		
}

function validPostcode(formControl, country_code) {

	if (country_code != 'uk')
	{
		if (formControl.value.trim().length == 0)
		{
			alert("You've forgotten to enter a postcode");
			return false;
		}
		else
		{
			return true;
		}
	}
		
	//uk postcodes can be validated properly
	pattern = new RegExp("^[a-zA-Z]{1,2}[0-9]{1,2}[ ]*[0-9][a-zA-Z][a-zA-Z]");
	
	if (!pattern.test(formControl.value.trim()))
	{
		alert("Please check your postcode");
		return false;
	}
	
	return true;
		
}


function validAddress(formControl) {

	if (formControl.value.trim().length == 0)
	{
		alert("You've forgotten to enter your address");
		return false;
	}
	
	return true;
		
}

function validDate(yyyy, mm, dd) 
{

	var today = new Date();
	var days_in_month;
	var check_it;
	var leap;
		
	--mm;	//for zero-based month
	
	check_it = new Date(yyyy, mm, dd, 0, 0, 1); 
	
	leap = ( (yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy %400 == 0) );
		
		
	days_in_month = (mm == 1) ? (leap ? 29 : 28) :
			(mm == 3 || mm == 5 || mm == 8 || mm == 10) ? 30 :
			31; 
	
	//alert("checkit date : " +  yyyy + "-" + 
	//		(mm + 1) + "-" + dd + " , days_in_month = " + days_in_month);
	
	if 	(dd > days_in_month)
	{
		alert("Invalid Date : " + yyyy
			+ ":" + (mm+1) + ":" + dd);
		return false;
	}
	
	return true;
			
	}