/*
*************************************************

THE PLEASE TOUCH MUSEUM
JS Functions

Created by the friendly folks at Pixelworthy 
http://www.pixelworthy.com

*************************************************
*/

/*---- Grey Initial Values ------ */

function greyInitialValues(){
	var filled = getElementsByClassName("filled");
	
	if(filled.length > 0){
		for(var i = 0; i < filled.length; i++){
			filled[i].className = "empty";
			filled[i].initialValue = filled[i].value;
			
			filled[i].onclick = filled[i].onfocus = function(){
				this.className = "filled";
				if(this.value == this.initialValue){
					this.value= "";
				}
			}
			
			filled[i].onblur = function(){
				if(this.value == this.initialValue || this.value == ""){
					this.className = "empty";
					this.value = this.initialValue;
				}else{
					this.className = "filled";
				}
			}
		}
	}else{ 
		return; 
	}
}

addLoadEvent(greyInitialValues);

/*---- Grey Initial Values ------ */



/*---- otherOption ------ */

var openTimer, closeTimer, openStatus;

function otherOption(selectID){
	
	if(selectID.parentNode.nextSibling.nodeName == "LABEL"){
		var otherInput = selectID.parentNode.nextSibling; // for IE		
		var browser = "IE";
	}else{
		var otherInput = selectID.parentNode.nextSibling.nextSibling; //for all the other browsers
		var browser = "";
	}
	
	var openedHeight = otherInput.offsetHeight;
	
	otherInput.style.display = 'none';
	openStatus = "closed";
			
	if(document.getElementById){
		selectID.onchange = function(){
			if(this.value == "other"){
				otherInput.style.display = 'block';
				
				if(browser == ""){
					
					//set height to 0
					otherInput.style.height = '0px';
					otherInput.style.overflow = 'hidden';
					otherInput.style.paddingBottom = '0';
					
					//run interval to open
					openTimer = setInterval(openOther, 1, otherInput, openedHeight);
				}
			}else{
				if(openStatus == 'open'){
					if(browser == ""){
						closeTimer = setInterval(closeOther, 1, otherInput);
					}
				}
			}
		}
	}
}

function openOther(e, limit){
	var eh_int = parseInt(e.style.height);
	if((limit-eh_int) > 2){
		e.style.height = (eh_int+((limit-eh_int)/2))+'px';
	}else{
		e.style.height = limit + 'px';
		e.style.paddingBottom = '1px';
		clearInterval(openTimer);
		openStatus = "open";
	}
}

function closeOther(e){
	if(parseInt(e.style.height) > 2){
		e.style.height = (parseInt(e.style.height)/3) + 'px';
	}else{
		e.style.height = '0';
		e.style.paddingBottom = '0';
		e.style.display = 'none';
		clearInterval(closeTimer);
		openStatus = "closed";
	}
}

/*---- end otherOption ------ */


/*---- dynamicPayPalPrice ------ */
function dynamicPayPalPrice()
{
	
}
/*---- end dynamicPayPalPrice ------ */



/*---- calculatePrice ------ */
function calculatePrice(){
	if(!document.getElementById) { return; }
	
	var singleTicket = 10.95;
	
	var totalPrice_p = document.createElement('p');

	if(document.getElementById('ticket_number').value == NaN || document.getElementById('ticket_number').value == "" || !document.getElementById('ticket_number').value.match(/^\d+$/)){
		if(!document.getElementById('ticket_number').value.match(/^\d+$/)){
			if(document.getElementById('ticket_number').value == ""){
				totalPrice_p.innerHTML = "<strong>Total Price</strong>: ---";
			}else{
				totalPrice_p.innerHTML = "<strong>Total Price</strong>: Please enter a number";
			}
		}else{
			totalPrice_p.innerHTML = "<strong>Total Price</strong>: ---";
		}
	}else{
		totalPrice_p.innerHTML = "<strong>Total Price</strong>: $" + (document.getElementById('ticket_number').value*10.95).toFixed(2);
	}
	
	insertAfter(totalPrice_p, document.getElementById('ticket_number'));
	
	document.getElementById('ticket_number').onkeyup = function(){
		if(this.value == "" || this.value == undefined || !this.value.match(/^\d+$/)){
			if(!this.value.match(/^\d+$/)){
				if(this.value == ""){
					totalPrice_p.innerHTML = "<strong>Total Price</strong>: ---";
				}else{
					totalPrice_p.innerHTML = "<strong>Total Price</strong>: Please enter a number";
				}
			}else{
				totalPrice_p.innerHTML = "<strong>Total Price</strong>: ---";
			}
		}else{
			totalPrice_p.innerHTML = "<strong>Total Price</strong>: $" + (this.value*singleTicket).toFixed(2);
		}
	}
}
/*---- end calculatePrice ------ */




/*---- form validation ------ */
function checkForm(emailID){
	if(!document.getElementById){ return; }

	var requiredFields;
	var submitForm = true;
	var emailTest = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	
	if(document.getElementById('required')){
		requiredFields = document.getElementById('required').value.split(','); //creates an array based on the required fields specified in the "hidden" input value
	}

	//see if they have values
	for(i = 0; i<requiredFields.length; i++){
		if(document.getElementById(requiredFields[i]).value == ""){
			highlightField(requiredFields[i]);
			submitForm = false;
		}else{
			if(requiredFields[i] == emailID){
				if(!emailTest.test(document.getElementById(emailID).value)){
					submitForm = false;
					highlightField(requiredFields[i]);
				}else{
					document.getElementById(requiredFields[i]).className = "";
				}
			}else{
				document.getElementById(requiredFields[i]).className = "";	
			}
		}
	}
	
	if(!submitForm){ 
		alert("Please fill in all required fields.");
		return false; 
	}
	
	return true;
}


function highlightField(fieldID){ document.getElementById(fieldID).className = "stillempty"; }


function attachValidationHandler(formID, emailID){
	if(!document.getElementById) { return; }
	
	
	if(attachValidationHandler.arguments.length == 1){	
		document.getElementById(formID).onsubmit = function(){ 
			return checkForm(); 
		}
	}else{
		document.getElementById(formID).onsubmit = function(){ 
			return checkForm(emailID); 
		}
	}
}

/*---- end form validation ------ */

function getElementsByAttribute(attribName,attribValue,node,tag)
{
	var attribElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	//var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++)
	{
		if (els[i][attribName])
		{
			attribElements[j] = els[i];
			j++;
		}
	}
	return attribElements;
}