// Form Validation

window.onload = initForms;

function initForms()
{
	for(var i = 0; i < document.forms.length; i++)
	{
		document.forms[i].onsubmit = function() {return validForm();}
	}
}

function validForm()
{
	if(!validTags())
		return false;
			
	return true;
	
	function validTags()
	{		
		var allGood = true;
		var thisForm = document.forms[0];
	
		document.getElementById("errors").innerHTML = ""; //reset error list
	
		for(var i = 0; i < thisForm.elements.length; i++)
		{
			var thisElement = thisForm.elements[i];
			
			if(thisElement.className == "invalid") //revert to original
				thisElement.className = "required";				
				
			switch(thisElement.className)
			{
				case "required": 
					if(thisElement.value == null || thisElement.value == "")
					{
						allGood = false;
						thisElement.className = "invalid";
						document.getElementById("errors").innerHTML += thisElement.name + " is required. <br>";
					}
					else if(thisElement.name == "Email")
					{
						if(!validEmail(thisElement.value))
						{
							allGood = false;
							thisElement.className = "invalid";
							document.getElementById("errors").innerHTML += thisElement.name + " address entered is not valid. <br>";
						}							
					}
					else if(thisElement.name == "Product Family" || thisElement.name == "Product")
					{
						if(thisElement.selectedIndex == 0)
						{
							allGood = false;
							thisElement.className = "invalid";
							document.getElementById("errors").innerHTML += thisElement.name + " is required. <br>";
						}
					}
					   
					break;
			}
		}
		
		return allGood;	
		
		function validEmail(email)
		{
			var re = /^\w+([\.-]?\w+)*@\w+([\.-]+)*(\.\w{2,3})+$/;
			return re.test(email);
		}
	}
	

}


// inquiry form drop down
function initProductFamily()
{
	var brand = document.getElementById("brand");
	var family = document.getElementById("product");
	
	clearOptions(family);
	
	if(brand.value == "Econowatd")
	{
		createOption(family, "Ozone Generator", "Ozone Generator");
		createOption(family, "Electronic Ballast", "Electronic Ballast");
	}
	else if(brand.value == "Starlux Lamps")
	{
		createOption(family, "Linear Fluorescent", "Linear Fluorescent");
		createOption(family, "Compact Fluorescent", "Compact Fluorescent");		
		createOption(family, "Fixtures and Luminaires", "Fixtures and Luminaires");
		createOption(family, "Control Gears", "Control Gears");
	}
	else if(brand.value == "Zebra Power Applications")
	{
		createOption(family, "Regulated Power Supply", "Regulated Power Supply");
		createOption(family, "Automated Voltage Regulator", "Automated Voltage Regulator");		
		createOption(family, "Transformers", "Transformers");
		createOption(family, "Uninterruptible Power Supply", "Uninterruptible Power Supply");
		createOption(family, "Inverters", "Inverters");
	}
	initProducts();
}

