/* This file contains the JavaScripts used by Amlib Netopacs. Do NOT modify the contents of this file */
/* Version History

Netopacs Version 5.1
	5.1.2.17 - 28/12/2007 - SR 23341 & 21521 - Added code for search result emailing.
	5.1.2.23 - 17/03/2008 - SR 23341 & 21521 - Email addresses are validated using regular expressions.
	5.1.2.27 - 22/05/2008 - SR 23341 & 21521 - When emailing results using checkboxes, the selected items are updated via GetPBNameC

*/


/************ Common functions **************/
function SetFocus() {
	var theForm = document.forms[0];
	
	for (var i=0; i<theForm.length; i++){
		if (theForm[i].type == "text") {
        theForm[i].focus();
        break;
   		}
	}
}

function NewWindow(href, winName, winWidth, winHeight) {
	var windowParams = "toolbar=no,scrollbars=yes,resizable=yes,status=yes";
	
	if (winWidth != 0 && winHeight != 0){
		windowParams += ",width="+winWidth+",height="+ winHeight;
	}
	
	var theWindow = window.open(href, winName, windowParams);	
}

function CloseWindow(){
	window.close();
}

function GetPBName(butt) {
	var theForm = document.forms[0];	
	theForm.v1.value = butt; 
	theForm.submit();
}

function GetPBNameC(butt) {
	if (butt == "pbDeleteInts" || butt == "pbAddInts"){
		SubmitSubInts(butt);
	}
	else{
		var theForm = document.forms[0];	
	
		theForm.v1.value = butt;
		
		if (theForm.v27){
		
			theForm.v27.value = "0"; 
	
			for (var i=0; i<theForm.length; i++){
				if ((theForm.elements[i].type=="checkbox") && (theForm.elements[i].checked == true)){				
					theForm.v27.value += ("," + theForm.elements[i].value);
				}
			}
		}
		
		theForm.submit();
	}
}

function ShowSubmitString(){
	var theForm = document.forms[0];
	var submitStr = "";
	
	for (var i=0; i<theForm.length; i++ ){
			var elementName = theForm.elements[i].name;
			if(elementName.substr(0,1) == "v"){
				if(submitStr != ""){submitStr += ",";}
				submitStr += elementName;
			}
	}	
	
	alert(submitStr);
}

/************ Form validation **************/

function ValidateForm(theForm, validType){
	switch (validType){
		case 0: // No validation
			return ValidateNothing();
			break;
		case 1: // ILLS
			return ValidateILLS(theForm);
			break;
		case 2: // Bor change details
			return ValidateChangeDets(theForm);
			break;
		case 3: // Bor change details no PIN
			return ValidateChangeDetsNoPin(theForm);
			break;
		case 4: // Join Online
			return ValidateJoinOnline(theForm, false);
			break;
		case 5: // Join Online w/location
			return ValidateJoinOnline(theForm, true);
			break;
		case 6: // Reserve
			return ValidateReserve(theForm, false);
			break;
		case 7: // Join Reserve w/location
			return ValidateReserve(theForm, true);
			break;
		case 8: // Keyword search
			return ValidateTextField(theForm, "v28", "You must enter a search term");
			break;
		case 9: // Authority search
			return ValidateTextField(theForm, "v2", "You must enter a search term");
			break;
		case 10: //Opac suggests
			return ValidateTextField(theForm, "v2", "You must enter a comment");
			break;
		case 11: //Book review
			return ValidateBookReview(theForm);
		case 12: //Never submit the form
			return false;
			break;
	}
}

function ValidateNothing(){
	return true;
}

