// 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=600,height=400,resizable=1,  toolbar=no');
		newWindow.document.writeln("<html><head><title>Commonwealth Women's Network :: Gallery</title>");
		newWindow.document.writeln("<LINK href='sylesheets/cwn.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='hometext' 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;
}

function deleteEntity(url, entity) {
	message = "Are you sure you want to delete this "+entity+"? \n" + 
					"Press OK to delete the "+entity+" \n" + 
					"Cancel to stay on the current page";
	if (window.confirm(message)) {
		window.location.href=url;
	}
}

// update a hidden field from a checkbox with a value
function updateHiddenFieldWithValue(fieldName, fieldValue) {
	if (document.getElementById(fieldName + "_checkbox").checked) {
		// set the hidden field value to true
		document.getElementById(fieldName).value = fieldValue;
	
	} else {
		// set the hidden field value to false
		document.getElementById(fieldName).value = "";
	}
}

// First checks whether the person should be given the rights
function checkRights(url, entity) {
	message = "Are you sure you want to give these rights to "+entity+"? \n" + 
					"Press OK to give "+entity+" the rights. \n" + 
					"Cancel to stay on the current page.";
	if (window.confirm(message)) {
		window.location.href=url;
	}
}