/*
 * Copyright 2010 The Australian National University
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this 
 * file except in compliance with the License. You may obtain a copy of the License at 
 *
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software distributed under 
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 * KIND, either express or implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */
 
// now calls PVII Equal CSS Columns scripts - Version 2
function P7_equalCols(leftcontent,middlecontent){
	P7_equalCols2(0,leftcontent,'DIV',middlecontent,'DIV');
}
function Check(dep)
{
 if (dep[dep.selectedIndex].value=="")
	{dep.options[0].selected=true}
}
function CheckDates()
{
	try{
       	var oStartDate  = document.getElementById("startdate");
       	var oEndDate  = document.getElementById("enddate");
		if (isDate(oStartDate.value)==false){
			oStartDate.focus();
			return false;
		}
	    if (isDate(oEndDate.value)==false){
			oEndDate.focus();
			return false;
		}
		return true;
   	}
   	catch(e){
   		alert('Something wrong: '+e.description);
	}
}
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   } 
   return this ;
}
function isDate(dtStr){
    var dtCh= "/";
    var minYear=2005;
    var maxYear=2015;
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	var strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	var month=parseInt(strMonth);
	var day=parseInt(strDay);
	var year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
return true;
}
function exportmasterfile()
{   var url="results.csv";
    alert(url);
    window.open(url,"Download");
    return   true;
}
// adjust the leftcontent column height to compensate hidden rows appearing after load in IE
function adjustPadding(display,adjust) {
	if(navigator.appName.indexOf("Netscape")<0){
		/* Note: initial value of 'columnIn' set in accountHeader.php in 'onLoad' and 
		 * should be same as 'padding-top' style setting of '.column-in' in header.css
		 */
		if(adjust>0 && display==""){
			;
		}else if(adjust<0 && display=="none"){
			;
		}else{
			columnIn = columnIn + adjust;
			if(columnIn<0){
				columnIn = 0;
			}
			document.getElementById("column-in").style.paddingBottom = columnIn + "em";
		}
	}
}
function OnOther(aname, avalue) {
	if (aname == '1') {
		oOther  = document.getElementById("OtherSpecifyPos");
	}else{
		oOther  = document.getElementById("WorkTypeSpecify");
	}
  	var display = oOther.style.display;
  	var adjust = 3;
	if (avalue=="Other")
	{
		oOther.style.visibility="visible";
		oOther.style.display="";
		adjustPadding(display,adjust);
	}
	else
	{
		oOther.style.visibility="hidden";
		oOther.style.display="none";
		adjustPadding(display,-adjust);
	}
}
function OnClickFold(aCountry){
	 var oAustInst;
	 var rad_val;
	 try{
	    oAustInst = document.getElementById("AustInst");
	    oNAustInst = document.getElementById("OtherInst");
	    oNAustInstType = document.getElementById("OtherInstType");
	 	var w = document.regform.country.selectedIndex;
	 	var aCountryText = document.regform.country.options[w].text;
	 	var display = oNAustInst.style.display;
	 	var adjust = 3;
	
	    if (aCountryText=="Australia")
	    {
			oAustInst.style.visibility="visible";
	        oAustInst.style.display="";
	        oNAustInst.style.visibility="hidden";
	        oNAustInst.style.display="none";
	        oNAustInstType.style.visibility="hidden";
	        oNAustInstType.style.display="none";	      	

	        for (var i=0; i < document.regform.InstA.length; i++)
	        {
				if (document.regform.InstA[i].checked)
	         	{
	           		rad_val = document.regform.InstA[i].value;
	         	}
	        }
	        setInstVisible(rad_val);
	        adjustPadding(display,-adjust);
		}
		else
	    {
	     	oNAustInst.style.visibility="visible";
	    	oNAustInst.style.display="";
	     	oNAustInstType.style.visibility="visible";
	    	oNAustInstType.style.display="";	 
	        oAustInst.style.visibility="hidden";
	    	oAustInst.style.display="none";
	    	setInstVisible("HH");
	    	adjustPadding(display,adjust);
		}
	}catch(e){
		alert('Something wrong: '+e.description);
	}
}
function setInstVisible(aValue){
    var oAustInst;
    var atest;
	try{
		atest = aValue.substring(0,1);
		oUni  = document.getElementById("AustUni");
		oDept  = document.getElementById("AustDept");
		oOther  = document.getElementById("AustOther");
		oOtherType = document.getElementById("AustOtherType");
		if(oDept.style.display=="" || oOther.style.display==""){
			var display = "";
		}else{
			var display = "none";
		}
	 	var adjust = 3;		
	    switch(atest)
		{
		case 'U':
			oUni.style.visibility="visible";
	        oUni.style.display="";
	        oDept.style.visibility="hidden";
	        oDept.style.display="none";
	        oOther.style.visibility="hidden";
	        oOther.style.display="none";
	        oOtherType.style.visibility="hidden";
	        oOtherType.style.display="none";
	        adjustPadding(display,-adjust);	        	        
			break;
		case 'D':
			oDept.style.visibility="visible";
			oDept.style.display="";
			oUni.style.visibility="hidden";
			oUni.style.display="none";
			oOther.style.visibility="hidden";
			oOther.style.display="none";
			oOtherType.style.visibility="hidden";
			oOtherType.style.display="none";
			adjustPadding(display,adjust);
			break;
		case 'O':
			oOther.style.visibility="visible";
			oOther.style.display="";
			oOtherType.style.visibility="visible";
			oOtherType.style.display="";			
			oDept.style.visibility="hidden";
			oDept.style.display="none";
			oUni.style.visibility="hidden";
			oUni.style.display="none";
			adjustPadding(display,adjust);	
			break;
		case 'H':
			oOther.style.visibility="hidden";
			oOther.style.display="none";
			oDept.style.visibility="hidden";
			oDept.style.display="none";
			oUni.style.visibility="hidden";
			oUni.style.display="none";
			adjustPadding(display,-adjust);
			break;
		default:
		    alert('Something wrong!');
		}
    }
    catch(e){
       alert('Something wrong: '+e.description);
    }       
}
/* requires scriptaculous */
function toggleForms(e) {
    if (e.value == "true") {
    	$('nonacspri_form').fade();
    	$('acspri_form').appear();
    } else {
    	$('acspri_form').fade();
    	$('nonacspri_form').appear();
    }
}
function upadate_bdataset_list(series, sort) {
  new Ajax.Request('/bdatasets.php',
    {
      method: 'get',
      parameters: {series: series, sort: sort},
      onLoading: function() {
         $('ajax_spinner').show();
      },
      onSuccess: function(transport) {
         $('ajax_spinner').hide();
         $('bdataset_list').update(transport.responseText);
      },
      onFailure: function() {
         $('ajax_spinner').hide();
         $('bdataset_list').update('Lost connection to the server, please reload this page');
      }
    })
}
function toggleSupervisorName(e) {
    if (e.checked) {
       $('supervisor_name').appear();
    } else {
       $('supervisor_name').fade();
    }
}

