// JavaScript Document

// Opens new window with specified image
function openImagePopup(imageURL, caption) {
   var newWindow = window.open('imagedoc.htm', 'imagePopup', 'scrollbars=yes, toolbar=no, width=580, height=500, resizable=yes, ');
   
   // Extract image table and table body
   var imageTable = newWindow.document.getElementById("zicofeTable"); 
   var tableBody = imageTable.tBodies[0];
   
   // Insert image into the popup
   insertImage(imageURL, caption, tableBody);
}

// Opens popup for selecting hotel
function openHotelPopup(){
	var newWindow = window.open('hotels.htm', 'hotelPopup', 'scrollbars=yes, toolbar=no, width=580, height=500, resizable=yes, ');
}

// Populates hotel details fields and closes the window
function selectHotel(hotelName, email) {
	// Populate the hidden fields and visible hotel field for the selected hotel
	opener.document.getElementById("hotel_hidden").value = hotelName;
	opener.document.getElementById("hotel").value = hotelName;
	opener.document.getElementById("email_hidden").value = email;
	
	// Close the popup window.
	window.close();
}

// Inserts the image into the popup
function insertImage(imageURL, caption, tableBody){
   var imageRow = tableBody.rows[0];
   var imageCell = imageRow.insertCell(0);
   imageCell.innerHTML = "<img src='" + imageURL + "'  align='middle'>";
   
   var captionRow = tableBody.rows[1];
   var captionCell = captionRow.insertCell(0);
   captionCell.innerHTML = "<div align='left'><span class='bodytext'>" + caption + "</span></div>";
}

// 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 an input value has been
// entered at all
function isEmpty(inputStr) {
	if (inputStr == null || inputStr == "") {
		return true;
	}
	return false;
}

// Validates the email entered.
function validateEmail(fieldValue){
   // The invalid characters that should not be used in an email address
   var invalidChars = " /:,;"; 
   var emailAddress = fieldValue;
   
   var atPosition = emailAddress.indexOf("@",1);
   var periodPosition = emailAddress.indexOf(".",atPosition);
   
   // Checks for the invalid characters listed above.
   for (var i=0; i<invalidChars.length; i++){
      badChar = invalidChars.charAt(i);
	  if (emailAddress.indexOf(badChar,0) > -1){
	     return false;		 
	  }
   }

   if (atPosition == -1){ // Checks for the @
      return false;
   }
   if (emailAddress.indexOf("@",atPosition + 1) > -1){ // Makes sure there is one @
      return false;
   }
   if (periodPosition == -1){ // Makes sure there is a period after the @ 
      return false;
   }
   // Makes sure there is at least 2 characters after the period
   if ((periodPosition + 3) > emailAddress.length){ 
      return false;
   }
   
   return true;
}

// function used to check email and display message
function isValidEmail(fieldname, msg) {
	if (!validateEmail(document.getElementById(fieldname).value)) {
		alert(msg);
		return false;
	}
	return true;
}