/* QuickerTek Site Configuration JavaScript v0.1
	Config and preference control
	
	Index:
		string mainnav (whereOnPage)
*/

/* Script Constants */
var SITE_CONFIG_URI = "inventory.xml";

/* void mainnav (XmlElement whosiwhatis) */
function makeMainnav (whosiwhatsit, currentPage) {

	var mnhtml = "";

//	var configXml = openXml (SITE_CONFIG_URI).getElementsByTagName ("site");
	// Move into <main_nav> element
	whosiwhatsit = whosiwhatsit.getElementsByTagName ("mainnav");
	var links = whosiwhatsit[0].getElementsByTagName ("link");
	
	// Make output HTML
	mnhtml += "<div class=\"mainnav\"><ul>";
	
	// Iterate links into mainnav element
	for (var link =0; link < links.length; link++) {
//	for (link in links) {
	
		if (links[link].nodeType == 1) {
		
			var thisLink = links[link].firstChild.data.replace (/ /gi, "_");
			thisLink = thisLink.toLowerCase ();
		
			mnhtml += "<li";
			// Set first class
			if (link == 0) {
			
				mnhtml += " class=\"first\"";
			
			}
			
			mnhtml += "><a href=\"";
			mnhtml += thisLink + ".html";
			mnhtml += "\""
			
			if (thisLink == currentPage) {
			
				mnhtml += " class=\"current\"";
			
			}
			
			mnhtml += ">";
			mnhtml += thisLink;
			mnhtml += "</a></li>";
		
		}
	
	}
			
	mnhtml += "</ul></div>";
//	alert (mnhtml);
	
//	printToPage (document, "topnav", mnhtml);
	return mnhtml;
	
}

function makeFooter (whosiwhatsit) {

	var fhtml = "";
//	var configXml = openXml (SITE_CONFIG_URI).getElementsByTagName ("site");
	whosiwhatsit = whosiwhatsit.getElementsByTagName ("footer");

	// Get values
	var copies = whosiwhatsit[0].getElementsByTagName ("copy");
	var links = whosiwhatsit[0].getElementsByTagName ("link");
	
	fhtml += "<p style=\"float: right;\">";	// Start Links in HTML
	
	for (var link = 0; link < links.length; link++) {
//	for (link in links) {

		if (links[link].nodeType == 1) {
		
			fhtml += "<a href=\"";
			fhtml += "\">";
			fhtml += links[link].firstChild.data;
			fhtml += "</a>";
			
			// Add divider if not last
			if ( (++link) < links.length) {
				fhtml += "&nbsp;|&nbsp;";
				}
		
		}

	}
	
	fhtml += "</p>";						// End Links in HTML
	fhtml += "<p>&copy;&nbsp;";				// Start Copyright in HTML
	
	for (var copy = 0; copy < copies.length; copy++) {
//	for (copy in copies) {
	
		if (copies[copy].nodeType == 1) {
		
			fhtml += "&nbsp;";
			fhtml += copies[copy].firstChild.data;
			fhtml += ".&nbsp;";
		
		}
	
	}
	
	fhtml += "</p>";						// End Copyright in HTML
	
//	printToPage (document, "footerpad", fhtml);
	return fhtml;

}

function makeCustComm () {
	// Get and clear existing element
	var where = document.getElementById("com_tile");
	if (!where) return;
	while(where.hasChildNodes()) where.removeChild(where.childNodes[0]);
	// Add Mac World informatics
	var chtml = document.createElement("div");
	chtml.id = "com_random";
//	chtml.className = "home_tile";
	whosiwhatsit = COMM_XML.getElementsByTagName ("comments")[0];
	// Move into <comment>s
	var comments = whosiwhatsit.getElementsByTagName ("comment");
	// Generate random index
	var which = Math.ceil (Math.random () * (comments.length - 1));
	// Print out content layer
	for (var comment =0; comment < comments.length; comment++) {
		if ((comment) == which) {
			if (comments[comment].nodeType == 1) {
				// Throttle
				var lim	= 300;
				var it	= 0;
				// Add title
				//var strongEl = document.createElement("strong");
				var strongEl = document.createElement("div");
				strongEl.className = "strong";
				var stext = comments[comment].getAttribute("name") + "&nbsp;says:";
				strongEl.innerHTML = stext;
				chtml.appendChild(strongEl);
				// Add paragraphs
				var peas = comments[comment].getElementsByTagName("p");
				for (var pea =0; pea < peas.length; pea++) {
//				for (var pea = 0; pea < 3; pea++) {
//				for (pea in peas) {
					if(peas[pea].nodeType==1&&it<lim) {
						var peaEl = document.createElement("p");
						var ptext = peas[pea].firstChild.data;
						if (it + ptext.length > lim) { // Make sure we have room
							var iend = lim - it;
							ptext = ptext.substring(0, iend);
							ptext += "&nbsp;[more]";
						}
						peaEl.innerHTML = ptext;
						chtml.appendChild(peaEl);
						// Increment it
						it += ptext.length + 50;
					}
				}
			}
			//alert (it);
			break;
		}
	}
	// Make sure to fade it in
	/* Removed per Rick
	chtml.style.opacity = '0';
	chtml.style.filter = 'alpha(opacity = 0)';
	chtml.FadeState = -2;
	*/
	// Attach content
	where.appendChild(chtml);
	// Fade in
	//Fade(chtml.id,null,1);
	//var COM_TIMEOUT = setTimeout("Fade('" + chtml.id + "'," + makeCustComm + ")", 18000);
}

