/**
 * Get computed height of element.
 * @param {Object} elmnt ID of element being checked.
 */
function getHeight(elmnt)
{
	var browserName=navigator.appName;

	if (browserName=="Microsoft Internet Explorer")
	{
		var is_ie=true;
	}
	if(is_ie)
	{
		tmphght = document.getElementById(elmnt).offsetHeight;
	}
	else
	{
		docObj = document.getElementById(elmnt);
		var tmphght1 = document.defaultView.getComputedStyle(docObj, "").getPropertyValue("height");
		tmphght = tmphght1.split('px');
		tmphght = tmphght[0];
	}
	return tmphght;
}

/**
 * Get computed width of element.
 * @param {Object} elmnt ID of element being checked.
 */
function getWidth(elmnt)
{
	var browserName=navigator.appName;

	if (browserName=="Microsoft Internet Explorer")
	{
		var is_ie=true;
	}
	if(is_ie)
	{
		tmphght = document.getElementById(elmnt).offsetWidth;
	}
	else
	{
		docObj = document.getElementById(elmnt);
		var tmpwdth1 = document.defaultView.getComputedStyle(docObj, "").getPropertyValue("width");
		tmpwdth = tmpwdth1.split('px');
		tmpwdth = tmpwdth[0];
	}
	return tmphght;
}

function addProduct()
{
	var numProducts = document.getElementById("numproducts").value *1;
	var newProductNo = numProducts + 1;

	var newProduct = "<div id=\"product"+newProductNo+"\" style=\"border-bottom:1px dotted #2A69A8;padding-top:5px;\">\n";
	newProduct += "<div style=\"position:relative;float:left;text-align:center;width:200px;\"><input type=\"text\" id=\"product"+newProductNo+"name\" name=\"product"+newProductNo+"name\" maxlength=\"30\" style=\"width:180px;\" \/><\/div>\n";
	newProduct += "<div style=\"position:relative;float:left;text-align:center;width:200px;\"><input type=\"text\" id=\"product"+newProductNo+"quantity\" name=\"product"+newProductNo+"quantity\" maxlength=\"5\" style=\"width:180px;\" \/><\/div>\n";
	newProduct += "<div style=\"position:relative;float:left;text-align:center;width:175px;\"><input type=\"text\" id=\"product"+newProductNo+"serial\" name=\"product"+newProductNo+"serial\" maxlength=\"30\" style=\"width:155px;\" \/><\/div>\n";
	newProduct += "<div style=\"position:relative;float:left;text-align:center;width:100px;\"><input type=\"text\" id=\"product"+newProductNo+"demo\" name=\"product"+newProductNo+"demo\" maxlength=\"10\" style=\"width:80px;\" \/><\/div>\n\n";

	newProduct += "<div style=\"clear:both;padding:0 0 5px 0;text-align:center;\">\n";
	newProduct += "Is product a duplicator? <input type=\"checkbox\" id=\"product"+newProductNo+"duplicator\" name=\"product"+newProductNo+"duplicator\" value=\"yes\" \/>\n";
	newProduct += "&#151;\n";
	newProduct += "Is product being leased? <input type=\"checkbox\" id=\"product"+newProductNo+"lease\" name=\"product"+newProductNo+"lease\" value=\"yes\" \/>\n";
	newProduct += "<\/div>\n";
	newProduct += "<div class=\"clearer\"><\/div>\n";
	newProduct += "<\/div>\n";

	document.getElementById("products").innerHTML += newProduct;
	document.getElementById("numProducts").value = newProductNo;
}

/**
 * Checks each input field for leading/trailing spaces.
 */
function spaceCheck()
{
	var i;
	var elmntID = "";
	var l;

	l = document.getElementsByTagName("input").length;

	for(i=0;i<l;i++)
	{
		type = document.getElementsByTagName("input")[i].type;
		if(type=="text"||type=="hidden"||type=="password")
		{
			document.getElementsByTagName("input")[i].onblur = function(){stripSpaces(this);};
		}
	}
}

/**
 * Strips excessive spaces from an input field.
 * @param {Object} elmnt ID of element being filtered.
 */
function stripSpaces(elmnt)
{
	var txt = elmnt.value;

	while(txt.charAt(0)==(" "))
	{
		txt = txt.substring(1);
	}
	while(txt.charAt(txt.length-1)==" ")
	{
		txt = txt.substring(0,txt.length-1);
	}

//	elmnt.value = (txt.replace(/^\W+/,'')).replace(/\W+$/,'');
	elmnt.value = txt;
}

/**
 * Creates standards-compliant external links by using the rel="external" attribute.
 */
function externalLinks()
{ 
	if(!document.getElementsByTagName)
	{
		return;
	} 
	var anchors = document.getElementsByTagName("a"); 
	for(var i=0;i<anchors.length;i++)
	{
		var anchor = anchors[i];
		if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
		{
			anchor.target = "_blank";
		}
	}
}

window.onload=function() {
	externalLinks();
	spaceCheck();
};