// JavaScript Document
var objForm= null;	

function init(szForm){
	objForm=document.getElementById(szForm);
	
	if ((objForm.formType.value.charAt(1)=="A")) {
		calculateStandCost();
		calculateSpaceHireCost();
	}
	
}



function calculateStandCost(){
	//reset pricePerMetre - we'll calculate full price and reduce later
	pricePerMetre=323.25;
	if(objForm.standWidth.value<2) { 
		standWidth = 2;
		objForm.standWidth.value = standWidth;
	} else { standWidth = objForm.standWidth.value; }
	if(objForm.standDepth.value<2) {
		standDepth = 2;
		objForm.standDepth.value = standDepth;
	} else { standDepth = objForm.standDepth.value; }
	standArea=(standWidth * standDepth);
	
	standCost=(standArea * pricePerMetre);
	
	//check to see if corner is selected
	if (objForm.cornerRequired.checked) standCost=(standCost*1.15);
	
	//check to see if table/chair selected
	if (objForm.tableChairRequired.checked) //standCost=(standCost+55);
	
	//check to see if magic word is used
	if (checkWordAt(objForm.contactHearAboutUs.value,1)=="nicholas"){
		standCost=standCost * 0.6;
		pricePerMetre=pricePerMetre * 0.6;
	}
	
	//output Stand Area and Cost
	objForm.standArea.value=standArea;
	document.getElementById("standTotal").innerHTML=standCost.toFixed(2);
	objForm.costStand.value=standCost.toFixed(2);
	//set display value of per metre price
	document.getElementById("standPricePer").innerHTML=pricePerMetre;
}

function calculateSpaceHireCost(){
	//reset pricePerMetre - we'll calculate full price and reduce later
	pricePerMetre=289.25;
	if(objForm.spaceHireWidth.value<4) { 
		spaceHireWidth = 4;
		objForm.spaceHireWidth.value = spaceHireWidth;
	} else { spaceHireWidth = objForm.spaceHireWidth.value; }
	if(objForm.spaceHireDepth.value<5) {
		spaceHireDepth = 5;
		objForm.spaceHireDepth.value = spaceHireDepth;
	} else { spaceHireDepth = objForm.spaceHireDepth.value; }
	spaceHireArea=(spaceHireWidth * spaceHireDepth);
	
	spaceHireCost=(spaceHireArea * pricePerMetre);
	
	//check to see if corner is selected
	if (objForm.spaceHireCornerRequired.checked) spaceHireCost=(spaceHireCost*1.15);
	
	//check to see if table/chair selected
	if (objForm.tableChairRequired.checked) //spaceHireCost=(spaceHireCost+55);
	
	//check to see if magic word is used
	//if (checkWordAt(objForm.contactHearAboutUs.value,1)=="nicholas"){
	//	spaceHireCost=spaceHireCost * 0.6;
	//	pricePerMetre=pricePerMetre * 0.6;
	//}
	
	//output Stand Area and Cost
	objForm.spaceHireArea.value=spaceHireArea;
	document.getElementById("spaceHireTotal").innerHTML=spaceHireCost.toFixed(2);
	objForm.costSpaceHire.value=spaceHireCost.toFixed(2);
	//set display value of per metre price
	document.getElementById("spaceHirePricePer").innerHTML=pricePerMetre;
}


	
function checkInventionDesc(){
	if (!objForm.inventionDescription.value) {
		return;
	}
	//calculate wordCount
	wordCount=checkWordCount(objForm.inventionDescription.value);
	lowerWordLimit=25;
	upperWordLimit=50;
	
	if ( ( wordCount > lowerWordLimit) && (objForm.inventionBoldEntry.checked == false)) {
		response=confirm("you have more than " + lowerWordLimit + " words in your invention description\nwould you like to select the bold 50 word boxed entry option?\nClick OK for yes, or Cancel for no");
		objForm.inventionBoldEntry.checked = response;
	}
	
	//get 
	if (objForm.inventionBoldEntry.checked){
		maxWords=upperWordLimit;
		// set cost for bold entry too
		objForm.costBoldEntry.value= "46.00";
	} else {
		maxWords=lowerWordLimit;
		//reset bold entry cost to zero
		objForm.costBoldEntry.value= "0.00";
	}
	
	if ( wordCount > maxWords){
		
		
		
		alert("Word limit exceeded - truncating entry to " + maxWords + " words!");
		wordArr=objForm.inventionDescription.value.split(" ");
		wordArr=wordArr.slice(0, maxWords);
		objForm.inventionDescription.value=wordArr.join(" ");
		}
	
}

