/*  JavaScript for ScootersFactory.com */
/* Runs at the onload() event of the <body> tag.  Anything you put in here will 
   be run when the page loads.
--------------------------------------------------------------------------------*/
function loadall() {
	if(document.getElementById("plisting")) {
		dropdowns();	
	}
}

Array.prototype.contains = 
	function(element) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == element) {  return true; }
		}
		return false;
	};

/* When you click the image inside a label, it triggers the onclick for the 
   <input> next to it by passing in their shared parent node
--------------------------------------------------------------------------------*/
function triggerinput(parent_node) {
	var inputs = parent_node.getElementsByTagName('input');
	inputs[0].click(); // triggers onclick event for the radio button
}

/* restricts keypresses to 0-9, and basics like backspace, delete, and arrow keys
--------------------------------------------------------------------------------*/
function allowonlydigits(the_event) {
	var key = the_event.keyCode; // for IE5/Mac
	var key = key ? key : the_event.which;
	return ( (key <= 57) && !(key >= 33 && key <= 47) ) ? true : false;
}
		
/* open a popup window with default attributes
--------------------------------------------------------------------------------*/
function popup(url, name, attr) {
	if (!name) { name = 'popup'; }
	if (!attr) { attr = "width=415, height=225, location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no, directories=no"; }
	var win = window.open(url, name, attr);
	win.focus();
}

function fixPNG(myImage) { // correctly handle PNG transparency in Win IE 5.5 or higher.
	if (window.ie55up) {
		var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
		var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
		var imgTitle = (myImage.title) ? "title='" + myImage.title + "' " : "title='" + myImage.alt + "' "
		var imgStyle = "display:inline-block;" + myImage.style.cssText 
		var strNewHTML = "<span " + imgID + imgClass + imgTitle
		strNewHTML += " style=\"" + "width:" + myImage.width + "px; height:" + myImage.height + "px;" + imgStyle + ";"
		strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		strNewHTML += "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>" 
		myImage.outerHTML = strNewHTML
	}
}
   
var infoIndex = 0;
function openInfo(url, title, width, height) {

	contentWin = new Window('content_win'+infoIndex, 
								{
									title: title,
									resizable: true, 
									width: width, 
									height: height, 
									hideEffect:Element.hide, 
									showEffect:Element.show,
									url: url
								})

	contentWin.setDestroyOnClose();
	contentWin.showCenter(true);
	infoIndex++; 
}

function removeName(el, name) {

  var i, curList, newList;

  if (el.className == null)
    return;

  // Remove the given class name from the element's className property.

  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}

function dropdowns() {
	if (document.all && document.getElementById) { // check if IE
		navRoot = document.getElementById("plisting");
		lis = navRoot.getElementsByTagName('li');
		for (i = 0; i < lis.length; i++) {
			//lis[i].className += " over";
			lis[i].onmouseover = 
				function() { 
					//alert(this.className);
					var before = this.className.match(' normal') ? ' normal' : 'normal';
					var after = (before == ' normal') ? ' over' : 'over';
					this.className = this.className.replace(before, after);
				}
				
			lis[i].onmouseout = 
				function() { 
					var before = this.className.match(' normal') ? ' normal' : 'normal';
					var after = (before == ' normal') ? ' over' : 'over';
					this.className = this.className.replace(after, before);
					//alert(this.className);
				}
		}
	}
}

function focusField(target) {
	// The form elements that will be tested. Anything with a dot indicates the "type" attribute of the element
	var formElements = ["input.text", "input.checkbox", "input.radio", "select", "textarea"];
	var selectedNode = null;

	// IE's selection method
	if (typeof document.selection != "undefined" && document.selection != null && typeof window.opera == "undefined")
	{
		var theSelection = document.selection;
		var textRange = document.selection.createRange();

		selectedNode = textRange.parentElement();
	}
	// W3 selection method. Currently only Mozilla & Safari support it. However, neither of them support ranges inside form objects, so this part is redundant. Merely included in case they decide to include support in the future
	else if (typeof window.getSelection != "undefined")
	{
		var theSelection = window.getSelection();

		// The Safari way to get the node that a selection starts in
		if (typeof theSelection.baseNode != "undefined")
		{
			selectedNode = theSelection.baseNode;
		}
		// The Mozilla way to get the node that a selection starts in
		else if (typeof theSelection.getRangeAt != "undefined" && theSelection.rangeCount > 0)
		{
			selectedNode = theSelection.getRangeAt(0).startContainer;
		}
	}

	// If a selected node was found above, check whether it's a selection inside one of the specified form element types
	if (selectedNode != null)
	{
		for (var i = 0; i < formElements.length; i++)
		{
			if (selectedNode.nodeName.toLowerCase() == formElements[i].replace(/([^.]*)\..*/, "$1"))
			{
				return false;
			}
		}
	}

	var forms = document.forms;

	// Do a check of each form element on the page. If one of them has a value, do not focus
	for (var i = 0; i < forms.length; i++)
	{
		var formElements = forms[i];

		for (var j = 0; j < formElements.length; j++)
		{
			if (formElements[j].getAttribute("type") == "checkbox" || formElements[j].getAttribute("type") == "radio")
			{
				if (formElements[j].checked != formElements[j].defaultChecked)
				{
					return false;
				}
			}
			else
			{
				if (typeof formElements[j].defaultValue != "undefined" && formElements[j].value != formElements[j].defaultValue)
				{
					return false;
				}
			}
		}
	}

	// If no form elements were found to be focused -- or with values -- go ahead and focus
	target.focus();

	return false;
}

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("content_imp")) //Disable text selection on element with id="mydiv"
function disableSelection(target) {
	if (typeof target.onselectstart!="undefined") { //IE route
		target.onselectstart=function(){return false}
	} else if (typeof target.style.MozUserSelect!="undefined") { //Firefox route
		target.style.MozUserSelect="none"
	} else { //All other route (ie: Opera)
		target.onmousedown=function(){return false}
	}
	target.style.cursor = "default"
}

function txtScreen(thisElm) {
	var elm = $(thisElm);
	var elmWidth = elm.offsetWidth ;
	var elmHeight = elm.offsetHeight;		
	var imageName = 'templates/Original/images/1pix_t.gif';

	var newImage = Builder.node('img', {
			id: thisElm + "_img_real",
			src: imageName,
			height: elmHeight+5,
			width: elmWidth,
			style: 'position: absolute; top: -5px; left: 0px'
		});

	elm.appendChild(newImage); 
}