/* ******************************************
 * ticketshop.js
 * 
 * Created on 30.08.2007
 * kfrey (c) ZiC internet & communication
 * ******************************************
 */

function NewWindow(mypage, myname, w, h, scroll)
{
	var winl = 15;
	var wint = 15;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
	win = window.open(mypage, myname, winprops);
	if(parseInt(navigator.appVersion) >= 4)
	{
		win.window.focus();
	}
}

function Countdown(time)
{
	try
	{
		var pth = window.location.pathname;
		document.getElementById('divTimer').style.visibility = "visible";
		document.getElementById('spTimer').innerHTML = time;
		time = time - 1; 
		if(time < 0)
		{
			location.href = pth + '?do=terminate';
		}
		else
		{
			window.setTimeout("Countdown("+time+")", 1000);
		}
	}
	catch (e)
	{
		// TODO: handle exception
	}
}

$.ajaxSetup({
	type: "POST"
});

function zic_processPlace(action, event, place, order)
{
	$.ajax({
		url: "placehandle.php",
		data: "do=" + action + "&event_id=" + event +
			"&place_id=" + place + "&order_id=" + order,
		success: function(msg){(zic_handleResponse(msg));}
	});
}

function zic_handleResponse(answer)
{
	var fields = new Array();
	if(answer.indexOf('|') != -1)
	{
		fields = answer.split('|');
		var i;
		for(i=0; i<fields.length; i=i+2)
		{
			var trigger = fields[i];
			var value = fields[i+1];
			
			if(trigger == 'showInfo')
			{
				document.getElementById('infoMsg').style.visibility = 'visible';
				document.getElementById('infoMsg').innerHTML = value;
			}
			else if(trigger == 'hideInfo')
			{
				document.getElementById('infoMsg').style.visibility = 'hidden';
				document.getElementById('infoMsg').innerHTML = '';
			}
			else if(trigger == 'grid')
			{
				document.getElementById('divPlacegrid').innerHTML = value;
			}
			else
			{
				document.getElementById(trigger).innerHTML = value;
			}
		}
	}
}

function zic_customerLogon()
{
	var data = new Array();
	var user = document.frmLogon.txtMail.value;
	var pass = document.frmLogon.txtPassword.value;
	var url = encodeURIComponent(window.location.href);
	$.ajax({
		url: "customerhandle.php",
		data: "do=logon&user=" + user + "&pass=" + pass + "&url=" + url,
		success: function(msg){(zic_logonResponse(msg));}
	});
}

function zic_customerLogout()
{
	$.ajax({
		url: "customerhandle.php",
		data: "do=logout&url=" + encodeURIComponent(window.location.href),
		success: function(msg){(zic_logonResponse(msg));}
	});
}

function zic_customerNewPassword()
{
	var user = document.frmLogon.txtMail.value;
	var url = encodeURIComponent(window.location.href);
	$.ajax({
		url: "customerhandle.php",
		data: "do=newPassword&user=" + user + "&url=" + url,
		success: function(msg){(zic_logonResponse(msg));}
	});
}

function zic_logonResponse(answer)
{
	if(answer != '')
	{
		window.location.href = answer;
	}
}

function zic_checkPW()
{
	var pw1 = document.frmRegister.txtPassword.value;
	var pw2 = document.frmRegister.txtPassword2.value;
	
	if(pw1 != pw2)
	{
		document.frmRegister.txtPassword2.style.backgroundColor = '#FFBFBF';
	}
	else
	{
		document.frmRegister.txtPassword2.style.backgroundColor = '#FFFFFF';
	}
}

function zic_customerSave()
{
	// check fields
	var frm = document.frmRegister;
	var url = encodeURIComponent(window.location.href);
	
	if(frm.txtMail.value == ""
		|| frm.txtFirstName.value == ""
		|| frm.txtLastName.value == ""
		|| frm.txtAddress.value == ""
		|| frm.txtZIP.value == ""
		|| frm.txtCity.value == "")
	{
		alert("Bitte füllen Sie sämtliche mit einem Stern (*)\ngekennzeichneten Felder aus.");
		return false;
	}
	else if(frm.intCustomerID.value == 0
		&& (frm.txtPassword.value == ""	|| frm.txtPassword2.value == ""))
	{
		alert("Bitte kontrollieren Sie die Passwörter.");
		return false;
	}
	else if(frm.txtPassword.value != frm.txtPassword2.value)
	{
		alert("Die angegebenen Passwörter sind nicht identisch.");
		return false;
	}
	else
	{
		$.ajax({
			url: "customerhandle.php",
			data: "do=saveDetails&txtMail=" + frm.txtMail.value + 
				"&txtPassword=" + frm.txtPassword.value + 
				"&txtPassword2=" + frm.txtPassword2.value + 
				"&txtFirstName=" + frm.txtFirstName.value +
				"&txtLastName=" + frm.txtLastName.value +
				"&txtAddress=" + frm.txtAddress.value +
				"&txtZIP=" + frm.txtZIP.value +
				"&txtCity=" + frm.txtCity.value +
				"&txtPhone=" + frm.txtPhone.value +
				"&txtMobile=" + frm.txtMobile.value +
				"&url=" + url,
			success: function(msg){(zic_logonResponse(msg));}
		});
		
		return false;
	}
}

function zic_customerDetails(action)
{
	var url	= window.location.href;
	
	if(action == 'show')
	{
		if(url.indexOf('?') == -1)
		{
			window.location.href = url + '?userribbon=myaccount';
		}
		else
		{
			window.location.href = url + '&userribbon=myaccount';
		}
	}
	else
	{
		url = url.replace(/&userribbon=myaccount/g, "");
		url = url.replace(/\?userribbon=myaccount/g, "");
		window.location.href = url;
	}
}


