var dirtyBit = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
var httpRegEx = /^(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$/i;

function setDirtyBit()
{
	dirtyBit = true;
}


function good2Go()
{
	if(dirtyBit)
	{
		return confirm('You have already made changes on this page. Are you sure you want to continue and lose these changes? Click \"OK\" to proceed without saving your changes.');
	}
	return true;
}

function cleanArrayStr(str)
{
	//replace characters that are gonna screw up the trickiness below...
	str = str.replace(/http:\/\//, "http_prefix");
	str = str.replace(/https:\/\//, "https_prefix");
	str = str.replace(/\|/, " ");
	str = str.replace(/:/, " ");
	str = str.replace(/;/, " ");
	return str;
}

function myLoginSubmit()
{
	var myForm		= document.forms["loginForm"];
	if(myForm.elements["memberLogin"].value == "" || myForm.elements["memberPW"].value == "")
	{
		alert("Empty credentials are not allowed. Please fill in both the username and the password to login.");
		return false;
	}
	return true
}

function mySubmit()
{
	var myForm		= document.forms["memberForm"];
	var lpArray	= "";
	if(myForm.elements["pword"].value.length < 6)
	{
		alert("Passwords must be at least six characters long.");
		return false;
	}
	if(myForm.elements["pword"] != null && myForm.elements["pword_confirm"].value != null )
	{
		if(myForm.elements["pword"].value != myForm.elements["pword_confirm"].value)
		{
			alert("Passwords don't match.");
			return false;
		}
	}
	
	if(myForm.elements["lp_lifetimePaymentID"] != null)
	{
		for(j=0;j<myForm.elements["lp_lifetimePaymentID"].length; j++)
		{
			lpArray		+= "lifetimePaymentID:" + cleanArrayStr(myForm.lp_lifetimePaymentID[j].value);
			lpArray		+= "|memberID:" + cleanArrayStr(myForm.lp_memberID[j].value);
			lpArray		+= "|payDate:" + cleanArrayStr(myForm.lp_payDate[j].value);
			lpArray		+= "|comment:" + cleanArrayStr(myForm.lp_comment[j].value) + ";";
		}
		myForm.elements["lifetimePayments"].value = lpArray;
	}
	
	var addArray	= "";
	if(myForm.elements["add_addressID"].length != null)
	{
		for(j=0;j<myForm.elements["add_addressID"].length; j++)
		{
			if(myForm.add_line1[j].value == "" || myForm.add_city[j].value == "" || myForm.add_stateProvince[j].value == "" || myForm.add_postalCode[j].value == "")
			{
				alert("Some required address information is missing. Please fill in the missing information and resubmit.");
				return false;
			}
			addArray		+= "addressID:" + cleanArrayStr(myForm.add_addressID[j].value);
			addArray		+= "|isVisible:";
			if(myForm.add_isVisible[j].checked == true)
			{
				addArray		+= "1";
			}
			else
			{
				addArray		+= "0";
			}
			addArray		+= "|label:" + cleanArrayStr(myForm.add_label[j].value);
			addArray		+= "|line1:" + cleanArrayStr(myForm.add_line1[j].value);
			addArray		+= "|line2:" + cleanArrayStr(myForm.add_line2[j].value);
			addArray		+= "|line3:" + cleanArrayStr(myForm.add_line3[j].value);
			addArray		+= "|city:" + cleanArrayStr(myForm.add_city[j].value);
			addArray		+= "|stateProvince:" + cleanArrayStr(myForm.add_stateProvince[j].value);
			addArray		+= "|postalCode:" + cleanArrayStr(myForm.add_postalCode[j].value);
			addArray		+= "|country:" + cleanArrayStr(myForm.add_country[j].value) + ";";
			if(j == 0)
			{
				myForm.elements["region"].value = determineRegion(myForm.add_stateProvince[j].value, myForm.add_postalCode[j].value);
			}
		}
	}
	else
	{
		if(myForm.elements["add_addressID"] != null)
		{
			if(myForm.add_line1.value == "" || myForm.add_city.value == "" || myForm.add_stateProvince.value == "" || myForm.add_postalCode.value == "")
			{
				alert("Some required address information is missing. Please fill in the missing information and resubmit.");
				return false;
			}
			
			addArray		+= "addressID:" + cleanArrayStr(myForm.add_addressID.value);
			addArray		+= "|isVisible:";
			if(myForm.add_isVisible.checked == true)
			{
				addArray		+= "1";
			}
			else
			{
				addArray		+= "0";
			}
			addArray		+= "|label:" + cleanArrayStr(myForm.add_label.value);
			addArray		+= "|line1:" + cleanArrayStr(myForm.add_line1.value);
			addArray		+= "|line2:" + cleanArrayStr(myForm.add_line2.value);
			addArray		+= "|line3:" + cleanArrayStr(myForm.add_line3.value);
			addArray		+= "|city:" + cleanArrayStr(myForm.add_city.value);
			addArray		+= "|stateProvince:" + cleanArrayStr(myForm.add_stateProvince.value);
			addArray		+= "|postalCode:" + cleanArrayStr(myForm.add_postalCode.value);
			addArray		+= "|country:" + cleanArrayStr(myForm.add_country.value) + ";";
			myForm.elements["region"].value = determineRegion(myForm.add_stateProvince.value, myForm.add_postalCode.value);
		}		
	}
	myForm.elements["addresses"].value = addArray;
	
	
	var ciArray	= "";
	if(myForm.elements["ci_contactInfoID"].length != null)
	{
		for(j=0;j<myForm.elements["ci_contactInfoID"].length; j++)
		{
			if(myForm.ci_type[j].value == "" || myForm.ci_info[j].value == "")
			{
				alert("Your primary email address or some other required contact information is missing. Please fill in the missing information and resubmit.");
				return false;
			}
     		if((myForm.ci_type[j].value == "L" || myForm.ci_type[j].value == "E") && myForm.ci_info[j].value.search(emailRegEx) == -1) 
     		{
          		alert("Please enter a valid email address.");
          		return false;
     		}
     		if(myForm.ci_type[j].value == "U" && myForm.ci_info[j].value.search(httpRegEx) == -1) 
     		{
          		alert("Please enter a valid web page address.");
          		return false;
     		}
			ciArray		+= "contactInfoID:" + cleanArrayStr(myForm.ci_contactInfoID[j].value);
			ciArray		+= "|isVisible:";
			if(myForm.ci_isVisible[j].checked == true  || myForm.ci_isVisible[j].value == "login")
			{
				ciArray		+= "1";
			}
			else
			{
				ciArray		+= "0";
			}
			ciArray		+= "|type:" + cleanArrayStr(myForm.ci_type[j].value);
			ciArray		+= "|label:" + cleanArrayStr(myForm.ci_label[j].value);
			ciArray		+= "|info:" + cleanArrayStr(myForm.ci_info[j].value) + ";";
		}
	}
	else
	{
		if(myForm.elements["ci_contactInfoID"] != null)
		{
			if(myForm.ci_type.value == "" || myForm.ci_info.value == "")
			{
				alert("Your primary email address or some other required contact information is missing. Please fill in the missing information and resubmit.");
				return false;
			}
     		if((myForm.ci_type.value == "L" || myForm.ci_type.value == "E") && myForm.ci_info.value.search(emailRegEx) == -1) 
     		{
          		alert("Please enter a valid email address.");
          		return false;
     		}
     		if(myForm.ci_type.value == "U" && myForm.ci_info.value.search(httpRegEx) == -1) 
     		{
          		alert("Please enter a valid web page address.");
          		return false;
     		}
			ciArray		+= "contactInfoID:" + cleanArrayStr(myForm.ci_contactInfoID.value);
			ciArray		+= "|isVisible:";
			if(myForm.ci_isVisible.checked == true  || myForm.ci_isVisible.value == "login")
			{
				ciArray		+= "1";
			}
			else
			{
				ciArray		+= "0";
			}
			ciArray		+= "|type:" + cleanArrayStr(myForm.ci_type.value);
			ciArray		+= "|label:" + cleanArrayStr(myForm.ci_label.value);
			ciArray		+= "|info:" + cleanArrayStr(myForm.ci_info.value) + ";";
		}
	}
	myForm.elements["contactInfos"].value = ciArray;
	return true;
}


function myCreateSubmit()
{
	var myForm		= document.forms["memberForm"];
	var lpArray	= "";
	if(myForm.elements["lp_lifetimePaymentID"] != null)
	{
		lpArray		+= "lifetimePaymentID:" + cleanArrayStr(myForm.lp_lifetimePaymentID.value);
		lpArray		+= "|memberID:" + cleanArrayStr(myForm.lp_memberID.value);
		lpArray		+= "|payDate:" + cleanArrayStr(myForm.lp_payDate.value);
		lpArray		+= "|comment:" + cleanArrayStr(myForm.lp_comment.value) + ";";
		myForm.elements["lifetimePayments"].value = lpArray;
	}
	
	var addArray	= "";
	if(myForm.add_line1.value != "")
	{
		if(myForm.add_city.value == "" || myForm.add_stateProvince.value == "" || myForm.add_postalCode.value == "")
		{
			alert("Some required address information is missing. Please fill in the missing information and resubmit.");
			return false;
		}
		
		addArray		+= "addressID:" + cleanArrayStr(myForm.add_addressID.value);
		addArray		+= "|isVisible:";
		if(myForm.add_isVisible != null && myForm.add_isVisible.checked == true)
		{
			addArray		+= "1";
		}
		else
		{
			addArray		+= "0";
		}
		if(myForm.add_label != null)
		{
			addArray		+= "|label:" + cleanArrayStr(myForm.add_label.value);
		}
		else
		{
			addArray		+= "|label:Main";
		}
		addArray		+= "|line1:" + cleanArrayStr(myForm.add_line1.value);
		addArray		+= "|line2:" + cleanArrayStr(myForm.add_line2.value);
		addArray		+= "|line3:" + cleanArrayStr(myForm.add_line3.value);
		addArray		+= "|city:" + cleanArrayStr(myForm.add_city.value);
		addArray		+= "|stateProvince:" + cleanArrayStr(myForm.add_stateProvince.value);
		addArray		+= "|postalCode:" + cleanArrayStr(myForm.add_postalCode.value);
		addArray		+= "|country:" + cleanArrayStr(myForm.add_country.value) + ";";
		myForm.elements["region"].value = determineRegion(myForm.add_stateProvince.value, myForm.add_postalCode.value);
	}
	myForm.elements["addresses"].value = addArray;
	
	
	var ciArray	= "";
	if(myForm.elements["ci_contactInfoID"].length != null)
	{
		for(j=0;j<myForm.elements["ci_contactInfoID"].length; j++)
		{
			if((myForm.ci_type[j].value != "" && myForm.ci_info[j].value == "") || (myForm.ci_type[j].value == "" && myForm.ci_info[j].value != "") || (myForm.ci_type[j].value == "L" && myForm.ci_info[j].value == ""))
			{
				alert("A primary email address or some required contact information is missing. Please fill in the missing information and resubmit.");
				return false;
			}
     		if((myForm.ci_type[j].value == "L" || myForm.ci_type[j].value == "E") && myForm.ci_info[j].value.search(emailRegEx) == -1) 
     		{
          		alert("Please enter email addresses in a standard format. (someone@somewhere.com)");
          		return false;
     		}
     		if(myForm.ci_type[j].value == "U" && myForm.ci_info[j].value.search(httpRegEx) == -1) 
     		{
          		alert("Please enter a valid web page address.");
          		return false;
     		}
			ciArray		+= "contactInfoID:" + cleanArrayStr(myForm.ci_contactInfoID[j].value);
			ciArray		+= "|isVisible:";
			if(myForm.ci_isVisible[j].checked == true  || myForm.ci_isVisible[j].value == "login")
			{
				ciArray		+= "1";
			}
			else
			{
				ciArray		+= "0";
			}
			ciArray		+= "|type:" + cleanArrayStr(myForm.ci_type[j].value);
			ciArray		+= "|label:" + cleanArrayStr(myForm.ci_label[j].value);
			ciArray		+= "|info:" + cleanArrayStr(myForm.ci_info[j].value) + ";";
		}
	}
	else
	{
		if(myForm.elements["ci_contactInfoID"] != null)
		{
			if((myForm.ci_type.value != "" && myForm.ci_info.value == "") || (myForm.ci_type.value == "" && myForm.ci_info.value != "") || (myForm.ci_type.value == "L" && myForm.ci_info.value == ""))
			{
				alert("Your primary email address or some required contact information is missing. Please fill in the missing information and resubmit.");
				return false;
			}
     		if((myForm.ci_type.value == "L" || myForm.ci_type.value == "E") && myForm.ci_info.value.search(emailRegEx) == -1) 
     		{
          		alert("Please enter email addresses in a standard format. (someone@somewhere.com)");
          		return false;
     		}
     		if(myForm.ci_type.value == "U" && myForm.ci_info.value.search(httpRegEx) == -1) 
     		{
          		alert("Please enter a valid web page address.");
          		return false;
     		}
			ciArray		+= "contactInfoID:" + cleanArrayStr(myForm.ci_contactInfoID.value);
			ciArray		+= "|isVisible:";
			if(myForm.ci_isVisible.checked == true  || myForm.ci_isVisible.value == "login")
			{
				ciArray		+= "1";
			}
			else
			{
				ciArray		+= "0";
			}
			ciArray		+= "|type:" + cleanArrayStr(myForm.ci_type.value);
			ciArray		+= "|label:" + cleanArrayStr(myForm.ci_label.value);
			ciArray		+= "|info:" + cleanArrayStr(myForm.ci_info.value) + ";";
		}
	}
	myForm.elements["contactInfos"].value = ciArray;
	return true;
}


function myPageSubmit()
{
	alert("Depending on the size and number of files you have chosen to upload, the page may take a long time to refresh. Do not click other links on the page or use your back button while the files are uploading .Please be patient and allow the page to refresh on its own.");
}

function determineRegion(state, pc)
{
	var thisState = state;
	var thisPostalCode = pc;
	if (thisState == "AL")
		{
			return "4";
		}
		if (thisState == "AK")
		{
			return "8";
		}
		if (thisState == "AZ")
		{
			return "7";
		}
		if (thisState == "AR")
		{
			return "6";
		}
		if (thisState == "CA")
		{
			if (thisPostalCode >= "93500")
			{
				return "8";
			}
			else
			{
				return "7";
			}	
		}
		if (thisState == "CO")
		{
			return "7";
		}
		if (thisState == "CT")
		{
			return "1";
		}
		if (thisState == "DE")
		{
			return "3";
		}
		if (thisState == "DC")
		{
			return "3";
		}
		if (thisState == "FL")
		{
			return "4";
		}
		if (thisState == "GA")
		{
			return "4";
		}
		if (thisState == "HI")
		{
			return "7";
		}
		if (thisState == "ID")
		{
			return "8";
		}
		if (thisState == "IL")
		{
			return "5";
		}
		if (thisState == "IN")
		{
			return "5";
		}
		if (thisState == "IA")
		{
			return "5";
		}
		if (thisState == "KS")
		{
			return "6";
		}
		if (thisState == "KY")
		{
			return "4";
		}
		if (thisState == "LA")
		{
			return "4";
		}
		if (thisState == "ME")
		{
			return "1";
		}
		if (thisState == "MD")
		{
			return "3";
		}
		if (thisState == "MA")
		{
			return "1";
		}
		if (thisState == "MI")
		{
			return "5";
		}
		if (thisState == "MN")
		{
			return "5";
		}
		if (thisState == "MS")
		{
			return "4";
		}
		if (thisState == "MO")
		{
			return "6";
		}
		if (thisState == "MT")
		{
			return "8";
		}
		if (thisState == "NE")
		{
			return "6";
		}
		if (thisState == "NV")
		{
			return "7";
		}
		if (thisState == "NH")
		{
			return "1";
		}
		if (thisState == "NJ")
		{
			return "2";
		}
		if (thisState == "NM")
		{
			return "7";
		}
		if (thisState == "NY")
		{
			return "2";
		}
		if (thisState == "NC")
		{
			return "4";
		}
		if (thisState == "ND")
		{
			return "5";
		}
		if (thisState == "OH")
		{
			return "5";
		}
		if (thisState == "OK")
		{
			return "6";
		}
		if (thisState == "OR")
		{
			return "8";
		}
		if (thisState == "PA")
		{
			return "3";
		}
		if (thisState == "RI")
		{
			return "2";
		}
		if (thisState == "SC")
		{
			return "4";
		}
		if (thisState == "SD")
		{
			return "5";
		}
		if (thisState == "TN")
		{
			return "4";
		}
		if (thisState == "TX")
		{
			return "6";
		}
		if (thisState == "UT")
		{
			return "7";
		}
		if (thisState == "VT")
		{
			return "1";
		}
		if (thisState == "VA")
		{
			return "3";
		}
		if (thisState == "WA")
		{
			return "8";
		}
		if (thisState == "WV")
		{
			return "3";
		}
		if (thisState == "WI")
		{
			return "5";
		}
		if (thisState == "WY")
		{
			return "8";
		}	
	//The Canadians	
		if (thisState == 'AB')
		{
			return "8";
			//$country="CANADA"
		}	
	
		if (thisState == 'BC')
		{
			return "8";
			//$country="CANADA"
		}
			
		if (thisState == 'MB')
		{
			return "5";
			//$country="CANADA"
		}
			
		if (thisState == 'NB')
		{
			return "1";
			//$country="CANADA"
		}	
		
		if (thisState == 'NL')
		{
			return "1";
			//$country="CANADA"
		}	
	
		if (thisState == 'NS')
		{
			return "1";
			//$country="CANADA"
		}
	
		if (thisState == 'ON')
		{
			return "5";
			//$country="CANADA"
		}
		
		if (thisState == 'PE')
		{
			return "1";
			//$country="CANADA"
		}
		
		if (thisState == 'QC')
		{
			return "1";
			//$country="CANADA"
		}
		if (thisState == 'SK')
		{
			return "8";
			//$country="CANADA"
		}
}
function addAnAlternativeForm(formName)
{
	if(good2Go())
	{
		document.forms[formName].submit();
	}
	return false;
}

function deleteMemberCheck(formName)
{
	if(good2Go())
	{
		if(confirm('Are you sure you want to delete this member?'))
		{
			document.forms[formName].submit();
		}
	}
}

function deletePageCheck(formName)
{
	if(good2Go())
	{
		if(confirm('Are you sure you want to delete this profile page?'))
		{
			document.forms[formName].submit();
		}
	}
}

function deleteWorkCheck(formName, wid)
{
	if(good2Go())
	{
		if(confirm('Are you sure you want to delete this work?'))
		{
			document.forms[formName].elements['workID'].value = wid;
			document.forms[formName].submit();
			document.forms[formName].submit();
		}
	}
}

function deleteAddressCheck(formName, aid)
{
	if(good2Go())
	{
		if(confirm('Are you sure you want to delete this address?'))
		{
			document.forms[formName].elements['addressID'].value=aid;
			document.forms[formName].submit();
			return false;
		}
	}
}

function deleteContactInfoCheck(formName, cid)
{
	if(good2Go())
	{
		if(confirm('Are you sure you want to delete this contact info?'))
		{
			document.forms[formName].elements['contactInfoID'].value=cid;
			document.forms[formName].submit();
			return false;
		}
	}
}


function replaceBanner(imageURL) {
    theMast = document.getElementById("masthead");
	theMast.style.backgroundImage = "url(" + imageURL + ")";
}

function randomizeBanner() {
	
	var theImages = new Array();
	theImages[0] = '/images/banners/banner1372a.jpg';
	theImages[1] = '/images/banners/banner1372b.jpg';
	theImages[2] = '/images/banners/banner1373a.jpg';
	theImages[3] = '/images/banners/banner1373b.jpg';
	theImages[4] = '/images/banners/banner1374.jpg';
	theImages[5] = '/images/banners/banner1376.jpg';
	theImages[6] = '/images/banners/banner1377.jpg';
	theImages[7] = '/images/banners/banner1378a.jpg';
	theImages[8] = '/images/banners/banner1378b.jpg';
	theImages[9] = '/images/banners/banner1385a.jpg';
	theImages[10] = '/images/banners/banner1385b.jpg';
	
	var p = theImages.length;
	var whichImage = Math.round(Math.random()*(p-1));
	
	theMast = document.getElementById("masthead");
	theMast.style.backgroundImage = "url(" + theImages[whichImage] + ")";
}

function replacePageLabel(theText) {
	theLabel = document.getElementById("pageLabel");
	if (theText == "") {
		theLabel.innerHTML = "";
	}
	else {
		theLabel.innerHTML = "<h4>" + theText + "</h4>";
	}
}

function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=625,height=540,left = 327.5,top = 242');");
}






/* these are the visual display elements from moo.fx */

var bodyAccordion;
window.addEvent('domready', function() {
	//create our Accordion instance on Body Elements
	bodyAccordion = new Accordion('a.menuPopperBody', 'div.menuPopOutBody', {
		opacity: false,
		onActive: function(menuPopperBody, menuPopOutBody){
			menuPopperBody.setStyle('color', '#f79239');
		},
		onBackground: function(menuPopperBody, menuPopOutBody){
			menuPopperBody.setStyle('color', '#094fa3');
		}
	});
});


var myAccordion;
window.addEvent('domready', function() {
	//create our Accordion instance
	myAccordion = new Accordion('a.menuPopper', 'ul.menuPopOut', {
		opacity: false,
		onActive: function(menuPopper, menuPopOut){
			menuPopper.setStyle('color', '#f79239');
		},
		onBackground: function(menuPopper, menuPopOut){
			menuPopper.setStyle('color', '#094fa3');
		}
	});
});

window.addEvent('domready', function() {
	bodyAccordion.display();
});











/* The functions below are for the CSS pop-Window */
/* for pop up window (McFerron) */

function toggle(div_id) 
{
	var el = document.getElementById(div_id);
	if ( el.style.display == 'none' ) {	el.style.display = 'block';}
	
	else {el.style.display = 'none';}
}


function PopUpBlanket_size(popUpDivVar) 
{
	
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	} else {
		viewportheight = document.documentElement.clientHeight;
	}
	
	if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
		PopUpBlanket_height = viewportheight;
	} else {
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			PopUpBlanket_height = document.body.parentNode.clientHeight;
		} else {
			PopUpBlanket_height = document.body.parentNode.scrollHeight;
		}
}
	var PopUpBlanket = document.getElementById('PopUpBlanket');
	PopUpBlanket.style.height = PopUpBlanket_height + 'px';
	var popUpDiv = document.getElementById(popUpDivVar);
	popUpDiv_height=PopUpBlanket_height/2-150;//150 is half popup's height
	popUpDiv.style.top = popUpDiv_height + 'px';
	
}

function window_pos(popUpDivVar) {
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerHeight;
	} else {
		viewportwidth = document.documentElement.clientHeight;
	}
	if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
		window_width = viewportwidth;
	} else {
		if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
			window_width = document.body.parentNode.clientWidth;
		} else {
			window_width = document.body.parentNode.scrollWidth;
		}
	}
	var popUpDiv = document.getElementById(popUpDivVar);
	window_width=window_width/2-150;
	
	/*150 is half popups width*/
	
	
	popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
	PopUpBlanket_size(windowname);
	window_pos(windowname);
	toggle('PopUpBlanket');
	toggle(windowname);		
}


//* END POP-UP SCRIPTS *//