var checkField = '';

function checklength(comments){
	if(comments.value.length > 1000){
		alert("Comments section is too long.  Must be under 1,000 characters");
	}
}

function af(theValue,theField){
	if((checkField == '' || checkField == 'state') && theField == 'state' && theValue.value != "ALL"){
		location.href='index.cgi?action=3&state='+theValue.value;
	}
	if((checkField == '' || checkField == 'breed') && theField == 'breed' && theValue.value != "ALL"){
		location.href='index.cgi?action=2&breed='+theValue.value;
	}	
}

function isValidMoneyChars(s){
	var i = 0;
	var c = '';
	var numOfDecimals = 0;
	var validMoneyChars = '0123456789.';
	for (i = 0; i < s.length; i++){ 
		c = s.charAt(i);
		if (validMoneyChars.indexOf(c) == -1){
			return false;
		}else{
			if (c == '.'){
				numOfDecimals++;
			}
		}
	}
	if (numOfDecimals <= 1){
		return true;
	}
	return false;
}
function stripExtraneousChars(s, keepDecimal){
	var i = 0;
	var returnString = '';
	var c = '';
	var charsToKeep = '0123456789';
	if (keepDecimal){
		charsToKeep = charsToKeep + '.';
	}
	while (s.charAt(i) == '0' || charsToKeep.indexOf(i) == -1){
		i++;
	}
	for (i; i < s.length; i++){ 
		c = s.charAt(i);
		if (charsToKeep.indexOf(c) >= 0){
			returnString += c;
		}
	}
	return returnString;
}

function stripExtraneousChars2(theField){
	if (navigator.appName.toLowerCase().indexOf('microsoft') > -1 && navigator.userAgent.toLowerCase().indexOf('opera') < 0){
		if ((event != null && (event.keyCode > 31 || event.keyCode == 8)) || event == null){
			var str = stripExtraneousChars(theField.value, false);
			theField.value = str;
		}
	}
}

function removeBad(theField){
        if (navigator.appName.toLowerCase().indexOf('microsoft') > -1 && navigator.userAgent.toLowerCase().indexOf('opera') < 0){
                if ((event != null && (event.keyCode > 31 || event.keyCode == 8)) || event == null){
                        var str = stripExtraneousChars(theField.value, false);
                        theField.value = str;
                }
        }
}

function addCommas(theField, useDecimal){
	if (navigator.appName.toLowerCase().indexOf('microsoft') > -1 && navigator.userAgent.toLowerCase().indexOf('opera') < 0){
		if ((event != null && (event.keyCode > 31 || event.keyCode == 8)) || event == null){
			var str = stripExtraneousChars(theField.value, false);
			var i = 0;
			if (str.length > 2){
				if (useDecimal){
					i = str.length - 2;
					str = str.substring(0, i) + '.' + str.substring(i);
				}
				else{
					i = str.length;
				}
				i = i - 3;
				while (i > 0){
					str = str.substring(0, i) + ',' + str.substring(i);
					i = i - 3;
				}
			}
			theField.value = str;
		}
	}
}

function premDescripSubmit(theForm){
	var errstr = "";
	if(Trim(theForm.comp_info.value) == ""){
		errstr += "Company Details is a required field.\n";
	}else{
		if(Trim(theForm.comp_info.value).length > 1000){
			errstr += "Company Details cannot be longer than 1,000 characters.\n";
		}
	}
	if(Trim(theForm.litter_descrip.value).length > 255){
		errstr += "Litter Description cannot be longer than 250 characters.\n";
	}
	
	if(errstr != ""){
		alert(errstr);
		return false;
	}	
}

function findBreedersSubmit(theForm){
	var errstr = "";
	if(Trim(theForm.state.value) == ""){
		errstr += "Please select a state.\n";
	}
	if(Trim(theForm.breed.value) == ""){
		errstr += "Please select a breed.\n";
	}
	if(Trim(theForm.state.value) == "ALL" && Trim(theForm.breed.value) == "ALL"){	
		errstr += "Cannot select 'All' for both state and breeds.  You must narrow down your selection.\n";	
	}
	if(errstr != ""){
		alert(errstr);
		return false;
	}	
}

function passwordSubmit(theForm){
	var errstr = "";
	mailRE = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z0-9\-]+\.)+([a-zA-Z]{2,4})))$/;
	if(!mailRE.exec(Trim(theForm.email.value))){
		errstr += "Your email address must be entered in the correct format.\n";
	}	
	if(errstr != ""){
		alert(errstr);
		return false;
	}
}

