
//this is what we are dividing entries with for the how to get lists
var entryDivider = "*";

//this is what we are dividing entry components with in the how to get lists
var partDivider  = "~"

//add a selecter retailer to the list
function addToList(toList, province, city, retailer) {

	//set us the variable for the text and the value
	var newValue = "";
	var newText = "";
	
	//make sure the entered something
	if (city.value != "" &&  retailer.value != "") {
		
		//get the selected province/state
		for(i = 0; i < province.options.length; i++) {
			if (province.options[i].selected) {
				newText = province.options[i].text + " - "
				newValue = "new" + partDivider + province.options[i].value + partDivider
			}
		}	
		
		//set the new text names
		newText += city.value + ": " + retailer.value + ";"
		newValue += city.value + partDivider + retailer.value
		
		//remove the retailer text
		retailer.value = "";
		
		//add a new entry in the list
		toList.length++; 
		
		//move the space to the end 
		toList.options[toList.length-1].value = toList.options[toList.length-2].value;
		toList.options[toList.length-1].text = toList.options[toList.length-2].text;		
		
		//add the new item
		toList.options[toList.length-2].value = newValue;
		toList.options[toList.length-2].text = newText;
		
		//resize away
		self.resizeBy( 1, 1);
		self.resizeBy( -1, -1);
	}
}

//delete the item from the list
function deleteItem(toList, deletedList) {

	var selectedFlag = "no"

	//loop through the list and found the selected item
	for(i = 0; i < toList.options.length; i++) {
		
		//if we have found a selected item to delete, set out delete flag to true
		if ((toList.options[i].selected) && (toList.options[i].value != "nodelete")){
			selectedFlag = "yes";
			
			//if it isn't new add it to the delete list
			if ((toList.options[i].value).substring(0, toList.options[i].value.indexOf(partDivider)) != "new") {
				deletedList.value = deletedList.value + entryDivider + (toList.options[i].value).substring(0, toList.options[i].value.indexOf(partDivider));
			}		
		}
		
		//move all the other items up
		if (selectedFlag == "yes" && (i < (toList.options.length - 1))) {
			toList.options[i].value = toList.options[i+1].value
			toList.options[i].text = toList.options[i+1].text
		}
	}
	
	//if it was found remove one from the list
	if (selectedFlag == "yes") {
		toList.length--
	}	
}

//add the entries into a list for submission
function selectAllLists(fromForm) {

	//set up the variables for each list
	var retailerValue = "";
	var wholesalerValue = "";
	var restaurantValue = "";

	//loop through the lists and if it's new we are going to add it	
	for (i = 0; i < fromForm.retailerList.options.length; i++) {
		if ((fromForm.retailerList.options[i].value != "nodelete") && ((fromForm.retailerList.options[i].value).substring(0, fromForm.retailerList.options[i].value.indexOf(partDivider)) == "new")) {	
			retailerValue += entryDivider + (fromForm.retailerList.options[i].value + partDivider + "1");
		}	
	}	
	
	//loop through the lists and if it's new we are going to add it		
	for (i = 0; i < fromForm.wholesalerList.options.length; i++) {
		if ((fromForm.wholesalerList.options[i].value != "nodelete") && ((fromForm.wholesalerList.options[i].value).substring(0, fromForm.wholesalerList.options[i].value.indexOf(partDivider)) == "new")) {	
			 wholesalerValue += entryDivider + (fromForm.wholesalerList.options[i].value + partDivider + "2");
		}	
	}	
	
	//loop through the lists and if it's new we are going to add it		
	for (i = 0; i < fromForm.restaurantList.options.length; i++) {
		if ((fromForm.restaurantList.options[i].value != "nodelete") && ((fromForm.restaurantList.options[i].value).substring(0, fromForm.restaurantList.options[i].value.indexOf(partDivider)) == "new")) {		
			 restaurantValue +=  entryDivider + (fromForm.restaurantList.options[i].value + partDivider + "3");
		}	
	}		
	
	//put all the additions together in one field
	fromForm.retailersToAdd.value = retailerValue +  wholesalerValue + restaurantValue;
}


