/**************************************************************************
** $Id:$
** Company:		World Nomads Group
** Author:		Ben Gillies -> ben.gillies at worldnomads.com
** Date:		11 September 2008
** Notes:		
**************************************************************************/
function dom_browser() {
	// browser identification based on dom capabilities
	this.myNav = this.navigator;
	this.version = this.navigator.appVersion;
	this.name = this.navigator.appName;
	this.userAgt = this.navigator.userAgent;
	this.ns4 = (document.layers) ? true : false;
	this.ns6 = (this.navigator.userAgent.indexOf("Netscape6") != -1) ? true : false;
	this.dom = (document.getElementById) ? true : false;
	this.ie4 = (document.all) ? true : false;
	this.mac = (this.version.indexOf("Mac") != -1) ? true : false;
	this.ie = (this.version.indexOf("MSIE") != -1) ? true : false;
	this.windows = (this.version.indexOf("Windows") != -1) ? true : false;
	this.hasPlugins = (this.navigator.plugins) ? true : false;
	this.ie6 = (this.version.indexOf("MSIE") != -1 && this.version.indexOf("6") != -1) ? true : false; 
	this.ie55 = (this.version.indexOf("MSIE 5.5") != -1) ? true : false;
	this.ie5 = (this.version.indexOf("MSIE 5.01") != -1) ? true : false;
	this.ns = (this.userAgt.indexOf("Netscape") != -1) ? true : false;
	this.ff = (this.userAgt.indexOf("Firefox") != -1) ? true : false;
	this.safari = (this.userAgt.indexOf("Safari") != -1) ? true : false;
}

dom_browser();

function trim(str) {
// removes white spaces from beginning and end of given string, str.
	return str.replace(/^\s*|\s*$/g,"");	
}

function addClassName(myElement, myClass) {
	var currentClassName = trim(myElement.className);
	var myReg = new RegExp("(^" + myClass + "$)|( " + myClass + "$)|(^" + myClass + " )|( " + myClass + " )", "g");
	if (!myReg.test(currentClassName)) {
		// not currently set on element, so add class..
		if (currentClassName != "") {
			myElement.className += " " + myClass;
		} else {
			myElement.className = myClass;
		}
	}
}

function removeClassName(myElement, myClass) {
	var currentClassName = trim(myElement.className);
	var myReg = new RegExp("(^" + myClass + "$)|( " + myClass + "$)|(^" + myClass + " )|( " + myClass + " )", "g");
	myElement.className = currentClassName.replace(myReg, "");
}

function switchClassName(myElement, removeClass, addClass) {
	removeClassName(myElement, removeClass);
	addClassName(myElement, addClass);
}

function hasClassName(myElement, myClass) {
	var currentClassName = trim(myElement.className);
	var myReg = new RegExp("(^" + myClass + "$)|( " + myClass + "$)|(^" + myClass + " )|( " + myClass + " )", "g");
	if (myReg.test(currentClassName)) {
		// has class name set..
		return true;
	} else {
		return false;
	}
}


