/*
Function List
	MM_preloadImages() \
	MM_swapImgRestore() \
	MM_findObj(n, d)     > General Image swap function
	MM_swapImage()      /
*/
var SITE_URL_PATH="";
glbl_str123 = '1234567890';

function validMobileNo(mobileNumber)
{
  intFail = 0;
  //strip spaces
  
  if(!checkSpace(mobileNumber))
  {
  	intFail = intFail + 1;
    alert("Please remove any spaces");
    return false;
  }
  mobileNumber = trim(mobileNumber);
  mobile = str_replace(' ', '', mobileNumber);

  if (permitOnly1(glbl_str123, mobile) == 0)
  {
    intFail = intFail + 1;
    alert("Please enter only numeric characters");
    return false;
    
  }
  if (mobile.length != 10)
  {
    alert("Mobile Number must be 10 digits");
    intFail = intFail + 1;
    return false;
  }
  //is the first two characters '04'?
  if (mobile.substring(0,2) != '04')
  {
    intFail = intFail + 1;
    alert("Mobile Number Must start with 04");
    return false;
  }

  //are there any non-numeric characters?
  

  //is it ten characters long?  ie., 0411-111-111
  if (intFail == 0)
  {
    return true;
  }
  else
  {
    return false;
  }
}
function permitOnly1 (permissibleString, haystack)
{
  aryHaystack = haystack.split('');
  returnInt = 1;

  for (x = 0; x < aryHaystack.length; x++)
  {
    tmpIndex = permissibleString.indexOf(aryHaystack[x]);
    if (tmpIndex == -1)
    {
      returnInt = 0;
      break;
    }
  }
  return returnInt;
}

function setpath(pathval)
{
	SITE_URL_PATH=pathval;
}

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) { //v3.0
  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); 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 changestyleover(element){
	var el = document.getElementById(element);
	el.className='atsidenavhover';
	el.style.cursor='pointer';
}

function changestyleout(element){
	var el = document.getElementById(element);
	el.className='atsidenav';
}

function clrSearchFld(fld)
{
	/*
	if (fld.name == 'bus_type')
	{
		document.basicsearch.bus_name.value ="";
		//document.basicsearch.bus_location.value ="";
	}
	else if (fld.name == 'bus_name')
	{
		document.basicsearch.bus_type.value ="";
		//document.basicsearch.bus_location.value ="";
	}
	else if (fld.name == 'bus_location')
	{
		//document.basicsearch.bus_name.value ="";
		//document.basicsearch.bus_type.value ="";
	}
	*/
}

function page_adv(pg)
{
	document.src_nav.page.value = pg;
	document.src_nav.submit();
}

function chkEmail(addr)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(addr);
}

function IsNumeric(num)
{
	var filter = /[^0-9]/;
	return !filter.test(num);
	
}

function chkDateFormat(date,format)
{
	switch (format)
	{
		case '1':
		default:
			var filter = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
			break;
	}
	return filter.test(date);
}

/************************************************
*  G L O B A L   F O R M   V A L I D A T I O N  *
*  - Validation that could be require on        *
*     ALL pages.                                *
************************************************/

function goLogin(form) {
	if (form.user.value=="") {
		alert("Please enter a Username")
		form.user.focus();
		return false
	}
	if (form.pass.value=="") {
		alert("Please enter a Password")
		form.pass.focus();
		return false
	}
	return true
}

