addEvent(window,'load',initialiseFeedbackData,false);

YAHOO.namespace("example.container");
YAHOO.util.Event.onDOMReady(initialise);

function initialise() {

	YAHOO.example.container.panel1 = new YAHOO.widget.Panel("panel1", { width:"1150px", visible:false, constraintoviewport:false, zIndex:99, modal:true, close:true, fixedcenter:true } );
	YAHOO.example.container.panel1.render(document.body);

	YAHOO.example.container.panel2 = new YAHOO.widget.Panel("panel2", { width:"360px", visible:false, constraintoviewport:false, zIndex:99, modal:true, close:true, fixedcenter:true } );
	YAHOO.example.container.panel2.render(document.body);

	YAHOO.example.container.panel4 = new YAHOO.widget.Panel("panel4", { width:"750px", visible:false, constraintoviewport:false, zIndex:99, modal:true, close:true, fixedcenter:true } );
	YAHOO.example.container.panel4.render(document.body);

	YAHOO.example.container.panel5 = new YAHOO.widget.Panel("panel5", { width:"970px", visible:false, constraintoviewport:false, zIndex:99, modal:true, close:true, fixedcenter:true } );
	YAHOO.example.container.panel5.render(document.body);

	YAHOO.example.container.panel3 = new YAHOO.widget.Panel("panel3", { width:"440px", visible:false, constraintoviewport:false, zIndex:99, modal:true, close:true, fixedcenter:true } );
	YAHOO.example.container.panel3.render(document.body);

	YAHOO.util.Event.addListener("showEast", "click", east);
	YAHOO.util.Event.addListener("showEast", "click", YAHOO.example.container.panel5.show, YAHOO.example.container.panel5, true);
	YAHOO.util.Event.addListener("showWest", "click", west);
	YAHOO.util.Event.addListener("showWest", "click", YAHOO.example.container.panel1.show, YAHOO.example.container.panel1, true);
	YAHOO.util.Event.addListener("showCentral", "click", central);
	YAHOO.util.Event.addListener("showCentral", "click", YAHOO.example.container.panel1.show, YAHOO.example.container.panel1, true);

	YAHOO.util.Event.addListener("sendMessage", "click", showComboBoxes);
	YAHOO.util.Event.addListener("sendMessage", "click", YAHOO.example.container.panel3.show, YAHOO.example.container.panel3, true);
	YAHOO.example.container.panel3.hideEvent.subscribe(hideComboBoxes);

}
function east() {
	var cardImage = document.getElementById("cardImage5");
	var email1 = document.getElementById("emailAddress51");
	var email2 = document.getElementById("emailAddress52");
	var email3 = document.getElementById("emailAddress53");
	var email4 = document.getElementById("emailAddress54");
	var allEmail = document.getElementById("allEmailAddress4");
	cardImage.src = "client/images/website/Team-East-Canada.png";
	email1.href = "mailto:wendy@kukrisports.com";
	email2.href = "mailto:dan.gordon@kukrisports.com";
	email3.href = "mailto:rahim.dharamsi@kukrisports.com";
	email4.href = "mailto:sam.nowell@kukrisports.com";
}
function central() {
	var cardImage = document.getElementById("cardImage");
	var email1 = document.getElementById("emailAddress1");
	var email2 = document.getElementById("emailAddress2");
	var email3 = document.getElementById("emailAddress3");
	var email4 = document.getElementById("emailAddress4");
	var email5 = document.getElementById("emailAddress5");
	var allEmail = document.getElementById("allEmailAddress5");

	cardImage.src = "client/images/website/Team-Central-Canada.png";
	email1.href = "mailto:wendy@kukrisports.com";
	email2.href = "mailto:dean.murten@kukrisports.com";
	email3.href = "mailto:jamie@kukrisports.com";
	email4.href = "mailto:ryan.smith@kukrisports.com";
	email5.href = "mailto:sam.nowell@kukrisports.com";
}
function west() {
	var cardImage = document.getElementById("cardImage");
	var email1 = document.getElementById("emailAddress1");
	var email2 = document.getElementById("emailAddress2");
	var email3 = document.getElementById("emailAddress3");
	var email4 = document.getElementById("emailAddress4");
	var email5 = document.getElementById("emailAddress5");
	var allEmail = document.getElementById("allEmailAddress5");
	cardImage.src = "client/images/website/Team-Canada-West.png";
	email1.href = "mailto:wendy@kukrisports.com";
	email2.href = "mailto:jamie@kukrisports.com";
	email3.href = "mailto:jen.ross@kukrisports.com";
	email4.href = "mailto:sue.beyer@kukrisports.com";
	email5.href = "mailto:sam.nowell@kukrisports.com";
}