function initProducts()
{
	var family = document.getElementById("product");
	var details = document.getElementById("product_details");
	
	clearOptions(details);
	
	if(family.value == "Ozone Generator")
	{
		createOption(details, "Cooling Tower", "for Cooling Towers");
		createOption(details, "Waste Water Treatment", "for Waste Water Treatment");
		createOption(details, "--> All Products", "--> All Products");
	}
	else if(family.value == "Electronic Ballast")
	{
		createOption(details, "Slim(for T5)", "Slim(for T5)");
		createOption(details, "Premium Compact", "Premium Compact");
		createOption(details, "Perform", "Perform");
		createOption(details, "Symply", "Symply");
		createOption(details, "IP55", "IP55");
		createOption(details, "DC Gold (12V)", "DC Gold (12V)");
		createOption(details, "Analog Dimming (for T8 and TC-L)", "Analog Dimming (for T8 and TC-L)");
		createOption(details, "Analog Dimming (for TC-TE/DE)", "Analog Dimming (for TC-TE/DE)");
		createOption(details, "EZY-SET", "EZY-SET");
		createOption(details, "E-SET Plugin", "E-SET Plugin");
		createOption(details, "E-SET I", "E-SET I");
		createOption(details, "E-SET II", "E-SET II");
		createOption(details, "E-SET DC", "E-SET DC");	
		createOption(details, "Analog Dimming Control Switch", "Analog Dimming Control Switch");	
		createOption(details, "--> All Products", "--> All Products");
	}
	else if(family.value == "Linear Fluorescent")
	{
		createOption(details, "T5 Linear Lamps", "T5 Linear Lamps");
		createOption(details, "T8 Linear Lamps (Thailand)", "T8 Linear Lamps (Thailand)");
		createOption(details, "T10 Linear Lamps (Thailand)", "T10 Linear Lamps (Thailand)");
		createOption(details, "T9 Circular Lamps (Thailand)", "T9 Circular Lamps (Thailand)");
		createOption(details, "--> All Products", "--> All Products");

	}
	else if(family.value == "Compact Fluorescent")
	{
		createOption(details, "Pinlight - Single", "Pinlight - Single");
		createOption(details, "Pinlight - Dual", "Pinlight - Dual");
		createOption(details, "Pinlight Dual - Electronic", "Pinlight Dual - Electronic");
		createOption(details, "Globe", "Globe");
		createOption(details, "Capsule", "Capsule");
		createOption(details, "2U Compact Fluorescent Lamp", "2U Compact Fluorescent Lamp");
		createOption(details, "Mini 2U Compact Fluorescent Lamp", "Mini 2U Compact Fluorescent Lamp");
		createOption(details, "3U Compact Fluorescent Lamp", "3U Compact Fluorescent Lamp");
		createOption(details, "Mini 3U Compact Fluorescent Lamp", "Mini 3U Compact Fluorescent Lamp");
		createOption(details, "4U Compact Fluorescent Lamp", "4U Compact Fluorescent Lamp");
		createOption(details, "Super - Twist", "Super - Twist");
		createOption(details, "GLS", "GLS");
		createOption(details, "Candle", "Candle");
		createOption(details, "DC 12 Volt", "DC 12 Volt");
		createOption(details, "Circline Electronic Lamp Sets", "Circline Electronic Lamp Sets");
		createOption(details, "T5 Linear Fluorescent Lamp Set", "T5 Linear Fluorescent Lamp Set");
		createOption(details, "--> All Products", "--> All Products");
	}
	else if(family.value == "Fixtures and Luminaires")
	{
		createOption(details, "Box Type", "Box Type");
		createOption(details, "Open Type", "Open Type");
		createOption(details, "Floodlight - Symmetrical", "Floodlight - Symmetrical");
		createOption(details, "Floodlight - Asymmetrical", "Floodlight - Asymmetrical");
		createOption(details, "Fluorescent Louver (Rectangle)", "Fluorescent Louver (Rectangle)");
		createOption(details, "Fluorescent Louver (Square)", "Fluorescent Louver (Square)");
		createOption(details, "Highbay - Polycarbonate Reflector", "Highbay - Polycarbonate Reflector");
		createOption(details, "Highbay - Aluminum Reflector", "Highbay - Aluminum Reflector");
		createOption(details, "Roadlighting", "Roadlighting");
		createOption(details, "Undercanopy Luminaire", "Undercanopy Luminaire");
		createOption(details, "Weatherproof Fluorescent Fixture", "Weatherproof Fluorescent Fixture");
		createOption(details, "--> All Products", "--> All Products");

	}
	else if(family.value == "Control Gears")
	{
		createOption(details, "Starter", "Starter");
		createOption(details, "Power Factor Capacitor", "Power Factor Capacitor");
		createOption(details, "Magnetic Ballast", "Magnetic Ballast");
		createOption(details, "Ignitor", "Ignitor");
		createOption(details, "Photoelectric Control", "Photoelectric Control");
		createOption(details, "--> All Products", "--> All Products");

	}
}

function clearOptions(objSelect)
{
	while(objSelect.options.length > 1)
		objSelect.remove(1);
}

function createOption(objSelect, value, text)
{
	var objOption = document.createElement("option");
	objOption.value = value;
	objOption.text = text;
	
	if(document.all && !window.opera)
  	{
		objSelect.add(objOption);
	}
 	else
  	{	
		objSelect.add(objOption, null);
	};
}

// retrieve url parameters -- http://webdeveloper.internet.com/forum/showthread.php?t=129968

function getURLVar(varName) {
  url = unescape(self.document.location);
  if (varstring = url.substring(url.indexOf('?')+1)) {
    varpairs  = varstring.split(/&/);
    for (i=0; i<varpairs.length; i++) {
      varpair = varpairs[i].split(/=/);
      if (varName==varpair[0]) { return varpair[1]; }
    }
  }
  return false;
}

function setSelectedIndex()
{
	var index0 = getURLVar("b");
	var index1 = getURLVar("f");
	var index2 = getURLVar("p");
	
	if(index1 != null && index0 != null)
	{
		var brand = document.getElementById("brand");
		var family = document.getElementById("product");
		var details = document.getElementById("product_details");
		
		brand.selectedIndex = index0;
		
		initProductFamily();
		
		family.selectedIndex = index1;
		
		initProducts();
		
		details.selectedIndex = index2 == null? 0 : index2;
	}
}
