/**
 * /project/apps/membership/js/app.js
 * 
 * Inschrijvingsformulier voor vzw
 * @author Peter Fastré <peter@lunatis.be>
 */
 
var appName = "Membership";
var appType = "P";
	
var totalPrice = 0;
var newMembershipExpires = 0;

var orderUserId = "";
var orderId = "";
	
function Membership_onload() {
	/*if (doReload != "") {
		window.location = doReload;
	}*/
	
	fillUser();
	
}


function fillUser() {
	oXmlRequest = new XmlRequest();
	oXmlRequest.functionName	= "getUser"
	oXmlRequest.onsuccess 		= getUser_success;
	oXmlRequest.onfailed 		= "";
	oXmlRequest.start();
	
	function getUser_success(oXmlResponse) {
		if (oXmlResponse.statusCode == 2000) {
			usr = oXmlResponse.data[0];
			lblUserName.setValue(usr["UserName"]);
			if (usr["ExternalUserID"] == "") {
				lblExternalUserID.setValue("wordt later toegekend");			
			}
			else {
				lblExternalUserID.setValue(usr["ExternalUserID"]);	
			}
			txtFirstName.setValue(usr["FirstName"]);
			txtName.setValue(usr["LastName"]);
			txtOrganisation.setValue(usr["Organisation"]);
			txtOrganisationNumber.setValue(usr["OrganisationNumber"]);
			txtPhone.setValue(usr["Phone"]);
			txtFax.setValue(usr["Fax"]);
			txtEmail.setValue(usr["Email"]);
			txtAddress.setValue(usr["Address"]);
			txtZip.setValue(usr["Zip"]);
			txtCity.setValue(usr["City"]);
			cboCountryID.setSelectedValue(usr["CountryID"]);
			txtDateOfBirth.setValue(usr["DateOfBirth"]);
			cboGender.setSelectedValue(usr["Gender"]);
			//alert(oXmlResponse.responseText);
			if (usr["Minoc"] == "1") {
				chkMinoc.setChecked(true);
			}
			else {
				chkMinoc.setChecked(false);
			}
				
			checkBirthDate();
		}
		else  {
			alert(oXmlResponse.message["nl"]);
		}
	}
}





function txtUserName_onkeyup(ev, el) {
	if (ev.keyCode == 13) {
		btnLogin_onclick();
	}
}

function txtPassword_onkeyup(ev, el) {
	if (ev.keyCode == 13) {	  
		btnLogin_onclick();
	}
}

function btnLogin_onclick(ev, el) {
  
	var params = new Array();
	params["UserName"] = txtUserName.getValue();
	var username = params["UserName"];
	params["Password"] = txtPassword.getValue();
		
	oXmlRequest = new XmlRequest();
	oXmlRequest.functionName	= "checkLogin";
	oXmlRequest.params 			= params;	
	oXmlRequest.onsuccess 		= checkLogin_success;
	oXmlRequest.start();
	function checkLogin_success(oXmlResponse) {
	 if (oXmlResponse.statusCode == 2000) {
		  //alert(oXmlResponse.responseText);
			var row = oXmlResponse.data[0];
			user.id = row["UserID"];
			user.name = row["Name"];
			user.fullName = row["FirstName"] + " " + row["LastName"];
			user.token = row["Token"];
			user.webroot = webroot;
			user.writeToCookie();
//			if (chkRemember.getChecked() == true){
//				createCookie('username', user.name , 365)
//				eraseCookie('username');
//			}
			
			window.location.reload();
			
		
		}
		else {
			debug.write(oXmlResponse.responseText);
			dlgLoginError = new Dialog("dlgLoginError", oXmlResponse.message["nl"], "failed");
			dlgLoginError.render();
			dlgLoginError.show(); 	
			
			
			if (txtUserName.getValue() == "") {
				txtUserName.focus();
			}
			else {
				txtPassword.focus();
			}
			
		}
		
	}

}