function makeFaqComm () {
	//HACK: for animate
	var where = document.getElementById("faq_tile");
	if (!where) return;
	// Clear existing content
	while(where.hasChildNodes()) where.removeChild(where.childNodes[0]);
	// Add Mac World informatics
	var chtml = document.createElement("div");
	chtml.id = "faq_random";
//	chtml.className = "home_tile";
	var whosiwhatsit = FAQ_XML.getElementsByTagName ("category")[0];
	// Move into <comment>s
	var comments = whosiwhatsit.getElementsByTagName ("ask");
	// Generate random index
	var which = Math.ceil (Math.random () * (comments.length - 1));
	// Print out content layer
	for (var comment =0; comment < comments.length; comment++) {
		if ((comment) == which) {
			if (comments[comment].nodeType == 1) {
				// Throttle
				var lim	= 300;
				var it	= 0;
				// Add title
				//var strongEl = document.createElement("strong");
				var strongEl = document.createElement("div");
				strongEl.className = "strong";
//				var stext = comments[comment].getAttribute("name") + "&nbsp;says:";
				var stext = comments[comment].getElementsByTagName("question")[0].firstChild.data;
				strongEl.innerHTML = stext;
				chtml.appendChild(strongEl);
				// Add paragraphs
				var peas = comments[comment].getElementsByTagName("answer");
				for (var pea =0; pea < peas.length; pea++) {
//				for (var pea = 0; pea < 3; pea++) {
//				for (pea in peas) {
					if(peas[pea].nodeType==1&&it<lim) {
						var peaEl = document.createElement("p");
						var ptext = peas[pea].firstChild.data;
						if (it + ptext.length > lim) { // Make sure we have room
							var iend = lim - it;
							ptext = ptext.substring(0, iend);
							ptext += "&nbsp;[more]";
						}
						peaEl.innerHTML = ptext;
						chtml.appendChild(peaEl);
						// Increment it
						it += ptext.length + 50;
					}
				}
			}
			//alert (it);
			break;
		}
	}
	// Make sure to fade it in
	/* Removed per Rick
	chtml.style.opacity = '0';
	chtml.style.filter = 'alpha(opacity = 0)';
	chtml.FadeState = -2;
	*/
	// Attach content
	where.appendChild(chtml);
	// Fade in
	//Fade(chtml.id,null,1);
	//var FAQ_TIMEOUT = setTimeout("Fade('" + chtml.id + "'," + makeFaqComm + ")", 17000);
}