function contactSubmit(theForm){
	var errstr = "";
	if(Trim(theForm.name.value) == ""){
		errstr += "You need to enter your name.\n";
	}
	mailRE = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z0-9\-]+\.)+([a-zA-Z]{2,4})))$/;
	if(!mailRE.exec(Trim(theForm.email.value))){
		errstr += "Your email address must be entered in the correct format.\n";
	}	
	if(Trim(theForm.subject.value) == ""){
		errstr += "You need to select your subject.\n";
	}
	if(Trim(theForm.comments.value) == ""){
		errstr += "You need to select your comments.\n";
	}else{
		if(theForm.comments.value.length > 1000){
			errstr += "Comments section is too long.  Must be under 1,000 characters";
		}
	}
	
	if(errstr != ""){
		alert(errstr);
		return false;
	}
}

function contactBreederSubmit(theForm){
	var errstr = "";
	if(Trim(theForm.user_name.value) == ""){
		errstr += "You need to enter your name.\n";
	}
	mailRE = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z0-9\-]+\.)+([a-zA-Z]{2,4})))$/;
	if(!mailRE.exec(Trim(theForm.user_email.value))){
		errstr += "Your email address must be entered in the correct format.\n";
	}
	if(!mailRE.exec(Trim(theForm.confirm_email.value))){
		errstr += "Your confirm email address must be entered in the correct format.\n";
	}
	if(Trim(theForm.confirm_email.value) != Trim(theForm.user_email.value)){
		errstr += "We cannot confirm your email address.  Please make sure they are correct and the same.\n";	
	}
	if(Trim(theForm.user_message.value) == ""){
		errstr += "You need to select your comments.\n";
	}else{
		if(theForm.user_message.value.length > 1000){
			errstr += "Comments section is too long.  Must be under 1,000 characters";
		}
	}
	
	if(errstr != ""){
		alert(errstr);
		return false;
	}
}

function newBreederSubmit(theForm){
	var errstr = "";
	if(Trim(theForm.first_name.value) == ""){
		errstr += "You need to enter your first name.\n";
	}
	if(Trim(theForm.last_name.value) == ""){
		errstr += "You need to enter your last name.\n";
	}
	phoneRE3 = /^[0-9]{3}$/;
	phoneRE4 = /^[0-9]{4}$/;
	
	if(Trim(theForm.phone_numberAC.value) != "" || Trim(theForm.phone_numberPRE.value) != "" || Trim(theForm.phone_numberSUF.value) != ""){
	 	if(!phoneRE3.exec(theForm.phone_numberAC.value)){
			errstr += "Your phone number area code must be 3 digits only.\n";
	 	}
	 	if(!phoneRE3.exec(theForm.phone_numberPRE.value)){
			errstr += "Your phone number prefix must be 3 digits only.\n";
	 	}
	 	if(!phoneRE4.exec(theForm.phone_numberSUF.value)){
			errstr += "Your phone number suffix must be 3 digits only.\n";
	 	}
	}
	if(Trim(theForm.fax_numberAC.value) != "" || Trim(theForm.fax_numberPRE.value) != "" || Trim(theForm.fax_numberSUF.value) != ""){
	 	if(!phoneRE3.exec(theForm.fax_numberAC.value)){
			errstr += "Your phone number area code must be 3 digits only.\n";
	 	}
	 	if(!phoneRE3.exec(theForm.fax_numberPRE.value)){
			errstr += "Your phone number prefix must be 3 digits only.\n";
	 	}
	 	if(!phoneRE4.exec(theForm.fax_numberSUF.value)){
			errstr += "Your phone number suffix must be 3 digits only.\n";
	 	}
	}	
	if(Trim(theForm.state.value) == ""){
		errstr += "You need to select your state name.\n";
	}
	if(Trim(theForm.breed.value) == ""){
		errstr += "You need to select your breed.\n";
	}
	if(Trim(theForm.username.value) == ""){
		errstr += "You need to enter your username.\n";
	}
	if(Trim(theForm.password.value) == ""){
		errstr += "You need to enter your password.\n";
	}
	
	zipRE = /^[0-9]{5}$/;
	canpostRE = /^[A-Za-z][0-9][A-Za-z][ ]?[0-9][A-Za-z][0-9]$/;
	if(Trim(theForm.zip.value) != ""){
		if(Trim(theForm.state.value) == "AB" || Trim(theForm.state.value) == "BC" || Trim(theForm.state.value) == "MB"
		|| Trim(theForm.state.value) == "NB" || Trim(theForm.state.value) == "NL"
		|| Trim(theForm.state.value) == "NS" || Trim(theForm.state.value) == "ON"
		|| Trim(theForm.state.value) == "PE" || Trim(theForm.state.value) == "QC"
		|| Trim(theForm.state.value) == "SK"){
			if(!canpostRE.exec(theForm.zip.value)){
				errstr += "Please enter a valid post code.\n";
			}			
		}else{
			if(!zipRE.exec(theForm.zip.value)){
				errstr += "Please enter a valid zip (5 digits only).\n";
			}
		}
	}
	
	if(Trim(theForm.email.value) != ""){
		mailRE = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z0-9\-]+\.)+([a-zA-Z]{2,4})))$/;
		if(!mailRE.exec(Trim(theForm.email.value))){
			errstr += "Your email address must be entered in the correct format.\n";
		}
	}
	
	

	
	if(errstr != ""){
		alert(errstr);
		return false;
	}
}

