function redirectTo(url) {
	if (url.indexOf("http://") < 0 ) url = "http://" + url;
	if (url.indexOf("http://") > 0 ) url = url.substring(url.indexOf("http://"));	
	var newWindow = window.open(url, '_blank');
	newWindow.focus();
}

function reSort(order, dir) {
	var currUrl = location.href;

	if ( currUrl.indexOf("#") == (currUrl.length - 1)  ) currUrl = currUrl.substring(0, currUrl.length - 1);

	currUrl = replaceInUrl(currUrl, "so", order);
	currUrl = replaceInUrl(currUrl, "sd", dir);

	location.href = currUrl;
}				

function setBg(id, color) {
	var item = document.getElementById(id);
	if ( item ) {
		item.style.backgroundColor = color;
	}
}

function accent(id) {
	var diff = 7;
	var item = document.getElementById(id);
	var color = item.style.backgroundColor;
	var temp = color.substring(color.indexOf("(") + 1);
	temp = temp.substring(0, temp.indexOf(")"));
	var segment = temp.split(",");
	var rh = parseInt(segment[0]);
	var gh = parseInt(segment[1]);
	var bh = parseInt(segment[2]);
	var r = rh + diff > 255 ? 255 : rh + diff;
	var g = gh + diff > 255 ? 255 : gh + diff;
	var b = bh + diff > 255 ? 255 : bh + diff;
	item.style.backgroundColor = "rgb(" + r + "," + g + "," + b + ")";
}

function unaccent(id) {
	var diff = 7;
	var item = document.getElementById(id);
	var color = item.style.backgroundColor;
	var temp = color.substring(color.indexOf("(") + 1);
	temp = temp.substring(0, temp.indexOf(")"));
	var segment = temp.split(",");
	var rh = parseInt(segment[0]);
	var gh = parseInt(segment[1]);
	var bh = parseInt(segment[2]);
	var r = rh - diff < 0 ? 0 : rh - diff;
	var g = gh - diff < 0 ? 0 : gh - diff;
	var b = bh - diff < 0 ? 0 : bh - diff;
	item.style.backgroundColor = "rgb(" + r + "," + g + "," + b + ")";
}

function doPointer(item) {
	item.style.cursor='pointer';
}

function replaceInUrl(theUrl, param, newVal) {	
	
	var ret = theUrl;
	
	if ( theUrl.indexOf("?") > -1 ) {
		// has query string
		var par = "?" + param + "=";
		var idx = theUrl.indexOf(par);
		var temp = ""
		
		if ( idx < 0 ) {
			par = "&" + param + "=";
			idx = theUrl.indexOf("&" + param + "=");
			if ( idx < 0 ) {
				// it's not in query string, so add
				ret = theUrl + "&" + param + "=" + newVal;
			} else {
				temp = theUrl.substring(0, idx + par.length);
				theUrl = theUrl.substring(idx + par.length);
				ret = temp + (theUrl.indexOf("&") > -1 ? newVal + theUrl.substring(theUrl.indexOf("&")) : newVal);
			}
		} else {
			temp = theUrl.substring(0, idx + par.length);
			theUrl = theUrl.substring(idx + par.length);
			ret = temp + (theUrl.indexOf("&") > -1 ? newVal + theUrl.substring(theUrl.indexOf("&")) : newVal);
		}
	} else {
		// no query string
		ret = theUrl + "?" + param + "=" + newVal;
	}
	
	return ret;
}

function safemailto(addr,site,tld) {
  document.write('<a href=\"mai'+'lto:'
    +addr+ '&#64;' + site + '&#46;' + tld + '\">'
    +addr+ '&#64;' + site + '&#46;' + tld + '</a>');
}

function toggleVis(it) {
	var item = document.getElementById(it);
	if ( item ) {
		if ( item.style.display == 'none' ) item.style.display = '';
		else item.style.display = 'none';
	}
}

