function submitForm(formId, action) {
  if(formId != null) {
    var formObject = document.getElementById(formId);
    if(action != null) {
      formObject.action.value=action;
    }
    //viewFormDebugged(formId);
    formObject.submit();
  }
}

function viewFormDebugged(formId) {

    var formObject = document.getElementById(formId);
    var str = 'formId: ' + formId + '\n';
	for (var i = 0; i < formObject.elements.length; i++) {
		str += formObject.elements[i].name + ": " + formObject.elements[i].value + "\n";
	}
	alert(str);
	alert(formObject.innerHTML);
}

function verifyThenSubmitToURL(formId, url) {
  verifyThenSubmitToURL_(formId, url, null, null);
}

function verifyThenSubmitToURL_(formId, url, submitMethod, command) {
	if(confirm("Are you sure?")) {
		submitToURL(formId, url, submitMethod, command);
	} 
}

function submitToURL(formId, url) {
  submitToURL(formId, url, null, null);
}

function submitToURLTarget(formId, url, target) {
  var targetElement = document.getElementById('_target');
  if(targetElement != null) {
  	targetElement.name = "_target" + target;
  	targetElement.value = "_target" + target;
	submitToURL(formId, url, null, null);
  } else {
    return false;
  }
}

function submitToURLTargetCmd(formId, url, target, command) {
  var targetElement = document.getElementById('_target');
  if(targetElement != null) {
  	targetElement.name = "_target" + target;
  	targetElement.value = "_target" + target;
	submitToURL(formId, url, null, command);
  } else {
    return false;
  }
}

function submitToURL(formId, url, submitMethod, command) {
  if(formId != null){
    var formObject = document.getElementById(formId);
    
    if(url != null){
      formObject.action=url;
    }
    
    if(submitMethod != null) {
      formObject.method=submitMethod;
    } 
    
    if(command != null) {
      formObject.command.value=command;
    }
   
    formObject.submit();
  }
}

function setAndSubmit(formId, elName, value, url, submitMethod) {
  setFormElement(formId, elName, value);
  submitToURL(formId, url, submitMethod);
}

function setFormElement(formId, elName, value) {
  if (formId != null) {
    var formObject = document.getElementById(formId);
    var el = formObject[elName];
    if (el != null && typeof el != "undefined") {
      el.value = value;
    }
  }
}


function setRadioValue(formId, elName, value) {
  if (formId != null) {
    var formObject = document.getElementById(formId);
    var el = formObject[elName];
    if (el != null && typeof el != "undefined") {
      if (typeof el.length == "undefined") {
      	el.checked = (el.value == value);
      } else {
        for (var i = 0; i < el.length; i++)
      	  el[i].checked = (el[i].value == value);
      }
    }
  }
}

var defaultBkg;  
function highlightRow(rowId) {
  var row = document.getElementById(rowId);
  defaultBkg=row.style.background;
  row.style.background='#cfcfbb';
  row.style.cursor='pointer';
}
  
function unHighlightRow(rowId) {
  var row = document.getElementById(rowId);
  row.style.background=defaultBkg;
  row.style.cursor='default';
  row.style.border='none';
}
  
function openURL(url) {
  if(url != null){
    window.location=url;
  }
}

function toggleElement(element) {
    var obj = document.getElementById(element);
    if(obj.style.display == "") {
    	obj.style.display = "none";
    } else {
     	obj.style.display = "";
    }
}

function showHideContrib() {
  var byline = document.getElementById("byline");
  if(byline.value == "contrib") {
  	showTRow("contrib_row");
  } else {
  	hideTRow("contrib_row");
  }
}

function showHideContrib_old() {
  var byline = document.getElementById("byline");
  if(byline.value == "contrib") {
   	enable("contributor");
  } else {
  	disable("contributor");
  }
}

function hideTRow(row) {
  var args = hideTRow.arguments; 
  
    var tRow = document.getElementById(row);
    tRow.style.display="none";
}