//function makePressComm (whosiwhatsit, eid) {
function makePressComm () {
	//HACK: for cycling
//	var whosiwhatsit = "press.xml";
	var eid = "press_tile";
	// Open document element
	var where = document.getElementById(eid);
	if (!where) return;
	// Clear existing content
	while (where.hasChildNodes()) where.removeChild(where.childNodes[0]);
	// Add Mac World informatics
	var chtml = document.createElement("div");
	chtml.id = "press_random";
//	chtml.className = "home_tile";
	// Move into <comment>s
	var comments = PRESS_XML.getElementsByTagName("release");
	// Generate random index
	var which = Math.ceil (Math.random () * (comments.length - 1));
	// Print out content layer
	for (var comment =0; comment < comments.length; comment++) {
		if ((comment) == which) {
			if (comments[comment].nodeType == 1) {
				// Throttle
				var lim	= 300;
				var it	= 0;
				// Add title
				var strongEl = document.createElement("div");
				strongEl.className = "strong";
				var stext = comments[comment].getElementsByTagName("title")[0].firstChild.data;
				strongEl.innerHTML = stext;
				chtml.appendChild(strongEl);
				// Add paragraphs
				var peas = document.createElement("p");
				peas.innerHTML = "Click to Read More...";
				chtml.appendChild(peas);
/*				var peas = comments[comment].getElementsByTagName("answer");
				for (var pea =0; pea < peas.length; pea++) {
//				for (var pea = 0; pea < 3; pea++) {
//				for (pea in peas) {
					if(peas[pea].nodeType==1&&it<lim) {
						var peaEl = document.createElement("p");
						var ptext = peas[pea].firstChild.data;
						if (it + ptext.length > lim) { // Make sure we have room
							var iend = lim - it;
							ptext = ptext.substring(0, iend);
							ptext += "&nbsp;[more]";
						}
						peaEl.innerHTML = ptext;
						chtml.appendChild(peaEl);
						// Increment it
						it += ptext.length + 50;
					}
				}
				*/
			}
			//alert (it);
			break;
		}
	}

	// Make sure to fade it in
	/* Removed per Rick
	chtml.style.opacity = '0';
	chtml.style.filter = 'alpha(opacity = 0)';
	chtml.FadeState = -2;
	*/
	// Attach content
	where.appendChild(chtml);
	// Fade in
	//Fade(chtml.id,null,1);
	//return chtml;
//	var PRESS_TIMEOUT = setTimeout("makePressComm ('" + whosiwhatsit + "', '" + eid + "')", 10000);
	//var PRESS_TIMEOUT = setTimeout("Fade('" + chtml.id + "'," + makePressComm + ")", 15000);
}

