// url: page that returns xml construct
// ID : id to swap content
var ID2Change;
var prev_character = "";
function getContentFromServer(url, ID) {
    ID2Change = ID;
    http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        //alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }


	http_request.onreadystatechange = getContents4Page;


    http_request.open('POST', url, true);
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    var postText = 'ID2change=' + ID2Change + '&';
    for (var i = 0; i < document.forms[0].elements.length; ++i) {
        if (document.forms[0].elements[i].type == "checkbox" || document.forms[0].elements[i].type == 'radio' ) {
            if (document.forms[0].elements[i].checked) {
                postText = postText + '&' + document.forms[0].elements[i].name +'='+document.forms[0].elements[i].value;
            }
        } else {
            postText = postText + '&'  + document.forms[0].elements[i].name +'='+document.forms[0].elements[i].value;
        }
    }

    http_request.send(postText);
}

var g_base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';


	function base64_decode(encStr)
	{
	    var bits;
	    var decOut = '';
	    var i = 0;


	    for(; i < encStr.length; i += 4)
	    {
	        bits = (g_base64_table.indexOf(encStr.charAt(i))     & 0xFF) << 18 |
	        (g_base64_table.indexOf(encStr.charAt(i + 1)) & 0xFF) << 12 |
	        (g_base64_table.indexOf(encStr.charAt(i + 2)) & 0xFF) <<  6 |
	        (g_base64_table.indexOf(encStr.charAt(i + 3)) & 0xFF);

	        decOut += String.fromCharCode((bits & 0xFF0000) >> 16, (bits & 0xFF00) >> 8, bits & 0xFF);
	    }

	    if(encStr.charCodeAt(i - 2) == 61)
	      return(decOut.substring(0, decOut.length - 2));
	    else if(encStr.charCodeAt(i - 1) == 61)
	      return(decOut.substring(0, decOut.length - 1));
	    else
	      return(decOut);
	}

function checkAll(nameElemente) {
   for (var i = 0; i < document.forms[0].elements.length; i++) {
	   if( document.forms[0].elements[i].name.indexOf(nameElemente) == 0 ) {
		   document.forms[0].elements[i].checked = true;
	   }
   }
}

function nmbOfCheckedItems(nameElemente) {
   var nmb = 0;
   for (var i = 0; i < document.forms[0].elements.length; i++) {
	   if( document.forms[0].elements[i].name.indexOf(nameElemente) == 0 ) {
		   if(document.forms[0].elements[i].checked == true){
		   	nmb++;
		   }
	   }
   }
   return nmb;
}

function inverse(nameElemente) {
   for (var i = 0; i < document.forms[0].elements.length; i++) {
	   if( document.forms[0].elements[i].name.indexOf(nameElemente) == 0 ) {
		   document.forms[0].elements[i].checked = !(document.forms[0].elements[i].checked);
	   }
   }
}
function unCheckAll(nameElemente) {
   checkAll(nameElemente);
   inverse(nameElemente);
}

function checkUncheckInvert()
{
	if (document.getElementById('checkboxSelects').value == 'all') {
		checkAll('checkedItem');
	} else if(document.getElementById('checkboxSelects').value == 'none') {
		unCheckAll('checkedItem');
	} else if(document.getElementById('checkboxSelects').value == 'inverse') {
		inverse('checkedItem');
	}
}

function changeCssCheckbox(checkboxName) {
	if(document.getElementById(checkboxName).value == '1') {
		document.getElementById("icon_"+checkboxName).className = 'chbxIcon0';
		document.getElementById(checkboxName).value = '0';
	} else {
		document.getElementById("icon_"+checkboxName).className = 'chbxIcon1';
		document.getElementById(checkboxName).value = '1'
	}
}

function go2url(url) {
	document.forms[0].target="_self";
	document.forms[0].action=url;
	document.forms[0].submit();
}

function speichern() {
	document.forms[0].target="_self";
	document.forms[0].saveData.value="1";
	document.forms[0].action=document.URL;
	document.forms[0].submit();
}

