//specific to search form on top of all pages
function validate_sform(f){

	var keywordSearch=(f.keywordsearch !=null && f.keywordsearch.checked)


		if (keywordSearch==true) {
			f.target="_blank"
		} else {
			f.target="_self"
		}	


	if(f.sterm.value.trim() == "" || f.sterm.value.trim() == "--- Search ---"){
		alert("Please enter a keyword to search for");
		f.sterm.className = 'stermshortreq';
		f.sterm.focus();
		return false;
	}else{

		f.sterm.className= 'stermshort'

		return true;
	}
}


//common for all forms
function form_validate(f,verify){
	//var els = f.getElements();
	var els=f.elements;
	
	for(i=0;i<els.length;i++){
		if(els[i].className == "frmreq" || els[i].className == "required"){
			switch(els[i].type){
				case "text":
					if(chkEmpty(els[i],'This field is required and cannot be empty') == false) return false;
					break;
				case "textarea":
					if(chkEmpty(els[i],'This field is required and cannot be empty') == false) return false;
					break;
				case "select":
					if(chkEmpty(els[i],'This field is required and cannot be empty') == false) return false;
					break;
				case "select-multiple":
					if(chkEmpty(els[i],'This field is required and cannot be empty') == false) return false;
					break;
				case "radio":
					if(chkRadio(f.elements[els[i].name],'This field is required. Please select atleast one option') == false) return false;
					break;
				case "checkbox":
					if(chkRadio(f.elements[els[i].name],'This field is required. Please select atleast one option') == false) return false;

					break;
			}
			if(els[i].id == 'captcha'){
				if(els[i].value != verify){
					alert('The verification text you entered does not match the image');	
					return false;
				}
			}
		}
	}
	
	return true;
}


//common to all popups - rewrite all abc and other stuff to match with this
function popup(url,w,h){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	
	childWindow=open(url, "", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+w+",height="+h+",left ="+LeftPosition+",top="+TopPosition+"");
	if (childWindow.opener == null) childWindow.opener = self;	
}

//used only for player so we can change player vars later without having to change the player itself
function popupplayer(url){
	w 	= 930;
	h	= 510;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	
	childWindow=open(url, "VideoPlayer", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width="+w+",height="+h+",left ="+LeftPosition+",top="+TopPosition+"");
	if (childWindow.opener == null) childWindow.opener = self;	
}

//check to see if any radio button is checked in a family
function chkRadio(btn,msg) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1){
		return true;
	}else{ 
		alert(msg);
		btn[0].focus();
		return false;
	}
}

//prototype function to trim strings
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, "");
}

//alert and focus in validations
function chkEmpty(el,msg){
	if(el.value.trim() == ""){
		message($(el),msg);
		return false;
	}else{
		el.className = 'frmreq';
		return true;
	}
}

//alert msg which is called by chkEmpty
function message(el,msg){
	alert(msg);
	if(el.type != 'radio' && el.type != 'checkbox')
		el.className = 'required';
	el.focus();
	return;
}

//window onload event - this isbetter than assigning directly to window.attach 
function addLoadEvent(func) {   
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func;
    } else {
        window.onload = function(){
        oldonload();
        func();
        }
    }
}

//common event handlers
function setupHovers(){
	var els = document.getElementsByClassName('gallery_image');

	for(var i=0;i<els.length;i++){
		var baseClass = els[i].className;
		Event.observe(els[i],'mouseover',function(){this.className = baseClass+'_hover';});
		Event.observe(els[i],'mouseout',function(){this.className = baseClass;});
	}
}

//ajax image loader
function setLoader(el){
	$(el).innerHTML = '<img src="/images/loader.gif" border="0" title="Loading ...">';	
	return;
}


//ajax div updater
function ajax_update_vars(el,page,vars){
	setLoader(el);
	new Ajax.Updater(el,page,{asynchronous:true,evalScripts:true,parameters:vars,method:'post'});	
	return;
}


//ajax request no updater
function ajax_request(page,vars){
	new Ajax.Request(page,{asynchronous:false,parameters:vars,method:'post'});	
	return;
}
function ajax_arequest(page,vars){
	new Ajax.Request(page,{asynchronous:true,parameters:vars,method:'post'});	
	return;
}

//this is the helper for pagethru_ajax custom tag
function setupPaging(form,page){
	var sortform = $(form);
	sortform.currentpage.value = page;
	sortform.onsubmit();
}

//ie menu need this to attach hovers in ie
sfHover = function() {
	var sfEls = document.getElementById("topnav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" menuhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" menuhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);