/** Copyright 2007 Urban Leash and Treat. All rights reserved.
	http://www.urbanleashandtreat.com
*/
/** Function to validate that a string is an email address. Returns true if the email address is valid, false otherwise. */
function echeck(str) {
	var at="@"
	var dot="."
	var errorMessage = "The email address '" + str + "' is not in a valid format. Please update and try again."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		alert(errorMessage)
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert(errorMessage)
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert(errorMessage)
		return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
		alert(errorMessage)
		return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert(errorMessage)
		return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
		alert(errorMessage)
		return false
	}

	if (str.indexOf(" ")!=-1){
		alert(errorMessage)
		return false
	}

	return true			
}

/* Function that validates the contents of the search form. */
function validateSearch() {
	var query=document.searchForm.search_query
	
	if ((query.value==null)||(query.value=="")){
		query.focus()
		return false
	}
	
	return true
}
 
/* Function that validates the contents of the contact form. */
function validateContact() {

	// Validate that a provided email address is valid. Otherwise, the form is valid
	var address=document.contactForm.email_address
	
	if ((address.value!=null) && (address.value!="") && (echeck(address.value)==false)){
		address.value=""
		address.focus()
		return false
	}
	
	return true
}

/* Function that pops up a new window. */
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}

/* Function that pops up a new window at a specified height & width*/
function popupImage(url, width, height) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=' + width + ',height=' + height + ',screenX=150,screenY=150,top=150,left=150')
}

/* Function that pops up the shipping estimator window. */
function estimatorpopupWindow(URL) {
  window.open(URL,'productsshippingestimator','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=450')
}

/***********************************************
* Form Field Progress Bar- By Ron Jonk- http://www.euronet.nl/~jonkr/
* Modified by Dynamic Drive for minor changes
* Script featured/ available at Dynamic Drive- http://www.dynamicdrive.com
* Please keep this notice intact
***********************************************/
function textCounter(field,counter,maxlimit,linecounter) {
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        
	// trim the extra text
	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	} else { 
	// progress bar percentage
	var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	document.getElementById(counter).innerHTML="Limit: "+percentage+"%"
	// color correction on style from CCFFF -> CC0000
	setcolor(document.getElementById(counter),percentage,"background-color");
	}
}

function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

