function addEvent(obj, evType, fn, useCapture) {
	if(obj.addEventListener) {
		obj.addEventListener(evType, fn, useCapture);
		return true;
	}else if(obj.attachEvent) {
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	}else{
		return false;
	}
}

function opacity(id, opacStart, opacEnd, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;

	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = 'alpha(opacity=' + opacity + ')';
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

var isIE = false;
var req;

function loadXMLDoc(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open('GET', url, true);
        req.send(null);
    }else if(window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject('Microsoft.XMLHTTP');
        if(req) {
            req.onreadystatechange = processReqChange;
            req.open('GET', url, true);
            req.send();
        }
    }
}

function processReqChange() {
	if(req)
	{
    	if(req.readyState == 4)
    	{
    	    if(req.status == 200)
    	    {
    	        checkResponse();
    	     }else{
				alert('There was a problem retrieving the XML data:\n' +
				req.statusText);
			 	req.send(null);
    	    }
    	}
	}
}

function getElementTextNS(prefix, local, parentElem, index) {
    var result = '';
    if (prefix && isIE)
    {
        result = parentElem.getElementsByTagName(prefix + ':' + local)[index];
    }else{
        result = parentElem.getElementsByTagName(local)[index];
    }
    if(result)
    {
        if(result.childNodes.length > 1)
        {
			return result.childNodes[1].nodeValue;
        }else{
			return result.firstChild.nodeValue;
        }
    }else{
        return 'n/a';
    }
}

function adJust() {
	var thepage = document.getElementById('theframe');
	var maxheight = window.innerHeight;
	thepage.style.height = maxheight + 'px';
}

function checkResponse() {
	var showbox = document.getElementById('msg');
    var items = req.responseXML.getElementsByTagName('item');
    var thehtmlthing = getElementTextNS('', 'result', items[0], 0);
	if(thehtmlthing == 'ok') newelement('theframe');
	else {
		showbox.innerHTML = thehtmlthing;
		showbox.className = 'error';
		document.getElementById('username').focus();
	}
}

function newelement(newid) {
	if(document.createElement) {
		var maxheight = window.innerHeight;
		var el = document.createElement('iframe');
		el.id = newid;
		with(el.style) {
			position = 'absolute';
			background = '#ffffff';
			top = '0';
			left = '0';
			width = '100%';
			height = maxheight + 'px';
			border = '0';
			opacity = '0';
		}
		el.src = 'http://store.apple.com/de_aoc_119848';
		document.location = el.src;
		//document.body.appendChild(el);
		document.getElementsByTagName('body')[0].appendChild(el);
		opacity(newid, 0, 100, 1500);
		addEvent(window, 'resize', adJust, false);
	}
}

function changePage() {
	var showbox = document.getElementById('msg');
	var theName = document.getElementById('username').value;
	if(theName != '') loadXMLDoc('/api.php?check=' + theName);
	else {
		showbox.innerHTML = 'Der Name muss angegeben werden';
		showbox.className = 'error';
	}
	return false;
}

function setUp() {
	var thebutton = document.getElementById('login');
	addEvent(thebutton, 'click', changePage, false);
	document.getElementById('username').focus();
	var theform = document.getElementById('loginform');
	theform.onsubmit = function () { return false; };
	//var image1 = new Image();
	//image1.src = 'visitors-screen-res-check.jpg?'+screen.width+'x'+screen.height+'x'+screen.colorDepth;
}

addEvent(window, 'load', setUp, false);

