// JavaScript Document 

var preloadFlag = false;
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];}}

    preloadFlag = true;
}

  function calculatePosition (myElement, lPos, tPos) {
    var srcElement = myElement;
    var leftPos = lPos;
	var topPos = tPos;
	var co_ords = new Array ();

    // If the element has an offsetParent,
	// continue to append offsetParent values to the elements co-ordinates
	while (srcElement.offsetParent != null) {
	  leftPos = leftPos + parseInt(srcElement.offsetParent.offsetLeft);
	  topPos = topPos + parseInt(srcElement.offsetParent.offsetTop);

	  srcElement = srcElement.offsetParent;
	}
	leftPos = leftPos + parseInt(srcElement.offsetLeft);
	topPos = topPos + parseInt(srcElement.offsetTop);

	co_ords[0] = leftPos;
	co_ords[1] = topPos;

	return (co_ords);
  }

  function showMenu(menuToShow, e) {
      var ns = (navigator.appName.indexOf('Netscape') >-1);
      var ie = (navigator.appName.indexOf('Microsoft Internet Explorer') >-1);
   if (ns) {
    var srcElement = e.target;
   } else {
    var srcElement = event.srcElement;
   }
   var co_ords = calculatePosition(srcElement, 0, 0);

   var xPos = co_ords[0];
   var yPos = co_ords[1];

   menuToShow.style.left = xPos + 0;
   menuToShow.style.top = yPos + 20;
  }

  function hideMenu(menuToHide, menuIDLength, e) {
   var ns = (navigator.appName.indexOf('Netscape') >-1);
   var ie = (navigator.appName.indexOf('Microsoft Internet Explorer') >-1);
   var toElementID;
   
   if (ns) {
    var mouseLastIn = e.relatedTarget;
   } else {
    var mouseLastIn = event.toElement;
   }

   if (mouseLastIn != null) {
    if (mouseLastIn.nodeType == 3) {mouseLastIn = mouseLastIn.parentNode;}
    toElementID = mouseLastIn.id;
   } else {
    return false;
   }

   if (typeof(toElementID) == "undefined") {
    toElementID = "UNDEF";
   }

   toElementID = toElementID.substr(0,menuIDLength);
   var divMenuID = menuToHide.id;
   divMenuID = divMenuID.substr(0,menuIDLength);

   if (toElementID != divMenuID) {
    menuToHide.style.left = -200 + 'px';
    menuToHide.style.top = -2400 + 'px';
   }
  }
 
function testCombo(name) {
  var myRegExp = /^([\w\W])+$/i;
  return (myRegExp.test(name));
}
 
function testEmail(email) {
  var myRegExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i;
  return (myRegExp.test(email));
}

function testAlpha(name) {
  var myRegExp = /^(\D{2,50})$/i;
  return (myRegExp.test(name));
}

function testPassword(passwd) {
  var myRegExp = /^([\w\W.]{5,20})$/i;
  return (myRegExp.test(passwd));
}

function testAlphaNum(login) {
  var myRegExp = /^(\w{2,50})$/;
  return (myRegExp.test(login));
}

function testCC(x) {
  var myRegExp = /^(\d{4,4})$/;
  return (myRegExp.test(x));
}

function testInteger(x) {
  var myRegExp = /^(\d+)$/;
  return (myRegExp.test(x));
}
 
function trim(value)  { 
 var startposn=0; 
 while( ( value.charAt(startposn) == " " ) && ( startposn<value.length ) ) { 
    startposn++; 
 } 
 if( startposn == value.length ) { 
    return(""); 
 } 
 var endposn=(value.length)-1;
 while(value.charAt(endposn)==" ") { 
     endposn--; 
 } 
 return(value.substring(startposn,endposn+1)); 
} 

function isinteger(x)
{
 if (validvalue(x,"1234567890",0)) 
 {
	return true;
 } 
 else 
 {
	return false;
 }
}

function checkPassword(x,y)
{
	if(x!=y)
		return false;
	else
		return true;
}

function isnull(x)
{
	var field=x.value;
	if(field=="") 
  {
		return true;
	} 
  else 
  {
		return false;
	}
}