function checkExhibitDesc(){
	if (!objForm.exhibitDescription.value) {
		return;
	}
	
	//calculate wordCount
	wordCount=checkWordCount(objForm.exhibitDescription.value);
	lowerWordLimit=25;
	upperWordLimit=50;
	
	if ( ( wordCount > lowerWordLimit) && (objForm.exhibitBoldEntry.checked == false)) {
		response=confirm("you have more than " + lowerWordLimit + " words in your invention description\nwould you like to select the bold 50 word boxed entry option?\nClick OK for yes, or Cancel for no");
		objForm.exhibitBoldEntry.checked = response;
	}
	
	//get 
	if (objForm.exhibitBoldEntry.checked){
		maxWords=upperWordLimit;
		// set cost for bold entry too
		objForm.costBoldEntry.value= "46.00";
	} else {
		maxWords=lowerWordLimit;
		//reset bold entry cost to zero
		objForm.costBoldEntry.value= "0.00";
	}
	
	if ( wordCount > maxWords){
		
		
		
		alert("Word limit exceeded - truncating entry to " + maxWords + " words!");
		wordArr=objForm.exhibitDescription.value.split(" ");
		wordArr=wordArr.slice(0, maxWords);
		objForm.exhibitDescription.value=wordArr.join(" ");
		}
	
}


function setNameBoardOption(){
	
	if (objForm.exhibitNameBoard.checked){
		objForm.costNameBoard.value="28.00";
	} else {
		objForm.costNameBoard.value="";
	}
	
}

function setBoldEntryOption(){
	
	if (objForm.exhibitBoldEntry.checked){
		objForm.costBoldEntry.value="46.00";
	} else {
		objForm.costBoldEntry.value="";
	}
	
}

function calcluateExtraExhibitCost() {
	extraExhibitCost=(objForm.exhibitionTotalExtraExhibits.value * 1293);
		
	//output Stand Area and Cost
	document.getElementById("exhibitCost").innerHTML=extraExhibitCost.toFixed(2);
	objForm.costExtraExhibits.value=extraExhibitCost.toFixed(2);;
}

function calcluateExtraTicketCost() {
	extraTicketCost=(objForm.dinnerTotalExtraTickets.value * 65); // MH Dinner changed to 65 as per email from Kane wed 19-08-09 
		
	//output Stand Area and Cost
	document.getElementById("ticketCost").innerHTML=extraTicketCost.toFixed(2);
	objForm.costExtraTickets.value = extraTicketCost.toFixed(2);

}

function calcluateExtraNameBoardCost() {
	nameBoardCost=(objForm.nameboardTotal.value * 28);
		
	//output Stand Area and Cost
	document.getElementById("nameBoardCost").innerHTML=nameBoardCost.toFixed(2);
	objForm.costNameBoards.value = nameBoardCost.toFixed(2);
}

function calcluateboldEntryCost() {
	boldEntryCost=(objForm.boldEntryTotal.value * 28);
		
	//output Stand Area and Cost
	document.getElementById("boldEntryCost").innerHTML=boldEntryCost.toFixed(2);
	objForm.costBoxedEntry.value=boldEntryCost.toFixed(2);
}