function hideVis(it) {
	var item = document.getElementById(it);
	if ( item ) {
		item.style.display = 'none';
	}
}

function showVis(it) {
	var item = document.getElementById(it);
	if ( item ) {
		item.style.display = '';
	}
}

function checkZip(zip) {
	
  var goodZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
  var currZip = document.getElementById(zip).value;
  
  if ( currZip.length > 0 ) {
	  if (!goodZip.test(currZip)) {
	    return false;
	  }
	}
	 
	return true;
}

function checkCurrency(f) {

	var nNum = 0;			// Total numbers for currency value.
	var nDollarSign = 0;	// Total times a dollar sign occurs.
	var nDecimal = 0;		// Total times a decimal point occurs.
	var nCommas = 0;		// Total times a comma occurs.
	var txtLen;				// Length of string passed.
	var xTxt;				// Assigned object passed.
	var sDollarVal;			// Assigned dollar amount with or without commas.
	var bComma;				
	var decPos;				// Assigned value of numbers or positions after decimal point.
	var nNumCount = 0;		// Total number between commas.
	var i;					// For forloop indexing.
	var x;					// Assigned each indivual character in string.

	xTxt = f;
	txtLen = xTxt.value.length

	for(i = 0; i < txtLen; i++) {

		// Assign charater in substring to x.
		x = xTxt.value.substr(i, 1);

		if(x == "$")
			nDollarSign = nDollarSign + 1; // Sum total times dollar sign occurs.
		else if(x == ".")
			nDecimal = nDecimal + 1; // Sum total times decimal point occurs.
		else if(x == ",")
			nCommas = nCommas + 1; // Sum total times comma occurs.
		else if(parseInt(x) >= 0 || parseInt(x) <= 9)
			nNum = nNum + 1; // If the character is a number sum total times a number occurs.
		else {
			return false;
		} // end else
	} // end for

	if(nDollarSign > 1)	{
		return false;
	} // end if

	if(nDecimal > 1) {
		return false;
	} // end if

	if(nDollarSign == 1)	{
		if(xTxt.value.indexOf("$") != 0) {
			return false;
		} // end if
	}// end if

	if(nDecimal == 1)	{
		decPos = (txtLen - 1) - xTxt.value.indexOf(".");
		if(decPos > 2) {
			return false;
		} // end if
	} // end if

	if(nCommas == 0) {
		return true;
	}	else {
		nNum = nNum - decPos;
		if(xTxt.value.indexOf("$", 0) == 0)
			sDollarVal = xTxt.value.substr(1, (nNum + nCommas));
		else
			sDollarVal = xTxt.value.substr(0, (nNum + nCommas));

		if(sDollarVal.lastIndexOf("0", 0) == 0 ) {
			return false;
		} else if(sDollarVal.lastIndexOf(",", 0) == 0) {
			return false;
		} else if(sDollarVal.indexOf(",", (sDollarVal.length - 1)) == (sDollarVal.length - 1)) {
			return false;
		} else {
			bComma = false;

			for(i = 0; i < sDollarVal.length; i++) {
				x = sDollarVal.substr(i, 1);

				if(parseInt(x) >= 0 || parseInt(x) <= 9) {
					nNumCount = nNumCount + 1;
					if(nNumCount > 3) {
						return false;
					} // end if
				} else {
					if(nNumCount != 3 && bComma) {
						return false;
					} // end if

					nNumCount = 0;
					bComma = true;
				} // end if
			} // end for

			if(nNumCount != 3 && bComma) {
				return false;
			} // end if
		} // end if
	} // end if

	return true;

}

function checkEmail(email)
{
	var currEmail = document.getElementById(email).value;
	var goodEmail  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (goodEmail.test(currEmail)) return true;
	else return false;
}

function prepInput(inp) {
	var iBox = document.getElementById(inp);
	if ( iBox ) {
		iBox.value = "";
		iBox.style.color = "#000000";
	}	
}
