
$(document).ready(function() {
		
			
			
			  // For FancyBox
			
			
			$("#infoboxlink").fancybox({
				'titlePosition'		: 'inside',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
			
		$("#thankyoulink").fancybox({
				'titlePosition'		: 'inside',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
		});


//validate form for 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 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);
}

//end phone validation

//validate form
function validateForm() {
	var firstname = document.getElementById("first_name").value;
	var isError = ""; //tracks error messaging
	var email = document.getElementById("email").value; //grabs the text entered in the email portion of the form
	var Phone = document.infoform.phone



	
	if (firstname.length < 1) {
			isError += "\n- your name is required.";
	}
	if (!checkEmail(email)) { //passes the amil variable to the checkEmail function and checks if the return was false
		isError += "\n- The email entered is invalid."; //since it failed we should format some text to respond to the user
	} else { //otherwise, we passed the email format inspection
		if (email.length > 80) { //check of the length of the email is more than 80 characters
			isError += "\n- Email cannot be more than 80 characters."; //emails greater than 80 character in length cannot be entered into Salesforce so tell the user sorry...
		}
	}
	
//phone validation
	if ((Phone.value==null)||(Phone.value=="")){
		isError += "\n- your phone number is required.";
	} else {
	
	if (checkInternationalPhone(Phone.value)==false){
		isError += "\n- invalid phone number entry. proper format xxx-xxx-xxxx.";
		}
	}
	
	if (isError!="") { //if the checks failed
		alert("There were problems with your submission:\n"+isError); //alert the user so they can fix the problems and submit again
		return false; //tell the form that is cannot be submitted to Salesforce
	} else { //otherwise, we passed the inspection
		return true; //tell the form to submit the entry as a lead
	}
}

//validates a passed text string for email format
function checkEmail(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	if (str.indexOf(at)==-1) {
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1) {
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1) {
		return false;
	}
	if (str.indexOf(" ")!=-1) {
		return false;
	}
	return true					
}