function calculateAwardsEntryCost(){
	awardsCount=0;
	if (getSelectedRadioValue(objForm.awardCategoryInvention) != "None") {
		awardsCount++;
	}
	if (getSelectedRadioValue(objForm.awardCategoryInnovation) != "None") {
		awardsCount++;
	}
	if (getSelectedRadioValue(objForm.awardCategoryDesign) != "None") {
		awardsCount++;
	}

	if (awardsCount > 1) {
		awardsEntryCost = ((awardsCount-1)*55);
	} else {
		awardsEntryCost = 0;
	}

	//output Stand Area and Cost
	//document.getElementById("boldEntryCost").innerHTML=boldEntryCost.toFixed(2);
	objForm.costAwardsEntry.value=awardsEntryCost.toFixed(2)
}

// advert full page
function calculateAdvertFullPageCost() {
	advertFullPageCost=(objForm.advertFullPageTotal.value * 870);
	
	document.getElementById("advertFullPageCost").innerHTML=advertFullPageCost.toFixed(2);
	objForm.costAdvertFullPage.value=advertFullPageCost.toFixed(2);
}
// advert half page
function calculateAdvertHalfPageCost() {
	advertHalfPageCost=(objForm.advertHalfPageTotal.value * 460);
	
	document.getElementById("advertHalfPageCost").innerHTML=advertHalfPageCost.toFixed(2);
	objForm.costAdvertHalfPage.value=advertHalfPageCost.toFixed(2);
}
// advert quarter page
function calculateAdvertQuarterPageCost() {
	advertQuarterPageCost=(objForm.advertQuarterPageTotal.value * 280);
	
	document.getElementById("advertQuarterPageCost").innerHTML=advertQuarterPageCost.toFixed(2);
	objForm.costAdvertQuarterPage.value=advertQuarterPageCost.toFixed(2);
}

function checkWordCount(szString){
	
	szString=szString.split(" ");
	return szString.length;
}

function checkWordAt(szString,posN){
	szString=szString.split(" ");
	if (szString.length >= posN){
		return szString[posN-1];
	} else {
		return " ";
	}
	
	
}

function checkRegType(szFormName){
	msg="";
	objForm=document.getElementById(szFormName);
	
	szRegType=getSelectedRadioValue(objForm.regType);//objForm.regType.value;
	
	szFormType=objForm.registrationType.value;
	arrMsg=Array();
	
	//check name
	if (objForm.registrantName.value == "") {
		arrMsg.push("Please enter your name");
	}
	
	//check emails are present and correct
	if (objForm.registrantEmail.value =="" || objForm.registrantEmailCheck.value ==""){ //one or other email field is empty
		arrMsg.push("Please enter your email into both fields");
	} else { //both email field are full - check they're the same
		if (objForm.registrantEmail.value != objForm.registrantEmailCheck.value) {
			arrMsg.push("Email addresses do not match - please check and try again");
		} else { //both email addresses match - make sure they're valid
			if (!isValidEmail(objForm.registrantEmail.value, false)) arrMsg.push("Email address gven is not valid - please check and try again");
		}
	}
	
	//check your registrant type
	if (szRegType == "") arrMsg.push("Please select registration type");
	
	//check Form type
	if (szFormType=="") arrMsg.push("Please select what you wish to register for");
	
	//if (objForm.inventionBoldEntry.checked && !objForm.inventionDescription.value) {
	//	arrMsg.push('You\'ve chosen the Bold 50 word boxed entry @ £46 but you havn\'t entered anything in the description box above it.');
	//}
	

	if (arrMsg.length > 0){
		tmpMsg="";
		for (n=0 ; n < arrMsg.length ; n++ ){
			tmpMsg += arrMsg[n] + "\n";
		}
		alert(tmpMsg);
	} else {
		//alert( szRegType + szFormType);
		szTarget=szRegType + szFormType + ".php";
		//document.location=szTarget.toLowerCase();
		objForm.action=szTarget.toLowerCase();
		objForm.submit();
	}
	
	
}
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function

// Checks the form to make sure necessary values are entered
function checkForm() {

	if (!objForm.inventionDescription.value && objForm.inventionBoldEntry.checked) {
		objForm.inventionBoldEntry.checked = 0;
	}
	
	if (!objForm.exhibitDescription.value && objForm.exhibitBoldEntry.checked) {
		objForm.exhibitBoldEntry.checked = 0;
	}
}