function checkForm() {
	/*if (renewalPossible == false) {
		return false;
	}*/
	
	ok = true;
	
	// formulier checken
	if (txtName_onblur() == false)
		ok = false;
	if (txtEmail_onblur() == false)
		ok = false;
	if (txtAddress_onblur() == false)
		ok = false;
	if (txtZip_onblur() == false)
		ok = false;
	if (txtCity_onblur() == false)
		ok = false;
	if (cboCountryID_onchange() == false)
		ok = false;
		
	return ok;
}	


function txtDateOfBirth_onblur() {
	checkBirthDate(true);
}

// die createerror is voor bij de onload, dan moeten we niet ineens dat veld al op error zetten
function checkBirthDate(createError) {

	if (renewalPossible == false) 
		return;
		
	txtDateOfBirth.clearError();
	
	var params = new Array();
	params["DateOfBirth"] = txtDateOfBirth.getValue();
	oXmlRequest = new XmlRequest();
	oXmlRequest.functionName	= "checkPrice"
	oXmlRequest.params 			= params;	
	oXmlRequest.onsuccess 		= checkPrice_success;
	oXmlRequest.onfailed 		= "";
	oXmlRequest.start();
	
	function checkPrice_success(oXmlResponse) {
		//alert(oXmlResponse.responseText);
		if (oXmlResponse.statusCode == 2000) {
			btnCreateOrder.setEnabled();
			lblRenewalPrice.setValue(oXmlResponse.message["nl"]);
			//lblRenewalPrice.highlight();
		}
		else if (oXmlResponse.statusCode == 2001) {
			btnCreateOrder.setDisabled();
			lblRenewalPrice.setValue(oXmlResponse.message["nl"]);
			//lblRenewalPrice.highlight();
			if (createError)
				txtDateOfBirth.setError("Gelieve een geldige datum in te vullen");
			//txtDateOfBirth.focus();
		}
		else {
			alert(oXmlResponse.message["nl"]);	
		}
	}
}



function txtFirstName_onblur() {
	ok = true;
	txtFirstName.clearError();
	if (txtFirstName.getValue() == "") {
		txtFirstName.setError("Gelieve een voornaam in te vullen");	
	}
}

function txtName_onblur() {
	ok = true;
	txtName.clearError();
	if (txtName.getValue() == "") {
		txtName.setError("Gelieve een naam in te vullen");	
	}
}

function txtEmail_onblur() {
	ok = true;
	txtEmail.clearError();
	if (txtEmail.getValue() == "") {
		txtEmail.setError("Gelieve een email in te vullen");	
		ok = false;
	}
}

function txtAddress_onblur() {
	ok = true;
	txtAddress.clearError();
	if (txtAddress.getValue() == "") {
		txtAddress.setError("Gelieve een adres in te vullen");	
		ok = false;
	}
}

function txtZip_onblur() {
	ok = true;
	txtZip.clearError();
	if (txtZip.getValue() == "") {
		txtZip.setError("Gelieve een postcode in te vullen");	
		ok = false;
	}
}
function cboCountryID_onchange() {
	ok = true;
	cboCountryID.clearError();
	if (cboCountryID.getSelectedValue() == "") {
		cboCountryID.setError("Gelieve een land te kiezen");	
		ok = false;
	}
}

function txtCity_onblur() {
	ok = true;
	txtCity.clearError();
	if (txtCity.getValue() == "") {
		txtCity.setError("Gelieve een stad in te vullen");	
		ok = false;
	}
	
	return ok;
	
}




function btnReset_onclick() {
	document.getElementById("F_cboCountryID").reset();
}



