// FORM VALIDATION
// Support Validation for next form element input,textarea, radio and checkboxes.
//Also contain validation for E-mail and Phone number(US 10 DIGIT PHONE NUMBER)
//Function - submitForms used to call multiple functions. Each function can contain specific validation method
//for different purpose

function submitForms() {if ( isEmail() && isTelephone() && isFname() && isLname() && isState() && isArrival() && isArrivalDate() && isDeparture() && isDepartureDate() && isCheckRadios() && isRVYear() && isRVLength() && isHookup() && isOtherVehicle() && isPeoplePets())
if (confirm("\n You are about to submit your information. \n\nYES to submit.    NO to abort.")){;
return true;
}
else
{
alert("\n You have chosen to abort the submission.");
return false
}
else 
return false;
}

function isArrival() {
if (document.forms[0].elements.arrival.value == "") {
alert ("\n The Arrival Month field is blank. \n\nPlease select your Arrival Month:")
document.forms[0].elements.arrival.focus();
return false;
}
return true;
}

function isArrivalDate() {
if (document.forms[0].elements.arrival_date.value == "") {
alert ("\n The Arrival Date field is blank. \n\nPlease select your Arrival Date:")
document.forms[0].elements.arrival_date.focus();
return false;
}
return true;
}

function isDeparture() {
if (document.forms[0].elements.departure.value == "") {
alert ("\n The Departure Month field is blank. \n\nPlease select your Departure Month:")
document.forms[0].elements.departure.focus();
return false;
}
return true;
}

function isDepartureDate() {
if (document.forms[0].elements.departure_date.value == "") {
alert ("\n The Departure Date field is blank. \n\nPlease select your Departure Date:")
document.forms[0].elements.departure_date.focus();
return false;
}
return true;
}

function isRVYear() {
if (document.forms[0].elements.rvyear.value == "") {
alert ("\n The RV Year field is blank. \n\nPlease select your RV Year:")
document.forms[0].elements.rvyear.focus();
return false;
}
return true;
}


function isRVLength() {
if (document.forms[0].elements.rvlenght.value == "") {
alert ("\n The RV Length field is blank. \n\nPlease enter your RV Length:")
document.forms[0].elements.rvlenght.focus();
return false;
}
return true;
}

function isHookup() {
if (document.forms[0].elements.hookup.value == "") {
alert ("\n The Hookup field is blank. \n\nPlease select your Hookup:")
document.forms[0].elements.hookup.focus();
return false;
}
return true;
}

function isOtherVehicle() {
if (document.forms[0].elements.othervehicle.value == "") {
alert ("\n The Other Vehicle field is blank. \n\nPlease select your Other Vehicle:")
document.forms[0].elements.othervehicle.focus();
return false;
}
return true;
}


function isPeoplePets() {
if (document.forms[0].elements.peopelandpets.value == "") {
alert ("\n The People and Pets field is blank. \n\nPlease select your number of People and Pets:")
document.forms[0].elements.peopelandpets.focus();
return false;
}
return true;
}

function isEmail() {
if (document.forms[0].elements.email.value == "") {
	alert ("\n The email field is blank. \n\nPlease enter your email address:.")
	document.forms[0].elements.email.focus();
	return false;
	}
	return true;
}

function isFname() {
if (document.forms[0].elements.firstname.value == "") {
alert ("\n The First Name field is blank. \n\nPlease enter your First Name:.")
document.forms[0].elements.firstname.focus();
return false;
}
return true;
}
function isLname() {
if (document.forms[0].elements.lastname.value == "") {
alert ("\n The Last Name field is blank. \n\nPlease enter your Last Name:.")
document.forms[0].elements.lastname.focus();
return false;
}
return true;
}

function isState() {
if (document.forms[0].elements.state.value == "") {
alert ("\n The State field is blank. \n\nPlease enter your State:.")
document.forms[0].elements.state.focus();
return false;
}
return true;
}

//////////////////////////
//Validate phone number///
// 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 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){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function isTelephone(){
	var Phone=document.forms[0].elements.contact_phone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid 10 digits Phone Number")
document.forms[0].elements.contact_phone.select();
document.forms[0].elements.contact_phone.focus();
		return false
	}
	return true
 }
///////////////////////////////

///////////////////////////////////////////////////////////////////////
function isCheckRadios()
{
	var el, i = 0, grp, rvtype, focus_me = null, sMsg = '';
	var scrAdj = -24; //fine-tunes scrolling to first unset radio
	while (el = document.forms[0].elements[i++])
		if (el.type == 'radio')
		{
			grp = document.forms[0].elements[el.name]; j = 0;
			while (rvtype = grp[j++])
				if (rvtype.checked)
					break;
			if (j > grp.length)
			{
				sMsg += 'Rv Type \n';
				if (focus_me == null)
					focus_me = el;
			}
			i += grp.length - 1;
		}
	if (sMsg != '')
	{
		var line = '_____________________________';
		sMsg = line + '\nThe following items were unselected:\n\n' + sMsg;
		sMsg += line + '\nPlease complete and re-submit.\nThank you.\n\n';
		alert(sMsg);
		if (focus_me.focus)
		{
			focus_me.focus();
			scrollBy(0, scrAdj);
		}
		return false;
	}
	return true;		
}