var xmlHttp;




function getCities(str) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Ihr Browser unterstützt leider kein XMLHTTP.");
		return;
	}
	var url="http://www.mccain.de/static/postdb/ger_cities.php";
	url=url+"?pc="+str+"&rand="+parseInt(Math.random()*999999999999999); // vermijd caching problemen;
	xmlHttp.onreadystatechange=citiesStatusChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

var street = "";

function citiesStatusChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {

		objStreets=document.getElementById("street");

		street = objStreets.value;

		objStreets.options.length = 0;

		objCities=document.getElementById("city");

		city = objCities.value;

		objCities.options.length = 0;

		objCitielabel=document.getElementById("city_label");
        
		xmlDoc=xmlHttp.responseXML;
		cities = xmlDoc.getElementsByTagName("city");

		switch(cities.length) {
			case 0:
            $("#city_label").html ("Bitte geben Sie zuerst Ihre Postleitzahl an.", "");
			objCities.options[objCities.options.length] = new Option('Bitte geben Sie zuerst Ihre Postleitzahl an.', "");
			break;
			case 1:
			objCities.options[objCities.options.length] = new Option(cities[0].childNodes[0].nodeValue, cities[0].childNodes[0].nodeValue);
            $("#city_label").html (cities[0].childNodes[0].nodeValue);
		//	$("#city option").attr("id",cities[0].getAttribute("id"));
		//	$("#city_id").val(cities[0].getAttribute("id"));
			getStreets($('#postalcode').val());
			break;
			default:
            $("#city_label").html ("Wählen Sie Ihre Stadt aus.");
			objCities.options[objCities.options.length] = new Option("Wählen Sie Ihre Stadt aus.", "");
			for (i=0;i<cities.length;i++) {
			objCities.options[objCities.options.length] = new Option(cities[i].childNodes[0].nodeValue, cities[i].childNodes[0].nodeValue);
	//		objCities.options[i+1].setAttribute("id", cities[i].getAttribute("id"));
			}
		}
	}
}

function getStreets(str) {
	xmlHttp2=GetXmlHttpObject();
	if (xmlHttp2==null) {
		alert ("Your browser doesn't support XMLHTTP");
		return;
	}
	var url="http://www.mccain.de/static/postdb/ger_streets.php";
	url=url+"?pc="+str+"&rand="+parseInt(Math.random()*999999999999999); // vermijd caching problemen;
	xmlHttp2.onreadystatechange=streetsStatusChanged;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);
}