function showComboBoxes() {
	var topic = document.getElementById("Topic");
	var ctry = document.getElementById("Country");
	if (topic) {
		topic.style.visibility = "visible";
	}
	if (ctry) {
		ctry.style.visibility = "visible";
	}
}
function hideComboBoxes() {
	var topic = document.getElementById("Topic");
	var ctry = document.getElementById("Country");
	if (topic) {
		topic.style.visibility = "hidden";
	}
	if (ctry) {
		ctry.style.visibility = "hidden";
	}
}

function initialiseFeedbackData() {
		populateTopic();
		populateCountry();
}

function addEvent(elm, evType, fn, useCapture) {
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);
			return true;
		}
		else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		}
		else {
			elm['on' + evType] = fn;
		}
	}

function populateTopic() {
		SystemLookupService.getForCombo("9TOP", function(iValues) {
			if (iValues) {
				var topic = document.getElementById("Topic");
				if (topic) {
					for (var i = 0; i < iValues.length; i=i+1) {
						var item = iValues[i];
						var opt = new Option(item.description, item.code, false, false);
						topic.options[topic.options.length] = opt;
				}
			}
		}
	});
}

function populateCountry() {
		SystemLookupService.getForCombo("9CTY", function(iValues) {
			if (iValues) {
				var country = document.getElementById("Country");
				if (country) {
					for (var i = 0; i < iValues.length; i=i+1) {
						var item = iValues[i];
						var opt = new Option(item.description, item.code, false, false);
						country.options[country.options.length] = opt;
				}
			}
		}
	});
}

function getSelectValue(iSelectObj) {
		if (iSelectObj) {
			for (var i = 0; i < iSelectObj.options.length; i=i+1) {
				var opt = iSelectObj.options[i];
				if (opt.selected) {
					return opt.value;
				}
			}
		}
		
		return "";
}

function processFeedback() {
	handleFeedbackSubmission('FEEDBACK')
}
function clearField(iField) {
	var theField = document.getElementById(iField);
	if (theField.value.length > 4 && theField.value.substring(0,4) == "Type")		{
		theField.value = "";
	}
}

function handleFeedbackSubmission(iCode, iElementIds) {
	var invalidElId = validateFeedbackInfo(['Topic','Enquiry','EmailAddress','FirstName','Surname','Country','Postcode','Phone']);
	if (invalidElId) {
		showErrorMessage(invalidElId);
	} else {
		stripQuotes();
		handleSubmission("FEEDBACK", ['Topic','Enquiry','EmailAddress','FirstName','Surname','Country','Postcode','Phone','KukriRegion'], handleCallBack);
	}
}
	
function validateFeedbackInfo(iElementIds) {
	for (var i = 0; i < iElementIds.length; i=i+1) {
		var elId = iElementIds[i];
		if (elId) {
			var el = document.getElementById(elId);
			if (el) {
				if (!el.value || "Type your first name here" == el.value || "Type your surname here" == el.value || "Type your email address here" == el.value) {
					return elId;
				}
			}
		}
	}
	return null;
}
	
function showErrorMessage(iElementId) {
	var elLbl = document.getElementById(iElementId + "Lbl");
	alert("Please supply a value for '" + elLbl.innerText + "'");
}
	 
function handleCallBack(iActionStatus) {
	alert(iActionStatus.actionMessage);
	
	if (iActionStatus.actionSuccessful) {
		document.forms['TellFriend'].reset();
	}

}

function stripQuotes() {
	var theText = document.getElementById("Enquiry");
	theText.value = replaceAll(theText.value,"'","");
}

function replaceAll(txt, replace, with_this) {
	return txt.replace(new RegExp(replace, 'g'),with_this);
}