function wn_formValidator(myFormId) {
	this.myForm = document.getElementById(myFormId);
	this.listen = function(evnt, elem, func) {
		if (elem.addEventListener) // W3C DOM
			elem.addEventListener(evnt,func,false);
		else if (elem.attachEvent) { // IE DOM
			var r = elem.attachEvent("on"+evnt, func);
			return r;
		}
	}
	// first setup onsubmit event on form..
//	this.listen("submit",this.myForm,document.getElementById(myFormId).checkForm());

	this.checkEmail = function(myEmail) {
		var myReg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
		if (myReg.test(myEmail)) {
			return true;
		} else {
			return false;
		}
	}
	this.showError = function(myErrorObj, myMessage) {
		try {
			myErrorObj.style.display = "block";
			myErrorObj.innerHTML += myMessage + " ";
		} catch (Exception) {}
	}
	this.clearErrors = function() {
		try {
			myErrorObj = document.getElementById(this.myForm.id + "_error");
			myErrorObj.innerHTML = "";
			myErrorObj.style.display = "none";
		} catch (Exception) {}
	}
	this.checkForm = function() {
		var errorFound = false;
		var showGenericError = true;
		var isMandatory = false;
		this.clearErrors();
		for (var i=0; i < this.myForm.elements.length; i++) {
			try {
				
				// clear existing errors..
				if(this.myForm.elements[i].type == "radio") {
					myErrorElement = document.getElementById(this.myForm.elements[i].name + "_error");
				} else {
					myErrorElement = document.getElementById(this.myForm.elements[i].id + "_error");
				}
				if (myErrorElement != null && typeof(myErrorElement) != "undefined") {
					myErrorElement.innerHTML = "";
				}
				
				// mandatory check..
				if (hasClassName(this.myForm.elements[i], "mandatory")) {
					isMandatory = true;
					switch (this.myForm.elements[i].type) {
						case "text":
							if (this.myForm.elements[i].value == "") {
								this.showError(myErrorElement, "Required.");
								errorFound = true;
							}
							break;
						case "select":
						case "select-one":
							if (this.myForm.elements[i].options.selectedIndex == 0) {
								this.showError(myErrorElement, "Required.");
								errorFound = true;
							}
							break;
						case 'radio':
							var radioSelected = false;
							if (!this.myForm.elements[i].checked) { 
								var radioElement = this.myForm.elements[i].name;
								for (k=0; k < this.myForm.elements.length; k++) {
									if (radioElement == this.myForm.elements[k].name && i != k) {
										if (this.myForm.elements[k].checked) {
											radioSelected = true;
										}
									}
								}
								if (radioSelected == false) {
									// showError(myErrorElement, "You are required to make a selection.");
									// showGenericError = false;
									if(myErrorElement) this.showError(myErrorElement, "You are required to make a selection.");
									errorFound = true;
								}
							}
							break;
						default:
					}
				} else {
					isMandatory = false;
				}
				
				// integer check..
				if (hasClassName(this.myForm.elements[i], "integer")) {
					var integerRegExp = /^[\d]+$/;
					if (!(!isMandatory && this.myForm.elements[i].value == "")) {
						// do not check the field if it doesn't have a value and is not mandatory..
						if (!this.myForm.elements[i].value.match(integerRegExp)) {
							try {
								this.showError(myErrorElement, "Must be a valid number. ");
							} catch (Exception) {}
							errorFound = true;
						}
					}
				}
				
				// email check..
				if (hasClassName(this.myForm.elements[i], "email")) {
					var emailRegExp = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
					if (!(!isMandatory && this.myForm.elements[i].value == "")) {
						// do not check the field if it doesn't have a value and is not mandatory..	
						var myTempEmail = this.myForm.elements[i].value;
						if (!emailRegExp.test(myTempEmail)) {
							this.showError(myErrorElement, "Please supply a valid email address.");
							errorFound = true;
						}
					}
				}
				
				
				
				// custom check..
				if (hasClassName(this.myForm.elements[i], "custom")) {
					var customRegExp = this.myForm.elements[i].rel;
					if (!(!isMandatory && this.myForm.elements[i].value == "")) {
						// do not check the field if it doesn't have a value and is not mandatory..	
						var myTempValue = this.myForm.elements[i].value;
						if (!customRegExp.test(myTempValue)) {
							this.showError(myErrorElement, "Custom error");
							errorFound = true;
						}
					}
				}
				
			} catch (Exception) {
				// prob error div not existing - do nothing..
			}
		}
		if (errorFound) {
			var errorObj = document.getElementById(this.myForm.id + "_error");
			if(showGenericError && errorObj) this.showError(errorObj, "Please fill out all fields as marked below.");
			return false;
		}
		return true;
		
	}
	
}

function qp_changeRegionDesc(myObj) {
	if ((myObj.value ==  'Worldwide including') || (myObj.value == 'Worldwide excluding')) {
		document.getElementById('qp2_regionDesc').innerHTML = "USA, Canada, Japan &amp; the Caribbean";
	} else if (myObj.value == 'Europe') {
		document.getElementById('qp2_regionDesc').innerHTML = "Countries of both Eastern &amp; Western Europe";
	}
}