function ValidateILLS(theForm){	
	// Check the title
	if (theForm.v3.value == "" || theForm.v3.value == " "){
		alert("You must enter a title for the item you wish to request.");
		return false;
	}
	
	// Check the input type for v14 (location)
	// If it's a hidden field then assume there is only one location and that it has
	// been entered as the pickup location.
	// Otherwise check if it is a drop down box and make sure the first index is not selected.
		
	sv14 = theForm.elements["v14"]
	// Make sure there was a v14 field specified.
	if (sv14 == null){
		alert("A location has not been specified. The template needs to be modified");
		return false;	
	}
	
	sType = sv14.type;
	
	if (sType == "hidden"){
		return true;
	}
	else if (sType.indexOf("select") >= 0){	

		if (sv14.selectedIndex != 0 ){
			return true;
		}
		else{
			alert("You must choose a location.");
			return false;
		}	
	}
}

function ValidateChangeDets(theForm){
		
	// Make sure they have confirmed their new pin.
	if (theForm.v36.value != "" & theForm.v37.value == "") {
		alert("You must confirm your pin.");
		return false;
	}

	// Make sure they have not just typed in the confirm pin field.
	else if (theForm.v36.value == "" & theForm.v37.value != "") {
		alert("You have only typed your pin in the confirm field.");
		return false;
		}
	// Make sure pins are the same.
	if (theForm.v36.value !=  theForm.v37.value ){
		alert("Please re-confirm your pin.");
		return false;			
	}
	
	// Counts how many valid form items there are and how many of these are blank.
	// Valid forms are anything other than forms with a type of "hidden" or "submit".
	validFormItems = 0;
	blankFormItems = 0;

	for (nIndex = 0; nIndex < document.forms[0].elements.length; nIndex++){
		formType = document.forms[0].elements[nIndex].type
		if (formType != "hidden" & formType != "submit"){
			validFormItems++;
		
			if (document.forms[0].elements[nIndex].value == ""){
				blankFormItems++;
			}
		}
	}
	
	if (validFormItems == blankFormItems){
		alert("You have not entered any data.");
		return false;		
	}
	else{
		return true;
	}
}

function ValidateChangeDetsNoPin(theForm){		
	// Counts how many valid form items there are and how many of these are blank.
	// Valid forms are anything other than forms with a type of "hidden" or "submit".
	validFormItems = 0;
	blankFormItems = 0;
	
	for (nIndex = 0; nIndex < document.forms[0].elements.length; nIndex++){
		formType = document.forms[0].elements[nIndex].type
		if (formType != "hidden" & formType != "submit"){
			validFormItems++;
		
			if (document.forms[0].elements[nIndex].value == ""){
				blankFormItems++;
			}
		}
	}

	if (validFormItems == blankFormItems){
		alert("You have not entered any data.");
		return false;		
	}
	else{
		return true;
	}
}

function ValidateJoinOnline(theForm, validateLoc){

	if (! (ValidateTextField(theForm, "v2", "You must enter your given name"))){return false;}
	if (! (ValidateTextField(theForm, "v3", "You must enter your surname"))){return false;}
	if (! (ValidateTextField(theForm, "v4", "You must specify your sex"))){return false;}	

	if (theForm.v5){
		if (theForm.v5.value == "" & theForm.v6.value == ""){alert("You must supply a password.");return false;}
		if (theForm.v5.value.length < 6){alert("Your password must contain at least 6 characters");return false;}
		if (theForm.v5.value != "" & theForm.v6.value == ""){alert("You must confirm your password.");return false;}		
		if (theForm.v5.value == "" & theForm.v6.value != ""){alert("You have only typed your password in the confirm field.");return false;}
		if (theForm.v5.value != theForm.v6.value ){alert("Please re-confirm your password.");return false;}						
	}

	if(validateLoc){
		if (ValidateLocation(theForm, "v9", "You must choose a location")){
			return true;
		}
		else{
			return false;
		}
	}
	else{
		return true;
	}		
	
}

function ValidateReserve(theForm, validateLoc){
	if (ValidateTextField(theForm, "v31", "Please enter your library card number")){
		if( validateLoc ){
		
			if (ValidateLocation(theForm, "v33", "You must choose a location")){
				return true;
			}
			else{
				return false;
			}			
		}
		else{
			return true;
		}
	}
	else{
		return false;
	}
}