function validateSearch(srcForm)
{
	if (srcForm.bus_type.value == '' && srcForm.bus_name.value == '')
	{
		alert("Please enter a keyword into either the 'Business Type' or 'Business Name' fields");
		return false;
	}
	else if(srcForm.bus_location.value == '')
	{
		alert("Please Select 'Business Location' fields");
		srcForm.bus_location.focus();
		return false;		
	}
	/*
	else if(srcForm.bus_name.value != '' && srcForm.bus_location.value == '')
	{
		alert("Please Select 'Business Location' fields");
		return false;		
	}
	*/
	else
	{
		var TmpBT=srcForm.bus_type.value;
		TmpBT1=TmpBT.replace("&","--");
		
		var TmpBN=srcForm.bus_name.value;
		TmpBn1=TmpBN.replace("&","--");
		
		//srcForm.action=SITE_URL_PATH + "search.php?post=1&bus_type=" + srcForm.bus_type.value + "&bus_name=" + srcForm.bus_name.value + "&bus_location=" + srcForm.bus_location.value;
		srcForm.action=SITE_URL_PATH + "search.php?post=1&bus_type=" + TmpBT1 + "&bus_name=" + TmpBn1 + "&bus_location=" + srcForm.bus_location.value;
	}
}

function TmpvalidateSearch(srcForm)
{
	if (srcForm.bus_type.value == '' && srcForm.bus_name.value == '')
	{
		alert("Please enter a keyword into either the 'Business Type' or 'Business Name' fields");
		return false;
	}
	else if(srcForm.bus_location.value == '')
	{
		alert("Please Select 'Business Location' fields");
		srcForm.bus_location.focus();
		return false;		
	}
	/*
	else if(srcForm.bus_name.value != '' && srcForm.bus_location.value == '')
	{
		alert("Please Select 'Business Location' fields");
		return false;		
	}
	*/
	else
	{
		var TmpBT=srcForm.bus_type.value;
		TmpBT1=TmpBT.replace("&","--");
		
		var TmpBN=srcForm.bus_name.value;
		TmpBn1=TmpBN.replace("&","--");
		
		//srcForm.action=SITE_URL_PATH + "search.php?post=1&bus_type=" + srcForm.bus_type.value + "&bus_name=" + srcForm.bus_name.value + "&bus_location=" + srcForm.bus_location.value;
		srcForm.action=SITE_URL_PATH + "temp_search.php?post=1&bus_type=" + TmpBT1 + "&bus_name=" + TmpBn1 + "&bus_location=" + srcForm.bus_location.value;
	}
}

function TmpvalidateSearch2(srcForm)
{
	if(document.basicsearch.Keyword.value == "" || document.basicsearch.Keyword.value == "Enter Keyword Here")
	{
		alert("Please enter a keyword.");
		document.basicsearch.Keyword.focus();
		return false;
	}
	else if(document.basicsearch.bus_location.value == '')
	{
		alert("Please Select 'Business Location' fields");
		document.basicsearch.bus_location.focus();
		return false;		
	}
	else
	{
		var TmpKey=document.basicsearch.Keyword.value;
		TmpKey1=TmpKey.replace("&","--");
		
		
		var Tmpsearchfor
		
		if(document.basicsearch.searchfor[0].checked==true)
			Tmpsearchfor=document.basicsearch.searchfor[0].value
		else if(document.basicsearch.searchfor[1].checked==true)
			Tmpsearchfor=document.basicsearch.searchfor[1].value
		else if(document.basicsearch.searchfor[2].checked==true)
			Tmpsearchfor=document.basicsearch.searchfor[2].value
		else if(document.basicsearch.searchfor[3].checked==true)
			Tmpsearchfor=document.basicsearch.searchfor[3].value		
		
		Tmpsearchfor1=Tmpsearchfor.replace("&","--");
		
		if(Tmpsearchfor1=="Event")
		{
			document.basicsearch.action=SITE_URL_PATH + "events_search_result.php?EveType=&keyword=" + TmpKey1 + "&EveRegion=Perth&Frdt=&Todt=";
			document.basicsearch.submit();
		}
		else if(Tmpsearchfor1=="Classified")
		{
			document.basicsearch.action=SITE_URL_PATH + "classified_search.php?Keyword=" + TmpKey1 + "&submit1=1";
			document.basicsearch.submit();
		}
		else
		{
			document.basicsearch.action=SITE_URL_PATH + "search_tmp.php?post=1&Keyword=" + TmpKey1 + "&searchfor=" + Tmpsearchfor1 + "&bus_location=" + document.basicsearch.bus_location.value;
			document.basicsearch.submit();
		}
		
		return true;
	}
}