function getInfo() {
	document.forms[0].target="_self";
	document.forms[0].action=document.URL;
	document.forms[0].submit();
}

function showPage(pagename, url) {
	document.forms[0].target=pagename;
	document.forms[0].action=url;
	document.forms[0].submit();
}

function change_lang(lang) {
	document.forms[0].target="_self";
	document.forms[0].action=document.URL;
	document.forms[0].plangcode.value=lang;
	document.forms[0].submit();
}

function change_lang4photographer(lang, url2go) {
	document.forms[0].target="_self";
	document.forms[0].action=url2go;
	document.forms[0].plangcode.value=lang;
	document.forms[0].submit();
}

function startSearch() {
	document.forms[0].target="_self";
	document.forms[0].page.value=0;
	document.forms[0].action="/result.php";
	document.forms[0].submit();

}
function checkEnter4Searchkey(e) {
	var characterCode;
	if (!e) var e = window.event;
	if (e.keyCode) characterCode = e.keyCode;
	else if (e.which) characterCode = e.which;
	//alert(characterCode+' '+prev_character);
	if(characterCode == 13 && prev_character!=40 && prev_character!=38) {
		startSearch();
		return false;
	} else {
		prev_character = characterCode;
		return true;
	}

}
function checkEnter4Startsearch(e) {
	var characterCode;
	if (!e) var e = window.event;
	if (e.keyCode) characterCode = e.keyCode;
	else if (e.which) characterCode = e.which;
	//alert(characterCode+' '+prev_character);
	if(characterCode == 13 && prev_character!=40 && prev_character!=38) {
		view(0);
		return false;
	} else {
		prev_character = characterCode;
		return true;
	}

}

function checkEnter4Key(e) {
    var characterCode;
	 if(e && e.which){
		 e = e
		 characterCode = e.which
	 } else {
		 e = event
		 characterCode = e.keyCode
	 }

	 if(characterCode == 13) {
		return false;
	 } else {
		return true;
	 }
}
function checkEnter4Email(e) {
    var characterCode;
	 if(e && e.which){
		 e = e
		 characterCode = e.which
	 } else {
		 e = event
		 characterCode = e.keyCode
	 }

	 if(characterCode == 13) {
		return false;
	 } else {
		return true;
	 }

}
function checkEnter4Password(e) {
    var characterCode;
	 if(e && e.which){
		 e = e
		 characterCode = e.which
	 } else {
		 e = event
		 characterCode = e.keyCode
	 }

	 if(characterCode == 13) {
	     doLogin();
		return false;
	 } else {
		return true;
	 }
}

function changeCheckedStatus(itemToChange) {
	if (document.getElementById(itemToChange).checked == 1) {
		document.getElementById(itemToChange).checked = 0;
	} else {
	   	document.getElementById(itemToChange).checked = 1;
	}
}

function showHideObj(whichObject) {
	if (document.getElementById(whichObject).style.display == "block") {
		document.getElementById(whichObject).style.display = 'none';
	} else {
		document.getElementById(whichObject).style.display = 'block';
	}
}

function showObj(whichObject) {
	document.getElementById(whichObject).style.display = 'block';
}

function hideObj(whichObject) {
	document.getElementById(whichObject).style.display = 'none';
}

function displayRegistrationWindow() {
	if (typeof Applicationfenster == 'undefined' || Applicationfenster.closed == true) {
		Applicationfenster = window.open('/registration.php','Application','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width=900,height=520,top=200,left=200,resizable=yes');
		Applicationfenster.window.focus();
	 } else {
	 	Applicationfenster.window.focus();
	 }
	 if (parseInt(navigator.appVersion) >= 4) { Applicationfenster.window.focus(); }
}

function openLoginWindow() {
	if (typeof LoginWindow == 'undefined' || LoginWindow.closed == true) {
		LoginWindow = window.open('/login.php','Application','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width=400,height=250,top=50,left=50,resizable=yes');
		LoginWindow.window.focus();
	 } else {
	 	LoginWindow.window.focus();
	 }
	 if (parseInt(navigator.appVersion) >= 4) { LoginWindow.window.focus(); }
}

