function sendXmlHttpRequest(handler, handlerParam, method, url, content, headers)
{
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
    if (!xmlhttp)
    {
        return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function()
    {
        handler(xmlhttp, handlerParam);
    };

    if (headers)
    {
        for (var key in headers)
        {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}

function loadZip(el, targetId, notallOption)
{
    var coid = el.value;

	if (coid !=320)
	{
		displayZip(targetId, false);
	    
		// default country
		if (coid == 0)
	    {
	    	coid = 320;
	    }
	    
		return;
	}
	else
	{
		displayZip(targetId, true);
	}

    var url = './ajax/zip.php?coid='+ coid;
    
    if (notallOption)
    {
    	url += '&notall=1';
    }
    
    if (targetId != '')
    {
	    var target = document.getElementById(targetId);
		target.innerHTML = '';
	    target.options[target.options.length] = new Option('Loading ...', '-1');
		sendXmlHttpRequest(handleZip, target, 'GET', url);
	}
}

function displayZip(targetId, bool)
{
	var zipSelect = document.getElementById(targetId);
	if (targetId == '')
	{
		return;
	}
	
	if (bool)
	{
		if (document.getElementById(targetId+'label'))
		{
			document.getElementById(targetId+'label').style.display='block';
		}
		zipSelect.style.display='block';
	}
	else
	{
		if (document.getElementById(targetId+'label'))
		{
			document.getElementById(targetId+'label').style.display='none';
		}
		zipSelect.style.display='none';
		zipSelect.selectedIndex = 0;
	}
}

function handleZip(xmlhttp, target)
{
    if (xmlhttp.readyState == 4)
    {
        var zips = xmlhttp.responseXML.getElementsByTagName('zip');
        var target = target;

        target.innerHTML = '';

        for (var i=0; i < zips.length; i++)
        {
            var label = zips[i].firstChild.data;
            var val = zips[i].getAttribute('id');
            target.options[target.options.length] = new Option(label, val);
        }
    }
}

function popupImage(image)
{
    popUp(image.href);
    return false;
}

function popUp(url)
{
    window.open(url, '', 'toolbar=no,menubar=no,location=no,directories=no,scrollbars=yes,resizable=yes,status=no,width=800,height=600');
}

function addFav()
{
	url = 'http://' + window.location.hostname + '/?id=' + projectId;
	title = window.document.title;
	if (window.sidebar) {
		// Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	}
	else if( window.external ) {
		// IE Favorite
		window.external.AddFavorite( url, title);
	}
	else if (window.opera && window.print){
		// Opera
		alert("Please use CTRL + D.");
	}
	else {
		// Mac and other
		alert("Please use CTRL + T, or similar.");
	}
}

function resizeImage(myImg, divide) {
	temp = document.createElement("img");
	temp.src = myImg.src;

	imgw = temp.width;
	imgh = temp.height;

	myImg.style.display = 'none';
	detailBlock = document.getElementById('detail');
	w = detailBlock.offsetWidth;
	w = w / divide;
	if (imgw > w) {
		ratio = w/imgw;
		myImg.style.width = ratio * imgw + 'px';
		myImg.style.height = ratio * imgh + 'px';
		document.getElementById('zoom').style.display = 'inline';
	}
	myImg.style.display = 'block';
}


function showSub(id) {
	if (document.getElementById("sub")) {
		mySub = document.getElementById("sub");
		subs = mySub.getElementsByTagName('DIV');
		for (i=0;i<subs.length;i++) {
			subs[i].style.display='none'
		}
		document.getElementById("sub" + id).style.display='block';
	}
}

function restoreSub() {
	showSub(currsub);
}

function goblink(){
	blinkMe = document.getElementById("blink");
	if (blinkMe != null){
		blinkMe.style.visibility = blinkMe.style.visibility != "visible" ? "visible" : "hidden";
		setTimeout("goblink()", 500);
	}
}

function submitMaleForm(selectElement, subElement)
{
	var form = selectElement.form;
	var value = selectElement.value;
	var submit = true;
	
	function addHiddenInput(name, value)
	{
		var input = document.createElement("input");

		input.type = 'hidden';
		input.name = name;
		input.value = value;

		form.appendChild(input);
	}

	function displayCategorySelect(bool, subElement)
	{
		var categorySelect = document.getElementById(subElement);
		
		categorySelect.style.display = bool ? 'inline' : 'none';
	}
	
	// hide category select
	displayCategorySelect(false, subElement);
	
	// write mail
	if (value == 1)
	{
		addHiddenInput('page', 'user');
		addHiddenInput('action', 'mail');
		addHiddenInput('mailaction', 'write');
	}
	// add to friend category
	else if (value == 2)
	{
		addHiddenInput('page', 'user');
		addHiddenInput('action', 'friend');
		addHiddenInput('friendaction', 'add');
		
		// show category select
		displayCategorySelect(true, subElement);
		
		submit = false;
	}
	// ignore
	else if (value == 3)
	{
		addHiddenInput('page', 'profilemale');
		addHiddenInput('action', 'ignore');
	}
	// delete friend(s)
	else if (value == 4)
	{
		addHiddenInput('page', 'user');
		addHiddenInput('action', 'friend');
		addHiddenInput('friendaction', 'delete');
	}
	else
	{
		submit = false;
	}
	
	if (submit)
	{
		form.submit();
	}
}

function submitMaleFormChild(selectElement)
{
	if (selectElement.value)
	{
		var input = document.createElement("input");

		input.type = 'hidden';
		input.name = 'ufcid';
		input.value = selectElement.value;

		selectElement.form.appendChild(input);
		selectElement.form.submit();
	}
}

function checkUncheckAll(theElement) 
{
	var theForm = theElement.form, z = 0;
	for (z = 0; z < theForm.length; z++)
	{
      	if (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
      	{
	 		theForm[z].checked = theElement.checked;
      	}
	}
}