// JavaScript Document
function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function findWidth(obj){
	xPos = obj.offsetWidth;
	return xPos;
}

//Start of drop down script
var subMnus="";
function showMenu(idx,img){
	try{
		hideAllMenu();
		var dv=document.getElementById("sub"+idx);
		if(idx==5)
			dv.style.left=(findPosX(document.getElementById(img))+findWidth(document.getElementById(img)))-findWidth(dv);
		else
			dv.style.left=findPosX(document.getElementById(img));
		dv.style.top=findPosY(document.getElementById(img))+15;
		dv.style.visibility="visible";
	}
	catch(e){}
}

//This function is to hide the dropdown menu
function hideAllMenu(){
	try{
		var ar=subMnus.split(',');
		for(i=0;i<ar.length;i++){
			var dv=document.getElementById("sub"+ar[i]);
			dv.style.visibility="hidden";
		}
	}
	catch(e){}
}
//End of drop down script

// This function checks to see if a selection was made in a checkbox group
function cbxSelectionMade(cbx){
	var isGood=false;
	if(cbx.checked){
		isGood=true;
	}
	for(i=0;i<cbx.length && !isGood;i++){
		if(cbx[i].checked){
			isGood=true;
		}
	}
	return isGood;
}

// This function checks to see if a selection was made in a dropdown box
function cboSelectionMade(cbo){
	var isGood=false;
	
	if(cbo.options[cbo.selectedIndex].value!="")
		isGood=true;
		
	return isGood;
}

// This function is used to validate email entries
function isValidEmail(thisEmail){
	var emailexp = /.*\@.*\..*/;
	if (!emailexp.test(thisEmail)) {
		return false;
	} 
	return true;
}

