// 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
	  }
	  
	  /* the function checkPersonAgeOver100() can be found in the my_ac_persons_addedit.js */
	  if(checkPersonAgeOver100()==false) {
		  rtn_val = false;
	  }
	  
	  
	  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);
	return toggle_showhide_e(e);
}

function toggle_showhide_e(e) {
	
	if(e.style.display != 'none')
	{
		e.style.display = 'none';
		return "hide";
	}
	else
	{
		e.style.display = '';
		return "show";
	}
}

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;
  }
}

/**
 * radios can be multiple elements in a page so get by id wont work
 * eg 'sex' might have 2 radios of m , f
 * @param name
 * @return
 */

function getSelectedRadioValue(name) {

	var radios = document.getElementsByName(name);

    for (var i = 0; i < radios.length; i++) {
     if (radios[i].checked) {
         return radios[i].value;
     }
    }

    return null;
}

/**
 * radios can be multiple elements in a page so get by id wont work
 * eg 'sex' might have 2 radios of m , f
 * @param name , val of radio (eg m in the example above)
 * @return
 */	     

function setRadio(name, val) {
	
	var radios = document.getElementsByName(name);

    for (var i = 0; i < radios.length; i++) {
            radios[i].checked = (radios[i].value == val);
    }

}

/**
 * tests if a js object isset/exists   
 * eg 
 * if(isset(document.getElementById('is_this_in_page')))
 * 	then do somtihng with it
 * else
 * 	do nout
 * @param me
 * @return
 */
function isset(me)
{
	 if (me == null || me == '')
	  return 0;
	 else
	  return 1;
}


/**
 * this is used on forms that fail because of dubious data entry to allow the user to contact the 
 * admin. all data from failed form is forwarded to this page (so admin will see why form failed)
 * @return
 */

function sendBogusFormToAdmin()
{
	// change the place the form submits to
	// 'id_add_property_form' form id
	//debugger;
	form = document.forms[0];
	
	form.action = '/my-account/messages/contact_to_verify.php';
	
	form.submit();
	
}
