function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_showHideLayers() { //v3.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 showFaq(divname) {
	if (document.getElementById) {
		hideOldFaq(currdiv);
		var divstyle;
		if(document.getElementById(divname)) {
			divstyle = document.getElementById(divname).style;
			repositionFaqs(divstyle);
			divstyle.visibility = 'visible';
			setCurrentFaq(divname);
		}
	} else {
		alert('We do not currently support older browsers.\nApologies from all at VisitScotland.com\nPlease do upgrade and come back for another try!');
	}
}
var currdiv = null;
var preloaded=0;
function setCurrentFaq(divname) {
	currdiv = document.getElementById(divname);
}
function repositionFaqs(divstyle) {
	if(document.getElementById){
		var div_position = document.body.clientHeight / 2; //half current window size
		var div_height = divstyle.height /2; //half div size
		var vertical = document.body.scrollTop;
		divstyle.top =  vertical + div_position - div_height;
	}
}
function hideOldFaq(thediv) {
	if (document.getElementById) {
		if(thediv) {
			if (thediv.style.visibility=='visible') {
				thediv.style.visibility='hidden';
			}
		}
	}
}
function hideDiv(theDiv) {
	if(document.getElementById) {
		var divObj = document.getElementById(theDiv);
		if (divObj.style) {
			divObj.style.visibility = 'hidden';
		}
	}
}
function repositionAction() {
	if(currdiv) {
		repositionFaqs(currdiv.style);
	}
}

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function parseDetails()
{
	if( document.subscribe.ishtml.checked )
		document.subscribe.html.value = 'yes';
	else
		document.subscribe.html.value = 'no';
		
	if( isEmail( document.subscribe.subscriber.value ) )
		return true;
	
	alert( document.subscribe.subscriber.value + " is not a valid email address." );
	return false;
}

function parseEnquiry()
{
	var validateOK = true
	if((document.enquiry.name.value==null)||(document.enquiry.name.value==''))
	{
	alert("Please enter your name before submitting");
	validateOK=false;
	}
	if(document.enquiry.question.value=="Enter your enquiry here.")
	{
        alert("Please enter your question before submitting");
        validateOK=false;
	}
	if(isEmail(document.enquiry.email.value))
        {
        validateOK=true;
        }
        else
        {
        alert(document.enquiry.email.value+" is not a valid email address.");
        validateOK=false;
        }
        return validateOK;
}

function MM_openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

function insertAND(searchString)
{
	var newString='';
	var parString = searchString.replace(/( or| and|\'|\?|\!|\*)/i, '');
	var newString = parString.replace(/ /, ' AND ');
	return newString;
}

function removeAND(searchString)
{
	searchString.replace(/AND/, " ");
	return searchString;
}




var ie = (document.all  && document.getElementById) ? 1 : 0;
var ns = (!document.all && document.getElementById) ? 1 : 0;


function createElement(typ) {
	return document.createElement(typ);
}


function removeElement(obj) {
	if (ie) {
		obj.removeNode(1);
	} else if (ns) {
		obj.parentNode.removeChild(obj);
	}
}

function submitSearchForm(form_name) {
	if ((document.forms[form_name].originalsearch.value == null) ||
		(document.forms[form_name].originalsearch.value.search(/^\W*$/) != -1)) {
		alert("Please insert keyword(s) before submitting search");
		return false;
	}

	var newSubmit=document.forms[form_name].originalsearch.value;
	document.forms[form_name].freesearch.value=insertAND(newSubmit);

	document.forms[form_name].submit();
}

function submitSearch() {
	return submitSearchForm('sitesearch');
}


function submitAgain() {
	return submitSearchForm('searchagain');
}

function sectionSearch(targetBranch)
{
	document.specifics.branch.value=targetBranch;
	document.specifics.submit();
}

function ttsubmit()
{
var specialities  = new Array();
specialities["15960"]="Backpacking";
specialities["15961"]="Cycling";
specialities["15962"]="Eco Tourism (Wildlife)";
specialities["15963"]="Fishing";
specialities["15964"]="Golfing Holidays";
specialities["15965"]="Snow Based Activities";
specialities["15966"]="Walking";
specialities["15967"]="Water Based Activities";
specialities["15959"]="All Things Scottish";

var location = new Array();
location["15988"]="Belgium";
location["15989"]="France";
location["15990"]="Germany";
location["15991"]="Netherlands";
location["15993"]="USA";
location["15992"]="UK";

if(document.ttsearch.item_index_15958.value!="")
{
var spekey = document.ttsearch.item_index_15958.value;
var spec = specialities[spekey];
document.ttsearch.speciality.value=spec;
}
else
{
document.ttsearch.item_index_15958.value=null;
}
if(document.ttsearch.item_index_15987.value!="")
{
var lockey=document.ttsearch.item_index_15987.value;
var loc =location[lockey];
document.ttsearch.location.value=loc;
}
else
{
document.ttsearch.item_index_15987.value=null;
}
if(document.ttsearch.item_index_15950.value=="")
{
document.ttsearch.item_index_15950.value=null;

}
}

//Touchvision: Reset combo box for named area on search screen
//Required for the Accommodation search screen.
function ResetArea(){

var objMultiform = document.getElementById("MultiForm");
         
        if(objMultiform)           
         {  
           var objNamedarea = document.getElementById("namedarea");
           if(objNamedarea)
           {                        
	     MultiForm.namedarea.selectedIndex=0
           }
           else
           { 
           var objlocation = document.getElementById("location");
           if(location)
           {                        
	     MultiForm.location.selectedIndex=0
           }
           }
         }
}

function openPopupArticle(article, height) {
	var height = (height != null) ? height : 200;
	window.open('/repositories/popups/'+ article, '', 'width=380, height='+height);
}
