
// Functions dealing with popup 'choose insitution' windows
// and with the use of the institution address as another kind
// of address (e.g. member address or event address

registerNS("InstWin");

// Clears any institution that might have been selected
InstWin.clearInst = function(form) {
	
	//form.instName.value = "";
	form.instID.value = "";
	
	form.use_inst_addr.checked = false;
	form.use_inst_addr.disabled = true;
	
	document.getElementById('instNameSpan').innerHTML = "None selected";
	document.getElementById('instName').value = '';
	document.getElementById('instNameSpan').className = "disabled";
	document.getElementById('instClearButton').style.display = 'none';
	document.getElementById('instChooseButton').value = 'Choose...';
	document.getElementById('useInstAddrBox').style.display = 'none';
	
	InstWin.enableAddressFields();

}

// Opens a new window to display the 'choose institution' page	
InstWin.newWindow = function(file,window) {
	
	msgWindow=open(file,window,'resizable=yes,scrollbars=yes,width=650,height=600');
	if (msgWindow.opener == null) 
		msgWindow.opener = self;

}

// Changes the parent window of the current window.
// Sets the instiutition id and name.
InstWin.setForm = function(id, name) {
	
	opener.document.form1.instID.value = id;
	//opener.document.form1.instName.value = name;
	opener.InstWin.setInstNameSpan(name);
	
	//opener.InstWin.instChosen(id);
	if (opener.InstWin.instChosen != null) {
		opener.InstWin.launchInstChosen();
	}
	
	self.close();
	return false;
}

// We need this intermediate function to work around a ff bug where it
// does the Ajax call in the child window instead of the parent.
// Naughty firefox!
InstWin.launchInstChosen = function(){
	window.setTimeout('InstWin.instChosen()', 0);
}



InstWin.setInstNameSpan = function(instName){
	
	document.getElementById('instNameSpan').innerHTML = instName;
	document.getElementById('instName').value = instName;
	document.getElementById('instNameSpan').className = "";
	document.getElementById('instClearButton').style.display = 'inline';
	document.getElementById('instChooseButton').value = 'Change...';
	document.getElementById('useInstAddrBox').style.display = 'inline';
	
}





// Called if the record gets an institution associated with it.
// Enables the 'use institution address' checkbox, and if the user
// hasn't already entered an address fills in the institution one
InstWin.instChosen = function(){
	
	
	
	var instID = $F('instID');
	$('use_inst_addr').disabled = false;
	
	// If we already have 'use institution address' checked and we have
	// just changed the institution, then reload the address
	if($('use_inst_addr').checked){
		InstWin.fetchAddress(instID);
	}
	
	// Save the institution we've just selected in the common institutions
	// session variable for later use
	InstWin.addCommonInst(instID);
	
}



// Called when an institution is selected or the 'use institution address'
// box is checked
InstWin.fetchAddress = function(instID){
	
	var url = xmlurl + '/instAddress.php';
	//alert("URL is " + url + ", ID is " + instID);
	var pars = 'instID=' + instID;
	
	var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: InstWin.updateAddressFields
			});
	
}



// Called when an institution is selected 
// adds the given instituion to the favourites for later use
InstWin.addCommonInst = function(instID){
	
	var url = xmlurl + '/addCommonInst.php';
	//alert("URL is " + url + ", ID is " + instID);
	var pars = 'instID=' + instID;
	
	var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars
			});

}


InstWin.nothing = function(){
	alert("favourite added");
}


// Called once we have the xml for the institution address. Sets the
// appropriate values in the form and disables those fields
InstWin.updateAddressFields = function(originalRequest){
	
	//alert(originalRequest.responseText);
	var xml = originalRequest.responseXML.documentElement;
	
	var xmlValues = Array('city', 'state', 'postcode', 'country_code');
	var recipientElements = Array('city', 'state', 'postcode', 'id_country');
	
	// Fill in the address with the institution name at the top of it
	document.getElementById('address').value = xml.getElementsByTagName('inst_name')[0].firstChild.data + "\n";
	if(xml.getElementsByTagName('address')[0].firstChild){
		document.getElementById('address').value += xml.getElementsByTagName('address')[0].firstChild.data;
	}
	document.getElementById('address').disabled = true;
	
	for(v = 0; v < xmlValues.length; v++){
		
		if(xml.getElementsByTagName(xmlValues[v])[0].firstChild){
			document.getElementById(recipientElements[v]).value = xml.getElementsByTagName(xmlValues[v])[0].firstChild.data;
		} else {
			document.getElementById(recipientElements[v]).value = "";
		}
		
		document.getElementById(recipientElements[v]).disabled = true;
	}
	
	// If we are in the new user registration page, 
	// update the national representative thing
	if(UserReg.updateCountry){
		UserReg.updateCountry('id_country', 'natrepcountry');
	}
	
}






// Called when one unchecks the 'use institution address' box.
// Enables all the address fields
InstWin.enableAddressFields = function(){
	
	document.getElementById('address').disabled = false;
	document.getElementById('city').disabled = false;
	document.getElementById('state').disabled = false;
	document.getElementById('postcode').disabled = false;
	document.getElementById('id_country').disabled = false;

}
