// JavaScript Document
//
//			wdMain.js										Main module for WebDirection.ca
//															This site uses the WebMap Library 
//															All WebMap variables are prefixed wm and defined in wmInit.js
//
if (!wd) {
	var wd = {};
}
function wdLoad() {
//
//														Set up workspace
//
	var displayDiv = document.getElementById ('workspace');
	wd.infoBook = new Book (displayDiv, "info");		// set up display book for information pages
	wd.bookCache = new Cache (wd.infoBook);			// set up cache for pages
//
//														Set up screen size
//
	setScreen(wd.infoBook);
//
//														Show the welcome page
//
	var welcomeDiv = document.getElementById ('welcome');
	wd.welcomeIndex = wd.infoBook.load(welcomeDiv);
	wd.infoBook.show(wd.welcomeIndex);
//
//														set up command variables
//
	wd.cmd = {};										// command objects - wd commands
	wd.menuOpts = {left:10,
					width:120};
//
//														Commands
//
	wd.cmd.home = document.getElementById ("cmdHome");
	wd.cmd.home.onclick = showHome;
	wd.cmd.about = new Command ("cmdAbout", wd.menuOpts);
	wd.cmd.research = new Command ("cmdResearch", wd.menuOpts);
	wd.cmd.contact = document.getElementById ("cmdContact");
//
//														About us
//
	var iX = commandCacheSetUp (wd.cmd.about, "Web Applications", "displayPage", wd.bookCache,
					   "Info/web_apps.html");
	wd.bookCache.setScript (iX, showMap);
	commandCacheSetUp (wd.cmd.about, "About Us", "displayPage", wd.bookCache,
						"Info/founder.html");
//
//														Research
//
	commandCacheSetUp (wd.cmd.research, "Web Map", "displayPage", wd.bookCache,
					   "Info/webmap.html");
	iX = commandCacheSetUp (wd.cmd.research, "Integration", "displayPage", wd.bookCache,
					   "Info/integration.html");
	wd.bookCache.setScript (iX, initExplain);
	commandCacheSetUp (wd.cmd.research, "Downloads", "displayPage", wd.bookCache,
					   "Info/downloads.html");
//
//														Contact Us
//
	wd.cmd.contact.onclick = contactUs;
	var contactDiv = document.getElementById ("contact");
	wd.contactIndex = wd.infoBook.load (contactDiv);

}
function wdUnload () {
	alert ("Thank you for visiting WebDirection");
	return;
}
function commandCacheSetUp (cmd, label, action, cache, URL) {
	cmd.addMenuItem (label, action);
	return cache.addEntry (label, URL);
}
function getSize() {
	size = {};
	if (window.innerWidth) {
		size.width = window.innerWidth;
		size.height = window.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientWidth) {
		size.width = document.documentElement.clientWidth;
		size.height = document.documentElement.clientHeight;
	}
	else if (document.body.clientWidth) {
		size.width = document.body.clientWidth;
		size.height = document.body.clientHeight;
	}
	return size;
}
function setScreen(book) {
//														Use view size to set divs
	wm.size = getSize();
	var width = (wm.size.width - 20);					// 10px margin on canvas from viewPort
	var height = (wm.size.height - 20);	
	if (width < 800) {
		width = 800;
	}
	if (height < 600) {
		height = 600;
	}
	var canvasDiv = document.getElementById ("canvas");
	canvasDiv.style.width = width + "px";
	canvasDiv.style.height = height + "px";
	var offsetTop = 60;								// Adjust if screen layout changes
	var commandDiv = document.getElementById ("command");
	commandDiv.style.height = (height - offsetTop - 10) + "px";
	var commandWidth = commandDiv.offsetWidth;
	var workDiv = document.getElementById ("workspace");
	workDiv.style.left = commandWidth + 10 + "px";
	workDiv.style.width = (width - commandWidth -20) + "px";
	workDiv.style.height = (height - offsetTop - 20) + "px";
	if (book) {											// if a book has been defined
		book.show();									// re-display to fit new screen size
	}
	return true;
}
//
//														Application processing starts
//
//
//														displayPage
//
//														User wants to browse browse documentation.
//														Sign in is not required.
//
function displayPage(label) {
//
//														Look up label in pageCache
//
//														Check if panels are visible - if so hide them
//
	if (wd.mapOptPanel) wd.mapOptPanel.show (false);
	if (wd.contactPanel) wd.contactPanel.show (false);
	wd.iCache = wd.bookCache.findPage (label);
	var bookIndex = wd.bookCache.getBookIndex (wd.iCache);
	if (bookIndex >= 0) {
		wd.infoBook.show (bookIndex);
		wd.bookCache.callScript (wd.iCache);
		return;
	}
//
//														Cache does not have a index to
//														the book, so the page has not yet been 
//														loaded.
//
	var docSet = wd.bookCache.getURL (wd.iCache);
	var getDocument = new DataLink (docSet, {async: true,  
							   				method: "GET",
											progress: pageStateInd,
											callback: setDocHTML});
	getDocument.tx ();									// Get documentation page
}
//
//														Set Active Indicator
//
function pageStateInd (i) {
	if ((i > 0) && (i < 4)) {
		document.compassImage.src = wdCompassA.src;
	} else {
		document.compassImage.src = wdCompass2.src;
	}
}
function setDocHTML (r) {
	pageStateInd (0);									// Reset active indicator
	var docDivString = getHTMLbody(r.responseText);		// Strip to body content
	var bookIndex = wd.bookCache.store (wd.iCache, docDivString);
	wd.infoBook.show(bookIndex);
	wd.bookCache.callScript (wd.iCache);
}
//
//														Show Home
//
function showHome() {
	if (wd.mapOptPanel) wd.mapOptPanel.show (false);
	if (wd.contactPanel) wd.contactPanel.show (false);
	wd.infoBook.show (wd.welcomeIndex);
}
//
//														Map functions
//
function showMap () {
	wd.map = new GMap2(document.getElementById("map"));
    wd.map.setCenter(new GLatLng(51, -114), 10);
	wd.map.addControl(new GSmallMapControl ());
	wd.map.addMapType(G_SATELLITE_MAP);
	wd.map.addMapType(G_PHYSICAL_MAP);
}
mapOptionPanel = function () {
	if (wd.mapOptPanel) {
		wd.mapOptPanel.show (true);
		return;
	}
	wd.mapOptPanel = new Panel ("Map Options", {top:260, left:260, width:260, height:140});
	var optDiv = document.getElementById ("mapOptions");
	wd.mapOptPanel.load (optDiv);
	optDiv.style.visibility = "inherit";
	var optForm = document.mapOpts;
	optForm.city.onclick = setMapCity;
	optForm.mapType.onclick = changeMapType;
	wd.mapOptPanel.show (true);
	return;
}
setMapCity = function () {
	var cityLatLng = [];
	cityLatLng[0] = new GLatLng(51, -114);				// Calgary
	cityLatLng[1] = new GLatLng(51.5, -0.1);			// London
	cityLatLng[2] = new GLatLng(40.75, -74);			// New York
	cityLatLng[3] = new GLatLng(48.8, 2.3);				// Paris
	cityLatLng[4] = new GLatLng(-33.9, 151.2);				// Sydney
	cityLatLng[5] = new GLatLng(35.6, 139.75);			// Tokyo
	pageStateInd (2);									// Indicate web activity
	wd.map.panTo(cityLatLng[this.value]);
	pageStateInd (0);
}
changeMapType = function () {
	switch (this.value) {
		case 'A':
			wd.map.setMapType (G_NORMAL_MAP);
			break;
		case 'B':
			wd.map.setMapType (G_SATELLITE_MAP);
			break;
		case 'C':
			wd.map.setMapType (G_PHYSICAL_MAP);
	}
	return;
}
//
//														Contact Us
//
function contactUs() {
	if (wd.mapOptPanel) wd.mapOptPanel.show (false);
	if (wd.contactPanel) wd.contactPanel.show (false);
	wd.infoBook.show (wd.contactIndex);
}
function contactRequest() {
	if (wd.contactPanel) {
		wd.contactPanel.show();
		return;
	}
	var contactDoc = new DataLink ("contactForm.html", {async:true,
								  method:"GET",
								  progress: pageStateInd,
								  callback: loadContactForm});
	contactDoc.tx ();
}
function loadContactForm (r) {
	pageStateInd (0);
	wd.contactPanel = new Panel("Contact Us", {top:40, left:300, width:400, height:440});
	var contactFmDivString = getHTMLbody(r.responseText);
	wd.contactPanel.load (contactFmDivString);
	var contactFmDiv = document.getElementById ("contactForm");
	contactFmDiv.style.visibility = "inherit";
	var contactFm = document.contactData;
//
//														Set up validation routines
//
	contactFm.firstName.onchange = validAlpha;
	contactFm.lastName.onchange = validAlpha;
	contactFm.organization.onchange = validAlpha;
	contactFm.phoneNumber.onchange = validPhone;
	contactFm.eMail.onchange = validMail;
	contactFm.processContact.onclick = processContactForm;
	wm.errorCount = 0;									// initialize validation error counter
//
//														Display panel
//
	wd.contactPanel.show();
	contactFm.firstName.focus();						// set input focus to first field
}
function validAlpha () {
	var field = this;									//	Event triggered by change of an alpha field
	var id = field.getAttribute("name");				//	id for error field
	clearError (id);									//	Clear any previous error message
	var name = new String (field.value);
	return checkFieldAlpha (id, name, wm.nameSp);
}
function validPhone () {
	var field = this;
	var id = field.getAttribute("name");
	clearError (id);
	var phone = new String (field.value);
	return checkFieldNum (id, phone, wm.phoneSp, true);
}
function validMail () {
	var field = this;
	var id = field.getAttribute ("name");
	clearError (id);
	var mail = new String (field.value);
	if (mail == "") return true;
	return checkFieldMail (id, mail);
}
function processContactForm() {
	var errorMessageId = document.getElementById ("contactError");
	if (wm.errorCount > 0) {
		errorMessageId.innerHTML = "Please fix all errors to proceed";
		return false;
	}
	errorMessageId.innerHTML = "";
	var contactFm = document.contactData;
	if (!(contactFm.firstName.value) || (contactFm.firstName.value == "")) {
		errorMessageId.innerHTML = "Please enter your first name";
		return false;
	}
	if (!(contactFm.lastName.value) || (contactFm.lastName.value == "")) {
		errorMessageId.innerHTML = "Please enter your last name";
		return false;
	}
	if ((!(contactFm.phoneNumber.value) || (contactFm.phoneNumber.value == "")) && 
		(!(contactFm.eMail.value) || (contactFm.eMail.value == ""))) {
		errorMessageId.innerHTML = "Please enter your e-mail address or phone number so we can contact you";
		return false;
	}
	if (!(contactFm.message.value) || (contactFm.message.value == "")) {
		errorMessageId.innerHTML = "Please enter a message";
		return false;
	}
//
//														Form valid - format contact request
//
	var contactReq = {
		firstName: contactFm.firstName.value,
		lastName: contactFm.lastName.value,
		organization: contactFm.organization.value,
		phoneNumber: contactFm.phoneNumber.value,
		eMail: contactFm.eMail.value,
		subject: contactFm.subject.value,
		message: contactFm.message.value
	};
	var sendContactReq = new DataLink ("contactRequest.php", {async: true,
									   						  method: 'POST',
															  progress:contactState,
									   						  callback:contactSent});
	sendContactReq.tx (contactReq);
}
function contactState (i) {
	wd.contactPanel.status (i);						// Set activity light
	return;
}
function contactSent (r) {
	wd.contactPanel.status (0);						// Reset activity light
//
//													Debug
//	alert (r.responseText);
//	return;
//
	var response = eval ( "(" + r.responseText + ")" );
	if (response.state != 'OK') {
		throw new Error ("Contact - Contact request failed state: " 
						 + response.state + "message: " + response.message);
	}
	wd.contactPanel.clear();
	delete wd.contactPanel;
	wd.requestComplete = new Panel ("Thank you", {left:((wm.size.width/2)-160),
																 top:((wm.size.height/2)-100),
																 width:320,
																 height:200});
	var message = '<div align="center" class="wdSubHead" >' +
				  'Your request has been sent. We will be in contact shortly.<br />&nbsp;<br />' +
				  '<input align="center" type="button" value="Continue" ' + 
				  'onclick="contactComplete();" /></div>';			
	wd.requestComplete.load(message);
	wd.requestComplete.show ();
}
function contactComplete () {
	wd.requestComplete.clear();
	delete wd.requestComplete;
	wd.infoBook.show(wd.welcomeIndex);
}
function initExplain () {
	var exDiv = document.getElementById ("ex");
	var p1 = document.getElementById ("ex_consult_text");
	var p2 = document.getElementById ("ex_require_text");
	var p3 = document.getElementById ("ex_develop_text");
	var p4 = document.getElementById ("ex_implement_text");
	exDiv.appendChild(p1);
	exDiv.appendChild(p2);
	exDiv.appendChild(p3);
	exDiv.appendChild(p4);
}
displayExplanation = function (ev) {
	ev = ev || window.event;
	if (window.event) {
		var self = ev.srcElement;
	} else {
		var self = ev.target;
	}
	var exId = self.id.substring(0, self.id.indexOf('_'));
	var exDiv = document.getElementById (exId);
	for (var n= exDiv.firstChild; n != null; n=n.nextSibling) {
		if (n.nodeType != 1) continue;
		if (n.nodeName != "DIV" ) continue;
		n.style.visibility = "hidden";
		}
	var showDiv = document.getElementById(self.id + "_text");
	showDiv.style.visibility = "visible";
}
blankExplanation = function (ev) {
	ev = ev || window.event;
	if (window.event) {
		var self = ev.srcElement;
	} else {
		var self = ev.target;
	}
	var exId = self.id.substring(0, self.id.indexOf('_'));
	var exDiv = document.getElementById (exId);
	for (var n= exDiv.firstChild; n != null; n=n.nextSibling) {
		if (n.nodeType != 1) continue;
		if (n.nodeName != "DIV" ) continue;
		n.style.visibility = "hidden";
		}
}

	