/******************************************
Name:			yarnFunctions.js
Author:			Andrei Vais
Date:			19/01/2009
Description:	File contains all the function required for the yarn pages
******************************************/

//functions calls
addLoadEvent(orderShade);

function orderShade()
{
	if (!document.getElementById("contentpage")) return false;
	var allYarnShades = document.getElementById("contentpage");
	
	if(allYarnShades)
	{
	    var yarnShadeForms = getElementsByClassName(allYarnShades,"form","shade");
	    
	    for (var i=0; i<yarnShadeForms.length; i++)
		{
		    if (typeof yarnShadeForms[i].onsubmit != 'function') // only apply this once to each form
			{
				yarnShadeForms[i].onsubmit = function()
				{
				    return validateQuantity(this.getAttribute("id"));
				}
			}
		}
	}	
}

function validateQuantity(pName)
{
    var blnValidQuantity = true;
    
    //1. from form id get the id of the form field
    //2. check the quantity entreed is: no less than one and numeric
    //3. based on the quantity alert user and return true or false
    var formElements = document.getElementById(pName).childNodes;
    
    for (var i=0; i<formElements.length; i++)
    {
        if (formElements[i].childNodes.length != 0)
        {
            var formParagraph = getElementsByClassName(formElements[i],"p","shadequantity");
            //assumption here that there will be only one paragraph in the each form with this class name
            var formFieldsetElements = formParagraph[0].childNodes;
            
            for (var j=0; j<formFieldsetElements.length; j++)
            {
                if(formFieldsetElements[j].getAttribute("type") == "text")
                {
                    var quantityRequested = formFieldsetElements[j].value;
                    if (quantityRequested < 1)
                    {
                        alert("You need to order at least one yarn for this shade to continue.");
                        incorrectValueInField(formFieldsetElements[j]);
                        blnValidQuantity = false;
                    }
                    else if (isNaN(quantityRequested))
                    {
                        alert("Please ensure the quantity order is numeric.");
                        incorrectValueInField(formFieldsetElements[j]);
                        blnValidQuantity = false;
                    
                    }
                }
            }
        }
    }
    return blnValidQuantity;
}

function incorrectValueInField(pFormField)
{
    pFormField.select();
    pFormField.style.background="#CC3333";
    pFormField.style.color="#FFFFFF";
}