function WebProxy() {
}

WebProxy.prototype.name = "WebProxy";
WebProxy.serviceURL = "data.php";

WebProxy.prototype.loginUsingToken = function(token) {

	$.post(WebProxy.serviceURL, {
		login_aktionscode : token,
		login : 1
	}, function(d) {
		WebProxy.processLoginRespose(d);
	});
}

WebProxy.prototype.login = function(email, password) {

	
	Phire.sendNotification(Notifications.SHOW_PRELOADER, true);
	$.post(WebProxy.serviceURL + "?al=1&aoksoap=1", {
		pass : password,
		user : email

	}, function(d) {
		WebProxy.processLoginRespose(d);
	});
}

WebProxy.processLoginRespose = function(d) {
	var data = $.parseQuery(d);
	Phire.sendNotification(Notifications.SHOW_PRELOADER, false);
	switch (data.loginOK) {
		case "OK":
			UserInfo.username = data.username;
			UserInfo.PHPSESSID = data.PHPSESSID;
			UserInfo.aktionscode = data.aktionscode.toUpperCase();
			UserInfo.jobtestDatum = data.jobtestdatum;
			UserInfo.jobtestType = data.jobtesttype;
			UserInfo.loggedIn = true;
			Phire.sendNotification(Notifications.LOGGED_IN);
			break;

		case "ERROR":
			if(data.abgelaufen == "1") {
				Phire.sendNotification(Notifications.SHOW_ALERT_PANEL, {
					text : "Die maximale Zahl<br>der Logins mit Ihrem<br>Aktionscode wurde erreicht.<br>Wir bedanken uns, dass Sie den AOK-JobKompass genutzt haben!",
					ok : "Zurück"
				});
				break;
			}
			Phire.sendNotification(Notifications.SHOW_ALERT_PANEL, {
				text : "Der Login ist leider fehlgeschlagen."
			});
			break;
	}
}

WebProxy.prototype.getJobTestXML = function(type) {

	Phire.sendNotification(Notifications.SHOW_PRELOADER, true);
	$.get(WebProxy.serviceURL, {
		PHPSESSID : UserInfo.PHPSESSID,
		get_data : 1,
		f_type : ("jobtest," + type)
	}, function(d) {
		Phire.sendNotification(Notifications.SHOW_PRELOADER, false);
		if( typeof (d) == "string")
			d = $.parseXML(d);
		Phire.sendNotification(Notifications.JOBTEST_XML_LOADED, d);

	});
}

WebProxy.prototype.sendJobTestResult = function(answers, type) {
	Phire.sendNotification(Notifications.SHOW_PRELOADER, true);

	var mess = {};
	for(var i = 0; i < answers.length; i++) {
		mess["frage_" + (i + 1)] = answers[i];
	};
	mess.PHPSESSID = UserInfo.PHPSESSID;
	mess.userNAME = UserInfo.userName;
	mess.userID = UserInfo.userId;
	mess.test_type = "jobtest," + type;
	mess.storedata = 1;
	
	$.post(WebProxy.serviceURL + "?&PHPSESSID=" + UserInfo.PHPSESSID + "&storedata=1", mess, function(d) {

		if(d == "OK") {
			Phire.sendNotification(Notifications.SHOW_PRELOADER, false);
			$.get(WebProxy.serviceURL, {
				PHPSESSID : UserInfo.PHPSESSID,
				get_auswertung : 1
			}, function(d) {
				//not valid?
				if( typeof (d) == "string") {
					if(d.indexOf("UNGUELTIG") != -1) {
						Phire.sendNotification(Notifications.SHOW_ALERT_PANEL, {
							text : "Wir konnten für Ihre Angaben leider keine Berufsliste errechnen.<b>Bitte starten Sie den JobTest neu.",
							callback : function(value) {
								
								Phire.sendNotification(Notifications.SHOW_MODULE, {module:"JobTestModule"} );
							}
						});
						return;
					}
					d = $.parseXML(d);
				
				}
				UserInfo.storeDate();
				Phire.sendNotification(Notifications.JOBTEST_RESULT, d);
			});
		}

	});
}
WebProxy.prototype.getJobTestResult = function() {
	$.get(WebProxy.serviceURL, {
		PHPSESSID : UserInfo.PHPSESSID,
		get_auswertung : 1
	}, function(d) {
		//not valid?
		if( typeof (d) == "string") {

			if(d.indexOf("UNGUELTIG") != -1) {
				Phire.sendNotification(Notifications.SHOW_ALERT_PANEL, {
					text : "Ungültig"
				});
				return;
			}
			d = $.parseXML(d);
		}
		Phire.sendNotification(Notifications.JOBTEST_RESULT, d);

	});
}

WebProxy.prototype.deleteJobTestResult = function() {
	$.get(WebProxy.serviceURL, {
		PHPSESSID : UserInfo.PHPSESSID,
		del_data : 1
	}, function(d) {
		UserInfo.jobtestDatum = "NULL";
		UserInfo.jobtestType = "NULL";
		Phire.sendNotification(Notifications.SHOW_MODULE, {
			module : "KompassModule"
		});

	});
}

WebProxy.prototype.getFeelWellXML = function() {

	Phire.sendNotification(Notifications.SHOW_PRELOADER, true);
	$.get(WebProxy.serviceURL, {
		PHPSESSID : UserInfo.PHPSESSID,
		get_data : 1,
		f_type : ("feelwell")
	}, function(d) {

		Phire.sendNotification(Notifications.SHOW_PRELOADER, false);
		if( typeof (d) == "string")
			d = $.parseXML(d);
		Phire.sendNotification(Notifications.FEELWELL_XML_LOADED, d);

	});
}

WebProxy.prototype.getInterviewXML = function(type) {

	Phire.sendNotification(Notifications.SHOW_PRELOADER, true);
	$.get(WebProxy.serviceURL, {
		PHPSESSID : UserInfo.PHPSESSID,
		get_data : 1,
		f_type : ("bewerbungsgespraech," + type)
	}, function(d) {

		Phire.sendNotification(Notifications.SHOW_PRELOADER, false);
		if( typeof (d) == "string")
			d = $.parseXML(d);
		Phire.sendNotification(Notifications.FIT_INTERVIEW_XML_LOADED, d);

	});
}

WebProxy.prototype.getVideoXML = function(type) {

	Phire.sendNotification(Notifications.SHOW_PRELOADER, true);
	$.get(WebProxy.serviceURL, {
		PHPSESSID : UserInfo.PHPSESSID,
		get_data : 1,
		f_type : ("bewerbungsgespraech,main,intid=" + type)
	}, function(d) {

		Phire.sendNotification(Notifications.SHOW_PRELOADER, false);
		if( typeof (d) == "string")
			d = $.parseXML(d);
		Phire.sendNotification(Notifications.VIDEO_XML_LOADED, d);

	});
};

