/*
 * Validator
 * 
 *    A collection of form validation functions.
 *
 * Tom Coppeto
 * 10 May 2007
 */


var Validator = {

    email : function(addr) {
	var valid = true;

	if (!addr) {
	    valid = false;
	} else if ((addr.indexOf("@") <= 0) || (addr.indexOf("@") == addr.length)) {
	    valid = false;
	} else if ((addr.indexOf(".") <= 0) || (addr.indexOf(".") == addr.length)) {
	    valid = false;
	} else if (addr.indexOf("@", addr.indexOf("@") + 1) != -1) {
	    valid = false;
	} else if (addr.substring(addr.indexOf("@") - 1, addr.indexOf("@")) == ".") {
	    valid = false;
	} else if (addr.substring(addr.indexOf("@") + 1, addr.indexOf("@") + 2)== ".") {
	    valid = false;
	} else if (addr.indexOf(".", addr.indexOf("@") + 2) == -1) {
	    valid = false;
	} else if (addr.indexOf(" ") != -1) {
	    valid = false;
	} else if (addr.indexOf(";") != -1) {
	    valid = false;
	}

	return (valid);
    },


    phone : function(s) {
	var minDigitsInIPhoneNumber = 10;
	s = Validator.stripPhoneChars(s);
	return (Validator.isInteger(s) && s.length >= minDigitsInIPhoneNumber);
    },


    text : function(str) {

	if (!str) {
	    return (false);
	}

	return (true);
    },


    isInteger : function(s) {
	var i;
	for (i = 0; i < s.length; i++) {   
	    var c = s.charAt(i);
	    if (((c < "0") || (c > "9"))) {
		return false;
	    }
	}

	return (true);
    },


    stripPhoneChars : function(s) {
	var digits = "0123456789";
	var phoneNumberDelimiters = "().- ";
	var validChars = phoneNumberDelimiters + "+";

	var i;
	var returnString = "";

	for (i = 0; i < s.length; i++) {
	    var c = s.charAt(i);
	    if (validChars.indexOf(c) == -1) {
		returnString += c;
	    }
	}
	return (returnString);
    }
}



String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
/*
 * validate_feedback
 * 
 *    Validates input data from the tour feedback form.
 *
 * Tom Coppeto
 * 5 June 2007
 */


var validate_feedback = {

    check : function() {

	if (!document.getElementById) {
	    return (true);
	}

	var ot = document.getElementById("origin");
	if (ot.value == "") {
	    alert("Where are you from?");
	    ot.focus();
	    return (false);
	}
	/*	
	var ht = document.getElementById("foundUs");
	if (ht.value == "") {
	    alert("How did you find out about Boston By Foot?");
	    ht.focus();
	    return (false);
	}
	*/
	var tt = document.getElementById("tour");
	if (tt.value == "") {
	    alert("What tour did you take?");
	    tt.focus();
	    return (false);
	}
	
	var st = document.getElementById("stateHouseArchitect");
	if (st.value == "") {
	    alert ("This is a required field as part of our anti-spam mechanism. Scroll up to see the photograph for the answer.");
	    st.focus();
	    return (false);
	}
	
	if (st.value == "ap") {
	    alert ("While he studed under the architect of the State House, Parris was known for his designs in the Greek Revival style of the early 19th century which includes Quincy Market. Please enter the correct value and try again.");
	    st.focus();
	    return (false);
	} 
	
	if (st.value == "norm") {
	    alert ("While Norm has spent considerable time on Beacon Street, he wasn't occupied by the design of the State House. Please enter the correct value and try again.");
	    st.focus();
	    return (false);
	}
	
	if (st.value == "mmw") {
	    alert ("McKim, Meade and White were architects during the Victorian age and designed the Boston Public Library. Please enter the correct value and try again.");
	    st.focus();
	    return (false);
	}
	
	if (st.value == "ab") {
	    alert ("Asher was another prolific architect of the Federal style, but the State House wasn't one of them. His works included the Charles Street Meeting House and the Old West Church. Please enter the correct value and try again.");
	    st.focus();
	    return (false);
	}
		
	var stt = document.getElementById("satisfaction");
	if (stt.value == "") {
	    alert("How satisfied are you with your tour?");
	    stt.focus();
	    return (false);
	}
	
	var ct = document.getElementById("compare");
	if (ct.value == "") {
	    alert("How would you compare Boston By Foot?");
	    ct.focus();
	    return (false);
	}
	
	
	var ft = document.getElementById("topOfFaneuil");
	if (ft.value == "") {
	    alert ("This is a required field as part of our anti-spam mechanism. Scroll up to see the photograph for the answer.");
	    ft.focus();
	    return (false);
	}
	
	if (ft.value == "chimney") {
	    alert ("Faneuil Hall does not have a chimney. Please enter the correct value and try again.");
	    ft.focus();
	    return (false);
	}
	
	if (ft.value == "dome") {
	    alert ("Domes were not part of the Federal style, but one does appear on Quincy Market. Please enter the correct value and try again.");
	    ft.focus();
	    return (false);
	}
	
	if (ft.value == "arod") {
	    alert ("We sincerely doubt his likeness would be placed on Faneuil Hall anytime soon. Please enter the correct value and try again.");
	    ft.focus();
	    return (false);
	}
	
	if (ft.value == "all") {
	    alert ("The building isn't large enough to support all these elements. Please enter the correct value and try again.");
	    ft.focus();
	    return (false);
	}
	
	var resp = document.getElementById("respond");
	if (resp.value == "yes") {
	    var addr = document.getElementById("email");
	    if (!addr.value) {
		alert ("Please enter an email address if you would like a response.");
		addr.focus();
		return (false);
	    }

	    if (!Validator.email(addr.value)) {
		alert ("Please check your email address if you would like a response.");
		addr.focus();
		return (false);
	    }
	}
	
	return (true);
    }
}
