// Global useful stuff
   var monthNames = new Array(13);
   monthNames[0]  = "Jan";
   monthNames[1]  = "Feb";
   monthNames[2]  = "Mar";
   monthNames[3]  = "Apr";
   monthNames[4]  = "May";
   monthNames[5]  = "Jun";
   monthNames[6]  = "Jul";
   monthNames[7]  = "Aug";
   monthNames[8]  = "Sep";
   monthNames[9]  = "Oct";
   monthNames[10] = "Nov";
   monthNames[11] = "Dec";

   


// sets a drop down to the value in 'hidField' - providing a match can be found
// if an unknown value is in the input field it will be set to the default value from the dropdown
function drpInit (hidField, dropName)
{
    var i=0;
    var foundit = 0;
    var hidden;
    var drop;

	//alert("Here : drpInit");
    hidden = document.getElementById(hidField); 
    drop =   document.getElementById(dropName);

    if (hidden == null || drop == null) {
    	alert ("ERROR : function drpInit -- could not find field : hidField(" + hidField + ")=" + hidden + ", dropName(" + dropName + ")=" + drop);
    }
        
    for (i=0 ; i<drop.length ; i++)
    {
    	//alert("Found : " + drop.options[i].value +" : " + hidden.value);
        if (drop.options[i].value == hidden.value) {
            drop.value = hidden.value;
            foundit = 1;
            break;
            //alert("found it ");
        }   
    }

    // if we didn't find it then set the hidden input field to the default value from the dropdown
    if (foundit == 0) {
        hidden.value = drop.options[0].value;
    }
}


// called by the onChange method of the dropdown -- sets the hidden input field
function drpUpdate (hidField, dropName)
{
	//alert("Here : drpUpdate");
	
    var hidden = document.getElementById (hidField);
    var drop   = document.getElementById (dropName);

    if (hidden == null || drop == null) {
    	alert ("ERROR : function drpUpdate -- could not find field : hidField(" + hidField + ")=" + hidden + ", dropName(" + dropName + ")=" + drop);
    }
    
    hidden.value = drop.value;
    //alert ("Hidden input field set to : " + hidden.value);
}


function drpChangeContents(dropName, modelList, req_model)	{

	var tmpArray;
	var dropDown = document.getElementById (dropName);

	//alert ("here drpChangeContents");
	if (dropDown)  {

		
		reqIndex = -1;

		// search for model in list  (rolex, cartier etc)
		for(index=0; index<modelList.length; index++)	{
			tmpArray = modelList[index];
			if (tmpArray[0] == req_model) {
				// type found
				//alert("found it " + index);
				reqIndex = index;
				break;
			}
		}

		if (reqIndex == -1) {
			//alert ("Didn't find watch type in list");
		} else {
			// change the dropdown list for the new one
			dropDown.length = 0;  		// zap existing stuff
			for (index=1 ; index<tmpArray.length ; index++) {
				dropDown[index-1] = new Option (tmpArray[index], tmpArray[index]);
			}

			dropDown.selectedIndex = 0;			// just default to the first one
		}

	}
}



// Calendar type functions
// in: object, int, int
// Note: javascript months are 0-11
function drpChangeDates(dropDown, newMonth, newDay)	
{

	var monthLenghts = new Array (31,29,31,30,31,30,31,31,30,31,30,31);

	//alert ("Changing to month/day "+newMonth+" : "+newDay+" : "+ monthLenghts[newMonth] +" days");

	dropDown.length = 0;
	for (i=0 ; i<monthLenghts[newMonth] ; i++)
	{
		dropDown[i] = new Option (i+1, i+1);
	}

	if (monthLenghts[newMonth] < newDay) {		// correct day if new month has less days
		//alert ("Day exceeded " + newDay + " : " + monthLenghts[newMonth]);
		// this is automatically handled by the browser during the save/retrieve to the hidden field
		newDay = monthLenghts[newMonth];
	}
	dropDown.selectedIndex = newDay-1;			// just default to the first one
	

	//alert ("here drpChangeDates");
}