function ValidateBookReview(theForm){
	if (ValidateTextField(theForm, "v31", "Please enter your library card number.")){
		if (ValidateLocation(theForm, "v33", "You must rate the item.")){
			return true;
		}
		else{
			return false;
		}
	}
	else{
		return false;
	}
}

// Ensures and text field has a valid value
function ValidateTextField(theForm, textField, errorMsg){
	var valid = true;
	if (theForm.elements[textField]) {
		(theForm.elements[textField].value == "")?valid=false:valid=true;
	}
	
	if ( !valid && errorMsg ){alert(errorMsg);}	
	
	return valid;
}

// Validates a location drop down.
function ValidateLocation(theForm, locField, errorMsg){
	var valid = true;
	if (theForm.elements[locField]){
		(theForm.elements[locField].selectedIndex != 0)?valid=true:valid=false;
	}
	
	if ( !valid && errorMsg ){alert(errorMsg);}
	return valid;	
}


/************ Change details **************/

function ChangePin(theForm){
		
	// Make sure a PIN has been entered
	if (theForm.v36.value == ""  & theForm.v37.value == ""){
		alert("Please enter a new PIN in both fields.");
		return false;
		}
	// Make sure they have confirmed their new PIN.
	else if (theForm.v36.value != "" & theForm.v37.value == "") {
		alert("You must confirm your PIN.");
		return false;
	}

	// Make sure they have not just typed in the confirm PIN field.
	else if (theForm.v36.value == "" & theForm.v37.value != "") {
		alert("You have only typed your PIN in the confirm field.");
		return false;
	}
	// Make sure PINs are the same.
	else if (theForm.v36.value !=  theForm.v37.value ){
		alert("Please re-confirm your PIN.");
		return false;			
		}
	// If everything is OK, set the values in the Amlib form and submit it.
	else{
			
		document.forms["amlib"].elements["v36"].value = theForm.elements["v36"].value
		document.forms["amlib"].elements["v37"].value = theForm.elements["v37"].value
		document.forms["amlib"].elements["v38"].value = "Y";
		document.forms["amlib"].submit();			
		return false;
	}
}

function ChangeEmail(theForm){
		
	// Make sure a PIN has been entered
	if (theForm.v36.value == ""){
		alert("Please enter a new Email address.");
		return false;
	}	
	// If everything is OK, set the values in the Amlib form and submit it.
	else{
		document.forms["amlib"].elements["v39"].value = theForm.v36.value;
		document.forms["amlib"].submit();			
		return false;
	}
}


/************ Join Online **************/

function InitJoinOnline(theLink, windowParams){
	
	if (theLink != "" && windowParams != ""){
		var theWindow = window.open(theLink, "Barcode", windowParams);
	}
	
	// Disable the submit button if the Terms and Conditions checkbox is available.
	if (document.forms[0].v8){
		DisableSubmit();
	}
}

// Disables the submit button.
function DisableSubmit(){
	var nIndex = GetSubmitIndex();
	document.forms[0][nIndex].disabled = true;
}

// Enables the submit button
function EnableSubmit(){
	var nIndex = GetSubmitIndex();
	document.forms[0][nIndex].disabled = false;
}

// Toggles the submit button, depending on the state of the check box.
function ToggleSubmit(){
	if (document.forms[0].v8){		
		if (document.forms[0].v8.checked){
			EnableSubmit();
		}
		else{
			DisableSubmit();
		}
	}
}

// Toggles the submit button, depending on the state of the check box.
function ToggleSubmit88(){
	if (document.forms[0].v88){	
		if (document.forms[0].v88.checked){
			EnableSubmit();
		}
		else{
			DisableSubmit();
		}
	}
}

