// JavaScript Document
// CONSTANTS
// When loading content from an IFRAME into an HTML element, the 
// convention followed is that if the name of the element is XX,
// the IFRAME is IFRAME_PREFIX + XX. The constant below defines
// the value of the prefix
var IFRAME_PREFIX = "i";
// the ID of the table containing the list
var LIST_TABLE = "listtable";
// url used to download a file

// open window
function openWindow(fileName) { 
  // To specify the window characteristics edit the "features" variable below:
  // width - width of the window
  // height - height of the window
  // scrollbar - "yes" for scrollbars, "no" for no scrollbars
  // left - number of pixels from left of screen
  // top - number of pixels from top of screen
  features = "width=600,height=400,left=100,top=130,resizable=1, scrollbars=1";
  listwindow = window.open(fileName,"newWin", features);
  listwindow.focus();   
}

// Transfer the HTML within the BODY tag from an IFRAME to the specified target.
// The convention followed is that content from an IFRAME called iXX is loaded
// into an element XX.
function transferHTML(target) {
   // The name of the IFRAME into which the external document is loaded
   var srcIFrame = document.getElementById(IFRAME_PREFIX + target);
   // The content in the IFrame contained within the body tag
   var srcContent = (srcIFrame.contentDocument) ? srcIFrame.contentDocument.getElementsByTagName("BODY")[0].  innerHTML : (srcIFrame.contentWindow) ? srcIFrame.contentWindow.document.body.innerHTML : "";
   // Set the content of the target document
   document.getElementById(target).innerHTML = srcContent;
}

// opens the url to download a file in a new window.
// the complete url is built within the function
function downloadFile(ID) {
	openUrl(DOWNLOAD_MEDIA_FILE, ID);
}

function openUrl(url, ID) {
	window.location.href = url + "?ID=" + ID;
}

// Checks whether the file is a Zip file
function isZipFile(fieldName, message) {
   var fileUrl = document.getElementById(fieldName).value;
   
   var urlLength = fileUrl.length;
   
   // Get the position of the period from the url
   if (fileUrl.indexOf(".zip") == -1)   {
    	alert(message);
		return false;
   }
   return true;

}

// Checks whether the file is a Zip file
function fieldContainsZipFileName(fieldName, message, requiredFlag) {

	var fieldValue = document.getElementById(fieldName).value;
	
   // If the field is required check whether it contains a zip file
   if (requiredFlag == 'true'){
      return isZipFile(fieldName, message);
   }
	// The field is nor required.
	// If a value has been entered i.e. the field value is not an empty string
	// check whether its a name of a zip file
	if (isNullOrEmpty(fieldValue)) {
		return true;
	} else {
		return isZipFile(fieldName, message);
	}       
}

// Returns false if the field is empty, null, or has the string "null", and pops up
// the message passed to the function
function isNotNullOrEmptyString(fieldName, message) {
	if (isNullOrEmpty(document.getElementById(fieldName).value)) {
		alert(message);
		return false;
	}
	return true;
}

// general purpose function to see if an input value has been
// entered at all or if the input value has a value "null"
function isNullOrEmpty(inputStr) {
	if (isEmpty(inputStr) || inputStr == "null") {
		return true;
	}
	return false;
}

// general purpose function to see if a field has an empty value or not
// entered at all or if the input value has a value "null"
function isNullOrEmptyField(fieldName) {
	 return isNullOrEmpty(document.getElementById(fieldName).value);
}

// general purpose function to see if an input value has been
// entered at all
function isEmpty(inputStr) {
	if (inputStr == null || inputStr == "") {
		return true;
	}
	return false;
}

// function to check that the 2 password fields match
function matchPasswords(fieldName1, fieldName2, message) {		
	if (document.getElementById(fieldName1).value == document.getElementById(fieldName2).value) {
		return true;
	} else {
		alert(message);
		return false;
	}
}

// requires subscription message
function displayInformationBox(msg) {
	alert(msg);
}