function streetsStatusChanged() {
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete") {

		objStreets=document.getElementById("street");
		objStreets.options.length = 0;

		xmlDoc=xmlHttp2.responseXML;
		streets = xmlDoc.getElementsByTagName("street");

		switch(streets.length) {
			case 0:
			objStreets.options[objStreets.options.length] = new Option('Bitte geben Sie zuerst Ihre Postleitzahl an.', "");
            $("#street_label").html ("Bitte geben Sie zuerst Ihre Postleitzahl an.", "");
			break;
			default:
			objStreets.options[objStreets.options.length] = new Option('Wählen Sie Ihre Straße aus.', "");
            $("#street_label").html ("Wählen Sie Ihre Straße aus.", "");
			for (i=0;i<streets.length;i++) {
				objStreets.options[objStreets.options.length] = new Option(streets[i].childNodes[0].nodeValue,streets[i].childNodes[0].nodeValue);
	//			objStreets.options[i+1].setAttribute("id", streets[i].getAttribute("id"));
			}
		}
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


var _dialogPromptID=null;
var _blackoutPromptID=null;

function IEprompt(innertxt,def) {

	that=this;

	var _isIE7=true;

	this.wrapupPrompt = function (cancled) {
		document.getElementById('city').style.display='';
		document.getElementById('street').style.display='';

		if (_isIE7) {
			val=document.getElementById('iepromptfield').value;
			_dialogPromptID.style.display='none';
			_blackoutPromptID.style.display='none';
			document.getElementById('iepromptfield').value = '';
			if (cancled) { val = '' }
			promptCallback(val);
		}
		return false;
	}
	if (def==undefined) { def=''; }
	if (_isIE7) {
		if (_dialogPromptID==null) {
			var tbody = document.getElementsByTagName("body")[0];
			tnode = document.createElement('div');
			tnode.id='IEPromptBox';
			tbody.appendChild(tnode);
			_dialogPromptID=document.getElementById('IEPromptBox');
			tnode = document.createElement('div');
			tnode.id='promptBlackout';
			tbody.appendChild(tnode);
			_blackoutPromptID=document.getElementById('promptBlackout');
			_blackoutPromptID.style.opacity='.5';
			_blackoutPromptID.style.position='absolute';
			_blackoutPromptID.style.top='0px';
			_blackoutPromptID.style.left='0px';
			_blackoutPromptID.style.backgroundColor='#555555';
			_blackoutPromptID.style.filter='alpha(opacity=90)';
			_blackoutPromptID.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px';
			_blackoutPromptID.style.display='block';
			_blackoutPromptID.style.fontFamily='Arial';
			_blackoutPromptID.style.zIndex='1000';
			_dialogPromptID.style.border='2px solid #a63137';
			_dialogPromptID.style.backgroundColor='#fff294';
			_dialogPromptID.style.position='absolute';
			_dialogPromptID.style.width='330px';
			_dialogPromptID.style.zIndex='1100';
		}

//		document.getElementById('city').style.display='none';
//		document.getElementById('street').style.display='none';

		var tmp = '<div style="width: 100%; background-color: #af2f12; color: white; font-family: Arial,Helvetica,sans-serif; font-size: 10pt; font-weight: bold; height: 20px">'+innertxt + '</div>';
		tmp += '<div style="padding: 10px;font-family:Arial,Helvetica,sans-serif;font-size:11px;"><BR>';
		tmp += '<form action="" onsubmit="return that.wrapupPrompt()">';
		tmp += '<input id="iepromptfield" name="iepromptdata" type="text" size="46" style="width:300px" value="'+def+'">';
		tmp += '<br><br><center>';
		tmp += '<input type="submit" style="font-family:Arial,Helvetica,sans-serif" value="&nbsp;&nbsp;&nbsp;OK&nbsp;&nbsp;&nbsp;">';
		tmp += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		tmp += '<input type="button" onclick="that.wrapupPrompt(true)" style="font-family:arial" value="&nbsp;Cancel&nbsp;">';
		tmp += '</form></div>';
		_blackoutPromptID.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px';
		_blackoutPromptID.style.width='100%';
		_blackoutPromptID.style.display='block';
		_dialogPromptID.innerHTML=tmp;
		_dialogPromptID.style.top=parseInt(document.documentElement.scrollTop+(screen.height/3))+'px';
		_dialogPromptID.style.left=parseInt((document.body.offsetWidth-315)/2)+'px';
		_dialogPromptID.style.display='block';
		document.getElementById('iepromptfield').value = document.getElementById('street2').value;
		document.getElementById('iepromptfield').focus();
	} else {
		promptCallback(prompt(innertxt,def));
	}
}

function promptCallback(val) {
	if (val != null && val.length > 2) {
		activateUserStreet(val);
	}
}

function activateUserStreet(val) {
    objStreets=document.getElementById("street");
    objStreets.options.length = 0;
	objStreets.options[objStreets.options.length] = new Option(val,val);
	document.getElementById('street_label').innerHTML = val;
	document.getElementById('user_street').value = val;
	document.getElementById('street_type').value = 'user';
	document.getElementById('street2').disabled=true;
}


function deactivateUserStreet() {
	document.getElementById('street').style.display='inline';
	objStreets=document.getElementById("street");
	objStreets.options.length = 0;
	objStreets.options[objStreets.options.length] = new Option("Bitte geben Sie zuerst Ihre Postleitzahl an.", "");
	document.getElementById('user_street').value = '';
	document.getElementById('street_type').value = 'db';
	document.getElementById('street2').disabled=false;
}
