// JavaScript Document

function loadSubContentByName(name){
	dojo.xhrGet( {
		url: "request/content.php?n="+name, 
		handleAs: "text",
		timeout: 10000,
		preventCache: true,
		load: function(response, ioArgs) {
			var contain = dojo.byId("contentContainer");
			dojo.style(contain, "opacity", 0.25);

			dojo.byId("subContentLoader").innerHTML = response;
			return response;
		},
		error: function(response, ioArgs) {
			if(ioArgs.xhr.status != 200){
				alert("Could not load data: "+ ioArgs.xhr.status);
			}
			return response;
		}
	});
}

function buildContactEmailAddress(){
	var d = 'bellini-events';
	var t = '.com';
	var s = '@';
	var n = 'info';
	var build = n+s+d+t;
	dojo.byId('contactLink').setAttribute('href', 'm'+'a'+'i'+'l'+'t'+'o'+':'+build);
	dojo.byId('contactLink').innerHTML=build;
}

function validateContactForm(){
	var errors = '';
	var firstName = dojo.byId('firstName').value;
	var lastName = dojo.byId('lastName').value;
	var primary_p1 = dojo.byId('primary_p1').value;
	var primary_p2 = dojo.byId('primary_p2').value;
	var primary_p3 = dojo.byId('primary_p3').value;
	var secondary_p1 = dojo.byId('secondary_p1').value;
	var secondary_p2 = dojo.byId('secondary_p2').value;
	var secondary_p3 = dojo.byId('secondary_p3').value;
	var email = dojo.byId('email').value;
	var emailReType = dojo.byId('emailReType').value;
	
	if(firstName==''){
		errors += '   => First Name is required\n';	
	}
	if(lastName==''){
		errors += '   => Last Name is required\n';	
	}
	if(primary_p1.length !=3 || primary_p2.length !=3 || primary_p3.length !=4 || !IsNumeric(primary_p1) || !IsNumeric(primary_p2) || !IsNumeric(primary_p3)){
		errors += '   => Primary Phone is not valid\n';	
	}
	
	if(email == emailReType){
		if(!IsValidEmail(email)){
			errors += '   => Email address is not valid\n';
		}
	}else{
		errors += '   => Email addresses do not match\n';	
	}
	
	if(errors === ''){
		dojo.byId('ContactForm').submit();
	}else{
		alert('The contact form reported the following error(s):\n\n'+errors);	
	}
}

function factorPhoneStepping(current,next,length){
	var cur = dojo.byId(current).value;
	if(cur.length >= length){
		dojo.byId(next).focus();
	}
}

function IsNumeric(strString){
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) return false;
	for (i = 0; i < strString.length && blnResult == true; i++){
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1){
			blnResult = false;
		}
	}
return blnResult;
}

function IsValidEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
		return false;
	}else{
		return true;	
	}
}

