// JavaScript Document

function clearOnFocus(id, text) {
	var input = document.getElementById(id);
	if(input.value == text) {
		input.value = '';
	}
}
function restoreOnBlur(id, text) {
	var input = document.getElementById(id);
	if(input.value == '') {
		input.value = text;
	}
}

function phoneUpdate() {    
    var phone = document.getElementById("ctl18_ctl02_txtPPhone").value;    
	if(phone.length >= 8) {
		getAddress();
	}
}

function getAddress() {
    var phone = document.getElementById("ctl18_ctl02_txtPPhone").value;	
	if(!isNaN(phone) && phone >= 10000000) {
	    var xmlHttp = initAjax();
	    alert("her");
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 2 || xmlHttp.readyState == 3) {
			}
			if (xmlHttp.readyState == 4) { //Dersom responsen er ferdig
				var response = xmlHttp.responseText;
				if (response != -1) {
				    alert("her5");
					response = eval('(' + response + ')');
					document.getElementById('ctl18_ctl02_txtAdr1').value = response.address1 + " " + response.address2;
					/*document.getElementById('address2').value = response.address2;
					document.getElementById('address3').value = response.address3; */
					document.getElementById('ctl18_ctl02_txtZipcode').value = response.zipcode;
					document.getElementById('ctl18_ctl02_txtCity').value = response.place;
				}
				else {
				}
			}
		}
		alert("her2");
		var name1 = document.getElementById("ctl18_ctl02_txtLName").value;
		var name2 = document.getElementById("ctl18_ctl02_txtFName").value;
		xmlHttp.open('GET', 'http://fadderskap.plan-norge.no/functions/sok.php?phone=' + phone + '&name1=' + name1 + '&name2=' + name2, true);
		alert("her3");
		xmlHttp.send(null);
	}
	else {
	}
}

function initAjax() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}
function newWindow(sourceFile, windowWidth, windowHeight) {
    if (navigator.appName.indexOf('Netscape') != -1) {
        nswidth = windowWidth - 2
        nsheight = windowHeight - 2
        openString = 'scrollbars=no,resizable=no,toolbar=no,location=no,directories=no,status=no,menubar=no,left=0,top=0,width=' + nswidth + ',height=' + nsheight
    }
    else {
        openString = 'scrollbars=no,resizable=no,toolbar=no,location=no,directories=no,status=no,menubar=no,left=0,top=0,width=' + windowWidth + ',height=' + windowHeight
    }
    globalOpenString = openString
    globalSourceFile = sourceFile
    detailWindow = window.open(globalSourceFile, 'flash', globalOpenString)
    detailWindow.focus()
}
