/* change page location to option selected from navigation drop-down */
function goUrl(Selected,anchor) {
	var newURL = "?ll=" + Selected.options[Selected.selectedIndex].value ;
	if(anchor){
		newURL  = newURL + "#" + anchor;
	}
	if (newURL!=" ") {
		document.location.href = newURL;
	}
}

function check4cookie()
{
	var tmpcookie = new Date();
   	chkcookie = (tmpcookie.getTime() + '');
    document.cookie = "chkcookie=" + chkcookie + "; path=/";
	var exp = new Date();
  	exp.setTime (exp.getTime() - 1);
	if (document.cookie.indexOf(chkcookie,0) < 0) {
		var version = parseInt(navigator.appVersion);
		// replace is supported
		if (version>=4 || window.location.replace){
		    window.location.replace("/help/website/cookies.html");
		}
		alert("Your browser does not appear to be accepting cookies.\nYou will need to accept a cookie from NDAD to access the data or documentation.\nPlease read the information on the following page for more information about cookies.");
		
		return false;
	}else{
	   	document.cookie = "chkcookie=" + chkcookie + "; path=/; expires=" + exp.toGMTString();
		return true;
	}
	

}

//this version allows people to define a lot more features...
//but may need some defaults setting...
function open_window(url,name,width,height,top,left,resize,scroll,location,toolbar) {
//	some default values
		if ( scroll == null ) scroll = 'yes';
	   	if ( width == null ) width = 400;
       	if ( height == null ) height = 300;
		if ( resize == null ) resize = 'yes';

	var temp = "popup = window.open('" 
		+ url + "', '" 
		+ name + "', '" 
		+ "width=" + width 
		+ ',height=' + height 
		+ ',top=' + top 
		+ ',left=' + left 
		+ ',resizable=' + resize 
		+ ',scrollbars=' + scroll 
		+ ',location=' + location 
		+ ',toolbar=' + toolbar 
		+ "'); popup.focus();";
		eval(temp);
	
}

function special_open_window(url,form_element,name,width,height,top,left,resize,scroll,location,toolbar) {
//	some default values
		url=url+form_element;
		if ( scroll == null ) scroll = 'yes';
	   	if ( width == null ) width = 400;
       	if ( height == null ) height = 300;
	var temp = "popup = window.open('" 
		+ url + "', '" 
		+ name + "', '" 
		+ "width=" + width 
		+ ',height=' + height 
		+ ',top=' + top 
		+ ',left=' + left 
		+ ',resizable=' + resize 
		+ ',scrollbars=' + scroll 
		+ ',location=' + location 
		+ ',toolbar=' + toolbar 
		+ "'); popup.focus();";
		eval(temp);

}

function dis_for_csv(sel_obj){
	if(sel_obj.value == "CSV"){
		document.steps.ndisp_r.disabled=true;
		document.steps.ndisp_c.disabled=true;
		document.steps.scrolling.disabled=true;
	}else{
		document.steps.ndisp_r.disabled=false;
		document.steps.ndisp_c.disabled=false;
		document.steps.scrolling.disabled=false;
	}
}


function isEmailAddr(email)
{
	var result = false;
    var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0){
		var pindex = theStr.indexOf(".",index);
    	
		if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;
	}
		return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;

  	if (required && !validRequired(formField,fieldLabel))
    	result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) ){
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}

	return result;
}

function inValidCharSet(str,charset)
{
	var result = true;

  	// Note: doesn't use regular expressions to avoid early Mac browser bugs
	for (var i=0;i<str.length;i++){
    	if (charset.indexOf(str.substr(i,1))<0){
			result = false;
			break;
		}
	}
	return result;
}
function allDigits(str)
{
 	return inValidCharSet(str,"0123456789");
}

