/************************************************
 *	SetupAjax() function
 *
 *	Description: Used to setup a AJAX handler
 *	Input: none
 *	Output: ajax handle
 ***********************************************/
function setupAjax(){
	var ajaxPtr;
	try{ // Firefox, Opera 8.0+, Safari
		ajaxPtr = new XMLHttpRequest();
	} catch (e){
		try { // Internet Explorer
			ajaxPtr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try { // Internet Explorer
				ajaxPtr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { // Not Compatible
				alert("Your browser does not support AJAX!");
				exit(1);
			}
		}
	}
	return ajaxPtr;
}

/************************************************
 *	usherSelectChange(selectBox) function
 *
 *	Description: Used to determine and display
 *				 USHER option choices
 *	Input: the select box of USHERS
 *	Output: none
 ***********************************************/
function usherSelectChange(selectBox){
	var ajaxHandler1 = setupAjax();

	var basePrice = "0";
	var monthlyPrice = "0";
	var enableAjax2 = false;

	document.getElementById('formContent1').innerHTML = "";
	document.getElementById('formContent2').innerHTML = "";
	document.getElementById('formContent3').innerHTML = "";

	ajaxHandler1.onreadystatechange=function() {
		if(ajaxHandler1.readyState==4)
			document.getElementById('formContent1').innerHTML = ajaxHandler1.responseText;
	}

	if(selectBox.value == "Interactive"){
		basePrice = "400";
		monthlyPrice = "30";
		ajaxHandler1.open("GET","/js/signup/interactive.txt",true);
	} else if(selectBox.value == "Graphical"){
		basePrice = "300";
		monthlyPrice = "20";
		ajaxHandler1.open("GET","/js/signup/graphical.txt",true);
	} else if(selectBox.value =="Audio"){
		basePrice = "500";
		monthlyPrice = "40";
		ajaxHandler1.open("GET","/js/signup/audio.txt",true);
	} else if(selectBox.value == "Video"){
		basePrice = "900";
		monthlyPrice = "50";
		ajaxHandler1.open("GET","/js/signup/video.txt",true);
	} else {
		ajaxHandler1.open("GET","/js/signup/blank.txt",true);
	}
	document.getElementById('setupCost').value = basePrice;
	document.getElementById('monthlyCost').value = monthlyPrice;

	ajaxHandler1.send(null);
}

/************************************************
 *	usherContentChange(selectBox) function
 *
 *	Description: Used to determine and display
 *				 USHER content choices
 *	Input: select box of content
 *	Output: none
 ***********************************************/
function usherContentChange(selectBox){
	var ajaxHandler = setupAjax();
	ajaxHandler.open("GET","/js/signup/content/"+selectBox.value+".txt",true);
	ajaxHandler.onreadystatechange=function() {
		if(ajaxHandler.readyState==4)
			document.getElementById('formContent2').innerHTML = ajaxHandler.responseText;
	}
	ajaxHandler.send(null);
}

/************************************************
 *	updateCount(ta) function
 *
 *	Description: Used to update the word count
 *				 of the given textarea
 *	Input: video/audio script textarea
 *	Output: none
 ***********************************************/
function updateCount(ta){
	ta.form.charcount.value = ta.value.length;
	if (ta.value == "")
		ta.form.wordcount.value = '0';
	else
		ta.form.wordcount.value = ta.value.split(' ').length;
}

/************************************************
 *	addUsher() function
 *
 *	Description: Used to add USHER to users
 *				 cart
 *	Input: cart form
 *	Output: none
 ***********************************************/
function addUsher(cartform){
	cartform.gotoTwo.name = "addNew";
	cartform.submit();
}

/************************************************
 *	removeUsher(cartform) function
 *
 *	Description: Used to remove one or more
 *				 USHER(S) from users cart
 *	Input: cart form
 *	Output: none
 ***********************************************/
function removeUsher(cartform){
	cartform.gotoTwo.name = "removeSelected";
	cartform.submit();
}

/************************************************
 *	gotoCart(form) function
 *
 *	Description: Used to remove one or more
 *				 USHER(S) from users cart
 *	Input: current steps form
 *	Output: none
 ***********************************************/
function gotoCart(form){
	if (form.gotoFinal != null){
		form.gotoFinal.name = "viewCart";
	} else if(form.cart != null){
		form.cart.name = "viewCart";
	} else {
		return false;
	}
	form.submit();
}

/************************************************
 *	checkForm(form) function
 *
 *	Description: Used to validate all required
 *				 fields are filled out
 *	Input: form needing validation
 *	Output: true if valid, false otherwise
 ***********************************************/
 function checkForm(form){
	for(var i=0; i<form.elements.length; i++){
		if (form.elements[i].value == "" && (form.elements[i].className != "mediascript" && form.elements[i].name != "clientLogo")){
			alert('Please fill in all required fields');
			return false;
		}
	}
	return true;
}
 
 /************************************************
 *	checkAgreement(form) function
 *
 *	Description: Used to validate that the user
 *				 agrees with the terms of the
 *				 User Agreement
 *	Input: form of the user agreement
 *	Output: true if valid, false otherwise
 ***********************************************/
 function checkAgreement(form){
	if (form.acceptAgreement.checked != true){
		alert('Please read the User Agreement and check the box if you agree');
		return false;
	}
	return true;
}

 /****************************************************
 *	updateVideoScripts(numScripts) function
 *
 *	Description: Used to make and display textareas
 *				 for a given number of video scripts
 *	Input: number of video scripts
 *	Output: none
 ****************************************************/
function updateVideoScripts(numScripts){
	document.getElementById('videoScripts').innerHTML = "";
	masterParagraph = document.createElement('p');
	for(i=1; i<=numScripts; i++){
		newTextarea = document.createElement('textarea');
		newTextarea.name = "videoScript"+i;
		newTextarea.className = "mediascript";
		masterParagraph.innerHTML += "Video Script #"+i+":<br />";
		masterParagraph.appendChild(newTextarea);
		masterParagraph.innerHTML += "<br />";
	}
	document.getElementById('videoScripts').appendChild(masterParagraph);
}

 /************************************************
 *	updateVideoPricing(videos) function
 *
 *	Description: Used to update the pricing
 *				 display when the number of
 *				 videos change
 *	Input: number of videos
 *	Output: none
 ***********************************************/
function updateVideoPricing(videos){
	if(videos == "1")
		basePrice = "900";
	if(videos == "2")
		basePrice = "1500";
	if(videos == "3")
		basePrice = "2000";
	if(videos == "4")
		basePrice = "2400";
	if(videos == "5")
		basePrice = "2700";

	document.getElementById('setupCost').value = basePrice;
}

 /************************************************
 *	updatePrice(cartform) function
 *
 *	Description: Used to update the price when
 *				 the price timeframe changes
 *	Input: form of the cart
 *	Output: none
 ***********************************************/
function updatePrice(cartform){
	cartform.gotoTwo.name = "updatePrice";
	cartform.submit();
}