// Returns the index of the first submit button on the form.
function GetSubmitIndex(){
	for (var i=0; i<document.forms[0].length; i++){
		if (document.forms[0][i].type == "submit"){
			return i;			
		}
	}
	return -1;
}

/************ Subject Interests **************/

function SubmitSubInts(sButtonName){		
	var frmAmlib = document.forms[0];
	var sAuths;

	frmAmlib.elements.v3.value = "";
	frmAmlib.elements.v4.value = "";

	if (sButtonName == "pbDeleteInts"){
		sAuths = BuildCheckBoxList(frmAmlib, "int");
		if (sAuths == ""){
			alert("Please select some authorities to delete");
		}
		else{
			if (confirm("Are you sure you want to delete these interests?")){
				frmAmlib.v1.value = sButtonName;
				frmAmlib.elements.v4.value = sAuths;
				frmAmlib.submit();
			}				
		}
	}
	else if (sButtonName == "pbAddInts"){
		sAuths = BuildCheckBoxList(frmAmlib, "auth");
		
		if (sAuths == ""){
			alert("Please select some authorities to add");
		}
		else{				
			frmAmlib.v1.value = sButtonName;
			frmAmlib.elements.v3.value = sAuths;
			frmAmlib.submit();							
		}
	}
}

// Returns a string of check box IDs where the check box ID begins with the passed string.

function BuildCheckBoxList(frmData, sPrefix){
	var nItems = frmData.length;
	var sID, sType;
	var nPrefixLength = sPrefix.length;
	var sValues = "";

	for (var i=0; i<nItems; i++){
		sID = frmData.elements[i].id;
		sType = frmData.elements[i].type;

		if (sType = "checkbox" && sID.substring(0,nPrefixLength) == sPrefix && frmData.elements[sID]  && frmData.elements[sID].checked){
			if ( sValues != "" ){
				sValues += ",";
			}
			sValues += frmData.elements[sID].value;
		}
	}
	
	return sValues;
}

/************ Borrower Details **************/

function ConfirmCancel(sLink, sItemNo, sBorBarcode, bBooking){
	var sType;

	bBooking?sType="booking":sType="reservation";
	
	if (confirm("Are you sure you want to cancel this " + sType + "?")){
		location.href = sLink + "&v8=" + sItemNo + "&v31=" + sBorBarcode + "&v9=" + bBooking;
	}
}

function ConfirmMultiCancel(checkString){
	var resNos;
	var sResType;
	var theForm = document.forms[0];
	var	bBooking = (checkString=="BOOKNO");

	resNos = GetCheckboxItems(checkString);
	
	bBooking?sResType="booking":sResType="reservation";
		
	if (resNos == ""){
		alert("You must select at least one " + sResType + ".")	
	}
	else if ( confirm("Are you sure you want to cancel the selected " + sResType + "(s)?") ){
			theForm.v11.value = bBooking + ":" + resNos;
			GetPBName("pbMultiCancel");
	}
}
	
function ConfirmChangeLoc(){
	var resNos;
	var sResType;
	var theForm = document.forms[0];

	resNos = GetCheckboxItems("RESNO");
		
	if (resNos == ""){
		alert("You must select at least one reservation.")	
	}
	else if ( confirm("Are you sure you want to change the collection location for the selected reservation(s)?") ){
				
			// Add loc codes to each res no

			var saResNos = new Array();
			var i, j;
			var newResNos = "";

			saResNos = resNos.split(",");
				
			j = saResNos.length;
			for (i=0;i<j;i++){
				var resNo = saResNos[i];
				if(newResNos != ""){newResNos += ",";}
				newResNos += (resNo + ":" + theForm.elements["RESLOC:" + resNo ].value);
			}

			theForm.v12.value = newResNos;
			GetPBName("pbChangeResLocs");
	}					
}