function TmpvalidateSearch3(srcForm)
{
	//if(document.basicsearch.Keyword.value == "" || document.basicsearch.Keyword.value == "Business Names" || document.basicsearch.Keyword.value == "Business Type" || document.basicsearch.Keyword.value == "Classified Ad's" || document.basicsearch.Keyword.value == "Events")
	if(document.basicsearch.Keyword.value == "" || document.basicsearch.Keyword.value == "Enter Keyword Here")
	{
		alert("Please enter a keyword.");
		document.basicsearch.Keyword.focus();
		return false;
	}
	else if(document.basicsearch.bus_location.value == '')
	{
		alert("Please Select 'Business Location' fields");
		document.basicsearch.bus_location.focus();
		return false;		
	}
	else
	{
		var TmpKey=document.basicsearch.Keyword.value;
		TmpKey1=TmpKey.replace("&","--");
		
		
		var Tmpsearchfor
		
		if(document.basicsearch.searchfor[0].checked==true)
			Tmpsearchfor=document.basicsearch.searchfor[0].value
		else if(document.basicsearch.searchfor[1].checked==true)
			Tmpsearchfor=document.basicsearch.searchfor[1].value
		else if(document.basicsearch.searchfor[2].checked==true)
			Tmpsearchfor=document.basicsearch.searchfor[2].value
		else if(document.basicsearch.searchfor[3].checked==true)
			Tmpsearchfor=document.basicsearch.searchfor[3].value		
		
		Tmpsearchfor1=Tmpsearchfor.replace("&","--");
		
		if(Tmpsearchfor1=="Event")
		{
			document.basicsearch.action=SITE_URL_PATH + "events_search_result.php?EveType=&keyword=" + TmpKey1 + "&EveRegion=Perth&Frdt=&Todt=";
			document.basicsearch.submit();
		}
		else if(Tmpsearchfor1=="Classified")
		{
			document.basicsearch.action=SITE_URL_PATH + "classified_search.php?Keyword=" + TmpKey1 + "&submit1=1";
			document.basicsearch.submit();
		}
		else
		{
			//document.basicsearch.action=SITE_URL_PATH + "search_tmp.php?post=1&Keyword=" + TmpKey1 + "&searchfor=" + Tmpsearchfor1 + "&bus_location=" + document.basicsearch.bus_location.value;
			document.basicsearch.action=SITE_URL_PATH + "search.php?post=1&Keyword=" + TmpKey1 + "&searchfor=" + Tmpsearchfor1 + "&bus_location=" + document.basicsearch.bus_location.value;
			document.basicsearch.submit();
		}
		
		return true;
	}
}

function TmpvalidateSearch3_New(srcForm)
{
	//if(document.basicsearch.Keyword.value == "" || document.basicsearch.Keyword.value == "Business Names" || document.basicsearch.Keyword.value == "Business Type" || document.basicsearch.Keyword.value == "Classified Ad's" || document.basicsearch.Keyword.value == "Events")
	if(document.basicsearch.Keyword.value == "" || document.basicsearch.Keyword.value == "Enter Keyword Here")
	{
		alert("Please enter a keyword.");
		document.basicsearch.Keyword.focus();
		return false;
	}
	else if(document.basicsearch.bus_location.value == '')
	{
		alert("Please Select 'Business Location' fields");
		document.basicsearch.bus_location.focus();
		return false;		
	}
	else
	{
		var TmpKey=document.basicsearch.Keyword.value;
		TmpKey1=TmpKey.replace("&","--");
		
		
		var Tmpsearchfor
		Tmplocation=document.getElementById("bus_location").value;
		
		Tmpsearchfor=document.basicsearch.searchfor.value
		Tmpsearchfor1=Tmpsearchfor.replace("&","--");
		
		if(Tmpsearchfor1=="Event")
		{
			document.basicsearch.action=SITE_URL_PATH + "events_search_result.php?EveType=&keyword=" + TmpKey1 + "&EveRegion=Perth&Frdt=&Todt=";
			document.basicsearch.submit();
		}
		else if(Tmpsearchfor1=="Classified")
		{
			document.basicsearch.action=SITE_URL_PATH + "classified_search.php?Keyword=" + TmpKey1 + "&submit1=1";
			document.basicsearch.submit();
		}
		else
		{
			//document.basicsearch.action=SITE_URL_PATH + "search_tmp.php?post=1&Keyword=" + TmpKey1 + "&searchfor=" + Tmpsearchfor1 + "&bus_location=" + document.basicsearch.bus_location.value;
			document.basicsearch.action=SITE_URL_PATH + "search.php?post=1&Keyword=" + TmpKey1 + "&searchfor=" + Tmpsearchfor1 + "&bus_location=" + Tmplocation;
			document.basicsearch.submit();
		}
		
		return true;
	}
}

