
/* 	
	------------------------------------------------------------------------------
	/project/apps/conferencesubscriptions/app.js
	
	Conference subscriptions
	
   	------------------------------------------------------------------------------
    
    
*/
	var appName = "ConferenceSubscriptions";
	var appType = "P";
	

	
	function ConferenceSubscriptions_onload() {
		getUser();
		getConferenceDinnerPrice();
		getHotelRooms(); // en ook de price
		getEARLIMembershipPrice();
	}

	function getUser() {
		var params = new Array();
		params["ConferenceID"] = ConferenceID;
		
		oXmlRequest = new XmlRequest();
		oXmlRequest.functionName	= "getUser";
		oXmlRequest.params 			= params;	
		oXmlRequest.onsuccess 		= getUser_success;
		oXmlRequest.onfailed 		= "";
		oXmlRequest.start();
		
		
		function getUser_success(oXmlResponse) {
			usr = oXmlResponse.data[0];
			
			txtSalutation.setValue(usr["Salutation"]);
			txtFirstName.setValue(usr["FirstName"]);
			txtName.setValue(usr["LastName"]);
			txtAddress.setValue(usr["Address"]);
			txtCity.setValue(usr["City"]);
			txtZip.setValue(usr["Zip"]);
			cboCountryID.setSelectedValue(usr["CountryID"]);
			txtEmail.setValue(usr["Email"]);
			txtPhone.setValue(usr["Phone"]);
			txtFax.setValue(usr["Fax"]);
			txtInstitution.setValue(usr["Organisation"]);
		}
	}
	
	
	function cmbHotelID_onchange(ev, el)  {
		getHotelRooms();
	}
	
	function getHotelRooms() {
		if (hotelsAvailable == false)
			return;
			
		var params = new Array();
		params["HotelID"] = cmbHotelID.getSelectedValue();
		
		/*if (cmbHotelID.getSelectedValue() == "0") {
			pnlHotelRoom.hide();
		}
		else {
			pnlHotelRoom.show();
		}*/
		cmbHotelRoomID.dataClear();	
		cmbHotelRoomID.dataSource = "getHotelRooms";
		cmbHotelRoomID.displayMember = "Name";
		cmbHotelRoomID.valueMember = "HotelRoomID";
		cmbHotelRoomID.dataParams = params;
		cmbHotelRoomID.ondatabind = getHotelPrice;
		cmbHotelRoomID.dataBind();
		
	}

	function cmbHotelRoomID_onchange(ev, el) {
		getHotelPrice();	
	}
	
	function getHotelPrice() {
		// setting all values to empty (if something goes wrong -> no wrong values would be displayed)
		lblHotelRoomPrice.setValue("");
		lblHotelPrice.setValue("");
		
		var params = new Array();
		params["HotelRoomID"] = cmbHotelRoomID.getSelectedValue();
		
		oXmlRequest = new XmlRequest();
		
		oXmlRequest.functionName	= "getHotelRoomPrice";
		oXmlRequest.params 			= params;	
		oXmlRequest.onsuccess 		= getHotelRoomPrice_success;
		oXmlRequest.onfailed 		= "";
		oXmlRequest.start();
		
		
		function getHotelRoomPrice_success(oXmlResponse) {
			hotelroom = oXmlResponse.data[0];
		
			if (hotelroom["Price"] != 0)
				lblHotelRoomPrice.setValue(hotelroom["Price"] + " EUR / room");
			else
				lblHotelRoomPrice.setValue("");
			
			// total Price for hotel = HotelRoomPrice * NumberOf Nights
			var hotelPrice = hotelroom["Price"] * 1; /** cmbHotelNights.getSelectedValue();*/
			
			lblHotelPrice.setValue(hotelPrice.toFixed(2) + " EUR");
			getTotalPrice();
		}
		
	}
	
	
	
	function cmbEARLIMembership_onchange(ev, el) {
		getEARLIMembershipPrice();
	}
	
	function getEARLIMembershipPrice() {
		if (earliMembershipAvailable == false)
			return;
			
		lblEARLIMembershipPrice.setValue("");
		
		var params = new Array();
		params["ConferenceID"] = ConferenceID;
		params["EARLIMembership"] = cmbEARLIMembership.getSelectedValue();
		
		oXmlRequest = new XmlRequest();
		
		oXmlRequest.functionName	= "getEARLIMembershipPrice";
		oXmlRequest.params 			= params;	
		oXmlRequest.onsuccess 		= getEARLIMembershipPrice_success;
		oXmlRequest.onfailed 		= "";
		oXmlRequest.start();
		
		
		function getEARLIMembershipPrice_success(oXmlResponse) {
			conference = oXmlResponse.data[0];
		
			price = parseFloat(conference["Price"]);
			lblEARLIMembershipPrice.setValue(price.toFixed(2) + " EUR");
			
			getTotalPrice();
		}
	}
	
	
	
	function cmbConferenceDinner_onchange(ev, el) {
		getConferenceDinnerPrice();
	}
	
	function getConferenceDinnerPrice() {
		lblConferenceDinnerPrice.setValue("");
		
		var params = new Array();
		params["ConferenceID"] = ConferenceID;
		params["ConferenceDinner"] = cmbConferenceDinner.getSelectedValue();
		
		oXmlRequest = new XmlRequest();
		
		oXmlRequest.functionName	= "getConferenceDinnerPrice";
		oXmlRequest.params 			= params;	
		oXmlRequest.onsuccess 		= getConferenceDinnerPrice_success;
		oXmlRequest.onfailed 		= "";
		oXmlRequest.start();
		
		
		function getConferenceDinnerPrice_success(oXmlResponse) {
			conference = oXmlResponse.data[0];
		
			price = parseFloat(conference["Price"]);
			lblConferenceDinnerPrice.setValue(price.toFixed(2) + " EUR");
			
			getTotalPrice();
		}
	}
	
	
	
	
	function getTotalPrice() {
		lblTotalConferencePrice.setValue("");
		
		var params = new Array();
		params["ConferenceID"] = ConferenceID;
		params["ConferenceDinner"] = cmbConferenceDinner.getSelectedValue();
		
		if (hotelsAvailable)
			params["HotelRoomID"] = cmbHotelRoomID.getSelectedValue();
		else
			params["HotelRoomID"] = 0;
			
		if (earliMembershipAvailable) 
			params["EARLIMembership"] = cmbEARLIMembership.getSelectedValue();
		else 
			params["EARLIMembership"] = 0;
				
			
		
		
		oXmlRequest = new XmlRequest();
		
		oXmlRequest.functionName	= "getTotalConferencePrice";
		oXmlRequest.params 			= params;	
		oXmlRequest.onsuccess 		= getTotalConferencePrice_success;
		oXmlRequest.onfailed 		= "";
		oXmlRequest.start();
		
		
		function getTotalConferencePrice_success(oXmlResponse) {
			conference = oXmlResponse.data[0];
		
			price = parseFloat(conference["Price"]);
			conferencePrice = parseFloat(conference["ConferencePrice"]);
			lblTotalConferencePrice.setValue(price.toFixed(2) + " EUR");
			lblConferencePrice.setValue(conferencePrice.toFixed(2) + " EUR");
			
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	var orderId = 0;
	var orderUserId = 0;
	var amount = 0;
	function btnSubmit_onclick() {
		//alert("abstractNeeded = " + abstractNeeded + ", summary = " + summaryNeeded);
		if (txtName.getValue() == "" || txtInstitution.getValue() == "" || txtEmail.getValue() == "" ||
			txtAddress.getValue() == "" || txtZip.getValue() == "" || cboCountryID.getSelectedValue() == "" ||
			txtCity.getValue() == "" || txtPhone.getValue() == ""
			)
		{
			alert("Please fill in your contact details");
			
		}
		
		else{
			
				
				
			var params = new Array();
			// user fields
			params["ConferenceID"] = ConferenceID;
			params["Salutation"] = txtSalutation.getValue();
			params["FirstName"] = txtFirstName.getValue();
			params["Name"] = txtName.getValue();
			params["Email"] = txtEmail.getValue();
			params["Organisation"] = txtInstitution.getValue();
			params["Address"] = txtAddress.getValue();
			params["Zip"] = txtZip.getValue();
			params["City"] = txtCity.getValue();
			params["Phone"] = txtPhone.getValue();
			params["Fax"] = txtFax.getValue();
			params["CountryID"] = cboCountryID.getSelectedValue();
			
			
			params["ConferenceID"] = ConferenceID;
			params["ConferenceDinner"] = cmbConferenceDinner.getSelectedValue();
			if (hotelsAvailable) {
				params["HotelID"] = cmbHotelID.getSelectedValue();
				params["HotelRoomID"] = cmbHotelRoomID.getSelectedValue();
			}
			else {
				params["HotelID"] = 0;
				params["HotelRoomID"] = 0;
			}
				
			if (earliMembershipAvailable) 
				params["EARLIMembership"] = cmbEARLIMembership.getSelectedValue();
			else 
				params["EARLIMembership"] = 0;
		
			
			params["OrderID"] = orderId;
			params["OrderUserID"] = orderUserId;
			
			
			oXmlRequest = new XmlRequest();
			oXmlRequest.functionName	= "createSubscription"
			oXmlRequest.params 			= params;	
			oXmlRequest.onsuccess 		= createSubscription_success;
			oXmlRequest.onfailed 		= "";
			oXmlRequest.start();
			
				
		}
		
		function createSubscription_success(oXmlResponse) {
			
			debug.write("createSubscription_success: " + oXmlResponse.responseText);
			
			if (oXmlResponse.statusCode == "2000") {
			
				//alert("Subscription created, now going to pay!");
				
				orderUserId = oXmlResponse.data[0]["OrderUserID"];
				orderId = oXmlResponse.data[0]["OrderID"];
				amount = oXmlResponse.data[0]["Amount"];
			
				document.getElementById('ogone').src = webroot + "/ogone/start.php?orderid=" + orderId + "&amount=" + amount;  
				
				pnlStep1.hide();
				pnlStep2.show();
				
				user.logOut();
			
			}
			else {
				alert(oXmlResponse.statusCode + " - " + oXmlResponse.status + " - " + oXmlResponse.responseText);
			}
			
		}
	
	}
	
	
	
	
	
