// JavaScript Document

// Variable to be used by many functions
var newWindow = "";

// Function to open images in a popup window and just replaces the image in the popup 
// with the clicked image.
function openPopup(url, caption) {
	if (newWindow.location && !newWindow.closed) {
        newWindow.location.href = url;
    	newWindow.focus(); 
	} else {
        newWindow=window.open('','htmlname','width=750,height=560,resizable=1,  toolbar=no');
		newWindow.document.writeln("<html><head><title>RALLY UGANDA - Photo Gallery</title>");
		newWindow.document.writeln("<LINK href='../../../stylesheets/rallyug.css' rel='stylesheet' type='text/css'></head>");
    	newWindow.document.writeln("<body style='margin: 0 0 0 0;' >");
    	newWindow.document.writeln("<table border='0' cellpadding='0' cellspacing='0' align='center'><tr><td class='imgborder' align='center'><a href='javascript:window.close();'>");
    	newWindow.document.writeln("<img src='" + url + "' alt='Click to close' id='bigImage'  border='0'/></a>");
    	newWindow.document.writeln("</td></tr><tr><td class='newsheader' align='center'>"+caption+"</td></tr></table>");
    	newWindow.document.writeln("</body></html>");
    	newWindow.document.close();
	}
}

// Neatly closes image window
function closeImagePopup() {
	if (newWindow.location && !newWindow.closed) {
   		newWindow.close();  
	}
}

// Inserts the image into the popup
function insertImage(imageURL, caption, imageTableBody){
   var imageRow = imageTableBody.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='text'>" + caption + "</span></div>";
}

// Validates form
function validateForm(){
   var form = document.forms[0];
	if(isFieldEmpty('email', 'Email Address') && isTextFieldEmpty() && isSelectionMade()){ 
		// validate email address
		emailString = document.getElementById('email').value;
		if((emailString.indexOf('@') == -1) && (emailString.indexOf('.') == -1) && ((emailString.indexOf('.') + 2) > emailString.length)){
			alert("Email is wrong");
			return false;
		} else {
			return true;
		}
	}
	return false;
}

// Checks whether the field is empty and returns a message
function isFieldEmpty(field, userFieldName){
	if (document.getElementById(field).value == "") {
		alert("Please enter a value for the " + userFieldName + ".");
		return false;
	}
	return true;
}

// Makes sure an option is selected
function isSelectionMade() {
	var list = document.forms[0].topic;
	var chosenItem = list.options[list.selectedIndex].value;
	if (chosenItem == ""){
	   alert("Please select a topic.");
		return false
	}
	return true;
}

// Checks whether the message textfield is empty
function isTextFieldEmpty(){
	if(document.forms[0].message.value == "") {
		alert("Please enter a message.");
		return false;
	}
	return true;
}

// Client Login message 
function clientLoginMessage(){
	//if(document.forms[0].message.value == "") {
		alert("Please enter correct Client name and Password.");
		//return false;
	//}
	return true;
}