/* Newsletter */
function togDisp(e){

	stopB(e);

	var elems = document.getElementsByName('newsletterSpan');
	
	for(var i = 0; i < elems.length; i++){

		var obj = elems[i];
		var dp = "";
		
		if(obj.style.display == ""){
			dp = "none";
		}
		
		obj.style.display = dp;
	}
	
	return false;
}

function staticDisp(e){

	stopB(e);

	var elems = document.getElementsByName('newsletterSpan');
	
	for(var i = 0; i < elems.length; i++){

		var obj = elems[i];
		var dp = "";
		
		//if(obj.style.display == ""){
		//	dp = "none";
		//}
		
		obj.style.display = dp;
	}
	
	return false;
}

function stopB(e){
	if(!e)
		e = window.event;
		
	e.cancelBubble = true;
}

document.onclick = function(event){
	var elems = document.getElementsByName('newsletterSpan');

	if(elems){
		if(elems[0].style.display == ""){
			togDisp(event);
		}		
	}

}

document.onclick = function(event){
	var elems = document.getElementsByName('login');

	if(document.getElementById("login"))
	{

		if(elems[0].style.display == ""){
			togDispLogin(event);
		}

	}

}

function togDispLogin(e){

	stopBLogin(e);

	var elems = document.getElementsByName('login');
	
	for(var i = 0; i < elems.length; i++){

		var obj = elems[i];
		var dp = "";
		
		if(obj.style.display == ""){
			dp = "none";
		}
		
		obj.style.display = dp;
	}
	
	return false;
}

function stopBLogin(e){
	if(!e)
		e = window.event;
		
	e.cancelBubble = true;
}


function login(theElement){

	var theForm = theElement.form, z = 0;
	var user_name = document.getElementById('user_name').value;
	var password = document.getElementById('password').value;

	var errorFlag = 0;
	var elems;

	var url;
	var response;
	
	//alert (user_name);
	//alert (password);
	
	if (user_name == "" || password == "") {
		errorFlag = 1;
	}


	// creates a new XMLHttpRequest object to talk to the web server
	var xmlHttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (e2) {
	    xmlHttp = false;
	  }
	}
	@end @*/

	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
	  xmlHttp = new XMLHttpRequest();
	}

		
	if(errorFlag == 0){

	  // build URL to connect to
	  url = "login.php?user_name=" + escape(user_name) + "&password=" + escape(password);
		
	  // open a connection to the server
	  xmlHttp.open("POST", url, true);
	
		//alert(url);
		
	  // setup a function for the server to run when it is done
	  xmlHttp.onreadystatechange = getLoginState;

			
	  // send the request
	  xmlHttp.send(null);
	

	} else {

		alert ("Please enter a valid User Name and Password.");

	}
	
	
	function getLoginState() {

	
		  if (xmlHttp.readyState == 4) {

				 // if "OK"
				if (xmlHttp.status != 200)
				{
					alert("Problem logging in.")

				} else {
	
			    response = xmlHttp.responseText;
			    //alert(response);
			    
			    if(response == user_name){

			    	window.location.reload();
			    } else {
						alert("Invalid User Name or Password.")
			    }

	
				} // end status check
	    
		  } // end readyState check
	
		} // end getLoginState()	
	
}


function checkUncheckAll(theElement) {
	
	var theForm = theElement.form, z = 0;

	for(z=0; z < theForm.length; z++){
		
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
			theForm[z].checked = theElement.checked;
		}
	}
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}

function subscribe(theElement){

	var theForm = theElement.form, z = 0;
	var emailAddress = 	document.getElementById('email').value;
	var errorFlag = 0;
	var somethingIsCheckedFlag = 0;
	var groupID;
	var elems;

	var url;
	var response;

	var groupIDArray = new Array();
	var groupIDArrayCounter = 0;
	
	// creates a new XMLHttpRequest object to talk to the web server
	var xmlHttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (e2) {
	    xmlHttp = false;
	  }
	}
	@end @*/

	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
	  xmlHttp = new XMLHttpRequest();
	}



	
	//alert(emailAddress);
	
	if(!isValidEmail(emailAddress)){
		alert("Invalid email address. Please enter a valid email address.");
		errorFlag = 1;
	}

	
	for(z = 0; z < theForm.length; z++){
		
		if(theForm[z].type == 'checkbox' && theForm[z].checked && theForm[z].name != 'checkall' && errorFlag != 1){
			
			somethingIsCheckedFlag = 1;

			//alert(theForm[z].name);
			//alert(theForm[z].id);
			//alert(theForm[z].checked);

			groupID = theForm[z].id;

			groupIDArray[groupIDArrayCounter] = groupID;
			groupIDArrayCounter++;

		} // end if
	} // end for

	if(somethingIsCheckedFlag == 0 && errorFlag == 0){
		alert("A newsletter group is not selected. Please select a newsletter group.");

	}
		
	if(errorFlag == 0 && somethingIsCheckedFlag == 1){
		groupIDArrayCounter--;
		groupID = groupIDArray[groupIDArrayCounter];

	  // build URL to connect to
	  url = "newsletter_subscribe.php?group_id=" + escape(groupID) + "&recipient_email=" + escape(emailAddress);
		//alert("before open" + url);
		
	  // open a connection to the server
	  xmlHttp.open("POST", url, true);
	
		//alert("before status: " + url);
		
	  // setup a function for the server to run when it is done
	  xmlHttp.onreadystatechange = getSubscribeState;

		//alert("after status: " + url);

		//alert("before send");
			
	  // send the request
	  xmlHttp.send(null);
	
		
		//alert("url: " + url);
	
		


		alert("You have subscribed successfully.");
	
		// hide dropdown
		elems = document.getElementsByName('newsletterSpan');
		elems[0].style.display = "none";


	}
	
	
	function getSubscribeState() {
			
			//alert("asdasd");
	
		  if (xmlHttp.readyState == 4) {

				 // if "OK"
				if (xmlHttp.status != 200)
				{
					alert("Problem updating.")
				} else {
	
			    response = xmlHttp.responseText;
			    //alert(response);
					
					if (groupIDArrayCounter > 0){
					
						groupIDArrayCounter--;
						groupID = groupIDArray[groupIDArrayCounter];
					
					  // build URL to connect to
					  url = "newsletter_subscribe.php?group_id=" + escape(groupID) + "&recipient_email=" + escape(emailAddress);
						//alert(url);
						
					  // open a connection to the server
					  xmlHttp.open("POST", url, true);
					
					  // setup a function for the server to run when it is done
					  xmlHttp.onreadystatechange = getSubscribeState;
					
					  // send the request
					  xmlHttp.send(null);							
						
					}
	
				} // end status check
	    
		  } // end readyState check
	
		} // end getSubscribeState()	
	
}





function clickClear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function clickRecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}