// in: object, int
// Note: javascript months are 0-11
function drpChangeMonth(dropDown, newMonth)	
{

	//alert ("Changing to month "+newMonth+" : "+monthNames[newMonth]);
	dropDown.length = 0;
	for (i=0 ; i<12 ; i++)
	{
		dropDown[i] = new Option (monthNames[i], monthNames[i]);
	}
	dropDown.selectedIndex = newMonth;			// just default to the first one
	
}



// takes a string from the named input field and sets the dropdowns accordingly
// e.g.  hidden input field = 'Expiry'
// sets: Expiry_Month, Expiry_Day, Expiry_Year    (year is an input field)
function drpDateExplode(fieldName)  
{

	var hiddenDate = document.getElementById (fieldName);
	if (hiddenDate == null) {
		alert ("Can't find field "+fieldName);
	}

	var tDate 	   = new Date(hiddenDate.value);	

	var drpMonth   = document.getElementById (fieldName+"_Month");
	var drpDay     = document.getElementById (fieldName+"_Day");
	var fieldYear  = document.getElementById (fieldName+"_Year");	
	var fyear;

	if (drpMonth == null || drpDay == null || fieldYear == null) 
	{
		alert ("Can't find all fields : "+fieldName+"_Month, "+fieldName+"_Day, "+fieldName+"_year");
	}


	drpChangeMonth (drpMonth, tDate.getMonth());	
	drpChangeDates (drpDay,   tDate.getMonth(), tDate.getDate());
	fyear = tDate.getYear();

	if (fyear < 1900) {		// To sort differences between IE6/7 and Netscape/Firefox
		fyear += 1900;
	}

	fieldYear.value = fyear;
}


function drpMonthOnChange(thisElement)
{
	rootname = thisElement.name.substr(0, thisElement.name.length-6);		// remove the '_Month' from the element's name
	//alert ("here : "+thisElement.name + " : " + rootname + " : " + thisElement.options[thisElement.selectedIndex].value);

	rebuildHiddenDate (rootname);
	drpDateExplode (rootname);		// reset all the dropdowns -- nice and easy, if a little inefficient
}

// In: string
// Out: writes to the hidden input field 'rootname'
function rebuildHiddenDate (rootname)
{
	var thefield   = document.getElementById (rootname);
	var drpMonth   = document.getElementById (rootname+"_Month");
	var drpDay     = document.getElementById (rootname+"_Day");
	var fieldYear  = document.getElementById (rootname+"_Year");


	if (drpMonth == null || drpDay == null || fieldYear == null) 
	{
		alert ("Can't find all fields : "+rootname+"_Month, "+rootname+"_Day, "+rootname+"_year");
	}

	rebuiltMonth = drpMonth.options[drpMonth.selectedIndex].value;
	rebuiltDay   = drpDay.options[drpDay.selectedIndex].value;	
	rebuiltDate  = rebuiltDay + " " + rebuiltMonth + " " + fieldYear.value;

	thefield.value = rebuiltDate;		// Aries AND JavaScript understand this format
	//alert (rebuiltDate);
}


function getBrowerType ()
{
	return (navigator.appName);
}



function initMultiShowHide() {
	if (document.getElementById && document.getElementsByTagName && document.createTextNode) {
		multiHide();
		var toggle = document.getElementById('toggle');
		var as = toggle.getElementsByTagName('a');
		for (var i = 0; i < as.length; i++) {
			as[i].onclick = function() {
				multiShow(this);
				return false;
			}
		}			
	}
}

function multiShow(s) {
	multiHide();
	var id = s.href.match(/#(\w.+)/)[1];
	document.getElementById(id).style.display = 'block';
}

function multiHide() {
	var toggleable = document.getElementById('toggleable').getElementsByTagName('div');
	for (var i = 0; i < toggleable.length; i++) {
		toggleable[i].style.display = 'none';
	}
}



