var Strands = {
	all : new Array('All Strands','all','Arts','all','  >> Dance','"Arts\\Dance"','  >> Drama','"Arts\\Drama"','  >> Media','"Arts\\Media"','  >> Music','"Arts\\Music"','  >> Visual arts','"Arts\\Visual arts"','English','all','  >> Listening and speaking','"English\\Listening and speaking"','  >> Reading and writing','"English\\Reading and writing"','  >> Viewing','"English\\Viewing"','Health and physical education','all','  >> Health of individuals and populations','"Health of individuals and populations\\Health of individuals and populations"','  >> Human development','"Health of individuals and populations\\Human development"','  >> Human movement','"Health of individuals and populations\\Human movement"','  >> Human relations','"Health of individuals and populations\\Human relations"','  >> People and food','"Health of individuals and populations\\People and food"','  >> Physical activity and the community','"Health of individuals and populations\\Physical activity and the community"','  >> Safety','"Health of individuals and populations\\Safety"','Languages other than english','all','  >> Oral interaction','"Languages other than english\\Oral interaction"','  >> Reading and responding','"Languages other than english\\Reading and responding"','  >> Writing','"Languages other than english\\Writing"','  >> Understanding culture','"Languages other than english\\Understanding culture"','  >> Understanding language','"Languages other than english\\Understanding language"','Mathematics','all','  >> Algebra','"Mathematics\\Algebra"','  >> Chance and data','"Mathematics\\Chance and data"','  >> Measurement','"Mathematics\\Measurement"','  >> Number','"Mathematics\\Number"','  >> Space','"Mathematics\\Space"','  >> Working mathematically','"Mathematics\\Working mathematically"','Science','all','  >> Life and living','"Science\\Life and living"','  >> Natural and processed materials','"Science\\Natural and processed materials"','  >> Earth and beyond','"Science\\Earth and beyond"','  >> Energy and change','"Science\\Energy and change"','  >> Working scientifically','"Science\\Working scientifically"','Studies of society and environment','all','  >> Culture','"Studies of society and environment\\Culture"','  >> Investigation, communication and participation','"Studies of society and environment\\Investigation, communication and participation"','  >> Place and space','"Studies of society and environment\\Place and space"','  >> Resources','"Studies of society and environment\\Resources"','  >> Time, continuity and change','"Studies of society and environment\\Time, continuity and change"','  >> Civics and citizenship','"Studies of society and environment\\Civics and citizenship"','Technology','all','  >> Designing, making and apprasing','"Technology\\Designing, making and apprasing"','  >> Information','"Technology\\Information"','  >> Materials','"Technology\\Materials"','  >> Systems','"Technology\\Systems"'),
	Arts : new Array('All Strands','all','Dance','"Arts\\Dance"','Drama','"Arts\\Drama"','Media','"Arts\\Media"','Music','"Arts\\Music"','Visual arts','"Arts\\Visual arts"'),
	English : new Array('All Strands','all','Listening and speaking','"English\\Listening and speaking"','Reading and writing','"English\\Reading and writing"','Viewing','"English\\Viewing"'),
	Health_and_physical_education : new Array('All Strands','all','Health of individuals and populations','"Health and physical education\\Health of individuals and populations"','Human development','"Health and physical education\\Human development"','Human movement','"Health and physical education\\Human movement"','Human relations','"Health and physical education\\Human relations"','People and food','"Health and physical education\\People and food"','Physical activity and the community','"Health and physical education\\Physical activity and the community"','Safety','"Health and physical education\\Safety"'),
	Languages_other_than_english : new Array('All Strands','all','Oral interaction','"Languages other than english\\Oral interaction"','Reading and responding','"Languages other than english\\Reading and responding"','Writing','"Languages other than english\\Writing"','Understanding culture','"Languages other than english\\Understanding culture"','Understanding language','"Languages other than english\\Understanding language"'),
	Mathematics : new Array('All Strands','all','Algebra','"Mathematics\\Algebra"','Chance and data','"Mathematics\\Chance and data"','Measurement','"Mathematics\\Measurement"','Number','"Mathematics\\Number"','Space','"Mathematics\\Space"','Working mathematically','"Mathematics\\Working mathematically"'),
	Science : new Array('All Strands','all','Life and living','"Science\\Life and living"','Natural and processed materials','"Science\\Natural and processed materials"','Earth and beyond','"Science\\Earth and beyond"','Energy and change','"Science\\Energy and change"','Working scientifically','"Science\\Working scientifically"'),
	Studies_of_society_and_environment : new Array('All Strands','all','Culture','"Studies of society and environment\\Culture"','Investigation, communication and participation','"Studies of society and environment\\Investigation, communication and participation"','Place and space','"Studies of society and environment\\Place and space"','Resources','"Studies of society and environment\\Resources"','Time, continuity and change','"Studies of society and environment\\Time, continuity and change"','Civics and citizenship','"Studies of society and environment\\Civics and citizenship"'),
	Technology : new Array('All Strands','all','Designing, making and apprasing','"Technology\\Designing, making and apprasing"','Information','"Technology\\Information"','Materials','"Technology\\Materials"','Systems','"Technology\\Systems"')
};