function showTRow(row) {
  var args = hideTRow.arguments; 
  
    var tRow = document.getElementById(row);
    tRow.style.display="";
}

function disable(element) {
  var args = hideTRow.arguments; 
  
    var tRow = document.getElementById(element);
    tRow.value="";
    tRow.disabled=true;
}

function enable(element) {
  var args = hideTRow.arguments; 
  
    var tRow = document.getElementById(element);
    tRow.disabled=false;
    tRow.focus();
}

function showModalWindow(url) {
  var uag = navigator.userAgent.toLowerCase();
  var uan = navigator.appName.toLowerCase();
  window.open(url,'Image_Browser','left=100,top=100,width=600,height=500,status=0,resizable=1,toolbar=0,modal=1');
}

function showImageWindow(html) {
  var xPos = 100;
  var yPos = 100;
  var defaultWidth  = 500;
  var defaultHeight = 500;
  
  var uag = navigator.userAgent.toLowerCase();
  var uan = navigator.appName.toLowerCase();
  var popup=null;
  if (uag.indexOf("microsoft") != -1 || uan.indexOf("microsoft") != -1) {
    popup=window.open('','Image_Browser','left='+yPos+',top='+xPos+',width='+defaultWidth+',height='+defaultHeight+',status=0,resizable=1,toolbar=0');
  } else {
    popup=window.open('','Image_Browser','left='+yPos+',top='+xPos+',width='+defaultWidth+',height='+defaultHeight+',status=0,resizable=1,toolbar=0,modal=1');
  }
  popup.document.writeln(html);
}

function popupImageWindow(image,title,legend){
  html='<html><head><script type="text/JavaScript" src="scripts/main.js" language="JavaScript"></script></head>';
  '<body bgcolor="black" text="white"><center>';
  html+='<table border="0" cellspacing="0" cellpading="0" >';
  html+='<tr><td colspan="2" align="center"><img name="'+title+'" src="'+image+'" onload="\'window.resizeTo(document.image.width,document.image.height)\'"></td></tr>';
  html+='<tr><td width="80%">'+legend+'</td>';
  html+='<td valign="top"><form><input type="button" class="action-button" value="Close" onClick="top.close()"></form></td></tr>';
  html+='</table></center></body></html>';
  showImageWindow(html);
}

function toggleSort(imgTagId, formElementId) {
    var sort = document.getElementById(formElementId); 
    var imgTag = document.getElementById(imgTagId); 
	if(sort.value == "asc") {
		sort.value = "desc";	
		imgTag.src="imgs/arrow_desc.gif";
	} else {
		sort.value = "asc";	
		imgTag.src="imgs/arrow_asc.gif";
	}
}

	function validateEMail(email,name){
		email = removeSpaces(email);
		if(email.length == 0){
			return true;
		}
		var emailArray = email.split(",");
		for(var i = 0 ; i < emailArray.length ; i++){
			var valid = isValidEmail(emailArray[i]); 
			if(valid == false){
				if(emailArray[i].length == 0){
					alert("Empty "+name+" email id is not allowed. Warning- There might be a comma at the first or last place. Please remove it. ");
				}
				else{
					alert(name+" email is not valid : ["+emailArray[i]+"]");
				}
				return false;
			}
		}
		return true;
	}

	function isValidEmail(str) {
		return str.length > 0 && (str.indexOf(".") > 2) && (str.indexOf("@") > 0 ) && ((str.length - 1 -  str.lastIndexOf(".")) > 1) && (str.lastIndexOf(".")  >  str.lastIndexOf("@")) ;
	}

	function removeSpaces(string) {
		var tstring = "";
		string = '' + string;
		splitstring = string.split(" ");
		for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];
		return tstring;
	}

	
function hideElement(elem) {
    var tElem = document.getElementById(elem);
    tElem.style.display="none";
}

function showElement(elem, display) {
    var tElem = document.getElementById(elem);
    tElem.style.display=display;
}

	