function doLogin() {
    getContentFromServer4Login("/ajax/a_login.php", "loginW");
}


function openPixFinder() {
	if (typeof PixFinderWindow == 'undefined' || PixFinderWindow.closed == true) {
		PixFinderWindow = window.open('/pixfinder.php','Application','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width=450,height=550,top=50,left=50,resizable=yes');
		PixFinderWindow.window.focus();
	 } else {
	 	PixFinderWindow.window.focus();
	 }
	 if (parseInt(navigator.appVersion) >= 4) { PixFinderWindow.window.focus(); }
}

function openFeedbackTool() {
	if (typeof FeedbackWindow == 'undefined' || FeedbackWindow.closed == true) {
		PixFinderWindow = window.open('/feedback.php','Application','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width=450,height=480,top=50,left=50,resizable=yes');
		PixFinderWindow.window.focus();
	 } else {
	 	PixFinderWindow.window.focus();
	 }
	 if (parseInt(navigator.appVersion) >= 4) { PixFinderWindow.window.focus(); }
}

function validate_email(field) {
	with (field) {
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) {
			return false
		} else {
			return true
		}
	}
}

function wpreview(url, modus) {
	var maxwidth = 990;
	var maxheight = 750;

	if(screen.width < maxwidth) {
		var winwidth = (screen.width - 40);
	} else {
		var winwidth = maxwidth;
	}

	if(screen.height < maxheight) {
		var winheight = (screen.height - 60);
	} else {
		var winheight = maxheight;
	}
	
	if(modus == 'single') {
		var target_url = url;
		var winheight = winheight - 100;
	} else {
		var target_url = url + '&pics='+document.list.thg.value;
	}

	if (typeof fenster == 'undefined' || fenster.closed == true) {
		fenster = window.open(target_url,'Sodapix','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width='+winwidth+',height='+winheight+',top=50,left=20,resizable=yes');
	} else {
		fenster.focus();
		fenster = window.open(target_url,'Sodapix','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width='+width+',height='+height+',resizable=yes');
	}
	if (parseInt(navigator.appVersion) >= 4) { fenster.window.focus(); }
}

function wpreviewSingleOld(pic_id) {
	     var width = 770;
	     var height = 650;
	     var winl = (screen.width - width) / 2;
	     var wint = (screen.height - height) / 2;
	     if (typeof fenster == 'undefined' || fenster.closed == true) {
		     fenster = window.open('detail_base.php?pic_id='+pic_id,'Sodapix | Bildagentur','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width='+width+',height='+height+',top='+wint+',left='+winl+',resizable=yes');
		 } else {
		     fenster.close();
		     fenster = window.open('detail_base.php?pic_id='+pic_id,'Sodapix | Bildagentur','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width=770,height=650,top='+wint+',left='+winl+',resizable=yes');
	    }
	    if (parseInt(navigator.appVersion) >= 4) { fenster.window.focus(); }
	 }

function wpreviewSingle(url) {
	    var width = 770;
	    var height = 650;
	    var winl = (screen.width - width) / 2;
	    var wint = (screen.height - height) / 2;
	    var target_url = url;

	    if (typeof fenster == 'undefined' || fenster.closed == true) {
	        fenster = window.open(target_url,'Sodapix','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width='+width+',height='+height+',top='+wint+',left='+winl+',resizable=yes');
	    } else {
	        fenster.focus();
	        fenster = window.open(target_url,'Sodapix','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width='+width+',height='+height+',resizable=yes');
	    }
	    if (parseInt(navigator.appVersion) >= 4) { fenster.window.focus(); }
	}

function showDemoCalculator() {
		 var width = 770;
	     var height = 650;
	     var winl = (screen.width - width) / 2;
	     var wint = (screen.height - height) / 2;
	     if (typeof fenster == 'undefined' || fenster.closed == true) {
		     fenster = window.open('rm_calculator.php','Sodapix | Bildagentur','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width='+width+',height='+height+',top='+wint+',left='+winl+',resizable=yes');
		 } else {
		     fenster.close();
		     fenster = window.open('rm_calculator','Sodapix | Bildagentur','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width=770,height=650,top='+wint+',left='+winl+',resizable=yes');
	    }
	    if (parseInt(navigator.appVersion) >= 4) { fenster.window.focus(); }
	}

