/* ================= FORM VALIDATION ========================== */

//===Form Validation 1

function getRadioValue(formname,radioname){
	var theRadioButtons = document[formname][radioname];
	for (i=0;i<theRadioButtons.length;i++){
		if(theRadioButtons[i].checked){
			return theRadioButtons[i].value;
		}
	}
}



function showRadioValue(formname,radioname,theValue){
	var theRadioButtons = document[formname][radioname];
	for (i=0;i<theRadioButtons.length;i++){
		var temp = theRadioButtons[i].value;
		if (temp == theValue){
			theRadioButtons[i].checked = true;
		}
	}
}

//===Form Validation 2  

function validate() {
	
	// Check that visitor has chosen  cruising area
	var chosenArea = getRadioValue("myForm","Area");
	
	if(chosenArea==null){
		alert("Please tell us your preferred Cruising Area");
		return false
	}
	
	// Check that there is a name
	if (!document.myForm.Name.value){
		alert ("We need your name so we know who you are!")
		document.myForm.Name.focus()
		document.myForm.Name.select()			
		return false
	}

	// Check that there is at least one of: an Address, a Phone number or an Email address
	if (!document.myForm.Address.value){
		alert ("We need your Address")
		document.myForm.Address.focus()
		document.myForm.Address.select()
		return false
	}
	
	// Check that there is at least one of: an Address, a Phone number or an Email address
	if (!document.myForm.Phone.value && !document.myForm.Email.value){
		alert ("We need either your Phone or Email address to assist us in responding to your request for information.")
		document.myForm.Address.focus()
		document.myForm.Address.select()
		return false
	}	
	
	// check to see if the email's valid
	if (!validEmail(document.myForm.Email.value)) {
		alert ("Invalid email address!")
		document.myForm.Email.focus()
		document.myForm.Email.select()
		return false
	}

}


//===================E-MAIL VALIDATION ===================================
// E-Mail Validator 1

function validEmail(email) {
	invalidChars = " /:,;"

	if (email == "") {						// can be empty
		return true
	}
	for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			document.myForm.Email.focus()
			document.myForm.Email.select()			
			return false
		}
	}
	atPos = email.indexOf("@",1)			// there must be one "@" symbol
	if (atPos == -1) {
		document.myForm.Email.focus()
		document.myForm.Email.select()				
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
		document.myForm.Email.focus()
		document.myForm.Email.select()					
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {					// and at least one "." after the "@"
		document.myForm.Email.focus()
		document.myForm.Email.select()					
		return false
	}
	if (periodPos+3 > email.length)	{		// must be at least 2 characters after the "."
		document.myForm.Email.focus()
		document.myForm.Email.select()					
		return false
	}
	return true
}

// E-Mail Camouflage
// Hide email address from crawlers

function contact(nano)

	{

	parent.location = 'm'+'ail'+'to'+String.fromCharCode(58)+'tom'+String.fromCharCode(nano)+'portwayyachtcharters.com';

	}
	
function contact2(nano)

	{

	parent.location = 'm'+'ail'+'to'+String.fromCharCode(58)+'jill'+String.fromCharCode(nano)+'portwayyachtcharters.com';

	}
	

// =========== Open and Close new Browser Window ================ 

var newWindow = null;

function openWindow(theURL,winName,features){
	  newWindow = window.open(theURL,winName,features);
}

function closeWindow(){
	if (newWindow != null){
		newWindow.close();
		newWindow = null;
	}
}

function toggleWindow(theURL,winName,features) {
	if (newWindow == null) {
		newWindow = window.open(theURL,winName,features);
		newWindow.focus()
	}
	else{
		newWindow.close();
		newWindow = null;
	}
}

// ========================= Hide default message which displays when JavaScript is disabled in Browser
	
function hidemsg(){
  document.getElementById('jsmsg').style.display='none';
  }
  
// ========================= Select all objects on page with the class (even where there are multiple classes???)

function getElementsByClass(searchClass,node,tag) {

        var classElements = new Array();
        if (node == null)
                node = document;
        if (tag == null)
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        var j = 0;
        for (i = 0; i < elsLen; i++) {
                if (pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}


