//start text area character counter
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}


function checkrequired53(which) {

var pass=true;
fieldlist = "";

  var bgBad = '#F7D16E';
  var bgGood = "white";

if (document.images) {
	for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
		if (tempobj.name=="orderCCNumber" && tempobj.value=='') {
			fieldlist = "Credit Card Number";
			pass=false;
			break;
		}
		if (tempobj.name=="orderCCName" && tempobj.value=='') {
			fieldlist = "Name On Card";
			pass=false;
			break;
		}
		if (tempobj.name=="orderCCExpMon" && tempobj.value=='X') {
			fieldlist = "Expiration Month";
			pass=false;
			break;
		}
		if (tempobj.name=="orderCCExpYear" && tempobj.value=='X') {
			fieldlist = "Expiration Year";
			pass=false;
			break;
		}
		if (tempobj.name=="ccTypeID" && tempobj.value=='X') {
			fieldlist = "Credit Card Type";
			pass=false;
			break;
		}
	}
}

if (!pass) {
	setColor(tempobj, bgBad);
	alert(""+fieldlist+" is required.");
	tempobj.focus();
//	tempobj.blur();
	return false;
}
else
return true;
}


function copyFields(form) {
	form1.shipToName.value = form1.customerFirstName.value + ' ' + form1.customerLastName.value;
	form1.shipToAddress.value = form1.customerAddress.value;
	form1.shipToAddress2.value = form1.customerAddress2.value;
	form1.shipToCity.value = form1.customerCity.value;
	form1.shipToStateID.value = form1.stateID.value;
	form1.shipToZip.value = form1.customerZip.value;
	form1.shipToCountryID.value = form1.countryID.value;
	return true;
}


function removeItem(which) {

	var agree=confirm("Are you sure you want to remove this item from your shopping cart?");
	if (agree)
		return true ;
	else
		return false ;
}


function deleteTestimonial(which) {

	var agree=confirm("Are you sure you want to delete this testimonial?");
	if (agree)
		return true ;
	else
		return false ;
}


function validate(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are accepted!");
field.focus();
field.select();
   }
}

function validateZip(field) {
var valid = "0123456789"
var ok1 = "yes";
var ok2 = "yes";
var temp;
if (field.value.length == 0) {
} else {
	for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok1 = "no";
	}
	if (field.value.length == 5) {
	} else {
	  ok2 = "no";
	}
	if (ok1 == "no") {
	alert("Invalid entry!  Only numbers are accepted!");
	field.focus();
	field.select();
	   }
	if (ok2 == "no") {
	alert("Field must contain five numbers");
	field.focus();
	field.select();
	   }
	}
}

function gtpage(url,frname,height,width) {

        var p1 = 'toolbar=no,'
        var p2 = 'location=no,'
        var p3 = 'directories=no,'
        var p4 = 'status=no,'
        var p5 = 'menubar=no,'
        var p6 = 'resizable=no,'
        var p7 = 'scrollbars=no,'
        var p8=  'copyhistory=no,'
        var p9 = 'width='+width+',';
        var p10 = 'height='+height
        var props = p1+p2+p3+p4+p5+p6+p7+p8+p9+p10;

        themainwindow = window.open(url,frname,props);
        themainwindow.focus();

     }

function gtpage2(url,frname,height,width) {

        var p1 = 'toolbar=no,'
        var p2 = 'location=no,'
        var p3 = 'directories=no,'
        var p4 = 'status=no,'
        var p5 = 'menubar=no,'
        var p6 = 'resizable=yes,'
        var p7 = 'scrollbars=yes,'
        var p8=  'copyhistory=no,'
        var p9 = 'width='+width+',';
        var p10 = 'height='+height
        var props = p1+p2+p3+p4+p5+p6+p7+p8+p9+p10;

        themainwindow = window.open(url,frname,props);
        themainwindow.focus();

     }

function printpage() {
	window.print();
}

function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = ".";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Date is incorrect!\n\nMust use format: mm/dd/yyyy");
      DateField.select();
	  DateField.focus();
   }
}


function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid e-mail address entered")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid e-mail address entered")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Invalid e-mail address entered")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Invalid e-mail address entered")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Invalid e-mail address entered")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Invalid e-mail address entered")
	    return false
	 }
		
	 if (str.indexOf(" ")!=-1){
	    alert("Invalid e-mail address entered")
	    return false
	 }

 	 return true					
}

function deleteDistList(which) {

	var agree=confirm("Are you sure you want to delete this saved list?\n\nThis action cannot be undone!");
	if (agree)
		return true ;
	else
		return false ;
}

