var host = location.hostname;

// +++++++++++++++++++++++++++++++++++++++++
// Local functions
// +++++++++++++++++++++++++++++++++++++++++
function checkRequired (formName) {

	var x = document.forms[formName].elements;
 	for (var i=0;i<x.length;i++)
 	{
  		if (x[i].getAttribute('required') && !x[i].value) {
  			if(formName == 'contact-form') {
  				alert('First Name, Last Name, Email, and Request Details are required fields. ')
  			} else {
  				alert('Please enter required field')
  			}
    		
    		return false;
 		}
 	}
 	return true;
}

function formatPhone(formName, fieldName)
{
	var phone = $F(fieldName).replace(/[\(\)\.\-\ ]/g, '');
	if(phone=='') { return true;}
	var field = $(fieldName);
	var phoneFormat ="";
	if (!IsNumeric(phone)) {
   		alert("The phone number contains illegal characters.");
   		field.focus();
   		return false;
	}
	if (phone.length != 10 ) {
		alert("Please enter area code plus a 7 digit number.");
		field.focus();
   		return false;
		}
	if(phone.length == 10) {
		for (i = 0; i < phone.length; i++) {
			if(i==0) { phoneFormat += '('; }
			if(i==3) { phoneFormat += ') '; }
			if(i==6) { phoneFormat += '-'; }
			phoneFormat += phone.charAt(i); 
		}
		field.value = phoneFormat;
		return true;
	}
}

function sendForm(formName, action, phoneField)
{
	var required = checkRequired(formName);
	var phoneField = (phoneField == null) ? "phone" : phoneField;
	var f = $(formName);
		
	if(required == true) {
		if(! formatPhone(formName, phoneField)) { return false;	}
		
		params = 'q=assets/snippets/ajax/modxFormHandler.php&action=' + action + '&' + Form.serialize(f);
		url = 'index-ajax.php';
		
		new Ajax.Request(url,{method:'post',parameters:params,onComplete:ajaxScript});
		Form.reset(f);
		return false;
	} 
}

// +++++++++++++++++++++++++++++++++++++++++
// AJAX functions
// +++++++++++++++++++++++++++++++++++++++++
	
function ajaxScript(response) {
		var cmdString = response.responseText;
		var cmdArray = cmdString.split('||');
		//alert(response.responseText);
		
		for(cnt=0;cnt<cmdArray.length;cnt++) {
			eval(cmdArray[cnt]);
		}
}

// +++++++++++++++++++++++++++++++++++++++++
// Housekeeping functions
// +++++++++++++++++++++++++++++++++++++++++
// http://www.codetoad.com/javascript/isnumeric.asp

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}



