// Set form for display
$(function () {
    $('#pnlMapButton').click(function () {
        MM_controlShockwave('Map', '', 'Rewind'); MM_controlShockwave('Map', '', 'Play'); resetAllRegions('adll');
    });

    $('#pnlMapRSButton').click(function () {
        MM_controlShockwave('MapRS', '', 'Rewind'); MM_controlShockwave('MapRS', '', 'Play'); resetAllRegions('adrs');
    });
});

function resetAllRegions(t) {
    $('input[hiddenregion*='+t+']').val('0');
}

function displayADRSMap(chk) {
    //Display / hide the relevant map/table information for ADRS.
    /*
    Amendment - D Fry | 08/03/2011
    Fix javascript so it works.
    */
    var map = $('.pnlMapRS');
    var box = $('.pnlCheckboxRS');

    if (map) { map.css('display', (chk) ? '' : 'none'); }
    if (box) { box.css('display', (chk) ? '' : 'none'); }

    $('.ADRSWeek').attr('disabled', (chk) ? false : true);

//    var doc = document.all;

//    if (doc.pnlMapRS) { doc.pnlMapRS.style.display = (chk) ? '' : 'none'; }
//    if (doc.pnlCheckboxRS) { doc.pnlCheckboxRS.style.display = (chk) ? '' : 'none'; }
//    doc.tblADRSReset.style.display = (chk) ? '' : 'none';
//    doc.ADRSWeek.disabled = (chk) ? false : true;
}

function displayADLLMap(chk) {
    //Display / hide the relevant map/table information for ADLL.
    /*
    Amendment - D Fry | 08/03/2011
    Fix javascript so it works.
    */
    var map = $('.pnlMap');
    var box = $('.pnlCheckbox');

    if (map) { map.css('display', (chk) ? '' : 'none'); }
    if (box) { box.css('display', (chk) ? '' : 'none'); }
    
    $('.ADLLWeek').attr('disabled', (chk) ? false : true);

//    if (doc.pnlMap) { doc.pnlMap.style.display = (chk) ? '' : 'none'; }
//    if (doc.pnlCheckbox) { doc.pnlCheckbox.style.display = (chk) ? '' : 'none'; }
//    doc.tblADLLReset.style.display = (chk) ? '' : 'none';
//    doc.ADLLWeek.disabled = (chk) ? false : true;
}