function deleteMessage(which) {

	var agree=confirm("Are you sure you want to delete this message?\n\nThis action cannot be undone!");
	if (agree)
		return true ;
	else
		return false ;
}

function deleteContact(which) {

	var agree=confirm("Are you sure you want to delete this contact?\n\nThis action cannot be undone!");
	if (agree)
		return true ;
	else
		return false ;
}

function checkrequiredOptions(which) {

var pass=true;
fieldlist = "";

  var bgBad = '#F7D16E';
  var bgGood = "white";
  
  if (document.form1.cancel.value=="Canceling...") {
	return true;
  }

if (document.images) {
	for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
		if (tempobj.name=="optionName" && tempobj.value=='') {
			fieldlist = "Option name is required";
			pass=false;
			break;
		}
	}
}

if (!pass) {
	setColor(tempobj, bgBad);
	alert(""+fieldlist);
	tempobj.focus();
//	tempobj.blur();
	return false;
}
else
return true;
}

function checkrequiredMessage(which) {

var pass=true;
fieldlist = "";

  var bgBad = '#F7D16E';
  var bgGood = "white";
  
 
if (document.images) {
	for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
		if (tempobj.name=="authID" && tempobj.value=='X') {
			fieldlist = "Select a recipient";
			pass=false;
			break;
		}
		if (tempobj.name=="messageText" && tempobj.value=='') {
			fieldlist = "Message Text is required.";
			pass=false;
			break;
		}
	}
}

if (!pass) {
	setColor(tempobj, bgBad);
	alert(""+fieldlist);
	tempobj.focus();
//	tempobj.blur();
	return false;
}
else
return true;
}

function checkrequiredSaveDistList(which) {

var pass=true;
fieldlist = "";

  var bgBad = '#F7D16E';
  var bgGood = "white";
  
 
if (document.images) {
	for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
		if (tempobj.name=="distributionList" && tempobj.value=='') {
			fieldlist = "Distribution List name";
			pass=false;
			break;
		}
	}
}

if (!pass) {
	setColor(tempobj, bgBad);
	alert(""+fieldlist+" is required.");
	tempobj.focus();
//	tempobj.blur();
	return false;
}
else
return true;
}

function checkrequiredEditContacts(which) {

var pass=true;
fieldlist = "";

  var bgBad = '#F7D16E';
  var bgGood = "white";
  
  if (document.form1.cancel.value=="Canceling...") {
	return true;
  }
  
if (document.images) {
	for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
		if (tempobj.name=="firstName" && tempobj.value=='') {
			fieldlist = "First Name";
			pass=false;
			break;
		}
	}
}

if (!pass) {
	setColor(tempobj, bgBad);
	alert(""+fieldlist+" is required.");
	document.form1.submit.value="Save";
	tempobj.focus();
//	tempobj.blur();
	return false;
}
else
return true;
}

function checkrequiredSearchContacts(which) {

var pass=true;
fieldlist = "";

  var bgBad = '#F7D16E';
  var bgGood = "white";

  var firstName = document.form1.searchfirstname.value;
  var lastName = document.form1.searchlastname.value;
  var address1 = document.form1.searchaddress1.value;
  var city = document.form1.searchcity.value;
  var state = document.form1.searchstate.value;
  var zip = document.form1.searchzip.value;
  var ziplen = document.form1.searchzip.value.length;
  var email = document.form1.searchemail.value;
  var company = document.form1.searchcompany.value;
  var radius = document.form1.radius.value;

	var optionID = "X";
	for (var i=0; i < document.form1.optionID.length; i++)
	{
		if (document.form1.optionID[i].checked)
		{
			optionID = optionID + document.form1.optionID[i].value + "\n";
		}
	}

  if (firstName=='' && lastName=='' && address1=='' && city=='' && state=='' && zip=='' && email=='' && company=='' && optionID=='X') {
	setColor(form1.searchfirstname, bgBad);
	alert("You must enter at least one search field");
	form1.searchfirstname.focus();
	return false;
  }
  if (zip=='' && radius>0) {
	setColor(form1.searchzip, bgBad);
	alert("You must enter a zip code if you select a radius");
	form1.searchzip.focus();
	return false;
  }
  if (ziplen!=5 && radius>0) {
	setColor(form1.searchzip, bgBad);
	alert("You must enter a 5-digit zip code if you select a radius");
	form1.searchzip.focus();
	return false;
  }
  
  return true;
}

