function setFocus(elementid) {
  var item = document.getElementById(elementid);
  if (item) {
    item.focus();
  }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

/*
Clear the search box
*/
function clearSearch(deftxt){

	var searchbox;
	searchbox = document.getElementById('searchbox');

	if(searchbox.value == deftxt){
		searchbox.value = '';
	}

}//clearSearch

/*
Check search box & submit
*/
function searchSubmit(deftxt){
	var searchbox = document.getElementById('searchbox');
	var valid     = true;

	if(searchbox.value == deftxt)    valid = false;
	if(searchbox.value.length <= 0)  valid = false;

	if (!valid) alert('Please enter a search term before clicking on the search button.');
  document.MM_returnValue = valid;

}//clearSearch


/*
Toggle visibility of saved coupons box
*/
function toggleSavedCoupons(){

	var block = document.getElementById('savedbox');

  if(block.style.display == 'none' || block.style.display == ''){
  	block.style.display = 'block';
  }else{
  	block.style.display = 'none';
  }

}//toggleSavedCoupons


/*
Toggle visibility of categories
*/
function catReveal($blockid){

	var block = document.getElementById($blockid);

  if(block.style.display == 'none' || block.style.display == ''){
  	block.style.display = 'block';
  }else{
  	block.style.display = 'none';
  }

}//catReveal


/*
Save coupon
*/
function saveCoupon(couponid, couponname, groupdir){

	window.location.href =
		'/' + groupdir + '/?save_coupon=' + couponid +
		'&save_coupon_name=' + couponname;

}//saveCoupon


/*
Print page
*/
function printPage(){

	window.print();

}//printPage


/*
printCoupon
*/
function printCoupon(recordid){
    var success = function(data) {						
	//alert(data.responseText);	
	}					
	url = '/print_record.php?q='+recordid;															
	var cObj = YAHOO.util.Connect.asyncRequest('GET', url, {success: success, failure: null});  
	window.open('/coupon.php?recordid=' + recordid, 'couponprint', 'scrollbars=1,width=700,height=650');

}//printCoupon


/*
emailFriend
*/
function emailFriend(recordid){

	window.open('/national/friend.html?recordid=' + recordid, 'emailafriend', 'width=400,height=200');

}//emailFriend


/*
Reveal inline contact form on ad pages
*/
function revealInlineContactForm(cfid){

	if ($("#" + cfid).is(":hidden")) {
		$("#" + cfid).slideDown("fast");

	}else{
		$("#" + cfid).slideUp("fast");
	}

}//revealBigMap




/*
Reveal inline review form on ad pages
*/
function revealInlineReviewForm(cfid){
if ($("#" + cfid).is(":hidden")) {
$("#" + cfid).slideDown("fast");

}else{
$("#" + cfid).slideUp("fast");
}

}//revealInlineReviewForm


/*
Reveal home page region detail
*/
function revealRegionDetail(regionid)
{

	if ($("#regdetail-" + regionid).is(":hidden")) {
		$("#regdetail-" + regionid).slideDown("fast");
		$("#regbtn-" + regionid).attr("src","/images/regionbox-ar-up.png");

	}else{
		$("#regdetail-" + regionid).slideUp("fast");
		$("#regbtn-" + regionid).attr("src","/images/regionbox-ar-down.png");
	}

}//revealRegionDetail


/*
Send
*/
function redirectLocalSearch(theform){
	//var baseurl = 'http://ytfront/';
	var baseurl = 'http://ytfrontdevel.webcertain.com/';
	var group   = theform.region.value;
	var query   = theform.q.value;
	window.location = baseurl + group + '/?q=' + query;

}//redirectLocalSearch


/**************
Ad Rotation
***************/

var Adverts    = new Array('r-ad-1', 'r-ad-2', 'r-ad-3');
var no_of_ads  = 3;
var cur_ad_pos = 0;

/*
Initialise ad rotation
*/
function startAdRotate() {

	setInterval('doAdRotate()', 5000);  // timer container

}//startAdRotate


/*
Do ad rotation
*/
function doAdRotate(){

	$("#" + Adverts[cur_ad_pos]).fadeOut('slow');
	cur_ad_pos++;
	if (cur_ad_pos == no_of_ads) cur_ad_pos = 0;
	$("#" + Adverts[cur_ad_pos]).fadeIn('slow');

}//adRotate




/*************
Advertise
*************/

function validate_email(input)
{
	apos=input.indexOf("@");
	dotpos=input.lastIndexOf(".");
	if (apos<1||dotpos-apos<2) return false;
	return true;
}


function adform_check_stage1()
{
	var alertmsg = "";

	if(document.forms['freead'].elements['your_first_name'].value  == "") alertmsg += "- Please enter your first name.\n";
	if(document.forms['freead'].elements['your_last_name'].value   == "") alertmsg += "- Please enter your last name.\n";
	if(!validate_email(document.forms['freead'].elements['your_email'].value)) alertmsg += "- Please enter a valid email address.\n";

	if(alertmsg.length >0)
	{
		alert("Please correct the following problems and then submit the form again:\n\n"+alertmsg);
		return false;
	}
	return true;
}

function adform_check_stage2()
{
	var alertmsg = "";

	if(document.forms['freead'].elements['your_company'].value   == "") alertmsg += "- Please enter your company name.\n";
	if(document.forms['freead'].elements['your_address1'].value  == "") alertmsg += "- Please enter at least one line of your address.\n";
	if(document.forms['freead'].elements['your_city'].value      == "") alertmsg += "- Please enter your city.\n";
	if(document.forms['freead'].elements['your_postcode'].value  == "") alertmsg += "- Please enter your post code.\n";
	if(document.forms['freead'].elements['your_tel'].value       == "") alertmsg += "- Please enter your telephone number.\n";

	if(document.forms['freead'].elements['your_about'].value     == "") alertmsg += "- Please tell us a little about your company in the about box.\n";


	if(alertmsg.length >0)
	{
		alert("Please correct the following problems and then submit the form again:\n\n"+alertmsg);
		return false;
	}
	return true;
}
