//specific to search form on top of all pages
function validate_sform(f){

	//var keywordSearch=(f.keywordsearch !=null && f.keywordsearch.checked)

	var keywordSearch=false
	var i=0

	for (i=0;i<f.keywordsearch.length;i++) {
		
		if (f.keywordsearch[i].value==1 && f.keywordsearch[i].checked==true) {
			keywordSearch=true
			break
		}		

	}


		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 	= 785;
	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;
	}
}

function chkSelect(el,msg) {
	if (el.options[el.selectedIndex].value=="") {
		message(el,msg);
		return false;	
	} else {
		el.className =  'text';
		return true;
	}
}

function chkMultiSelect(el,msg) {
	var i=0
	var isSelected=false
	for (i=0;i<el.options.length;i++) {
		if (el.options[i].selected == true && el.options[i].value != "" ) {
			isSelected=true;
			break;
		}
	}
	if (!isSelected) {
		message(el,msg);
		return false;
	} else {
		el.clasName = 'text';
		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){
	document.getElementById(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 = document.getElementById(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);***/

//This is the function that sets the story size for a story
function setStoryFontSize(id, newClass){
	$(id).className = newClass;
}


function getContinuousNewsPage(content_id,page,zone){
	if(page%2 == 1){
		new Ajax.Updater('continuousnews'+content_id+'_odd','../content_blocks/continuous_news_ajax.cfm',{evalScripts:true,parameters:'?paged=true&content_id='+content_id+'&page='+page+'&zone='+zone, onSuccess:function(){
			new Effect.Fade('continuousnews'+content_id+'_even', { duration: .5, 
				afterFinish:function(){
					new Effect.Appear('continuousnews'+content_id+'_odd', { duration: .5});
			}});
		}});
	}
	else{
		new Ajax.Updater('continuousnews'+content_id+'_even','../content_blocks/continuous_news_ajax.cfm',{evalScripts:true,parameters:'?paged=true&content_id='+content_id+'&page='+page+'&zone='+zone, onSuccess:function(){
			new Effect.Fade('continuousnews'+content_id+'_odd', { duration: .5, 
				afterFinish:function(){
					new Effect.Appear('continuousnews'+content_id+'_even', { duration: .5});
			}});
		}});
	}
	
}


function submitForm(form_id, verify){
	res = form_validate($('form_ajx_'+form_id),verify);
	if(res == true){
		$('form_ajx_'+form_id).submit(); 
		return true;
	}
	return false;
}

function showFormResponse(form_id, response){
	$('form_general_'+form_id).innerHTML = "<p>"+response+"</p>"; 
}