function btnCreateOrder_onclick() {
	if (checkForm() == false) {
		alert("Gelieve alle velden in het formulier correct in te vullen");
	}
	else {
		txtDateOfBirth.clearError();
	
		btnCreateOrder.setDisabled();
		
		var params = new Array();
		params["DateOfBirth"] = txtDateOfBirth.getValue();
		params["FirstName"] = txtFirstName.getValue();
		params["Name"] = txtName.getValue();
		params["Organisation"] = txtOrganisation.getValue();
		params["OrganisationNumber"] = txtOrganisationNumber.getValue();
		params["Phone"] = txtPhone.getValue();
		params["Email"] = txtEmail.getValue();
		params["Address"] = txtAddress.getValue();
		params["Fax"] = txtFax.getValue();
		params["Zip"] = txtZip.getValue();
		params["City"] = txtCity.getValue();
		params["CountryID"] = cboCountryID.getSelectedValue();
		params["Gender"] = cboGender.getSelectedValue();
		params["Minoc"] = chkMinoc.getChecked() ? 1 : 0;
		oXmlRequest = new XmlRequest();
		oXmlRequest.functionName	= "createOrder"
		oXmlRequest.params 			= params;	
		oXmlRequest.onsuccess 		= createOrder_success;
		oXmlRequest.onfailed 		= "";
		oXmlRequest.start();
	}
	
	function createOrder_success(oXmlResponse) {
		btnCreateOrder.setEnabled();
		if (oXmlResponse.statusCode == 4500) {
			//btnCreateOrder.setDisabled();
			lblRenewalPrice.setValue("<span style=\"color:red\">" + oXmlResponse.message["nl"] + "</span>");
			txtDateOfBirth.setError("Gelieve een geldige geboortedatum in te vullen");
			txtDateOfBirth.focus();
			//btnCreateOrder.setEnabled();
		}
		else if (oXmlResponse.statusCode == 4501) {
			//alert(oXmlResponse.message["nl"]);	
			
			//btnCreateOrder.setDisabled();
			lblRenewalPrice.setValue("<span style=\"color:red\">" + oXmlResponse.message["nl"] + "</span>");
			txtCity.setError(oXmlResponse.message["nl"]	);
			txtCity.focus();
			//btnCreateOrder.setEnabled();
		}
		else if (oXmlResponse.statusCode == 2000) {
			// order is gemaakt
			pnlStep1.hide();
			pnlStep2.show();
		}
		else {
			alert(oXmlResponse.message["nl"]);	
			btnCreateOrder.setEnabled();
		}
	}
	
}




function btnUpdate_onclick() {
	if (checkForm() == false) {
		alert("Gelieve alle velden in het formulier correct in te vullen");
	}
	else {
		txtDateOfBirth.clearError();
	
		btnUpdate.setDisabled();
		
		var params = new Array();
		params["DateOfBirth"] = txtDateOfBirth.getValue();
		params["FirstName"] = txtFirstName.getValue();
		params["Name"] = txtName.getValue();
		params["Organisation"] = txtOrganisation.getValue();
		params["OrganisationNumber"] = txtOrganisationNumber.getValue();
		params["Phone"] = txtPhone.getValue();
		params["Email"] = txtEmail.getValue();
		params["Address"] = txtAddress.getValue();
		params["Fax"] = txtFax.getValue();
		params["Zip"] = txtZip.getValue();
		params["City"] = txtCity.getValue();
		params["CountryID"] = cboCountryID.getSelectedValue();
		params["Gender"] = cboGender.getSelectedValue();
		params["Minoc"] = chkMinoc.getChecked() ? 1 : 0;
		oXmlRequest = new XmlRequest();
		oXmlRequest.functionName	= "updateUser"
		oXmlRequest.params 			= params;	
		oXmlRequest.onsuccess 		= updateUser_success;
		oXmlRequest.onfailed 		= "";
		oXmlRequest.start();
	}
	
	function updateUser_success(oXmlResponse) {
		if (oXmlResponse.statusCode == 4500) {
			//btnUpdate.setDisabled();
			alert(oXmlResponse.message["nl"]);	
			//txtDateOfBirth.setError("Gelieve een geldige geboortedatum in te vullen");
			btnUpdate.setEnabled();

		}
		else if (oXmlResponse.statusCode == 2000) {
			// gebruiker is geupdate
			pnlStep1.hide();
			pnlStep2Updated.show();
		}
		else {
			alert(oXmlResponse.message["nl"]);	
				btnUpdate.setEnabled();

		}
	}
	
}