//if there isn't a name, don't submit
function validateForum(forum) {
	if (forum.nameInput.value == "") {
		alert("You must enter a name to submit forum commentary.");
		return false;
	}	
	if (forum.subjectInput.value == "") {
		alert("You must enter a subject to submit forum commentary.");
		return false;
	}		
}
//if there isn't a name, don't submit
function validateOther(form) {


	if (form.wineStyleDropDown.options[0].selected) {
		alert("You must select a wine style.");
		return false;	
	}	


	if (form.appellationDropDown.options[0].selected) {
		alert("You must select an appellation.");
		return false;	
	}	
	
	foundOther = "no";
	if (form.grapeDropDown1.options[1].selected) {
		foundOther = "yes";
	}
	if (form.grapeDropDown2.options[1].selected) {
		foundOther = "yes";
	}
	if (form.grapeDropDown3.options[1].selected) {
		foundOther = "yes";
	}
	if (form.grapeDropDown4.options[1].selected) {
		foundOther = "yes";
	}
	if (form.grapeDropDown5.options[1].selected) {
		foundOther = "yes";
	}
	if (form.grapeDropDown6.options[1].selected) {
		foundOther = "yes";
	}					
	if (foundOther == "yes" && form.otherGrapeInput.value == "") {
		alert("You selected an other grape type please specify the other name.");
		return false;
	}	
	
	if (foundOther == "no" && form.otherGrapeInput.value != "") {
		alert("You typed an other grape please select OTHER in one of the grape dropdowns.");
		return false;
	}	
		
	if (form.grapeDropDown1.options[0].selected) {
		alert("You must select at least one grape for your wine.");
		return false;	
	} else if (form.productNameInput.value == "") {
		alert("You must enter a name for your wine.");
		return false;	
	}		
	
	if (checkNumbers(form.ml250.value) == "failed") {
		return false;
	}
	if (checkNumbers(form.ml375.value) == "failed") {
		return false;
	}
	if (checkNumbers(form.ml500.value) == "failed") {
		return false;
	}
	if (checkNumbers(form.ml750.value) == "failed") {
		return false;
	}	
	if (checkNumbers(form.ml1000.value) == "failed") {
		return false;
	}
	if (checkNumbers(form.ml1500.value) == "failed") {
		return false;
	}
	if (checkNumbers(form.ml2000.value) == "failed") {
		return false;
	}
	if (checkNumbers(form.ml3000.value) == "failed") {
		return false;
	}		
	if (checkNumbers(form.ml6000.value) == "failed") {
		return false;
	}
	if (checkNumbers(form.ml9000.value) == "failed") {
		return false;
	}
	if (checkNumbers(form.otherPriceInput.value) == "failed") {
		return false;
	}			
}

//if there isn't an appellation, don't submit
function validateWinery(form) {
	if (form.mainAppellationDropDown.options[0].selected) {
		alert("You must select an appellation.");
		return false;	
	}	
}

//number checker
function checkNumbers(fieldValue) {
	//make sure the prices are numbers
	var decallowed = 2;  // how many decimals are allowed?

	if (isNaN(fieldValue)) {
		alert("	Please check your bottle prices, there is a non-numeric entry.");
		return "failed";
	} else {
		if (fieldValue.indexOf('.') == -1) {
			fieldValue += ".";
		}	
		var dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);
		if (dectext.length > decallowed)	{
			alert ("Please check your bottle prices, there is an entry with too many numbers after the decimal");
			return "failed";			
	    } else {
			return "passed";
		}
	}
}


//word counter used for other scripts
function wordCounter(field) {

	var wordcounter = 0;

	for ( i=0 ; i < field.value.length ; i++) {
		if (field.value.charAt(i) == " " && field.value.charAt(i-1) != " "){
			wordcounter++
		}  // Counts the spaces while ignoring double spaces, usually one in between each word.
	}
	return(wordcounter)
}

function validateAwards(awards) {

	var	count = wordCounter(awards.awardsText);
	
	if (count > 75) {
		alert("Your award exceeded the 75 word maximum.");
		return false;
	} else {
		return true;
	}	
}			

function countDescription(form) {

	var	count = wordCounter(form.wineryDescription);

	if (count > 140) {
		alert("Your description exceeded the 140 word maximum.");
		return false;	
	}
}	


//this will do something someday
function catchKeys(form) {
	if (window.event.keyCode == 13)
		return false;
}	
	
//if there isn't a name, don't submit
function warning(message) {
	 if (confirm(message)){
		 return true;
	 } else {
		 return false;
	 }
}	

//see if they agree to the payment
function paymentWarming(message) {
	 if (confirm(message)){
		 return true;
	 } else {
		 return false;
	 }
}	