/* Builds Resellers Map */
function cartResellers (resellers, where) {
//	alert(resellers.length);
	// Make float and static info fields
	var floatDiv, statDiv, imgMap;
	floatDiv		= document.createElement('div');
	floatDiv.id		= "float";
	statDiv			= document.createElement('div');
	statDiv.id		= "static";
	imgMap			= document.createElement('map');
	imgMap.name		= "resell";
	// Read info about map from xml
	for (var rs =0; rs < resellers.length; rs++) {
//	for (rs in resellers) {
		if (resellers[rs].nodeType == 1) {
			var rName = resellers[rs].getAttribute ("name");
			var img = rName.replace (/[ .,']/gi, "_");
			img = img.toLowerCase ();
			var url = resellers[rs].getAttribute ("url");
			var address = new Array ();
			var phone = new Array ();
			var hours = new Array ();

			// get dimensions
			var dim = resellers[rs].getAttribute ("dim").split (",", 2);
			// get coordinates
			var coords = resellers[rs].getAttribute ("coords").split (",", 2);
			// get attributes
			var attrs = resellers[rs].getElementsByTagName ("attr");
			for (var at = 0; at < attrs.length; at++) {
//			for (at in attrs) {
				// get address lines
				if (attrs[at].nodeType == 1) {
					switch (attrs[at].getAttribute ("type")) {
						case "addr":
							address.push (attrs[at].firstChild.data);
							break;
						case "fone":
							phone.push (attrs[at].firstChild.data);
							break;
						case "hour":
							hours.push (attrs[at].firstChild.data);
							break;
					}
				}
			}

			// Make float element
			var ifloatDiv		= document.createElement('div');
			ifloatDiv.id		= img;
			ifloatDiv.className	= "reseller";
			// Set style elements
			ifloatDiv.style.cursor		= "pointer";
			ifloatDiv.style.width		= dim[0] + "px";
			ifloatDiv.style.height		= dim[1] + "px";
			ifloatDiv.style.background	= "white url(images/resellers/" + img + ".png) no-repeat";
			ifloatDiv.style.display		= "none";
			// Hook event handles
			ifloatDiv.setAttribute ('onmouseout', "hideSeller('" + img + "')");
			ifloatDiv.setAttribute ('onclick', "window.open('" + url + "')");
			// Add to list
			floatDiv.appendChild(ifloatDiv);
//			alert(floatDiv.innerHTML);
			
			// Make static element
			ustat				= document.createElement('ul');
			ustat.id				= img + "_static";
			ustat.style.display	= "none";
			// if QuickerTek
			if (coords[0] == 0 && coords[1] == 0) {
				ustat.style.display = "block";
				}
			// List data : name
			lstat_name = document.createElement('li');
			lstat_name.id = "name";
			// data div
			lstat_nameDiv = document.createElement('div');
			lstat_nameDiv.setAttribute('onclick', "window.open('" + url + "')");
			lstat_nameDiv.style.cursor = "pointer";
			lstat_nameDiv.style.width = dim[0] + "px";
			lstat_nameDiv.style.height = dim[1] + "px";
			var strbk = "#fff url(images/resellers/" + img + ".png) no-repeat";
			lstat_nameDiv.style.background = strbk;
//			alert(lstat_nameDiv.style.background);
			// Close list data : name
			lstat_name.appendChild(lstat_nameDiv);
			ustat.appendChild(lstat_name);
			// List data : addr
			lstat_addr = document.createElement('li');
			lstat_addr.id = "addr";
			// data divs
			for (var addr = 0; addr < address.length; addr++) {
				var lstat_addrDiv = document.createElement('div');
				lstat_addrDiv.innerHTML = address[addr];
				lstat_addr.appendChild(lstat_addrDiv);
			}
			// Close list data : addr
			ustat.appendChild(lstat_addr);
			// List data : fone
			var lstat_fone = document.createElement('li');
			lstat_fone.id = "fone";
			// data divs
			for (var fone = 0; fone < phone.length; fone++) {
				var lstat_foneDiv = document.createElement('div');
				lstat_foneDiv.innerHTML = phone[fone];
				lstat_fone.appendChild(lstat_foneDiv);
			}
			// Close list data : fone
			ustat.appendChild(lstat_fone);
			// List data : hour
			var lstat_hour = document.createElement('li');
			lstat_hour.id = "hour";
			for (var hour = 0; hour < hours.length; hour++) {
				lstat_hourDiv = document.createElement('div');
				lstat_hourDiv.innerHTML = hours[hour];
				lstat_hour.appendChild(lstat_hourDiv);
			}
			// Close list data : hour
			ustat.appendChild(lstat_hour);
			// Append to Static Div
			statDiv.appendChild(ustat);
			
			// Make map element
			var aimgMap = document.createElement('area');
			aimgMap.shape = "circle";
			aimgMap.coords = coords[0] + "," + coords[1] + ",5";
			aimgMap.setAttribute('onmouseover', "showSeller('" + img + "')");
			// Append to Map
			imgMap.appendChild(aimgMap);
		}
	}
	// Get Page Location
	//var write = document.getElementById(where);
	where.appendChild(floatDiv);
	where.appendChild(statDiv);
	where.appendChild(imgMap);
}

/* Sets and Resets FAQ display */
function setFaq (strFaq) {
	// limit
	var lim = 500;
	//var it = 0;
	var boxBod = document.getElementById("faqs_go_here");
	// clear FAQs
	while (boxBod.hasChildNodes()) {
	 	boxBod.removeChild(boxBod.childNodes[0]);
	}
	// Add Categories
	var m_faq = FAQ_XML.getElementsByTagName("category");
	for (var f = 0; f < m_faq.length; f++) {
		// Get <category> name
		var mf_name = m_faq[f].getAttribute("name");
		if (strFaq && mf_name!=strFaq) {continue;}
		// Add title tag
		var c_title = document.createElement("div");
		c_title.id = "column";
		// Customize id="column"
		c_title.style.width = strFaq ? "800px" : "122px";
		c_title.style.margin = "10px";
		c_title.style.cssFloat = "left";
		c_title.style.cursor = "pointer";
		c_title.style.textAlign = "center";
		// Create icon and title element
		var sCtLink = "setFaq(";
		sCtLink += strFaq ? "" : "'" + mf_name + "'";
		sCtLink += ");";
		c_title.setAttribute("onclick", sCtLink);
		// Insert FAQ Icon
		var itEl = document.createElement("img");
		var strImage = mf_name.replace (/[ .,']/gi, "_");
			strImage = strImage.toLowerCase ();
		itEl.src = "images/faq/" + strImage;
		// Insert FAQ Title
		var tEl = document.createElement("strong");
		tEl.innerHTML =  mf_name;
		tEl.innerHTML += strFaq ? " - back" : "";
		// Append Elements
		c_title.appendChild(itEl);
		c_title.appendChild(document.createElement("br"));
		c_title.appendChild(tEl);
		boxBod.appendChild(c_title);
		// Add questions
		var q_faq = m_faq[f].getElementsByTagName("ask");
//		if (!strFaq) q_faq = q_faq[0];
		var itLen = strFaq ? q_faq.length : 1;
//		if (strFaq) { itLen = q_faq.length; }
		for (var q = 0; q < itLen; q++) {
			//if (it<lim) {
				var m_question = q_faq[q].getElementsByTagName("question")[0].firstChild.data;
				var m_answer = q_faq[q].getElementsByTagName("answer")[0].firstChild.data;
				if (m_answer.length>lim) {
					var iend = m_answer.length - lim;
					m_answer = m_answer.substring(0, iend);
					m_answer += "&nbsp;[more]";
				}
				if (m_question.length>lim) {
					// code
					var uend = m_question - lim;
					m_question = m_question.substring(0, uend);
					m_question += "&nbsp;[more]";
				}
				// Add question
				var askEl = document.createElement("div");
				askEl.id = "column";
				// Optionally hookup
				if (!strFaq) {
					askEl.style.cursor = "pointer";
					askEl.setAttribute("onclick", sCtLink);
				}
				// Customize class="column"
				askEl.style.width = strFaq ? "800px" : "660px";
				askEl.style.margin = "10px";
				askEl.style.cssFloat = "left";
				var qEl = document.createElement("div");
				var sqEl = document.createElement("strong");
				sqEl.innerHTML = m_question;
				qEl.appendChild(sqEl);
				askEl.appendChild(qEl);
				askEl.innerHTML += m_answer;
				boxBod.appendChild(askEl);
				// limit isht
				//it += m_question.length + m_answer.length;
			//}
		}
		if (strFaq && m_faq[f].getAttribute("name")==strFaq) {break;}
		// spacer
		var bClear = document.createElement("div");
		bClear.className = "clear";
		boxBod.appendChild(bClear);
	}
}

/* Gets information from URL for use in "exposing" products programmatically
	string getUrlParameters ()
	returns a string value formatted for loading a product */
function getUrlParameters () {
	var params = document.URL;
	alert (params);
}

/* Floating buy options */
function SetStatic(){
	if(document.all){buy_box.style.pixelTop=document.documentElement.scrollTop+document.documentElement.clientHeight-160;}
	else if(document.layers){eval(document.buy_box.top=eval(window.pageYOffset+window.innerHeight-160));}
	else if(document.getElementById && !document.all){document.getElementById("buy_box").style.top=window.pageYOffset+window.innerHeight-160+"px";}
}

function position() {
	if(document.all){
		buy_box.style.pixelLeft = document.documentElement.clientWidth-40-226;
		setTimeout('buy_box.style.visibility = "visible"', 50);}
	else if(document.layers){
		document.buy_box.left+=window.innerWidth-40-226;
		setTimeout('document.buy_box.visibility = "visible"', 50);}
	else if(document.getElementById && !document.all){
		document.getElementById("buy_box").style.left=window.innerWidth-40-226+"px";
		setTimeout('document.getElementById("buy_box").style.visibility = "visible"', 50);}
}//end function

/*
setInterval("SetStatic()",1);
position();
*/

// Place shopping cart form
function shopHookUp(fid, name, value, id) {
	// Try to find form
	var r_form = document.getElementById(fid).parentNode;
	if (r_form) r_form.removeChild(r_form);
	// Hide form data
	var n_form = document.createElement("form");
	n_form.id = fid;
	n_form.action = "https://intuitivesecure.com/uCart/uCart.php";
	n_form.method = "POST";
	n_form.target = "_blank";
	var elForm = document.getElementById (strFormId);
	// If submit
	//if (submit) elForm.submit();
	var strFHtml = "<input type=\"hidden\" name=\"Item\" value=\"" + name + "\">";
	strFHtml += "<input type=\"hidden\" name=\"Price\" value=\"" + value + "\">";
	strFHtml += "<input type=\"hidden\" name=\"ShippingWeight\" value=\"\">";
	strFHtml += "<input type=\"hidden\" name=\"SKU\" value=\"" + id + "\">";
	strFHtml += "<input type=\"hidden\" name=\"Qty\" value=\"1\">";
	strFHtml += "<input type=\"hidden\" name=\"merchant\" value=\"quickertek\">";
	strFHtml += "<input type=\"hidden\" name=\"ShopMoreURL\" value=\"http://www.quickertek.com/\">";
	n_form.innerHTML = strFHtml;
	// append/replace
}