var clients = new Array;
clients[0]='<a href="http://www.democrats.org/" title="Democratic National Committee" target="_blank"><img src="images/client-democratic.png" width="248" height="89" alt="Democratic National Committee" title="Democratic National Committee" /></a>';
clients[1]='<a href="http://doctorswithoutborders.org/" title="Doctors Without Borders" target="_blank"><img src="images/client-doctors.png" width="248" height="89" alt="Doctors Without Borders" title="Doctors Without Borders" /></a>';
clients[2]='<a href="http://www.plannedparenthood.org/" title="Planned Parenthood" target="_blank"><img src="images/client-planned.png" width="248" height="89" alt="Planned Parenthood" title="Planned Parenthood" /></a>';
clients[3]='<a href="http://www.worldwildlife.org/" title="World Wildlife Fund" target="_blank"><img src="images/client-wildlife.png" width="248" height="89" alt="World Wildlife Fund" title="World Wildlife Fund" /></a>';
clients[4]='<a href="http://www.sierraclub.org/" title="Sierra Club" target="_blank"><img src="images/client-sierra.png" width="248" height="89" alt="Sierra Club" title="Sierra Club" /></a>';
var index = Math.floor(Math.random()*clients.length);

function flashInstanciate() {
	var flashvars = {};
	var params = {};
	var attributes = {};
	attributes.id = "flash-header";
	params.wmode = "transparent";
	params.loop = "true";
	swfobject.embedSWF("flash/banner.swf", "flash-banner", "923", "234", "9.0.0", false, flashvars, params, attributes);
	if(document.getElementById('home-clients-box')){
		loadLogoImages();
		clientRotate(index);
	}
}

function changeHTML (output, id){
	document.getElementById(id).innerHTML=output;
}

function clientRotate(index) {
	changeHTML(clients[index],'home-clients-box');
	index++;
	if(index == (clients.length)) {
		index = 0;
	}
	var callFunction= "clientRotate("+index+")";
	setTimeout(callFunction, 3000);
}

//FINDS PAGE NAME OF CURRENT PAGE
function pageName() {
	var URL = window.location.toString();
	var breakdown = URL.split("/");
	return (breakdown[(breakdown.length-1)]);
}

//FOR CONTACT.PHP
function checkForm(theform) {
	var returnval=true //by default, allow form submission
	var checkit = document.getElementById('required_fields').value;
	var requireds=theform.required_fields.value.split(";") //split using blank space as delimiter
	for (i=0; i<requireds.length; i++){
		var fieldnamez = requireds[i];
		theform[fieldnamez].style.border = ''
		if(fieldnamez.length > 1){
			if (theform[fieldnamez].type=="text" || theform[fieldnamez].type=="textarea" || theform[fieldnamez].type=="file"){
				if (theform[fieldnamez].value==""){ //if empty field
					alert("Please make sure all required fields are filled out.") //alert error message
					theform[fieldnamez].style.border = '1px solid #F15A22'
					theform[fieldnamez].focus();
					returnval=false //disallow form submission
					break //end loop. No need to continue.
				}
			}
			if (theform[fieldnamez].type=="select-one"){
				if (theform[fieldnamez].value==""){
					alert("Please make sure all required fields are filled out.") //alert error message
					theform[fieldnamez].style.border = '1px solid #F15A22'
					theform[fieldnamez].focus();
					returnval=false //disallow form submission
					break //end loop. No need to continue.
				}
			}
			if(theform[fieldnamez].name=="email"){
				var str = theform.email.value
				var at="@"
				var dot="."
				var comma=","
				var lat=str.indexOf(at)
				var lstr=str.length
				var ldot=str.indexOf(dot)
				if (str.indexOf(at)==-1){
					alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
				}
				if (str.indexOf(comma)>0){
					alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
				}
				if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
   			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(at,(lat+1))!=-1){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(dot,(lat+2))==-1){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(" ")!=-1){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
				}
			}
		}
		theform[fieldnamez].style.border = ''
	}
	return returnval; 
}

//PRELOADER FOR CSS HOVER ITEMS
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 loadLogoImages() {
	mm_preloadimages('images/client-democratic.png','images/client-doctors.png','images/client-planned.png','images/client-wildlife.png','images/client-sierra.png');
}

function init() {
	flashInstanciate();
}

window.onload=init;