function Left(str, n)
{
	if (n <= 0)
	    return "";
	else if (n >= String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function MM_checkPlugin(plgIn, theURL, altURL, autoGo) { //v4.0
  var ok=false; document.MM_returnValue = false;
  with (navigator) if (appName.indexOf('Microsoft')==-1 || (plugins && plugins.length)) {
    ok=(plugins && plugins[plgIn]);
  } else if (appVersion.indexOf('3.1')==-1) { //not Netscape or Win3.1
    if (plgIn.indexOf("Flash")!=-1 && window.MM_flash!=null) ok=window.MM_flash;
    else if (plgIn.indexOf("Director")!=-1 && window.MM_dir!=null) ok=window.MM_dir;
    else ok=autoGo; }
  if (!ok) theURL=altURL; if (theURL) window.location=theURL;
}

function MM_controlShockwave(objStr,x,cmdName,frameNum) { //v3.0
    var obj = MM_findObj(objStr);
    if (obj) eval('obj.' + cmdName + '(' + ((cmdName == 'GotoFrame') ? frameNum : '') + ')');
}


function CheckAllRegions(obj) {
/*
Amendment - D Fry | 24/03/2011
Fix javascript so it works.
Set/Reset all 
*/
    var t = $(obj).attr('regiontype');
    $('input[regiontype*=' + t + ']').attr('checked', $(obj).attr('checked'));
    $('input[hiddenregion*=' + t + ']').val(($(obj).attr('checked'))?1:0);
    

//	for (var i=0;i<document.adll_update.elements.length;i++)
//	{
//		var e = document.adll_update.elements[ i];
//		if (Left(e.name,10)=='region_no_')
//			e.checked = document.adll_update.region_no_all.checked;
//  }

}

function uncheckALL() {
/*
Amendment - D Fry | 24/03/2011
Fix javascript so it works.
*/

    $('input[name=region_no_all]').attr('checked', false);
	//document.adll_update.region_no_all.checked = false;
}

//called by flash_map.swf when user click on region
function ASH_CheckDaBox(DaChosenCheckbox,DaValue) {
/*
Amendment - D Fry | 24/03/2011
Fix javascript so it works.
*/

	//eval("aspnetForm.region_no_"+DaChosenCheckbox+".value='"+DaValue+"'")
    $('input[name=region_no_' + DaChosenCheckbox + ']').val(DaValue);

/*
	if (daValue==1)
	eval("adll_update.region_no_"+DaChosenCheckbox+".checked='true'");
	else
	eval("adll_update.region_no_"+DaChosenCheckbox+".checked='false'");
	*/
	uncheckALL();
}

var formWin = null; 

function openFormWin() { 
formWin = open('','formWin','left=100,top=100,width=500,height=330,status=0'); 
if (formWin && !formWin.closed) formWin.focus(); 
return true; 
} 


function ValidateType(theForm) {
//check either or both update types are checked
/*
Amendment - D Fry | 24/03/2011
Fix javascript so it works.
*/
    if ($('.ADLLCheckBox input:checkbox').attr('checked')) { return true; }
    if ($('.ADRSCheckBox input:checkbox').attr('checked')) { return true; }
	alert('Please select at least one type of update to download');
	return false;
}

function ValidateRegion(theForm) {
    //check at least one or all regions are checked
    /*
    Amendment - D Fry | 24/03/2011
    Fix javascript so it works.
    */

    if ($(':input[value=1][hiddenregion]').length > 0)
    {return true;}
    else {
        alert('Please select at least 1 area to update.');
        return false;
     }


//	for (var i=0;i<document.adll_update.elements.length;i++)
//	{
//	
//		var e = document.adll_update.elements[ i]
//	
//		if (Left(e.name,10)=='region_no_')
//		{
//		//	alert(e.name + e.checked);	
//		//	alert(e.name + e.value);
//			if (e.value == 'checked') return true; //checkboxes
//			if (e.value ==1) return true; 		//flash uses hidden
//		}
//	}
//    alert('Please select at least 1 area to update.')
//    return false;
//	return true;
}

 function ValidateInput(theForm)
{
    //called by onclick of submit button validates user input before sent to the server
	if  (ValidateType(theForm)==true && ValidateRegion(theForm)==true)
	{
		return true;
	}
	else
	{
		return false;	
    }
}



function hiddenValidate(theForm) {
		el=theForm.region_no_1_2.value; {
//		if(el.type=="hidden") {
			if(el==1) { return true; }
		}
		el=theForm.region_no_3.value; {
			if(el==1) { return true; }
		}
		el=theForm.region_no_4.value; {
			if(el==1) { return true; }
		}
		el=theForm.region_no_5.value; {
			if(el==1) { return true; }
		}
		el=theForm.region_no_6.value; {
			if(el==1) { return true; }
		}
		el=theForm.region_no_7.value; {
			if(el==1) { return true; }
		}
		el=theForm.region_no_8.value; {
			if(el==1) { return true; }
		}
		el=theForm.region_no_9.value; {
			if(el==1) { return true; }
		}
		el=theForm.region_no_10.value; {
			if(el==1) { return true; }
		}

//	for (i=3; i<=10; i++;) {
//		el=eval("theForm.region_no_"+i.value);
//		el=theForm.region_no_[i].value; 
//		el=theForm.region_no_[i];
//		if(el.type=="hidden") {
//		if(el==1) { return true; }
	alert('Please select at least 1 area to update.')
	return false;
}

function checkboxValidate(theForm) {
	for (i=0; i<theForm.elements.length; i++) {
		el=theForm.elements[i];
		if(el.type=="checkbox") {
			if(el.checked) { return true; }
			}
		}
	alert('Please select at least 1 area to update.')
	return false;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
