//Standard AJAX functionality
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function ajaxRequest(url, params, handler) {

	http.open('post', url);
	
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.onreadystatechange = eval(handler);
	
	http.onreadystatechange = function() {
		
		if (http.readyState == 4) {
			handler += "()";
			eval(handler);
			
		}
	}
	
	http.send(params);

}


function submitCB1(saythanks) {

        fullname = document.getElementById('cb1_fullname').value;
        email = document.getElementById('cb1_email').value;
        telephone = document.getElementById('cb1_telephone').value;
        mobile = document.getElementById('cb1_mobile').value;
        signupform = document.getElementById('cb1_signupform').value;

        errormsg = "";
        
        if (fullname == "") { errormsg += "Please provide your name\n"; }
        if (email == "") { 
			errormsg += "Please provide an email address\n"; 
		} else {
			if (echeck(email)==false) { errormsg += "Please provide a valid email\n"; }
		}
        if ((telephone == "") && (mobile == "")) {
			errormsg += "Please provide at least one contact number\n"; 
		} else {
			if (telephone != "") { 
				if (checkInternationalPhone(telephone)==false) {
					errormsg += "Please provide a valid phone number\n"; 
				}
			}
			if (mobile != "") { 
				if (checkInternationalPhone(mobile)==false) {
					errormsg += "Please provide a valid mobile number\n"; 
				}
			}
		}
        
        if (errormsg == "") {
    
            document.getElementById('contactbox').innerHTML = '<img src="/images/ajax1.gif" />';

            params = 'fullname=' + fullname + '&email=' + email + '&telephone=' + telephone + '&mobile=' + mobile+'&signupform=' + signupform;
        	if (!saythanks) {
            	ajaxRequest("/includes/cb1submit.php", params, "cb1_submitted");
			} else {
				ajaxRequest("/includes/cb2submit.php", params, "cb1_submitted");
			}
            pageTracker._trackPageview("brochurecomplete.html");   
        
        } else {
        
            alert(errormsg);
        
        }
    
    }
    
    function cb1_submitted() {
    
        document.getElementById('contactbox').innerHTML = http.responseText;
    
    }
    
    function submitCallBack() {

        fullname = document.getElementById('cb2_fullname').value;
        email = document.getElementById('cb2_email').value;
        telephone = document.getElementById('cb2_telephone').value;
        mobile = document.getElementById('cb2_mobile').value;
        signupform = document.getElementById('cb2_signupform').value; 

        errormsg = "";
        
        if (fullname == "") { errormsg += "Please provide your name\n"; }
        if (email == "") { 
			errormsg += "Please provide an email address\n"; 
		} else {
			if (echeck(email)==false) { errormsg += "Please provide a valid email\n"; }
		}
        if ((telephone == "") && (mobile == "")) {
			errormsg += "Please provide at least one contact number\n"; 
		} else {
			if (telephone != "") { 
				if (checkInternationalPhone(telephone)==false) {
					errormsg += "Please provide a valid phone number\n"; 
				}
			}
			if (mobile != "") { 
				if (checkInternationalPhone(mobile)==false) {
					errormsg += "Please provide a valid mobile number\n"; 
				}
			}
		}
        
        if (errormsg == "") {
    
            document.getElementById('callBack').innerHTML = '<img src="images/ajax1.gif" />';

            params = 'fullname=' + fullname + '&email=' + email + '&telephone=' + telephone + '&mobile=' + mobile + '&signupform=' + signupform;
        
            ajaxRequest("includes/cb2submit.php", params, "callBack_submitted");
            
            pageTracker._trackPageview("callbackcomplete.html");
            
        } else {
        
            alert(errormsg);
        
        }
    
    }

    function callBack_submitted() {

        document.getElementById('callBack').innerHTML = http.responseText;
    
    }
	
	
	function echeck(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					
	}
	
// 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 = 6;

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);
}