function setEditUserVisibilityPosition() {
	if ($('position').value == 'Other') {
		$('other-position').show();
	} else {
		$('other-position').hide();
	}
}

function setEditUserVisibilityAction() {
	if ($('action').value == 'Other') {
		$('other-work').show();
	} else {
		$('other-work').hide();
	}
}

function setEditUserVisibilityAustinstitution() {
	if ($('austinstitution-Uni').checked == true) {
		$('australian-institutions-universities').show();
		$('australian-institutions-government').hide();
		$('australian-institutions-other').hide();
		$('australian-institutions-other-type').hide();
	} else if ($('austinstitution-Dept').checked == true) {
	    $('australian-institutions-universities').hide();
		$('australian-institutions-government').show();
		$('australian-institutions-other').hide();
		$('australian-institutions-other-type').hide();
	} else if ($('austinstitution-Other').checked == true) {
		$('australian-institutions-universities').hide();
		$('australian-institutions-government').hide();
		$('australian-institutions-other').show();
		$('australian-institutions-other-type').show();
	}
}

function setEditUserVisibilityCountryid() {
	if ($('countryid').value == 15) {
		$('australian-institutions').show();
		$('australian-institutions-universities').show();
		$('australian-institutions-government').show();
		setEditUserVisibilityAustinstitution()
		$('overseas-institutions').hide();
		$('overseas-institutions-type').hide();
	} else {
		$('australian-institutions').hide();
		$('australian-institutions-universities').hide();
		$('australian-institutions-government').hide();
		$('australian-institutions-other').hide();
		$('australian-institutions-other-type').hide();
		$('overseas-institutions').show();
		$('overseas-institutions-type').show();
	}
}

function setEditUserVisibilities() {
	setEditUserVisibilityPosition();
	setEditUserVisibilityAction();
	setEditUserVisibilityAustinstitution();
	setEditUserVisibilityCountryid();
}

