//RadioTemplate 2006 Common Javascript
//TITLE TAGGER - migrated from XML/XSL titletagger

function titleTagger(){
//testing only
//network = "CBC Radio";
//showName = "Name of Show";
//ee
//targ = document.getElementById('leftnav');
		//for (a=1;a<zonesActive.length;a++){
		//alert (zonesActive[a]);
		//}


}


//LEFT NAVIGATION - this script sets the active zone for the current page, based on the query string it receives
function queryStringToActive(){
								//get the query string
								var query = window.location.search.substring(1);
										if(query.match("-sub")){
											//alert("SUB");
											//var zo = toString("zradioprogram-"+query);
											var zo = "zradioprogram-"+query;
											zonesActive.push(zo);
											var n1=document.getElementById(zo).parentNode.parentNode;
											var n2=document.getElementById(zo).parentNode.parentNode.parentNode.parentNode;
											n1.className="active";
									    	n2.className="active";
											}
											
											
											else{
												//NO QUERY STRING - set the active zone as MAIN (index page)
											if (query!==""){
														zonesActive.push("zradioprogram-"+query);
											}
											else {zonesActive.push("zradioprogram-copy-index");}
											}
									var blogcheck = document.location.href;
											//alert(blogcheck);
									if (blogcheck.match("blog/")){
									//zonesActive.splice("zradioprogram-blog");
									//zonesActive.length=0;
									
									//zonesActive="";
									
									zonesActive.splice(0,(zonesActive.length));
									zonesActive.push("zradioprogram-blog");
									}
				zonesActivate();
				//titleTagger();
				//set the Dropdown to the correct spot each time
				setDD();
}

function setDD(){
document.getElementById('radioProgs').options.selectedIndex = document.getElementById('radioProgs').options[0];
}

//YAHOO YUI CODE
//DROPDOWN SELECTOR FOR SHOW FINDER

function successHandler(o){
	var scha = document.getElementById('dropDown');
	// This is a reference to the HTML form.
	var oForm = document.getElementById('programSel');
	scha.innerHTML=o.responseText;

}


function failureHandler(o){
alert("fail!");
	scha.innerHTML = o.status + " " + o.statusText;
}

var oCallback = {
	success: successHandler,
	failure: failureHandler
}


function getModule(locArg){
//alert("trying with" + locArg);
	var sLocation = locArg;
	// entryPoint is the base URL
	var entryPoint = '';
	var sUrl= '/common_radio/includes/'+sLocation+'.html';
	// Initiate the HTTP GET request.
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, oCallback);
	document.getElementById("radioProgs").focus;
}
//
//
//
//
//SHOW SELECTOR - navigates to the given show via the dropdown box in the masthead of each page
function visitProgram(){
	//get the program's name
	var progName = document.programSel.radioProgs.options[document.programSel.radioProgs.selectedIndex].value;

		//check to see if user is paicking a DD or a show selection
		if (progName.match("dd-")){
		//alert ("this is a DD!");
		getModule(progName);
		}
			else {
			
			//put it together as a URL
			var pathName = "http://www.cbc.ca/";
			var dest = pathName + progName;
		
			//go visit the show
			location.href=dest;
			}
	
	}
//FORM VALIDATION for standard feedback forms
function validate(){
formMessage="";
flag=0;
//alert("Validating");
valEmail();
valTextFields();
valDropDowns();
valBoxes();	

//alert(flag);
if (flag===0){return true;}else {

validMessage();
return false;}

	}
//textfield validator
function valTextFields(){
	for (c=0;c<=fields.length-1;c++){
	
	 var TEX=fields[c];
		 if(document.getElementById(TEX).value.length===0){
		 //alert("boxcheck!");
		     formMessage+="Please enter some text in the "+fieldsExt[c]+" field. <br/>";
			 flag+=1;
			 //alert("textfield validator+1");
			 }
	
	}

}
//email validator

function valEmail(){
	for (d=0;d<=email.length-1;d++){
		 var EEM=email[d];
		 if(document.getElementById(EEM).value.length===0){
		//alert("boxcheck!");
		     formMessage+="Please enter an address in the "+emailExt[d]+" field. <br/>";
			 flag+=1;
			  //alert("textfield validator+1");
			 }
			 else {
						 var x = document.getElementById(EEM).value;
			 // alert("E_CHECK" + x);
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(x)) { formMessage+="Please enter a valid address in the "+emailExt[d]+" field. <br/>"; flag+=1;}
	
			 }
	

	}

}
//dropdown validator
function valDropDowns(){
//alert("ready for "+dropdowns.length+" dds");
	for (a=0;a<=dropdowns.length-1;a++){
	 var DORP=dropdowns[a];
	 //alert("this is dorp"+DORP);
		if (document.getElementById(DORP).options.selectedIndex===0){
		//alert ("Please select an option for"+dropdownsExt[a]);
		formMessage+=dropdownsExt[a]+" needs a selection. <br/> ";
		flag+=1;
		}
		
	}
}

//textbox validator
function valBoxes(){
//alert("ready for"+boxes.length+"boxes");
	for (b=0;b<=boxes.length-1;b++){
	
	 var BOGS=boxes[b];
	//alert("Bogs= "+BOGS);
		 if(document.getElementById(BOGS).value===""){
		//alert("boxcheck!"+document.getElementById(BOGS));
		     formMessage+="The "+boxesExt[b]+" box is empty. <br/>";
			 flag+=1;
			 }
			
	}
}




function validMessage(){
document.getElementById('warnings').innerHTML="<h4>Incomplete Form</h4><p class=\"alert\">The following fields require your attention: <br />"+formMessage+"</p>";
}

//RADIO PLAYER FOR ALL RADIO SITES - NEW TEMPLATE
function radioPlayer(mediaFile){

var fullURL = "http://www.cbc.ca/common_radio/includes/radio_player.html?"+mediaFile;
window.open(fullURL, "codWindow", "location=0,menubar=0,resizable=0,width=370,height=10").focus();

}