// ************************************************************************
// Function: search_app_user
// Input: stype - search type which equates to application or user
// Result: forwards user to the specified page found in the selection
// Found: side menu of navigation (search function)
// ************************************************************************

function search_app_user(stype)
{
	if (document.getElementById(stype).options[document.getElementById(stype).selectedIndex].value != -1)
        location.href = document.getElementById(stype).options[document.getElementById(stype).selectedIndex].value;
}

// ************************************************************************
// Function: remove_quote
// Input: model - model number to remove from quote
// Result: removes model from quote
// Found: quote cart
// ************************************************************************

function remove_quote(model)
{
	location.href = "index.php?req=quote&delete="+model;
}

// ************************************************************************
// Function: update_quote
// Input: model - model number to remove from quote
// 		  box - box to get updated quantity from
// Result: increases model count for quote
// Found: quote cart
// ************************************************************************

function update_quote(model, boxvar)
{
	var box = 'totalq'+boxvar;
	var quant = document.getElementById(box).value;
	location.href = "index.php?req=quote&update="+model+"&quantity="+quant;
}

// ************************************************************************
// Function: submit_quote
// Input: qid - quote id
// Result: takes user to collect user information
// Found: quote cart
// ************************************************************************

function submit_quote(qid)
{
	if(document.getElementById('vac1').checked == true || document.getElementById('vac2').checked == true || document.getElementById('vac3').checked == true)
	{
		if((document.getElementById('vac2').checked == true || document.getElementById('vac3').checked == true) && document.qbox.plugpattern.value != '')
		{
			var radioObj = document.qbox.plugpattern;
			var radioLength = radioObj.length;
			var plug = '';
			for(var i = 0; i < radioLength; i++) 
			{
				if(radioObj[i].checked == true)
					plug = radioObj[i].value;
			}
			if(plug == '')
			{
				alert("You indicated that you need an International Cordset but you did not select one.  Please select a cordset.");
				return false;
			}
			else
			{
				if(document.getElementById('vac2').checked == true)
					location.href = "index.php?req=quote&processq="+qid+"&vac=2&plug="+plug;
				else if(document.getElementById('vac3').checked == true)
					location.href = "index.php?req=quote&processq="+qid+"&vac=3&plug="+plug;
			}
		}
		else if(document.getElementById('vac1').checked == true)
			location.href = "index.php?req=quote&processq="+qid+"&vac=1&plug=K";
		else
			location.href = "index.php?req=quote&processq="+qid+"&vac=0";
	}
	else
		location.href = "index.php?req=quote&processq="+qid;

}

// ************************************************************************
// Function: clear_example
// Input: boxid - object holding example text
// Result: clears example text from form object
// Found: various forms
// ************************************************************************

function clear_example(boxid)
{
	var strval = document.getElementById(boxid).value;
	if(strval.substr(0,5) == '(e.g.')
	{
		document.getElementById(boxid).value = '';
		document.getElementById(boxid).style.color = "black";
	}
}

// ************************************************************************
// Function: link_jump
// Input: none
// Result: jumps to internal link
// Found: product pages
// ************************************************************************

function link_jump()
{
	var url = location.href;
	if(strpos(url,'#') > 0)
	{
		var spliturl = url.split("#");
		location.href = "#"+spliturl[1];	
	}	
}

function strpos( haystack, needle, offset){
 
    var i = haystack.indexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}


// ************************************************************************
// Function: show_opts
// Input: none
// Result: shows custom options for quoting
// Found: ordering/quote page
// ************************************************************************

function show_opts()
{
	if(document.getElementById('customs').style.display == 'none')
	{
		document.getElementById('customs').style.display = '';
		document.getElementById('show_custom').value = 'Remove Customizations';
	}
	else
	{
		document.getElementById('plugs1').style.display = 'none';
		document.getElementById('plugs2').style.display = 'none';
		document.getElementById('customs').style.display = 'none';
		document.getElementById('vac1').checked = false;
		document.getElementById('vac2').checked = false;
		document.getElementById('vac3').checked = false;
		document.getElementById('show_custom').value = 'Require product to operate on 220-240 VAC or have an International Cordset? Click Here!';
	}
}


// ************************************************************************
// Function: show_plugs
// Input: none
// Result: shows plug selection
// Found: ordering/quote page
// ************************************************************************

function show_plugs(_id, _show)
{
	var plugselect = 'vac'+_id;
	var plugshow = 'plugs'+_show; 
	if(document.getElementById('vac1').checked == false && document.getElementById('vac2').checked == false && document.getElementById('vac3').checked == false)
	{
		document.getElementById('plugs1').style.display = 'none';
		document.getElementById('plugs2').style.display = 'none';
		document.getElementById('vac1').checked = false;
		document.getElementById('vac2').checked = false;
		document.getElementById('vac3').checked = false;
	}
	else
	{
		document.getElementById('plugs1').style.display = 'none';
		document.getElementById('plugs2').style.display = 'none';
		document.getElementById('vac1').checked = false;
		document.getElementById('vac2').checked = false;
		document.getElementById('vac3').checked = false;
		document.getElementById(plugselect).checked = true;	
		document.getElementById(plugshow).style.display = '';
	}
}


function pick_plug(_plug) 
{
	var plug = 'plug'+_plug;
	var radioObj = document.qbox.plugpattern;
	var radioLength = radioObj.length;
	for(var i = 0; i < radioLength; i++) 
	{
		radioObj[i].checked = false;
		if(radioObj[i].value == _plug) 
			radioObj[i].checked = true;
	}
}


// ************************************************************************
// Function: show_prod_info
// Input: box
// Result: shows product information
// Found: product page
// ************************************************************************

function show_prod_info(_id)
{
	document.getElementById('related').style.display = 'none';
	document.getElementById('replace').style.display = 'none';
	document.getElementById('videos').style.display = 'none';
	document.getElementById('fb').style.display = 'none';
	document.getElementById('apps').style.display = 'none';
	document.getElementById('desc').style.display = 'none';
	document.getElementById('tech').style.display = 'none';
	document.getElementById('prodsheet').style.display = 'none';
	document.getElementById(_id).style.display = '';
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};