function openRMcalculator(url) {
	    var width = 770;
	    var height = 650;
	    var winl = (screen.width - width) / 2;
	    var wint = (screen.height - height) / 2;
	    var target_url = url;

	    if (typeof fenster == 'undefined' || fenster.closed == true) {
	        fenster = window.open(target_url,'Sodapix','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width='+width+',height='+height+',top='+wint+',left='+winl+',resizable=yes');
	    } else {
	        fenster.focus();
	        fenster = window.open(target_url,'Sodapix','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width='+width+',height='+height+',resizable=yes');
	    }
	    if (parseInt(navigator.appVersion) >= 4) { fenster.window.focus(); }
	}

function openRMcalculatorOld(pic_id, article_id) {
	var width = 770;
	     var height = 500;
	     var winl = (screen.width - width) / 2;
	     var wint = (screen.height - height) / 2;
	     if (typeof fenster == 'undefined' || fenster.closed == true) {
		     fenster = window.open('rm_calculator.php?pic_id='+pic_id+'&article_id='+article_id,'Sodapix | Bildagentur','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width='+width+',height='+height+',top='+wint+',left='+winl+',resizable=yes');
		 } else {
		     fenster.close();
		     fenster = window.open('rm_calculator.php?pic_id='+pic_id+'&article_id='+article_id,'Sodapix | Bildagentur','scrollbars=yes,menubar=no,toolbar=no,status=yes,directories=no,location=no,width=770,height=650,top='+wint+',left='+winl+',resizable=yes');
	    }
	    if (parseInt(navigator.appVersion) >= 4) { fenster.window.focus(); }
	}
function getContentFromServer4Login(url, ID) {
    ID2Change = ID;
    http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        //alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }


	http_request.onreadystatechange = getContents4Page4Login;


    http_request.open('POST', url, true);
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    var postText = 'ID2change=' + ID2Change + '&';
    for (var i = 0; i < document.forms[0].elements.length; ++i) {
        if (document.forms[0].elements[i].type == "checkbox" || document.forms[0].elements[i].type == 'radio' ) {
            if (document.forms[0].elements[i].checked) {
                postText = postText + '&' + document.forms[0].elements[i].name +'='+document.forms[0].elements[i].value;
            }
        } else {
            postText = postText + '&'  + document.forms[0].elements[i].name +'='+document.forms[0].elements[i].value;
        }
    }

    http_request.send(postText);
}

function getContents4Page4Login() {

		if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                var xmldoc = http_request.responseXML;
                var nodes = xmldoc.getElementsByTagName("root").item(0);
                switch (ID2Change) {
                    case 'loginW':
                        var error_msg = base64_decode(nodes.getElementsByTagName("error_msg")[0].firstChild.data);
                        if (error_msg == 'none') {
                            var userLoginInfoContent = base64_decode(nodes.getElementsByTagName("userLoginInfo")[0].firstChild.data);
                            //document.getElementById("userLoginInfo").innerHTML = userLoginInfoContent;
                            var loginWContent = base64_decode(nodes.getElementsByTagName("loginWInfo")[0].firstChild.data);
                            document.getElementById("login_logout").innerHTML = loginWContent;
                            document.getElementById('loginW').style.display = 'none';
                            document.getElementById('customerNavigation').style.display = 'block';
                            document.getElementById("error_msg").innerHTML = '&nbsp;';
                        } else {
                            document.getElementById("error_msg").innerHTML = error_msg;
                            document.forms[0].login_passwort.value = "";
                        }
                        break;
                }
            } else {
                alert('Es ist ein Verbindungsproblem aufgetreten ('+http_request.status+'). Bitte versuchen Sie es erneut....');
            }
        }
	}