/* This script handles the monthly special choices
Original author: My Colorful Treasures
Last Updated: Nov. 27, 2010
*/

function specialChoiceSelected(frm, selection) {
    for (var i=0; i < frm.elements.length; ++i) {
        var formField = frm.elements[i];
        var formId = formField.id;
        if (formId.match(/specialdip/)) {
	        if (selection == "dips") {
	            document.getElementById(formId).disabled = false;
            } else {
			    document.getElementById(formId).value = "";
			    document.getElementById(formId).disabled = true;
	        }
        }
		if (formId.match(/specialchocolate/)) {
            if (selection == "hotchocolate") {
	            document.getElementById(formId).disabled = false;
            } else {
	            document.getElementById(formId).value = "";
			    document.getElementById(formId).disabled = true;
        	}
        }
		if (formId.match(/specialcoffee/)) {
            if (selection == "coffee") {
	            document.getElementById(formId).disabled = false;
        	} else {
			    document.getElementById(formId).value = "";
			    document.getElementById(formId).disabled = true;
			}
		}
    }
}

function processSpecialOrder(frm) {
    var passedvalidation = validateSpecialOrder(frm);
	if (!passedvalidation) {
	    return false;
	}
    
	var specialorder = "";
	for (var i=0; i < frm.elements.length; ++i) {
        var formField = frm.elements[i];
        var formId = formField.id;
        if (formId.match(/specialdip/) || formId.match(/specialchocolate/) || formId.match(/specialcoffee/)) {
		    quantity = parseInt(document.getElementById(formId).value);
			if (!isNaN(quantity)) {
				if (specialorder != "") {
	                specialorder += ", ";
	            }
				specialorder += document.getElementById(formId).name + " - " + quantity;
            }		
		}
    }
	frm.os0.value = specialorder;
	frm.os1.value = frm.specialcardmessage.value + ", " + frm.specialfirstlastname.value + ", " + frm.specialaddress1.value + " " + frm.specialaddress2.value + ", " + frm.specialcitystatezip.value + ", " + frm.specialdeliverydate.value;
	return true;
}

function validateSpecialOrder(frm) {
    var selection = getSelectedValue(frm, 'specialchoice');
    if (selection != '') {
	    var totalQuantity = 0;
		var quantity = 0;
        for (var i=0; i < frm.elements.length; ++i) {
            var formField = frm.elements[i];
            var formId = formField.id;
            if (formId.match(/specialdip/) || formId.match(/specialchocolate/) || formId.match(/specialcoffee/)) {
	            quantity = parseInt(document.getElementById(formId).value);
				if (!isNaN(quantity)) {
				    totalQuantity += quantity;
				}
            }
        }

	    if (totalQuantity < 12) {
	        if (selection == "dips") {
	            alert("You need to select 12 dips for the holiday gift box special");
            } else if (selection == "hotchocolate") {
  	            alert("You need to select 12 hot chocolates for the holiday gift box special");
  	        } else if (selection == "coffee") {
	            alert("You need to select 12 flavored coffees for the holiday gift box special");
	        }
	        return false;
	    } else if (totalQuantity > 12) {
	        if (selection == "dips") {
  	            alert("You can not select more than 12 dips for the holiday gift box special");
	        } else if (selection == "hotchocolate") {
	            alert("You can not select more than 12 hot chocolates for the holiday gift box special");
	        } else if (selection == "coffee") {
	            alert("You can not select more than 12 flavored coffees for the holiday gift box special");
	        }
	        return false;
	    }
		if (!validateSpecialInfo(frm)) {
		    return false;
		}
    } else {
	    alert("Please select a gift box type!");
		return false;
	}
	return true;
}

function getSelectedValue(frm, radioname) {
    var radio = frm.elements[radioname];
    for(var i = 0; i < radio.length; i++) { 
        if(radio[i].checked) {
            return radio[i].value;
        }

    }
    return '';
}

function validateSpecialInfo(frm) {
    if (!validcharacters(frm.specialcardmessage.value) || !frm.specialcardmessage.value.length) {
	   alert("Please enter a valid card message!");
	   return false;
	} else if (!validcharacters(frm.specialfirstlastname.value) || !frm.specialfirstlastname.value.length) {
	   alert("Please enter a valid customer name!");
	   return false;
	} else if (!validcharacters(frm.specialaddress1.value) || !frm.specialaddress1.value.length) {
	   alert("Please enter a valid customer address!");
	   return false;
	} else if (!validcharacters(frm.specialcitystatezip.value) || !frm.specialcitystatezip.value.length) {
	   alert("Please enter a valid customer city, state, zip!");
	   return false;
	} else if (!validcharacters(frm.specialdeliverydate.value) || !frm.specialdeliverydate.value.length) {
	   alert("Please enter a valid delivery date!");
	   return false;
	}
	return true;
}

function validcharacters(text)
{
	var currentText = text;
	var character;
	var code;
	for(var j=0; j< currentText.length; j++)
	{
		  character = currentText.charAt(j);
		  code = character.charCodeAt(0);
		  if((code < 32 || code > 126) && code != 13 && code != 10)
		  {
		    return false;
		  }
	}
    return true;
}