function checkLoginForm()
{
 if(isnull(document.login_form.username))
 {
   alert('Please fill in your username.');
   document.login_form.username.focus();
   document.login_form.username.select();
   return false;
 }
  if(!testAlphaNum(document.login_form.username.value))
 {
  alert('Please fill in valid username.');
  document.login_form.username.focus();
  document.login_form.username.select();
  return false;
 }
  if(!testAlphaNum(document.login_form.password.value))
 {
  alert('Please fill in valid password.');
  document.login_form.password.focus();
  document.login_form.password.select();
  return false;
 }
 return true;
}

function checkContactForm()
{
 if(isnull(document.post_contact.name))
 {
   alert('Please fill in your name.');
   document.post_contact.name.focus();
   document.post_contact.name.select();
   return false;
 }
  if(!testCombo(document.post_contact.name.value))
 {
  alert('Please fill in valid name.');
  document.post_contact.name.focus();
  document.post_contact.name.select();
  return false;
 }
 if(!testEmail(trim(document.post_contact.email.value)))
 {
  alert('Please fill in a valid email address.');
  document.post_contact.email.focus();
  document.post_contact.email.select();
  return false;
 }
 if(isnull(document.post_contact.subject))
 {
  alert('Please fill in a message subject.');
  document.post_contact.subject.focus();
  document.post_contact.subject.select();
  return false;
 }
 if(isnull(document.post_contact.c_body))
 {
  alert('Please fill in your message.');
  document.post_contact.c_body.focus();
  document.post_contact.c_body.select();
  return false;
 }
 if(!testCombo(document.post_contact.c_body.value))
 {
  alert('Please fill in valid message.');
  document.post_contact.c_body.focus();
  document.post_contact.c_body.select();
  return false;
 }
 return true;
}

function checkRegForm () {
 // Check Firstname
 if(isnull(document.reg_form.name)) {
   alert('Please fill in your first name.');
   document.reg_form.name.focus();
   document.reg_form.name.select();
   return false;
 }

 if(!testCombo(document.reg_form.name.value)) {
  alert('Please fill in a valid first name.');
  document.reg_form.name.focus();
  document.reg_form.name.select();
  return false;
 }

 // Check Lastname
 if(isnull(document.reg_form.address)) {
   alert('Please fill in your address.');
   document.reg_form.address.focus();
   document.reg_form.address.select();
   return false;
 }

 // Post Code
 if(!testCC(trim(document.reg_form.postcode.value))) {
  alert('Please fill in a valid post code.');
  document.reg_form.postcode.focus();
  document.reg_form.postcode.select();
  return false;
 }

 // Check Phone
 if(isnull(document.reg_form.phone)) {
   alert('Please fill in your phone number.');
   document.reg_form.phone.focus();
   document.reg_form.phone.select();
   return false;
 }

 // Check Email
 if(!testEmail(trim(document.reg_form.email.value))) {
  alert('Please fill in a valid email email.');
  document.reg_form.email.focus();
  document.reg_form.email.select();
  return false;
 }
 
 // Check Username
 if(isnull(document.reg_form.username)) {
   alert('Please fill in your username.');
   document.reg_form.username.focus();
   document.reg_form.username.select();
   return false;
 }

 // Origin Post Code
 if(!testCC(trim(document.reg_form.originpostcode.value))) {
  alert('Please fill in a valid origin post code.');
  document.reg_form.originpostcode.focus();
  document.reg_form.originpostcode.select();
  return false;
 }

 // Check Origin Suburb
 if(isnull(document.reg_form.originsuburb)) {
   alert('Please fill in your origin suburb.');
   document.reg_form.originsuburb.focus();
   document.reg_form.originsuburb.select();
   return false;
 }

 // Destination Post Code
 if(!testCC(trim(document.reg_form.destinationpostcode.value))) {
  alert('Please fill in a valid destination post code.');
  document.reg_form.destinationpostcode.focus();
  document.reg_form.destinationpostcode.select();
  return false;
 }

 // Check Destination Suburb
 if(isnull(document.reg_form.destinationsuburb)) {
   alert('Please fill in your destination suburb.');
   document.reg_form.destinationsuburb.focus();
   document.reg_form.destinationsuburb.select();
   return false;
 }
 
 return true;
}
