// get browser
var agent       = navigator.userAgent.toLowerCase(); 
var isIE        = (navigator.appName == "Microsoft Internet Explorer") ? true : false;
var isNetscape6 = (!document.all && !document.layers && navigator.appName == "Netscape" && parseInt(navigator.appVersion) >=5) ? true : false;
var isMac       = (agent.indexOf("mac") != -1) ? true : false;
var isIE4       = (agent.indexOf("msie") != -1 && agent.indexOf("4.0;") != -1) ? true : false;
var isNetscape  = (agent.indexOf("mozilla") != -1 && agent.indexOf("4.") != -1) ? true : false;
var win2000     = ((agent.indexOf("winnt") != -1) || (agent.indexOf("windows nt 5.0") != -1)) ? true : false; 
var winXP       = ((agent.indexOf("winnt") != -1) || (agent.indexOf("windows nt 5.1") != -1)) ? true : false; 
var isWin       = (agent.indexOf("windows") != -1) ? true : false;
var isMoz       = (agent.indexOf("gecko") != -1) ? true : false;


// popup functions
function open_popup (ref, url, width, height, scroll, menu, toolbar, location, status, resize) {
  var win = window.open(
    url,
    ref,
    "menubar="    + ((menu) ? 'yes' : 'no')  + ", " +
    "scrollbars=" + ((scroll) ? 'yes' : 'no')  + ", " +
    "toolbar="    + ((toolbar) ? 'yes' : 'no')  + ", " +
    "status="     + ((status) ? 'yes' : 'no')  + ", " +
    "location="   + ((location) ? 'yes' : 'no')  + ", " +
    "resizable="  + ((resize) ? 'yes' : 'no')  + ", " +
    "width=" + width + ", height=" + height
  );
  
  win.focus();
}


// empty form
function empty_form (form) {
  for (var i = 0; i < form.elements.length; i++) {
    if (form.elements[i].type == 'text') {
      form.elements[i].value = '';
    } else if (form.elements[i].type == 'checkbox' || form.elements[i].type == 'radio') {
      form.elements[i].checked = false;
    } else if (form.elements[i].type == 'select-one') {
      form.elements[i].selectedIndex = 0;
    }
  }
}


// these are page specific functions will need to put them in better place when get time
// this prevents the add / edit form getting submitted if no source has been filled in
function edit_prop_form_submit()
{	
	
	icount=12; // the number of evidence sources that exist
	
	atLeastOneSrc = false;
	
	for(i=1;i<icount;i++){
		src_id = "source_id_";
		src_id += i;
		s = document.getElementById(src_id);
		if(s.checked)
			atLeastOneSrc = true;
		
	}
	
	if(!atLeastOneSrc)
	{
		alert('please enter at least 1 source');
	}
	return atLeastOneSrc;
	
	
}


function checkTitleGender() {
	  /* get the values need to complete verify no mismatch */
	  var title = document.getElementById('title_select').value;
	  var male = document.getElementById('gender-male').checked;
	  var female = document.getElementById('gender-female').checked;

	  /* create flag to indicate mismatch */
	  var flag = 'NO';
	  var rtn_val = new Boolean(true);
	  	  
	  /* create array to hold reference data */
	  var male_array = new Array([1,"Mr"],[6,"Master"],[8,"Sir"],[9,"Lord"]);
	  var female_array = new Array([2,"Mrs"],[3,"Miss"],[5,"Ms"],[10,"Lady"]);
	  
	  /* check male gender was selected then check its not assigned a female title */
	  if (male == true) {
		  for(var i=0; i < female_array.length; i++) {
		      if (title == female_array[i][0])
		          flag = 'YES';
		  }
	  }

	  /* check female gender was selected then check its not assigned a male title */
	  if (female == true) {
		  for(var i=0; i < male_array.length; i++) {
		      if (male_array[i][0] == title)
		          flag = 'YES';
		  }
	  }

	  /* when a mismatch occurs the user is alerted with an option box */
	  if (flag == 'YES') {
		  var msgBox = confirm("The gender you have chosen appears to contradict the title chosen above. Are you sure you want to continue?");
			if (msgBox)
			  rtn_val = true;  //do submit for - correction not needed
			else
			  rtn_val = false;  //dont submit form - allow edit
	  }
	  return rtn_val;
}

//////////// used in the registration process if an existing property is found//////////////
function ifNeededAddParamUrl(url)
{
	fromYear = document.getElementById('id_start_date_from_year');
	if(fromYear != null ){
		// we are coming from the reg process so need to add someting to the url
		
		//alert(url.href);
		url.href += "&start_date_from_year="+fromYear.value;
		//alert(url.href);
	}
	else
	{
		//alert('start_date_from_year is null');   therefore we on the normal add property
	}
	
	
	// if its null dont add anything to the url
	
}

/* this is used in the reigster user form and in the acount details form */
//if a male title is selected hide the maiden name etc
function showhide_maiden_name_if_needed(selectedIndexValue)
{
	var hideOnThese = Array(1,3,6,8,9); // 1=mr , 6=master, 8=sir ,9=lord 
	
	hideRows = false;
	
	for ( var i=0, len=hideOnThese.length; i<len; ++i ){
		if(hideOnThese[i] == selectedIndexValue)
		{
			document.getElementById('id_maiden_name_row').style.display = 'none';   // remember to re-show ifmale title selected again	
			document.getElementById('id_other_married_row').style.display = 'none';
			hideRows=true;
			break;			
		}
	}
	
	if(!hideRows) // if we dont need to hide make sure we show them
	{
		document.getElementById('id_maiden_name_row').style.display = ''; 	
		document.getElementById('id_other_married_row').style.display = '';
		
	}
	
	
}


// utility
function trim(str)
{
	var trimmed = str.replace(/^\s+|\s+$/g, '') ;
	return trimmed;
}
/**
 * tells us if a variable has been defined
   example isdefined('window','my_var')
 */
function isdefined(object, variable)
{
	return (typeof(eval(object)[variable]) == "undefined")? false: true;
}



function toggle_showhide(id) {

	var e = document.getElementById(id);
	toggle_showhide_e(e);
}

function toggle_showhide_e(e) {
	
	if(e.style.display != 'none')
		e.style.display = 'none';
	else
		e.style.display = '';	
}

function is_numeric(n)
{

	return !isNaN(parseFloat(n)) && isFinite(n);

}
function confirm_delete() {	
	str='are you sure you want to delete this record';
	return confirm_delete_str(str) ;
}
function confirm_delete_str(str) {
   if (confirm(str)) {
    return true;
  } else {
    return false;
  }
}