/* ------------------------------------------------------------------------------------
 Show/Hide Tabs
------------------------------------------------------------------------------------ */

function initShowHide() {
	// Hide the container with all toggleable elements 
	document.getElementById('toggleable').style.display = 'none';
	var as = document.getElementById('toggle').getElementsByTagName('a');
	for (var i = 0; i < as.length; i++) {
		as[i].onclick = function() {
			toggle(this);
			return false;
		}	
	}
	toggle(as[0]);
} // end initShowHide()

function toggle(s) {
	// Try to get a reference to an element with the id 'current' (a previously active tab header)
 	var oldCurrent = document.getElementById('current-tab');
	// If this element exists, remove its ID attribute
	if (oldCurrent) oldCurrent.removeAttribute('id');
	// Add the ID attribute with value 'current' to the newly active tab header (LI element)
	s.parentNode.setAttribute('id', 'current-tab');
	// Retrieve the id of the toggleable section
	var id = s.href.match(/#(\w.+)/)[1];
	// Clone the activated toggleable element from the content pool
	var clone = document.getElementById(id).cloneNode(true);
	// This clone will be wrapped in a generated container element called 'cloneDiv' 
	// Try to get a reference to an existing container element
	var cloneDiv = document.getElementById('tab-content');
	// In case the container element already exists
	if (cloneDiv) {
		// Replace the previously cloned toggleable element with the new one
		cloneDiv.replaceChild(clone, cloneDiv.firstChild);	
	}
	// In case the container element isn't created yet
	else {
		// Create the container element
		var cloneDiv = document.createElement('div');
		// Add the id attribute and set its value
		cloneDiv.setAttribute('id', 'tab-content');
		// Put the cloned element in the cloned container
		cloneDiv.appendChild(clone);
		// Append the cloned container to the body of the document
		document.body.appendChild(cloneDiv);
	}
} // end toggle()

/* ------------------------------------------------------------------------------------
Email to Friend Popup and Print Page functions
------------------------------------------------------------------------------------ */

function emailFriend(url,name) {
	windowWidth = 500;
	windowHeight = 430;
	myleft = (screen.width)?(screen.width-windowWidth)/2:100;
	mytop = (screen.height)?(screen.height-windowHeight)/2:100;
	properties = "width="+windowWidth+",height="+windowHeight+",scrollbars=no,resizable=yes,status=no,top="+mytop+",left="+myleft;
	newwindow = window.open(url,name,properties)
	if (window.focus) { newwindow.focus(); }
	return false;
} // end emailFriend()

function printPage() {
    document.printpage.submit();
} // end printPage()

/* ------------------------------------------------------------------------------------
Show / Hide for Quote Form
------------------------------------------------------------------------------------ */

function change(id)
{
	ID = document.getElementById(id);
	
	if(ID.style.display == "")
	{
		ID.style.display = "none";
	}
	else
	{
		ID.style.display = "";
	}
} // end show/hide


/* ------------------------------------------------------------------------------------
Show / Hide for Quote Form
------------------------------------------------------------------------------------ */

var http_request = false;
function makeRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
}

function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			result = http_request.responseText;
			document.getElementById('quote').innerHTML = result;            
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function get(obj) {
	var getstr = "?";
	for (i=0; i<obj.childNodes.length; i++) {
		if (obj.childNodes[i].tagName == "INPUT") {
			if (obj.childNodes[i].type == "text") {
				getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&amp;";
			}
/*
			if (obj.childNodes[i].type == "checkbox") {
				if (obj.childNodes[i].checked) {
					getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
				} else {
					getstr += obj.childNodes[i].name + "=&";
				}
			}
			if (obj.childNodes[i].type == "radio") {
				if (obj.childNodes[i].checked) {
					getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
				}
			}
*/
		}
/*
		if (obj.childNodes[i].tagName == "SELECT") {
			var sel = obj.childNodes[i];
			getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
		}
*/
	}
	makeRequest('/assets/includes/request-a-quote.cfm', getstr);
}