// Javascript Document
function addToMailingList() 
{		
	new Ajax.Request('/admin/mailinglist.php', 
					 {
					 	method: 'post',
		 			  	parameters: {cmd:'addemail', email:$('emaillist').value },
						onLoading: function() { printProcessWindow('Adding you to our mailing list...'); },
						onComplete: function() { closeProcessWindow(); },
						onSuccess: function(transport)
						{
							alert('Thank you for joining our mailing list.\n You will recieve a confirmation email asking you to verify your email address within the next 30 minutes.');
						},
   						onFailure: function(transport)
						{ 
							//alert('Failed');
							if(404 == transport.status)
								alert('Your email address is already in our system or is invalid.\nPlease Choose a different email address or make sure your email address is correct.');
							if(409 == transport.status)
								alert('Your password is incorrect. Please Try again.');
							if(422 == transport.status)
								alert(transport.responseText);
						}
	 				 }
					 ); 
	return false;
}

function recentSponsorVideos() 
{		
	new Ajax.Request('/admin/locator.php', 
					 {
					 	method: 'post',
		 			  	parameters: {action:'getvideos' }
	 				 }
					 ); 
}

function checkState()
{
	if($('gsstate').value == 'State' || $('gsstate').value == '')
	{
		alert("You must select a state");
		return false;
	}
	else
		return true;
}

function checkFields()
{
	if(($('gscity').value == 'City or Zipcode' || $('gscity').value == '') && ($('gsstate').value != 'State' || $('gsstate').value != '') )
	{
		alert("You must enter a city or zipcode.");
		return false;
	}
	else
		return true;
}

function updateButtons()
{
	new Ajax.Request('/search/googlemap_search.php',
					 {
						method: 'post',
						parameters: { action: 'updatebuttons', state: $('gsstate').value }
					 });
}


function goToState(area,url)
{
	if(url != "")
		location.href= area+'/'+url+'/'
}

function courseMembershipVideo(vd)
{
	vid = ($('videoContainercms'))?$('videoContainercms'):$('videoContainer');
	aswindow_width = get_document_window_width();
	aswidth = convert_width(500,aswindow_width);
	aswindow_height = get_document_window_height();
	asheight = convert_height(305,aswindow_height);
	asleft = calculate_left(aswidth,aswindow_width);
	astop = calculate_top(asheight,aswindow_height);
	vid.style.left = asleft+'px';
	vid.style.top = 250+'px';
	vid.style.display = '';
	$('video').innerHTML = AC_FL_PreloadContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width','320','height','240','movie',vd,'quality','high','allowfullscreen','true','wmode','transparent','autostart','true' );
		
}

function hideMemVideo()
{
	vid = ($('videoContainercms'))?$('videoContainercms'):$('videoContainer');
	vid.style.display = 'none';
	$('video').innerHTML = '';
}

function get_document_window_width()
{
   if (document.body && document.body.clientWidth)
      return document.body.clientWidth;
   else if (window && window.innerWidth) return window.innerWidth;
   else if (document.documentElement && document.documentElement.offsetWidth)
      return document.documentElement.offsetWidth;
   else if (document.body && document.body.offsetWidth)
      return document.body.offsetWidth;
   return 0;
}

function get_document_window_height()
{
   if (document.body && document.body.clientHeight &&
       (document.body.clientHeight > 1)) return document.body.clientHeight;
   else if (window && window.innerHeight) return window.innerHeight;
   else if (document.documentElement && document.documentElement.offsetHeight)
      return document.documentElement.offsetHeight;
   else if (document.body && document.body.offsetHeight)
      return document.body.offsetHeight;
   return 0;
}

function convert_width(width,window_width)
{
   if (width == null) width = window_width + 52;
   else if (typeof(width) == 'string') {
      var percent_pos = width.indexOf('%');
      if (percent_pos != -1)
         width = window_width * (width.substr(0,percent_pos) / 100);
   }
   if (! document.all) {
      if (width > (window_width - 25)) width = window_width - 25;
   }
   else if (width > (window_width - 10)) width = window_width - 10;
   return width;
}

function convert_height(height,window_height)
{
   if (height == null) height = window_height;
   else if (typeof(height) == 'string') {
      var percent_pos = height.indexOf('%');
      if (percent_pos != -1)
         height = window_height * (height.substr(0,percent_pos) / 100);
   }
//   else height += header_offset;
   if (height > (window_height - 40)) height = window_height - 40;
   return height;
}

function calculate_left(width,window_width)
{
   var left = (window_width - width) / 2;
   if (! document.all) left += 10;
   if (left < 0) left = 0;
   return left;
}

function calculate_top(height,window_height)
{
   var top = ((window_height - height) / 2) - 3;
   if (top < 0) top = 0;
   return top;
}

function changeImage(dir,cid,total){
	//find current least unhidden;
	var currentHigh=false;
	var currentLow =false;
	var next=false;
	var problems="dir: "+dir+", cid: "+cid+", total: "+total+"<br>";
	for(var i=0; i<=total; i++){
		if($(cid+'_image_'+i)){
			if($(cid+'_image_'+i).style.display == "block"){
				currentLow=$(cid+'_image_'+i);
				problems+="currentLow: "+cid+'_image_'+i+"<br>";
				break;
			}
		}
	}
	//if fail to find an element -- should not happen;
	if(currentLow==null || currentLow==false){
		alert('ERROR 101: Failed to find displayed image.');
		return false;
	}
	var currhighi = i+1;
	currentHigh= $(cid+'_image_'+currhighi)
	problems+="currentHigh: "+cid+'_image_'+currhighi+"<br>";
	if(currentHigh==null || currentHigh==false){
		alert('ERROR 102: Failed to find displayed image.');
		return false;
	}
	if(dir>0){
		dir++;
		currentLow.style.display="none";
		problems+="currentLow is now display:none<br>";
	}else{
		currentHigh.style.display="none";
		problems+="currentHigh is now display none<br>";
	}
	var itoUnhide = i+dir;
	problems+="itoUnhide is now: "+itoUnhide+" ("+i+"+"+dir+")<br>";
	problems+=itoUnhide+">"+total+"<br>";
	if(itoUnhide>total){
		itoUnhide=1;
		currentHigh.style.display="none";
		$(cid+'_image_0').style.display="block";
		problems+="currentHigh is now display none, itoUnide=1 AND "+cid+"_image_0 is now display:block;<br>";
	}
	problems+=itoUnhide+"<=0<br>";
	if(itoUnhide<=0){
		itoUnhide=total-1;
		currentLow.style.display="none";
		$(cid+'_image_'+total).style.display="block";
		problems+="currentLow: isn ow display none, itoUnhide is now "+(total-1)+" AND "+cid+"_image_"+total+"is now display:block;<br>";
	}
	next = $(cid+'_image_'+itoUnhide);
	//error handling;
	if(next==null || next==false){
		alert('ERROR 103: Failed to find displayed image.');
	}else{
		next.style.display="block";
	}
	problems+="<br>";
	$(cid+'_showerr').innerHTML=$(cid+'_showerr').innerHTML+problems;
	return true;
}