function getadvalidation(frm)
{
	
	if(isblank(frm.fname.value)) 
	{
		alert('Please enter First Name');
		frm.fname.focus();
		return false;	
	}
	else if(isblank(frm.surname.value)) 
	{
		alert('Please enter Last Name');
		frm.surname.focus();
		return false;
	}
	else if (!chkEmail(frm.emailaddr.value)) 
	{ 
		alert('Please enter a valid email address'); 
		frm.emailaddr.focus();
		return false; 
	}
	else if(!isblank(frm.mobilenumber.value)) 
	{
		if(!validMobileNo(frm.mobilenumber.value))
		{
			frm.mobilenumber.focus();
			frm.mobilenumber.focus();
			return false; 
		}	
	}
	
	
	return true;
}
function isblank(s)
{
	for(var i=0; i < s.length; i++)
	{
		var c = s.charAt(i);
		
		if((c != ' ') && (c != '\n') && (c != '\t'))
			return false;
	}
	return true;
}
function checkSpace(s)
{
	for(var i=0; i < s.length; i++)
	{
		var c = s.charAt(i);
		
		if((c == ' ') || (c == '\n') || (c == '\t'))
			return false;
	}
	return true;
}

function str_replace(needle, newNeedle, haystack)
{
  maxLoop = haystack.length;
  newNeedleLength = newNeedle.length;
  needleLength = needle.length;

  x = 0;

  proceed = true

  while (proceed)
  {
    //if this is the needle we're looking for...
    if (haystack.substring(x, (x + needleLength)) == needle)
    {
      //get the substring before this character (watch out for the first character)
      if (x == 0)
      {
        preString = '';
      }
      else
      {
        preString = haystack.substring(0, x);
      }

      //get the substring after this character (watch out for the last character
      if (x == (haystack.length - needleLength))
      {
        postString = '';
      }
      else
      {
        postString = haystack.substring(x + needleLength);
      }

      //build new haystack
      haystack = preString + newNeedle + postString;

      x = x + newNeedleLength;

      //if the newNeedle is 0 characters long, the haystack is shorter, so run the search for charAt(x) again
      if (newNeedleLength == 0)
      {
        if (x != 0)
        {
          x = x - 1;
        }
      }
    }

    //Move along
    x = x + 1;

    //if we're shortened the haystack so that the new length is less than the original, and we're at the end of the new, then break.
    if (haystack.length < x)
    {
      proceed = false;
    }
    else
    {
      proceed = true;
    }

  }

  //return the new haystack
  return haystack;
}



function trim(sString)
{
  trimString = lTrim(rTrim(sString));
  return trimString;
}



function rTrim(sString)
{
  while (sString.substring(sString.length-1, sString.length) == ' ')
  {
    sString = sString.substring(0,sString.length-1);
  }
  return sString;
}



function lTrim(sString)
{
  while (sString.substring(0,1) == ' ')
  {
    sString = sString.substring(1, sString.length);
  }
  return sString;
}
