/*
	Javascri[t File for RiverCity Solutions
	Created 9.11.08
	by Jay
	copyright River City Studio
*/


//////////////////////////////////////
//  GENERIC RETURN ELEMENT FUNCTION //
//////////////////////////////////////

function getE( v ) {
  // e is for element!
  e = false;
  
	var d = window.document;
  if ( d.getElementById ) {
    e = d.getElementById( v );
  }
  else e = d[ v ];

  return e;
}

/*
	AJAX BASE FUNCTIONALITY
*/

var xmlhttp = null;
// var displayDiv = "attendeeBoxes";
var nextFunc = "";

////////////////////////////////
//	BASIC AJAX LOADER					//
////////////////////////////////

function getPage( displayDiv, url, second ) {
	
	if ( second != false ) nextFunc = second;
	else nextFunc = "";
	
	xmlhttp=null;
	if (window.XMLHttpRequest) {// code for all new browsers
  	xmlhttp=new XMLHttpRequest();
  }
	else if (window.ActiveXObject) {// code for IE5 and IE6
  	xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP" );
  }
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange=function() {
			stateChange( displayDiv );
		};
		xmlhttp.open( "GET", url, true );
		xmlhttp.send( null );
  }
	else {
  	alert( "Your browser does not support XMLHTTP." );
  }
}

////////////////////////////////
//	EVENT HANDLER FOR HTTP		//
//	STATE CHANGE							//
////////////////////////////////

function stateChange( displayDiv, nextFunc ) {
	if (xmlhttp.readyState==4) {// 4 = "loaded"
		if (xmlhttp.status==200) {// 200 = OK
			e = getE( displayDiv );
			e.innerHTML = xmlhttp.responseText;
			if ( nextFunc != "" ) eval( nextFunc );
		}
		else {
			alert("Problem retrieving XML data");
		}
	}
}


function popBoxes( displayDiv, numAtt ) {
	e = getE( displayDiv );
	e.innerHTML = "<p>loading...</p>";
	getPage( displayDiv, "getAttendeeBoxes.php?numAtt="+numAtt );
}

function popAttendees( displayDiv, stringValue ) {
	e = getE( displayDiv );
	e.innerHTML = "<p>loading...</p>";
	getPage( displayDiv, "/admin/form/getFormContents.php?string=" + stringValue );
}

function saveConfirmationForm( displayDiv, id, email, calendar ) {
	
	if (email.checked) {
		email = 1;
	} else {
		email = 0;
	}
	
	if (calendar.checked) {
		calendar = 1;
	} else {
		calendar = 0;
	}

	e = getE( displayDiv );
	e.innerHTML = "<p>loading...</p>";
// 	alert( "The Value of Email is: " + email );
	getPage( displayDiv, "/admin/form/getConfirmationContents.php?id=" + id + "&emailConfirmation=" + email + "&enteredIntoCalendar=" + calendar );
}


///////////////////////////////////////
//	TERMS AND CONDITIONS VALIDATION  //
///////////////////////////////////////

function validateTerms(thisForm) {
	with(thisForm) {
		if (!terms.checked) {
			terms.focus();
			alert("Please Agree to the Terms and Conditions");
			return false;
		}
		
		return true;
	}
}

///////////////////////////////////////
//	REGISTER NOW BUTTONS FOR  LINKS  //
///////////////////////////////////////

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/* ------------------- Accordian Function for Mac Rpair landing page ---------------------------- */
	
function popReason(e) {
	var currClass = e.childNodes[0].className;
	shrinkAllAccordians();
	
	var accordiansShrunk = checkAllAccordians();

	if(accordiansShrunk == true) {
		var targetP = e.nextSibling;
		if(targetP.nodeType != 1) {
			targetP = targetP.nextSibling;
		}
		
		if( targetP.style.display != "block" ) {
			targetP.style.display = "block";
		}
		
		var maxHeight = targetP.offsetHeight;
		// For some reason if I set it to 0px, the DOM didn't recognize the object
		targetP.style.height = "1px";
		timer = setTimeout(function() {expandAccordian(targetP, maxHeight)}, 32);
	}
	
	if(currClass == "link active")
		e.childNodes[0].setAttribute("class", "link nonActive");
	else
		e.childNodes[0].setAttribute("class", "link active");
}


function checkAllAccordians() {
	var pElements = document.getElementsByTagName("div");
	
	for(i=0; i<pElements.length; i++) {
		if( pElements[i].className == "reasonBlock" ) {
			if(pElements[i].style.diplay == "none") {
				return false;
			}
		}
	}
	
	return true;
}


function shrinkAllAccordians() {
	var pElements = document.getElementsByTagName("div");
	
	// Sinc Javascript doesn't know much info until it interacts with the page ... these 5 lines teach it.
	if(document.getElementById("initialShow") != null) {
		var intialShowingDiv = document.getElementById("initialShow");
		if(intialShowingDiv.style.display = "block") {
			intialShowingDiv.style.height = intialShowingDiv.offsetHeight + "px";
			intialShowingDiv.id = null;
		}
	}
	
	for(i=0; i<pElements.length; i++) {
		if( pElements[i].className == "reasonBlock" ) {
			var cellHeight = pElements[i].style.height;
			cellHeight = parseFloat(cellHeight.replace("px", ""));
			
			if(cellHeight > 0) {
				openTarget = pElements[i];
				shrinkTimer = setTimeout( function() { shrinkAccordian(openTarget) }, 32 );
			}
		}
	}
	
	var pElements = document.getElementsByTagName("a");
	for(i=0; i<pElements.length; i++) {
		if( pElements[i].className == "link active" ) {
			//set background to default
			pElements[i].setAttribute("class", "link nonActive");
		}
	}
	
}

function expandAccordian(e, maxHeight) {
	var currentHeight = e.style.height;
	currentHeight = currentHeight.replace("px", "");
	var multiplier = 1.5;
	var newHeight = (parseFloat(currentHeight) * multiplier);
	
	if( newHeight >= maxHeight) {
		e.style.height = maxHeight + "px";
		clearTimeout(timer);
	} else {
		e.style.height = Math.round(newHeight) + "px";
		timer = setTimeout( function() { expandAccordian(e, maxHeight) }, 32 );
	}
}


function shrinkAccordian(e) {
	var currentHeight = e.style.height;
	currentHeight = currentHeight.replace("px", "");
	var multiplier = .7;
	var newHeight = (parseFloat(currentHeight) * multiplier);
	
	if( newHeight <= 2) {
		e.style.display = "none";
		e.style.height = null;
		clearTimeout(shrinkTimer);
	} else {
		e.style.height = newHeight + "px";
		shrinkTimer = setTimeout( function() { shrinkAccordian(e) }, 32 );
	}
}

/* ------------------- Help  ---------------------------- */

function setCurrentAccordianHeader(e) {
}
