// JavaScript Document

function bob(){
}

var currentDropDown=0;// this exposes currently open dropdown to language toggle function
var qsParm = new Array();

//language-specific values for dropdown show/hide buttons
var statematch 	= new Array(/Show/,/afficher/) ;//the text to find to determine current state of this dropdown
var showtext 	= new Array("Show","afficher") ;//the 'Show' text
var hidetext 	= new Array("Hide","cacher") ;	//the 'Hide' text

function initDropdowns(numberofDDs,typeofDD){

	//content panels start off open (arrow down, content displayed) so close them one by them
	//unless they are called from external via querystring
	//in that case, leave that one open and intrapage link to it
	
	//Why is this done this way?
	//The dropdown script only toggles, it does not 'set' up or 'down' absolutely
	//so the only way to be sure you're opening, rather than closing, a particular dropdown
	// is to reset them all by refreshing the page

	//typeofDD is a flag to separate out the "content only" type dropdowns on Colour&Lighting page
	
	qs();
	for (dd=1;dd<=numberofDDs;dd++){
		if (parseInt(qsParm["show"]) != dd){
			//close panel (since it's pen on page load by default)
			if (typeofDD=='ArrowOnly'){
				toggleArrowDropdown(dd);
			}
			else{
				toggleDropdown(dd,true);
			}
		}
		else{
			//don't close this panel
			// intrapage link to it instead
			currentDropDown = dd;//set so lang toggle can use it
			location.href="#D"+dd
		}
	}
}

function toggleDropdown(contentID,onloadFlag){
	//for all dropdowns

	//flip state of scriptaculous div (separate .js)
	Effect.toggle('dropdown_content' + contentID, 'blind'); 
	//flip Hide/Show text and arrow
	if(document.getElementById('hideDropdown' + contentID).innerHTML.match(statematch[lang])) {
		
		document.getElementById('hideDropdown' + contentID).innerHTML = xreplace(document.getElementById('hideDropdown' + contentID).innerHTML,showtext[lang],hidetext[lang]);
		document.getElementById('hideDropdown' + contentID).innerHTML = xreplace(document.getElementById('hideDropdown' + contentID).innerHTML,"list_arrow_up","list_arrow_dn");
		currentDropDown = contentID;//set so lang toggle can use it
	}
	else{
		document.getElementById('hideDropdown' + contentID).innerHTML = xreplace(document.getElementById('hideDropdown' + contentID).innerHTML,hidetext[lang],showtext[lang]);
		document.getElementById('hideDropdown' + contentID).innerHTML = xreplace(document.getElementById('hideDropdown' + contentID).innerHTML,"list_arrow_dn","list_arrow_up");
	//if onloadflag is true then it knows these close calls are coming from page load function, so preserve currentDropdown state
	//if onloadflag is false or null, it knows this being called by the user from the page itself, meaning user is closing a dropdown, so set currentDropdown to 0
		if (!onloadFlag){ currentDropDown=0; }
	}
	return false; 		
}

function toggleArrowDropdown(contentID){
	//for "content only" type dropdowns (Colour&Lighting page)
	//these are much simpler than other dropdowns as they do not have the toggling Show/Hide button

	//flip state of arrow (below) and of scriptaculous div (separate .js)
	Effect.toggle('dropdown' + contentID, 'blind'); 
	toggleArrow(contentID );
	return false; 		
}

function toggleArrow(contentID){
	//toggles >> arrow on Colour&Lighting page
	if (document.getElementById("arrow"+contentID).style.listStyleImage == "url(images/design/list_arrow_up.gif)"){
		setArrow = "dn"
		currentDropDown = contentID;//set so lang toggle can use it
	}
	else{
		setArrow = "up"
		currentDropDown = 0;
	}
	document.getElementById("arrow"+contentID).style.listStyleImage = "url(images/design/list_arrow_" + setArrow + ".gif)";
}

function toggleLang(thisPage){
//language toggle button is context-sensitive: will open to correct dropdown
	// if currentDropDown is other than 0, then call alternate language page with dropdown open
	if (currentDropDown >0){ thisPage += "?show=" + currentDropDown;	}
	location.href = thisPage;
}

function toggleRefDropdown(){
	//this is for references only (Find Colour - History of Colour)
	//flip state of scriptaculous div (separate .js)
	Effect.toggle('RefDropdown', 'blind'); 
	return false; 		
}

function toggleFAQDropdown(contentID){
	//flip state of scriptaculous div (separate .js) for FAQ
	Effect.toggle('dropdown' + contentID, 'blind'); 
	return false; 		
}


function qs() {
//generic querystring parser, stores params in associative array qsparm[]
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
}

function xreplace(checkMe,toberep,repwith){
	var temp = checkMe;
	var i = temp.indexOf(toberep);
	while(i > -1){
		temp = temp.replace(toberep, repwith);
		i = temp.indexOf(toberep, i + repwith.length + 1);
	}
	return temp;
}

function popWindow(sURL){
	window.open(sURL)
}
var	default_text_first = new Array("first name","Prénom")
var	default_text_last = new Array("last name","Nom")
var	default_text_email = new Array("enter email","Courriel")

function validateNewsletter(){
	// ************* INCOMPLETE - needs back-end wired up  ********
	
	//checks newsletter fields for valid entries
	// note langauge-sensitivity flag: lanf oulled from head of page
	
	//turn all errors off at first
	document.getElementById("err_first").style.visibility = 'hidden';
	document.getElementById("err_last").style.visibility = 'hidden';
	document.getElementById("err_email").style.visibility = 'hidden';
	var valid = true;
	//get feild values
	fname = document.getElementById("txt_first").value
	lname = document.getElementById("txt_last").value
	email = document.getElementById("txt_email").value
	//check that fields aren't simply default
	if (fname == "" || fname == " " || fname == default_text_first[lang] ){
		document.getElementById("err_first").style.visibility = 'visible';
		valid = false;
	}
	if (lname == "" || lname == " " || lname == default_text_last[lang] ){
		document.getElementById("err_last").style.visibility = 'visible';
		valid = false;
	}
	if (!(email.indexOf(".") > 2) || !(email.indexOf("@") > 0)){
		document.getElementById("err_email").style.visibility = 'visible';
		valid = false;
	}
	//passed all tests
	if (valid){
		alert("<sends email>")
		location.href="index.html" // process email
	}
}

function clearField(fieldName){
	// designed to wipe fields of default text when field is clicked
	// not currently in use
	if (document.getElementById(fieldName).value = default_text_first[lang]){
		document.getElementById(fieldName).value = ""
	}
	if (document.getElementById(fieldName).value = default_text_last[lang]){
		document.getElementById(fieldName).value = ""
	}
	if (document.getElementById(fieldName).value = default_text_email[lang]){
		document.getElementById(fieldName).value = ""
	}
}

function popup(url,w,h) {
    // Dealer's > other > cans & logos
    // pops up larger image
	newwindow = eval("window.open(url,'name','width="+w+",height="+h+"')");
	if (window.focus) {newwindow.focus()}
	return false;
}

function ft_colourChipOrders(state){
    // footer > Colour Chip Orders
    // shows/hides submenu
    document.getElementById('OrdersPopUp').style.visibility = state;
}