// JavaScript Document
// scripts for emmfestival.org


// rotating images
var theImages = new Array() 
theImages[0] = 'http://www.emmfestival.org/data/images/banners/banner1-01.png'
theImages[1] = 'http://www.emmfestival.org/data/images/banners/banner2-01.png'
theImages[2] = 'http://www.emmfestival.org/data/images/banners/banner3-01.png'
theImages[3] = 'http://www.emmfestival.org/data/images/banners/banner4-01.png'
theImages[4] = 'http://www.emmfestival.org/data/images/banners/banner5-01.png'
theImages[5] = 'http://www.emmfestival.org/data/images/banners/banner6-01.png'
theImages[6] = 'http://www.emmfestival.org/data/images/banners/banner7-01.png'
theImages[7] = 'http://www.emmfestival.org/data/images/banners/banner8-01.png'
theImages[8] = 'http://www.emmfestival.org/data/images/banners/banner9-01.png'
theImages[9] = 'http://www.emmfestival.org/data/images/banners/banner10-01.png'

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}
// end rotating images












//Account Area Stuff //


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







function lengthCheck(elmnt)
{
	if(elmnt.value.length > 500)
	{
		alert("This field has a limit of 500 characters.");
		elmnt.value = elmnt.value.substr(0,500);
	}
}

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

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function myTest()
{
	// remember, these are the possible parameters for Set_Cookie:
	// name, value, expires, path, domain, secure
	Set_Cookie( 'test', 'none', '', '/', '', '' );
	// if Get_Cookie succeeds, cookies are enabled, since
	//the cookie was successfully created.
	if ( Get_Cookie( 'test' ) )
	{
		//alert( 'cookies are currently enabled.' );
		/*
		this is an example of a set cookie variable, if
		you want to use this on the page or on another script
		instead of writing to the page you would just check that value
		for true or false and then do what you need to do.
		*/
		//cookie_set = true;
		// and these are the parameters for Delete_Cookie:
		// name, path, domain
		// make sure you use the same parameters in Set and Delete Cookie.
		Delete_Cookie('test', '/', '');
	}
	// if the Get_Cookie test fails, cookies
	//are not enabled for this session.
	else
	{
		alert( 'Cookies are not currently enabled for your browser. You will not be able to use the account area of this site without first enabling cookies.' );
		document.location.href = "http://emmfestival.org/data/cookie_kick.html";
	}
}

function mySubmit()
{
	var myForm		= document.forms["memberForm"];
	var lpArray	= "";
	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	= "";
	for(j=0;j<myForm.elements["add_addressID"].length; j++)
	{
		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) + ";";
	}
	myForm.elements["addresses"].value = addArray;
	
	
	var ciArray	= "";
	for(j=0;j<myForm.elements["ci_contactInfoID"].length; j++)
	{
		ciArray		+= "contactInfoID:" + cleanArrayStr(myForm.ci_contactInfoID[j].value);
		ciArray		+= "|isVisible:";
		if(myForm.ci_isVisible[j].checked == true)
		{
			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) + ";";
	}
	myForm.elements["contactInfos"].value = ciArray;
}






// ---------------------------------------------
//create account form required elements
function myCreateSubmit(myForm)
{
	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.uname.value == "" || myForm.pword.value == "")
		{
			alert ("You must select a username and password to create an account");
			return false;
		}
	
		if(myForm.firstName.value == "" || myForm.lastName.value == "" || myForm.line1.value == "" ||  myForm.city.value == "" || myForm.stateProvince.value == "" || myForm.email_address1.value == "" || myForm.postalCode.value == "")
		{
			alert ("Some required fields have been left blank.  Please return to the form and complete the missing the information.");
			return false;
		}
	
	
	
		return true;
	}
}


//------------------------------------------------------
	// submission create form required elements
function mySubCreateSubmit(myForm)
{
	if(!myForm.elements["terms"].checked)
	{
		alert("EMM requires all composers, whose work is selected, to attend the Electronic Music Midwest Festival.  \n \n You must check the box next to \"I agree to attend EMM\" in order to submit a work for consideration.");
		return false;
	}
	
	if(myForm.title.value == "" || myForm.composer_name.value == ""|| myForm.duration.value == "" || myForm.genre.value == "")
	{
		alert ("Some required fields have been left blank.  Please return to the form and complete the missing information.");
		return false;
		
	}

	//no uploads, so this isn't necessary.  commented out by mcferron
	//alert("Depending on filesize, uploads may take a few minutes.");
	for(i=0;i<myForm.elements.length;i++)
	{
	    myForm.elements[i].value = cleanArrayStr(myForm.elements[i].value);
	}
	
	
	return true;
}


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');");
}







function myAdminEmailForm(myForm)
{
	if(myForm.subject.value == "")
	{
		alert ("Ooops!  Please include a subject.");
		return false;	
	}

	
	
	if(myForm.message.value == "")
	{
		alert ("Ooops!  You're trying to send a blank message!  Please write something in the message box.");
		return false;	
	}


return true;


}

function showJudgeCheckboxFixerSubmit(myForm)
{
	for(j=0;j<myForm.elements["submissionID[]"].length; j++)
	{
		if(myForm.elements["invited_cb[]"] != null)
		{
			if(myForm.elements["invited_cb[]"][j].checked == true)
			{
				myForm.elements["invited[]"][j].value = "Y";
			}
			else
			{
				myForm.elements["invited[]"][j].value = "N";
			}
		}
		if(myForm.elements["confirmed_cb[]"] != null)
		{
			if(myForm.elements["confirmed_cb[]"][j].checked == true)
			{
				myForm.elements["confirmed[]"][j].value = "Y";
			}
			else
			{
				myForm.elements["confirmed[]"][j].value = "N";
			}
		}
	}
	return true;
}



function setDirtyFlag(myForm, mySub)
{
	if(myForm.elements["amIDirty_"+mySub] != null)
	{
		myForm.elements["amIDirty_"+mySub].value = "Y";
	}
	return true;
}










/* 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-300;
	//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-300;
	
	/*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 *//