function CheckChangeLoc(resNo){	
	var resLoc = "RESLOC:"
	var resNos;
	var saResNos = new Array();
	var i, j;
	var theForm = document.forms[0];
	
	resNos = GetCheckboxItems("RESNO");
	saResNos = resNos.split(",");
	j = saResNos.length;
	
	for (i=0;i<j;i++){
		if (!(theForm.elements[resLoc + saResNos[i]])){
			if(theForm.changeLoc){ theForm.changeLoc.disabled = true;}
			return false;
		}
	}
	
	if(theForm.changeLoc){ theForm.changeLoc.disabled = false;}
	return true;
}


/************ Reserve Functions **************/

function FixLocation(sLoc, sLocName){
	var ReserveForm;
	
	ReserveForm = document.forms[0].v33;
	
	if (ReserveForm){
		var sType = ReserveForm.type;
		if (sType.substr(0,6) == "select"){
			ReserveForm.length = 0;
				
			var locOption = document.createElement("option");
			locOption.text = sLocName;
			locOption.value = sLoc;
						
			ReserveForm.add(locOption);
		}
	}
}

/************ Opac Suggests Functions **************/

function ConfirmSugDelete(){	
	var suggestNos;
	var theForm = document.forms[0];

	suggestNos = GetCheckboxItems("OSNO");	
		
	if (suggestNos == ""){
		alert("You must select at least one suggestion.")	
	}
	else if ( confirm("Are you sure you want to delete the selected suggestion(s)?") ){
			theForm.v13.value = suggestNos;
			GetPBName("pbOSDelete");
	}
}

/************ Book Reviews Functions **************/

function ConfirmReviewDelete(){	
	var reviewNos;
	var theForm = document.forms[0];

	reviewNos = GetCheckboxItems("BRNO");	
		
	if (reviewNos == ""){
		alert("You must select at least one book review.")	
	}
	else if ( confirm("Are you sure you want to delete the selected review(s)?") ){
			theForm.v14.value = reviewNos;
			GetPBName("pbBRDelete");
	}
}

/************ Misc Functions **************/

function GetCheckboxItems(checkSting){
	var theForm;		
	var itemNos = "";
	var checkSting;
	
	theForm = document.forms[0];
		
	var i, j;		
					
	j = theForm.length;
	
	for (i=0;i<j;i++){
		var sType, sID;
		var n;
		sType = theForm[i].type
		sID = theForm[i].id;
		n = sID.indexOf(":");	
		
		if ((sType.toLowerCase() == "checkbox") && (sID.substr(0,n) == checkSting) && (theForm[i].checked)){							
			if (itemNos != ""){itemNos += ",";}					
			itemNos += sID.substr((n+1),sID.length);
		}	
	}
	
	return itemNos;
}

function EnableRadio(theForm, radioName, radioValue ){
	if(theForm.elements[radioName]){
		var j = theForm.elements[radioName].length;
	
		for(var i=0; i<j; i++){
			if(theForm.elements[radioName][i].value == radioValue){
				theForm.elements[radioName][i].checked = true;
				break;
			}
		}
	}
}

function SetValue( theForm, theElement, theValue ){
	if(theForm.elements[theElement]){
		theForm.elements[theElement].value = theValue;
	}
}

function DisableField( theForm, theElement){
	if(theForm.elements[theElement]){
		theForm.elements[theElement].disabled = true;
	}	
}

function GetEmailAddress(button){
	var email = prompt("Please enter your email address", "");
	var reg = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	
	if (email != null){
		if (!reg.test(email)){
			bContinue = confirm("The email address you have entered does not appear to be valid.\nDo you want to continue anyway?");
		}
		else{
			bContinue = true;
		}
	
	if (bContinue){
			var frmAmlib = document.forms[0];
			frmAmlib.v10.value = email;
			
			if (button == "pbEmailSaved"){
				GetPBNameC(button);
			}		
			else{
				frmAmlib.v1.value = button;			
				frmAmlib.submit();
			}
		}
	}
}