var Search = {
	
	changeArea : function changeArea(strand) {
	    var areaSel = $('learning_area');
	    var areaKey = areaSel.options[areaSel.selectedIndex].value;
	    var strandsArr = eval('Strands.' + Search.encodeAreaKey(areaKey));
	    Search.refreshStrands(strand, strandsArr);
	},
		
	refreshStrands : function (strand, strandsArr) {
		var strandSel = $('strand');
	    strandSel.options.length = 0;
	    var selectedOption;
	    for (i = 0; i < strandsArr.length; i=i+2) {
	    	var opt = new Option(strandsArr[i], strandsArr[i+1], false, true);
	    	strandSel.options[strandSel.length] = opt;
	    	if (opt.value == strand) {
	    		selectedOption = opt;
	    	}
	    }
	    if (selectedOption != null) {
			selectedOption.selected = true;
	    } else {
			strandSel[0].selected = true;
	    }
	},
	
	encodeAreaKey : function (areaKey) {
		if (areaKey.indexOf('\"') == 0)  
			areaKey = areaKey.substring(1);
   		if (areaKey.lastIndexOf('\"') == (areaKey.length-1)) areaKey = areaKey.substring(0, areaKey.length-1);
			return areaKey.replace(/ /g,'_');
	},
	
	setLearningArea : function () {
	
		var qs = new Querystring();
		var la = null;
		if (qs != null) {
			la = qs.get('learningarea');
		}

	
		var laSel = $('learning_area');
		for (i = 0; i < laSel.length; i++) {
			var opt = laSel.options[i];
			if (opt.value == la) {
				opt.selected = true;
			}
		}
	},
	
	validateAndSubmit : function () {	
		if (validateDateField(null) == false)
			return false;
		// make sure the dates are in the format of dd/MM/yyyy
		var pubmin = $('fromdate');
		var pubmax = $('todate');
		var valid = true;		
		
		if (pubmin.value == "" || pubmin.value == null) {
			pubminValid = true;
		} else {
			var pubminDate = Search.validateDate(pubmin.value, "%d/%m/%Y"); 
			if (pubminDate == "") {
				// invalid date
				var errorText = $('date_from_validation') ;
				errorText.innerHTML = "Invalid Date provided.";
				valid = false;
			}
		}
		
		if (pubmax.value == "" || pubmax.value == null) {
			pubmaxValid = true;
		} else {
			var pubmaxDate = Search.validateDate(pubmax.value, "%d/%m/%Y"); 
			if (pubmaxDate == "") {
				// invalid date
				var errorText = $('date_to_validation');
				errorText.innerHTML = "Invalid Date provided.";
				valid = false;
			}
		}
		
		//if ( (pubmin.value == "" || pubmin.value == null) && (pubmax.value == "" || pubmax.value == null) ) {
		if ( (pubmin.value == "" && pubmax.value == "") || (pubmin.value != "" && pubmax.value != "") ) {
		} else {
			alert("Date range is incorrect. Please select date again!");
			valid = false;
		}		
		
		if (valid == true) {
			$('search-form').submit();
		}
	},
	
	clear : function() {
		$('search-form').reset();
		$('date_to_validation').innerHTML = ""; 
		$('date_from_validation').innerHTML = ""; 
	},
	
	validateDate : function(str, fmt) {
		var today = new Date();
		var y = 0;
		var m = -1;
		var d = 0;
		var a = str.split(/\W+/);
		var b = fmt.match(/%./g);
		var i = 0, j = 0;
		var hr = 0;
		var min = 0;
		for (i = 0; i < a.length; ++i) {
			if (!a[i])
				continue;
			switch (b[i]) {
			    case "%d":
			    case "%e":
				d = parseInt(a[i], 10);
				break;
	
			    case "%m":
				m = parseInt(a[i], 10) - 1;
				break;
	
			    case "%Y":
			    case "%y":
				y = parseInt(a[i], 10);
				(y < 100) && (y += (y > 29) ? 1900 : 2000);
				break;
	
			    case "%b":
			    case "%B":
				for (j = 0; j < 12; ++j) {
					if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { m = j; break; }
				}
				break;
	
			    case "%H":
			    case "%I":
			    case "%k":
			    case "%l":
				hr = parseInt(a[i], 10);
				break;
	
			    case "%P":
			    case "%p":
				if (/pm/i.test(a[i]) && hr < 12)
					hr += 12;
				else if (/am/i.test(a[i]) && hr >= 12)
					hr -= 12;
				break;
	
			    case "%M":
				min = parseInt(a[i], 10);
				break;
			}
		}
		if (isNaN(y)) y = today.getFullYear();
		if (isNaN(m)) m = today.getMonth();
		if (isNaN(d)) d = today.getDate();
		if (isNaN(hr)) hr = today.getHours();
		if (isNaN(min)) min = today.getMinutes();
		
		
		if (y == 0 || m == -1 || d == 0) {
			return "";
		}
		if (m < 0 || m > 12) return "";
		if (d < 1 || d > 31) return "";
		
		if (y != 0 && m != -1 && d != 0)
			return new Date(y, m, d, hr, min, 0);
		
		y = 0; m = -1; d = 0;
		for (i = 0; i < a.length; ++i) {
			if (a[i].search(/[a-zA-Z]+/) != -1) {
				var t = -1;
				for (j = 0; j < 12; ++j) {
					if (Calendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; }
				}
				if (t != -1) {
					if (m != -1) {
						d = m+1;
					}
					m = t;
				}
			} else if (parseInt(a[i], 10) <= 12 && m == -1) {
				m = a[i]-1;
			} else if (parseInt(a[i], 10) > 31 && y == 0) {
				y = parseInt(a[i], 10);
				(y < 100) && (y += (y > 29) ? 1900 : 2000);
			} else if (d == 0) {
				d = a[i];
			}
		}

		alert(d);
		if (y == 0)
			y = today.getFullYear();
		if (m != -1 && d != 0)
			return new Date(y, m, d, hr, min, 0);
		return today;
	},
	
	toggleYears : function () {
		toggleVis('years',  document.getElementById('learning_object').checked || document.getElementById('collection').checked) 
	}
	
	

};
function toggleVis(objID,show) {
		
		if (show) {
			document.getElementById(objID).style.display='block';
		}	else {
			document.getElementById(objID).style.display='none';
		}
}

function toggleLayer(whichLayer)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}


function toggleVis(objID,show) {
		if (show) {
			document.getElementById(objID).style.display='block';
		}	else {
			document.getElementById(objID).style.display='none';
		}
}


/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
    // split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}