function validNum(formField,fieldLabel,required)
{
	var result = true;

 	if (required && !validRequired(formField,fieldLabel))
    	result = false;

	if (result)
	{
		if (!allDigits(formField.value))
		{
			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	}

	return result;
}
function validInt(formField,fieldLabel,required)
{
 	var result = true;

  	if (required && !validRequired(formField,fieldLabel))
    	result = false;

	if (result){
		var num = parseInt(formField.value);
		if (isNaN(num)){
			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	}

	return result;
}
function validDate(formField,fieldLabel,required)
{
 var result = true;

	if (required && !validRequired(formField,fieldLabel))
    	result = false;

	if (result){
		var elems = formField.value.split("/");
		
		result = (elems.length == 3); // should be three components

		if (result){
			var month = parseInt(elems[0]);
			var day = parseInt(elems[1]);
			var year = parseInt(elems[2]);
			result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
			allDigits(elems[1]) && (day > 0) && (day < 32) &&
			allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
		}

		if (!result){
			alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.');
			formField.focus();
		}
	}
	return result;
}

function checkFieldsAreSame(formField1,formField2)
{
    var result = true;
	
  	if (!validRequired(formField1,"Password"))
		return false;

	if(formField1.value != formField2.value){
		alert('Please make sure that your two passwords match.');
		formField1.focus();
		result=false;	
	}

	return result;
}
function validRequired(formField,fieldLabel)
{
	var result = true;

  	if (formField.value == "")
   	{
    	alert('Please enter a value for the "' + fieldLabel +'" field.');
	   	formField.focus();
	    result = false;
	}

	return result;
}
// drop an EXAMPLE_validateForm type function in your page (call it somethinf else) so it can us the 
// above to do all those boring form checking chores...

function EXAMPLE_validateForm(theForm)
{

  // Start ------->
   	if (!validRequired(theForm.fullname,"Name"))
		return false;

	if (!validEmail(theForm.email,"Email Address",true))
    	return false;

	if (!validDate(theForm.available,"Date of Birth",true))
		return false;

	if (!validNum(theForm.yearsexperience,"No of eyes",true))
		return false;
  // <--------- End

    return true;
}

//slightly dopey disabler... will diable upto two form elements on checking of one form element
function disable_elements(disabler,disablee1,disablee2){
	
	if(disabler.checked){
		disablee1.disabled=true;
		disablee2.disabled=true;
	}else{
		disablee1.disabled=false;
		disablee2.disabled=false;
	}
}

// hide/unhide_block - Dynamically hide or show a block-level element
// We set both display and visibility, because not all browsers
// support both. If a browser doesn't support display but does
// support visibility (eg NS4), you'll get a blank space where
// the hidden block was instead of everything shuffling up, but
// at least it should disappear.
function hide_block(eid)
{
  if (document.getElementById(eid)) {
    document.getElementById(eid).style.visibility = 'hidden';
    document.getElementById(eid).style.display = 'none';
  } else if (document.layers) {
    document.layers[eid].visibility = 'hide';
    document.layers[eid].display = 'none';
  } else if (document.all) {
    document.all[eid].style.visibility = 'hidden';
    document.all[eid].style.display = 'none';
  }
}

function unhide_block(eid)
{
  if (document.getElementById(eid)) {
    document.getElementById(eid).style.display = 'block';
    document.getElementById(eid).style.visibility = 'visible';
  } else if (document.layers) {
    document.layers[eid].display = 'block';
    document.layers[eid].visibility = 'show';
  } else if (document.all) {
    document.all[eid].style.display = 'block';
    document.all[eid].style.visibility = 'visible';
  } 
}

// This is an alternative to hide/unhide_block, which in a few browsers
// when used on an <img> tag (or block enclosing it) caused the image
// not to be downloaded while the block was hidden. (Reported in Moz 1.2.1,
// also apparently applies to some versions of Opera and MSIE).
// Here we instead position the block off-screen, then re-instate it 
// to the normal flow. Cross-browser supported not tested. I have 
// doubts about the document.layers (NS4) support, but who still uses that?
function move_block_offscreen(eid)
{
  if (document.getElementById(eid)) {
    document.getElementById(eid).style.position = 'absolute';
    document.getElementById(eid).style.top = '-4000px';
  } else if (document.layers) {
    document.layers[eid].position = 'absolute';
    document.layers[eid].top = '-4000px';
  } else if (document.all) {
    document.all[eid].style.position = 'absolute';
    document.all[eid].style.top = '-4000px';
  }
}

function move_block_onscreen(eid)
{
  if (document.getElementById(eid)) {
    document.getElementById(eid).style.position = 'static';
    document.getElementById(eid).style.top = '0';
  } else if (document.layers) {
    document.layers[eid].position = 'static';
    document.layers[eid].top = '0';
  } else if (document.all) {
    document.all[eid].style.position = 'static';
    document.all[eid].style.top = '0';
  }
}


// getElementById plus equivalents in earlier browsers
// (Can't use for hide/unhide and other style-based switching)
function elementById(eid)
{
  if (document.getElementById(eid)) {
    return document.getElementById(eid);
  } else if (document.layers) {
    return document.layers[eid];
  } else if (document.all) {
    return document.all[eid];
  }
}


// Disable form elements on submission. 
function disableForm(theform) {
    for (i =0 ; i < theform.length; i++) {
      var obj = theform.elements[i];
      obj.readOnly = 'true';
      if (obj.name == "submitbutton") {
        obj.disabled = "true";
        // Would be nice to replace with 'Please Wait', but not working
        // obj.setattribute("src", "/tna/images/buttons/searchwales.gif");
        // obj.setattribute("alt", "Please Wait");
      }
    }
  }

/**************  The following are used in the querybuilder user interface *******************/

//opens popup field sum based on menu choice
function explainOptions(the_select,the_other_select,prefix,suffix,page)
{
	var location='';
	for (loop=0; loop < the_select.options.length; loop++){
		if (the_select.options[loop].selected == true){
			location = prefix + encodeURIComponent(the_select.options[loop].value) + suffix;
			popup = 'popup' + page;
			PRO_openPopupWindow(location,popup,'650','400','toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes','yes');
 		}
	}
	if(typeof(the_other_select.options) != "undefined"){
		for (loop=0; loop < the_other_select.options.length; loop++){
			if (the_other_select.options[loop].selected == true){
				location = prefix + encodeURIComponent(the_other_select.options[loop].value) + suffix;
				popup = 'popup' + page;
				PRO_openPopupWindow(location,popup,'650','400','toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes','yes');
	 		}
		}
	}
}

//changes values of multiple select menus in ui when things get swapped about
function transformMultiple(varname1,the_select,varname2,the_other_select)
{
	var the_select_as_str = "";	
	var the_other_select_as_str = "";
		
	for (loop=0; loop < the_select.options.length; loop++){
		if (the_select.options[loop].selected == true){
			the_select_as_str += the_select.options[loop].value + ';';
 		}
	}

	for (loop=0; loop < the_other_select.options.length; loop++){
		if (the_other_select.options[loop].selected == true){
			the_other_select_as_str += the_other_select.options[loop].value + ';';
 		}
	}
	
	eval('window.document.steps.'+ varname1 +'.value = the_select_as_str;');
	eval('window.document.steps.'+ varname2 +'.value = the_other_select_as_str;');

}

//Displays a confirm box when a user ticks the save display options check box
function save_choice()
{
	document.steps.save_clicked.value=1;
	if(document.steps.save.checked==true){
		
		var name=confirm("Apply settings to all tables?\n - If you would like these settings saved as your default settings press OK.\n - If you wish them only to apply to this table ({REF}) press Cancel.");
		if (name==true){
			document.steps.save_default.value="1";
		}else{
			document.steps.save_default.value="0";
		}
		document.steps.submit();
	}
}

/**************** The following are for rejiggin the widths of two tables so they appear as one *********/

// The reason we want two tables to appear as one is so that one can be scrolled using a div element. 
// This is to give the impression of a scolling tbody.. which works but not in a standards compliant world
// I'm guessing that the widths don't need rejigged for IE etc but some testing still needed....


function init()
{
	if(window.addEventListener){
		AdjustDivWidthForMozilla();
		window.addEventListener("resize", AdjustDivWidthForMozilla, false);
	}else if(document.addEventListener && window.opera){
		AdjustDivWidthForOpera7();
		document.addEventListener("resize", AdjustDivWidthForOpera7, false);
	}
}

function AdjustDivWidthForMozilla(){
	var ScrollbarWidth = 11; 
	var TableBorder = 0;
//	alert(navigator.userAgent);
/*	if(navigator.userAgent && navigator.userAgent.indexOf("Gecko") != -1 && (navigator.userAgent.indexOf("Firefox/1.") != -1 || navigator.userAgent.indexOf("K-Meleon/0.") != -1 || navigator.userAgent.indexOf("Epiphany/1.") != -1)){
		ScrollbarWidth = 11; TableBorder = 0;
	}else*/
	
	if(navigator.userAgent && navigator.userAgent.indexOf("Konqueror") != -1 ){
		document.getElementById("idTDHeadIndex").style.width =  25 + "%";	
		document.getElementById("idTDBodyIndex").style.width =  25 + "%";	
	}
	document.getElementById("idDivScrollingTBody").style.width = parseInt(document.defaultView.getComputedStyle(document.getElementById("idTableHeader"), null).getPropertyValue("width"), 10) + TableBorder + 2 + 2 + TableBorder + ScrollbarWidth + "px";


	/* 15 for Mozilla suite; 17 for FF 1.x and K-meleon 1.0+ assuming that Start/Settings/Control Panel/Display/Appearance tab/Font size is Normal and here assuming that Advanced button/Item: Scrollbar/Size: has not been user-defined, then 17 */
}

function AdjustDivWidthForOpera7(){
	document.getElementById("idTableHeader").style.width = document.getElementById("idDivScrollingTBody").offsetWidth - 17 + "px";
}

