//Validate Form
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_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+=' Please enter your e-mail address in the form name@host.com.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+=nm+' must contain only numbers.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) 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(errors);
  document.MM_returnValue = (errors == '');
}

//Toggle table (see rsvr2.php)
function toggleLayer(whichLayer)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}

//Mouse coordinates and image popup code
//Important: Function is called using the onload (seen below) that can be found in the body of each adventure story
//window.onload = cordInit;

function cordInit() {
  if (window.Event) {
    document.captureEvents(Event.MOUSEMOVE);
  }
  document.onmousemove = getXY;
}

function getXY(e) {
	x = (window.Event) ? e.pageX : event.clientX;
	y = (window.Event) ? e.pageY : event.clientY;
	
}

function Xpand() {
	var imgName = 'http://www.scuba-adventures.com/images/reservation/balloon.png';
	
	var image = new Image();
	image.src = imgName;
	var imgWidth = image.width;
	var imgHeight = image.height;

	var app=navigator.appName

	document.getElementById('imgLayer').style.left= x - 30 + 'px';

	if (app.indexOf('Microsoft') != -1) // Browser sniffer
		{
			document.getElementById('imgLayer').style.top = (document.documentElement.scrollTop + y) - imgHeight - 10 + 'px';
		}
		else 
		{
			document.getElementById('imgLayer').style.top = y - imgHeight - 10 + 'px';
		}
	
	document.getElementById('imgLayer').style.visibility='visible';

}

function hideXpand() {
	document.getElementById('imgLayer').style.visibility='hidden';
}

function BrowserInfo()
{
  this.name = navigator.appName;
  this.codename = navigator.appCodeName;
  this.version = navigator.appVersion.substring(0,4);
  this.platform = navigator.platform;
}
var browser = new BrowserInfo();

//End Mouse coordinates and image popup code

//Online Reservation form validation
function checkWholeForm(onlinersv) {
	var why = "";
	why += checkFirstname(onlinersv.Firstname.value);
	why += checkLastname(onlinersv.Lastname.value);
	why += checkPhone(onlinersv.phone.value);
	why += checkEmail(onlinersv.Email.value);
	why += verEmails(onlinersv.Email2.value);
	why += checkCCNum(onlinersv.ccnumber.value);
	why += checkCCMonth(onlinersv.ccmonth.value);
	why += checkCCYear(onlinersv.ccyear.value);
	why += isDifferent(onlinersv.different.value);
	if (why != "") {
		alert(why);
		return false;
	}
return true;
}

function checkEmail(strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
		//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}

function checkPhone(strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}

function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The mandatory text area has not been filled in.\n"
  }
return error;	  
}

function checkCCNum(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter your credit card number.\n"
  }
return error;	  
}

function checkCCMonth(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter your credit card's expiration month.\n"
  }
return error;	  
}

function checkCCYear(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter your credit card's expiration year.\n"
  }
return error;	  
}

function checkFirstname(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter your first name.\n"
  }
return error;	  
}

function checkLastname(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter your last name.\n"
  }
return error;	  
}

function verEmails(strng){
var error = "";
	// check if both email fields are the same
	if (onlinersv.Email.value != onlinersv.Email2.value) {
		error = "The two email addresses are not the same. Please check.\n"
	}
return error;
}