// function checks a date value to make sure its valid
function isValidDate(aDate) {
	var dateexp = /^(\d{1,2}\/\d{1,2}\/\d{4})$/;
    if (!dateexp.test(aDate)) { return false; } 	
	var	temp = new String(aDate), m = 0, d = 0;
	
	for (i=0; i < temp.length; i++){ 
		var str = temp.charAt(i); 
		if (str == "/") { if (m == 0) { m = i; } else { d = i; break; }}			
	}
	
	var month = parseInt(temp.substring(0, m), 10);
	var day = parseInt(temp.substring(m + 1, d), 10); 
	var year = parseInt(temp.substring(d + 1, temp.length), 10);								
					
	switch (month) {
		case 1:	case 3:	case 5:	case 7:	case 8:	case 10: case 12:					
			if ((day < 1) || (day > 31)) {  return false;	} break;
		case 2:		
			if ((year % 400 == 0) || (year % 4 == 0)) {	if ((day < 1) || (day > 29)) {  return false;	} break}
			if ((day < 1) || (day > 28)) {  return false; } break;
		case 4:	case 6:	case 9: case 11:
			if ((day < 1) || (day > 30)) {	 return false; } break;
		default: return false;			
	}		
	return true;		
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//This function is to validate feedback form
function validateFeedback(thisForm){
	if(!isValidEmail(thisForm.userEmailAddress.value)){
		alert("Please provide a valid email address.");	
		thisForm.userEmailAddress.focus()
		return false;
	}
	
	if(thisForm.userComments.value==""){
		alert("Please fill out your comments.");	
		thisForm.userComments.focus()
		return false;
	}
	
	return true;
}

// function to validate subscription submissions
function validateSubscription(thisForm,mode){
	var errstr="";
	var ret=true;
	if(thisForm.fname.value==""){
		errstr+="<li class='errTxt'>Please provide your first name.</li>";	
		document.getElementById("imgFError").src="images/error.png"
		ret=false;
	}
	else{
		document.getElementById("imgFError").src="images/valid.png"
	}
	if(!isValidEmail(thisForm.email.value)){
		errstr+="<li class='errTxt'>Please provide a valid email address.</li>";
		ret=false;
		document.getElementById("imgEError").src="images/error.png"
	}
	else{
		document.getElementById("imgEError").src="images/valid.png"
	}
	
	if(mode==1){
		var nochkbx=true;
		/*if(cbxSelectionMade(thisForm.subcats)){
			nochkbx=false;
		}*/
		if(cbxSelectionMade(thisForm.subregs)){
			nochkbx=false;
		}
		if(cbxSelectionMade(thisForm.sublocs)){
			nochkbx=false;
		}
		if(nochkbx){
			errstr+="<li class='errTxt'>Please choose the type of email notification you would like to receive.</li>";
			ret=false;
			document.getElementById("imgCError").src="images/error.png"
		}
		else{
			document.getElementById("imgCError").src="images/valid.png"
		}
	}
	
	if(!ret){
		document.getElementById("subscribeErrors").innerHTML="The following errors were found in your subscription.<ul>"+errstr+"</ul>";
		scroll(0);
	}
	else{
		document.getElementById("subscribeErrors").innerHTML="&nbsp;";
	}
	return ret;
}

function validateSubShort(thisForm,mode){
	if(thisForm.fname.value==""||thisForm.fname.value=="First Name"){
		alert("Please provide your first name.");
		thisForm.fname.focus;
		return false;
	}
	if(!isValidEmail(thisForm.email.value)){
		alert("Please provide a valid email address.");
		thisForm.email.focus;
		return false;
	}
	
	return true;
}

// checks or unchecks region locations
function checkKids(idstart,idend,val){
	var cbx=document.fsubscription.sublocs;
	for(i=idstart;i<idend;i++){
		cbx[i].checked=val;
	}
}

// checks region locations
function checkAllSubscriptions(val){
	/*var cbx=document.fsubscription.subcats;
	cbx.checked=val;
	for(i=0;i<cbx.length;i++){
		cbx[i].checked=val;
	}*/
	cbx=document.fsubscription.subregs;
	cbx.checked=val;
	for(i=0;i<cbx.length;i++){
		cbx[i].checked=val;
	}
	cbx=document.fsubscription.sublocs;
	cbx.checked=val;
	for(i=0;i<cbx.length;i++){
		cbx[i].checked=val;
	}
}

// function to validate contest submissions
function validateContestSignUp(thisForm){
	var errstr="";
	var ret=true;
	if(thisForm.entryName.value==""){
		errstr+="<li class='errTxt'>Please provide your name.</li>";	
		document.getElementById("imgCNError").src="images/error.png"
		ret=false;
	}
	else{
		document.getElementById("imgCNError").src="images/valid.png"
	}
	if(!isValidEmail(thisForm.entryEmail.value)){
		errstr+="<li class='errTxt'>Please provide a valid email address.</li>";
		ret=false;
		document.getElementById("imgCEError").src="images/error.png"
	}
	else{
		document.getElementById("imgCEError").src="images/valid.png"
	}
	if(thisForm.imageVerify.value==""){
		errstr+="<li class='errTxt'>Please enter the security code.</li>";
		ret=false;
		document.getElementById("imgVImgError").src="images/error.png"
	}
	else{
		document.getElementById("imgVImgError").src="images/valid.png"
	}
	/*var phoneexp = /^(\(?\d{3}\)?)?\s?\d{3}[\s\-]?\d{4}$/;
	if(!phoneexp.test(thisForm.entryPhone.value)){
		errstr+="<li>Please enter a valid phone number on no phone number.</li>";
		ret=false;
		document.getElementById("imgCPError").src="images/error.png"
	}
	else{
		document.getElementById("imgCPError").src="images/valid.png"
	}*/
	if(!ret){
		document.getElementById("contestSubErrors").innerHTML="The following errors were found in your sign up information.<ul>"+errstr+"</ul>";
		scroll(0);
	}
	else{
		document.getElementById("contestSubErrors").innerHTML="&nbsp;";
	}
	return ret;
}


function showLocate(location){
	if(document.getElementById(location).style.height == '1px'){
		document.getElementById(location).style.height = '';
		document.getElementById('img'+location).src = 'images/iconarrowdown.png';
	}else{
		document.getElementById(location).style.height = '1px';
		document.getElementById('img'+location).src = 'images/iconarrowr.png';
	}
}

// function to validate contest submissions
function validateShare(thisForm){
	var errstr="";
	var ret=true;
	if(thisForm.shareName.value==""){
		errstr+="<li class='errTxt'>Please provide your name.</li>";	
		document.getElementById("imgSNError").src="images/error.png"
		ret=false;
	}
	else{
		document.getElementById("imgSNError").src="images/valid.png"
	}
	if(!isValidEmail(thisForm.shareEmail.value)){
		errstr+="<li class='errTxt'>Please provide a valid email address for your friend.</li>";
		ret=false;
		document.getElementById("imgSEError").src="images/error.png"
	}
	else{
		document.getElementById("imgSEError").src="images/valid.png"
	}
	
	if(!ret){
		document.getElementById("shareSubErrors").innerHTML="The following errors were found in your the information provided.<ul>"+errstr+"</ul>";
		document.location="#shareFormAnchor";
		//scroll(0);
	}
	else{
		document.getElementById("shareSubErrors").innerHTML="&nbsp;";
	}
	return ret;
}

function go(url, pars){
	var ach = 'earl='+url+'&'+pars;
	var boo = new Ajax.Updater('blah', 'sendgetres.cfm' , {method: 'get', parameters:ach
							   , onSuccess: function(){document.getElementById('dosub').style.display = 'none';document.getElementById('donesub').style.display = 'inline';}
							   });
}



//this function writes the swf for index
function writeSWF(divName,swfName,w,h){	
	var str='<object id="'+divName+'_swf" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="'+w+'" height="'+h+'">';
	str+='<param name="movie" value="'+swfName+'">';
	str+='<param name="quality" value="high">';
	str+='<param name="wmode" value="opaque">';
	str+='<param name="menu" value="false">';
	str+='<embed name="'+divName+'_swf" src="'+swfName+'" quality="high" wmode="opaque" menu="false" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'"></embed>';
	str+='</object>';
	document.getElementById(divName).innerHTML=str;
}