// JavaScript Document

//rollovers
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];}
}

//mailto
function noSpam(user,domain) { 
locationstring = "mailto:" + user + "@" + domain; 
window.location = locationstring; 
}


//print listing popup-----------------
function popup(id) { 
string = "http://www.jarrettbayyachtsales.com/inventory/print_listing.php?id=" + id;
window.open(string,'mywindow','width=770,height=650,toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,copyhistory=no,resizable=yes'); 
}
function popup_m(id) { 
string = "http://www.jarrettbayyachtsales.com/new-models/print_model.php?id=" + id;
window.open(string,'mywindow','width=770,height=650,toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,copyhistory=no,resizable=yes'); 
}


// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function validateContactForm() {
var formID = document.getElementById('contactForm');

// check name
with(formID) {
 if (name.value == "") {
	alert ("Please enter a name!");
	name.focus();
	return false;
 }
 // check email address
 if ((email.value==null)||(email.value=="")){
	alert("Please Enter your Email Address.")
	email.focus();
	return false;
 }
 if (echeck(email.value)==false){
	email.value="";
	email.focus();
	return false;
 }
 if (telephone.value!="" && checkInternationalPhone(telephone.value)==false){
	alert("Please enter a valid phone number for the contact form.");
	telephone.value="";
	telephone.focus();
	return false;
 }
 if (fax.value!="" && checkInternationalPhone(fax.value)==false){
	alert("Please enter a valid fax number for the contact form.");
	fax.value="";
	fax.focus();
	return false;
 }
 if (subject.value == "") {
	alert ("Please enter a subject for the contact form.");
	subject.focus();
	return false;
 }
 if(zip.value!="" && !isInteger(zip.value)) {
	alert ("Please enter a valid zipcode for the contact form.");
	zip.value="";
	zip.focus();
	return false;
 }
 if (message.value == "") {
	alert ("Please enter a message for the contact form.");
	message.focus();
	return false;
 }
 var spamCheck = spam.value.toLowerCase();
 if (spamCheck != "blue") {
	alert ("Please answer the spam question correctly.");
	spam.focus();
	return false;
 }
}
document.getElementById('contactForm').submit();
return true;
}

function validateFriend() {
var formID = document.getElementById('sendFriend');
// check name
with(formID) {
 formval = spam.value.toLowerCase();
 if(formval != "c") {
	alert ("Please enter the third letter in the alphabet.");
	spam.focus();
	spam.style.backgroundColor = "#ff8888";
	return false;
 } else if(spam.value == "14") {
 	spam.style.backgroundColor = "#7bee68";
 }
 if (name.value == "") {
	alert ("Please enter a name!");
	name.focus();
	return false;
 }
 // check email address
 if ((email.value==null)||(email.value=="")){
	alert("Please Enter your Email Address.")
	email.focus();
	return false;
 }
 if (echeck(email.value)==false){
	email.value="";
	email.focus();
	return false;
 }
 // check friend email address
 if ((femail.value==null)||(femail.value=="")){
	alert("Please Enter your Email Address.")
	femail.focus();
	return false;
 }
 if (echeck(femail.value)==false){
	femail.value="";
	femail.focus();
	return false;
 }
}
return true;
}

function validateRegister() {
var formID = document.getElementById('contactForm');

// check name
with(formID) {
 if (name.value == "") {
	alert ("Please enter a name!");
	name.focus();
	return false;
 }
 // check email address
 if ((email.value==null)||(email.value=="")){
	alert("Please Enter your Email Address.")
	email.focus();
	return false;
 }
 if (echeck(email.value)==false){
	email.value="";
	email.focus();
	return false;
 }
 if (telephone.value == ""){
	alert("Please enter a valid phone number for the contact form.");
	telephone.focus();
	return false;
 }
 if (mailing.value == "") {
	alert ("Please enter a valid mailing address for the contact form.");
	mailing.focus();
	return false;
 }
 if (city.value == "") {
	alert ("Please enter a valid city for the contact form.");
	city.focus();
	return false;
 }
 if (state.value == "") {
	alert ("Please enter a valid state for the contact form.");
	state.focus();
	return false;
 }
 if(zip.value == "") {
	alert ("Please enter a valid zipcode for the contact form.");
	zip.focus();
	return false;
 }
}
return true;
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail Address")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail Address")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Invalid E-mail Address")
	    return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
	    alert("Invalid E-mail Address")
	    return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Invalid E-mail Address")
	    return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
	    alert("Invalid E-mail Address")
	    return false
	}
		
	if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail Address")
		return false
	}
 	return true					
}
