﻿// This is the javascript support code for the request Free Trial form page

function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
	window.onload = func; 
	} else { 
	    window.onload = function() { 
	    oldonload(); 
	    func(); 
	   } 
	} 
} 



function insertAfter(newElement,targetElement)	{
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement)	{
		parent.appendChild(newElement);
	}	else	{
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}	

function addClass(element,value)	{
	if (!element.className)	{
		element.className = value;
	}	else	{
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}


function formValidator(){
	// Make quick references to our fields
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	var subscribe = document.getElementById('subscribe');
	var comments = document.getElementById('comments');

	
	// Check each input in the order that it appears in the form!


	if(notEmpty(name, "Please enter your name")){
		if(notTooLong(name, "Please limit your name to 75 characters")){
			if(emailValidator(email, "Please enter a valid email address")){
				if(notEmpty(phone, "Please enter your phone number")){
					if(notTooLong(phone, "Please verfiy your phone number (max 75 characters)")){
						if(checkBox("Please tick box to acknowledge your agreement to our Terms of Service")){
							if(notTooLongComment(comments, "Please limit your comments to a maximum of 500 characters")){
							return true;
							}
						}
					}
				}
			}
		}
	}

	return false;
	
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}


function notTooLong(elem, helperMsg){
	if(elem.value.length > 75){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function notTooLongComment(elem, helperMsg){
	if(elem.value.length > 550){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}



function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function checkBox (helperMsg){

var subscribe = document.getElementById('subscribe');

	if(subscribe.checked == false){;
		alert(helperMsg);
		subscribe.focus();
		return false;
	}else{
		return true;
	}
}

/*
function checkBoxBac (){
	if(!document.freeTrialRequest.subscribe[0].checked) {
		alert("Please acknowledge your agreement to out Terms of Service");
		return false;
	}else{
		return true;
	}
}
*/

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
