// This function is only called once in this form, from the course dropdown
// it fills the dates select from the array populated on load of the page.

function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
	var i, j;
	var prompt;

	// empty existing items
	for (i = selectCtrl.options.length; i >= 0; i--) {
		selectCtrl.options[i] = null; 
	}
	prompt = (itemArray != null) ? goodPrompt : badPrompt;
	if (prompt == null) {
		j = 0;
	} else {		
		selectCtrl.options[0] = new Option(prompt);
		j = 1;
	}
	if (itemArray != null) {
		// add new items
		for (i = 0; i < itemArray.length; i++) {
			selectCtrl.options[j] = new Option(itemArray[i][0]);
			if (itemArray[i][1] != null) {
				selectCtrl.options[j].value = itemArray[i][1]; 
			}
			j++;
		}
		// select first item (prompt) for sub list
		selectCtrl.options[0].selected = true;
	}
}

/***************************************
/  Function to check details in the search form
/***************************************/

	function checkForm(theForm)
	{
	  
	  selectedOption =  theForm.selectCategory.selectedIndex;
	  //alert("Selected option is" +  selectedOption);
	  optionObject = theForm.selectCategory.options[selectedOption];
	  //alert("Selected option object is" +  optionObject);
	  itsParent = optionObject.parentNode.tagName;
	  if(itsParent == 'OPTGROUP'){
		 // alert("options parent is" +  itsParent);
		  parentsLabel = optionObject.parentNode.label;
		  theForm.selectAntique.value = parentsLabel;
		 // alert("options parents label is" +  parentsLabel);
	  }
	  
	  // check min price is not greater than max price
	  minPrice = theForm.selectMinPrice.value;
	  maxPrice = theForm.selectMaxPrice.value;
	  
	  minPrice = Number(minPrice);
	  maxPrice = Number(maxPrice);
	  
		if ((minPrice > 0) && (maxPrice > 0))
		{	  
		  
		  if(minPrice > maxPrice){
			//alert("The maximum price is smaller than the minimum." + minPrice);	// debug
			//alert("The maximum price is smaller than the minimum." + maxPrice);	// debug
			theForm.selectMinPrice.value = 0; // sets min price to 0
		  }
		  
		}
	 
	  // check that min date is not greater than max date
		
		minCentury = theForm.selectMinCentury.value;
		maxCentury = theForm.selectMaxCentury.value;
		minDecade = theForm.selectMinDecade.value;
		maxDecade = theForm.selectMaxDecade.value;
		
		minCentury = Number(minCentury);
		maxCentury = Number(maxCentury);
		minDecade = Number(minDecade);
		maxDecade = Number(maxDecade);
	    
	    if ((minCentury > 0) && (maxCentury > 0))
		{
			if(minCentury > maxCentury){
		  
			alert("The maximum century is smaller than the minimum.");	// debug	
			return false;
		     		
			}	
		} 
		
		if ((minCentury == maxCentury) && (minDecade > maxDecade)){
		alert("The maximum decade is smaller than the minimum.");	// debug
		return false;
		}
		
	}
	
/***************************************
/  Function to check details in the your details form
/***************************************/

	
	function checkform(of)
		{
		// Test if DOM is available and there is an element called required
			if(!document.getElementById || !document.createTextNode){return;}
			if(!document.getElementById('required')){return;}
		
		// Define error messages and split the required fields
			var errorID='errormsg';
			var errorClass='error';
			var errorMsg='Please enter or change the highlighted fields';
			var errorAlt='*Required field';
			var errorTitle='This field has an error.';
			var reqfields=document.getElementById('required').value.split(',');
	
			if(of.Password){
			if (of.Password.value !== of.confirmPassword.value ){
						//alert ( "password:" + of.Password.value );
						//alert ( "confirm pass:" + of.confirmPassword.value);
						alert ( "The passwords entered do not match." );
					return false;
			}
			}
		
			
		// Cleanup old mess
			// if there is an old errormessage field, delete it
			if(document.getElementById(errorID))
			{
				var em=document.getElementById(errorID);
				em.parentNode.removeChild(em);
			}

			// remove old images and classes from the required fields
			for(var i=0;i<reqfields.length;i++)
			{
				var f=document.getElementById(reqfields[i]);
				if(!f){continue;}
				if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
				{
					f.parentNode.removeChild(f.previousSibling);
				}
				f.className='';
			}

		// loop over required fields
			for(var i=0;i<reqfields.length;i++)
			{
		// check if required field is there
				var f=document.getElementById(reqfields[i]);
				if(!f){continue;}
		// test if the required field has an error, 
		// according to its type
				switch(f.type.toLowerCase())
				{
					case 'text':
						if(f.value=='' && f.id!='Email'){cf_adderr(f)}							
		// email is a special field and needs checking
						if(f.id=='Email' && !cf_isEmailAddr(f.value)){cf_adderr(f)}							
					break;
					case 'textarea':
						if(f.value==''){cf_adderr(f)}							
					break;
					case 'checkbox':
						if(!f.checked){cf_adderr(f)}							
					break;
					case 'select-one':
						if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}							
					break;
					case 'password':
						if(f.value==''){cf_adderr(f)}							
					break;
				}
			}

			return !document.getElementById(errorID);

			/* Tool methods */
			function cf_adderr(o)
			{
				// create image, add to and colourise the error fields
				//var errorIndicator=document.createElement('p');
				//errorIndicator.value=errorAlt;
				//errorIndicator.class=errorImg;
				//errorIndicator.title=errorTitle;
				o.className=errorClass;
				//o.parentNode.insertBefore(errorIndicator,o);

			// Check if there is no error message
				if(!document.getElementById(errorID))
				{
				// create errormessage and insert before submit button
					var em=document.createElement('div');
					em.id=errorID;
					var newp=document.createElement('p');
					newp.className='red';
					newp.appendChild(document.createTextNode(errorMsg))
					// clone and insert the error image
					//newp.appendChild(errorIndicator.cloneNode(true));
					em.appendChild(newp);
					// find the submit button 
					for(var i=0;i<of.getElementsByTagName('input').length;i++)
					{
						if(/submit/i.test(of.getElementsByTagName('input')[i].type))
						{
							var sb=of.getElementsByTagName('input')[i];
							break;
						}
					}
					if(sb)
					{
						sb.parentNode.insertBefore(em,sb);
					}	
				} 
			}
			function cf_isEmailAddr(str) 
			{
			    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
			}
		
	
	
		 
	
			
	}