function updateBreederSubmit(theForm){
	var errstr = "";
	
	if(Trim(theForm.first_name.value) == ""){
		errstr += "You need to enter your first name.\n";
	}
	if(Trim(theForm.last_name.value) == ""){
		errstr += "You need to enter your last name.\n";
	}
	phoneRE3 = /^[0-9]{3}$/;
	phoneRE4 = /^[0-9]{4}$/;
	
	if(Trim(theForm.phone_numberAC.value) != "" || Trim(theForm.phone_numberPRE.value) != "" || Trim(theForm.phone_numberSUF.value) != ""){
	 	if(!phoneRE3.exec(theForm.phone_numberAC.value)){
			errstr += "Your phone number area code must be 3 digits only.\n";
	 	}
	 	if(!phoneRE3.exec(theForm.phone_numberPRE.value)){
			errstr += "Your phone number prefix must be 3 digits only.\n";
	 	}
	 	if(!phoneRE4.exec(theForm.phone_numberSUF.value)){
			errstr += "Your phone number suffix must be 3 digits only.\n";
	 	}
	}
	if(Trim(theForm.fax_numberAC.value) != "" || Trim(theForm.fax_numberPRE.value) != "" || Trim(theForm.fax_numberSUF.value) != ""){
	 	if(!phoneRE3.exec(theForm.fax_numberAC.value)){
			errstr += "Your phone number area code must be 3 digits only.\n";
	 	}
	 	if(!phoneRE3.exec(theForm.fax_numberPRE.value)){
			errstr += "Your phone number prefix must be 3 digits only.\n";
	 	}
	 	if(!phoneRE4.exec(theForm.fax_numberSUF.value)){
			errstr += "Your phone number suffix must be 3 digits only.\n";
	 	}
	}	
	if(Trim(theForm.state.value) == ""){
		errstr += "You need to select your state name.\n";
	}
	if(Trim(theForm.breed.value) == ""){
		errstr += "You need to select your breed.\n";
	}
	canpostRE = /^[A-Za-z][0-9][A-Za-z][ ][0-9][A-Za-z][0-9]$/;
	zipRE = /^[0-9]{5}$/;
	if(Trim(theForm.zip.value) != ""){
		if(Trim(theForm.state.value) == "BC" || Trim(theForm.state.value) == "MB"
		|| Trim(theForm.state.value) == "NB" || Trim(theForm.state.value) == "NL"
		|| Trim(theForm.state.value) == "NS" || Trim(theForm.state.value) == "ON"
		|| Trim(theForm.state.value) == "PE" || Trim(theForm.state.value) == "QC"
		|| Trim(theForm.state.value) == "SK"){
			if(!canpostRE.exec(theForm.zip.value)){
				alert(theForm.zip.value);
				errstr += "Please enter a valid post code.\n";
			}			
		}else{
			if(!zipRE.exec(theForm.zip.value)){
				errstr += "Please enter a valid zip (5 digits only).\n";
			}
		}
	}
	
	if(Trim(theForm.email.value) != ""){
		mailRE = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z0-9\-]+\.)+([a-zA-Z]{2,4})))$/;
		if(!mailRE.exec(Trim(theForm.email.value))){
			errstr += "Your email address must be entered in the correct format.\n";
		}
	}
	
	

	
	if(errstr != ""){
		alert(errstr);
		return false;
	}
}


function loginSubmit(theForm){
	var errstr = "";

	if(Trim(theForm.username.value) == ""){
		errstr += "You need to enter your username.\n";
	}
	if(Trim(theForm.password.value) == ""){
		errstr += "You need to enter your password.\n";
	}
	if(errstr != ""){
		alert(errstr);
		return false;
	}
}


function Trim(strValue){
	return LTrim(RTrim(strValue));
}
function LTrim(strValue){
	var LTRIMrgExp = /^\s */;
	return strValue.replace(LTRIMrgExp, "");
}
function RTrim(strValue){
	var RTRIMrgExp = /\s *$/;
	return strValue.replace(RTRIMrgExp, "");
}