// Holds the global cursor positions
var globalCursorPosX = 0;
var globalCursorPosY = 0;

// Executes an AJAX Request
function MakeHttpRequest(url, callback_function, destination)
{
	var http_request = false;
	
	if (window.XMLHttpRequest)
	{
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject)
	{
		try
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}

	if (!http_request)
	{
		alert('Dein Browser unterstützt ein verwendetes Feature nicht.');
		return false;
	}
	
	http_request.onreadystatechange = function()
	{
	    try {
    		if (http_request.readyState == 4)
    		{
    			if (http_request.status == 200)
    			{
    				if (destination)
    				{
    					eval(callback_function + '(http_request.responseText, \'' +destination+ '\')');
    				}
    				else
    				{
    					eval(callback_function + '(http_request.responseText)');
    				}
    			}
    			else
    			{
    				alert('Bei einem httpRequest ist ein Fehler aufgetreten (Code: ' + http_request.status + ').');
    			}
    		}
	    }
	    catch(e) { return false; }
	}
	
	http_request.open('GET', url, true);
	http_request.send(null);
}

// Register Onload Functions
function myOnload() {
    // focus the first coupon field
    if(document.getElementById("field1")) {
        document.getElementById("field1").focus();
    }
    
    // initialize gm?
    if(document.getElementById("GM")) {
        load();
    }
}

// Register UnOnload Functions
function myOnUnload() {
    // initialize gm?
    if(document.getElementById("GM")) {
        GUnload();
    }
}

// Selects a product from the quick shop
function selectQuickShopProduct(productId, productCount) {
    // running upload? return
    if(isUploading()) {
        return;
    }
    
    // unmark all products first
    for(i=0; i <= productCount; i++) {
        // product found?
        if(document.getElementById("QuickShopProduct" + i)) {
            // unmark the found product
            document.getElementById("QuickShopProduct" + i).className = "QuickShopProduct";
        }
    }
    
    // mark the new product
    document.getElementById("QuickShopProduct" + productId).className = "QuickShopProduct SelectedProduct";
    
    // get the list of formats
    getQuickShopFormats(null, productId);
}

// Adds the Decision Step
function getQuickShopFormats(responseText, productId)
{
    // get the step, or display it?
    if(responseText == null) {
        
        // build the url
        var url  = "./?do=shop&area=rpc&action=getformats&id=" + productId;
        
        // execute the httpRequest
        MakeHttpRequest(url, "getQuickShopFormats");
    }
    else {
        // assign the response text
        document.getElementById("QuickShopFormats").innerHTML = responseText;
        
        // show the text a cool way
        myFx = new Fx.Height("QuickShopFormats", {duration: 400});
        myFx.hide();
        myFx.toggle();
        
    }
}

// Selects a format from the quick shop
function selectQuickShopFormat(formatId, formatCount, productId) {
    // running upload? return
    if(isUploading()) {
        return;
    }
    
    // unmark all formats first
    for(i=0; i <= formatCount; i++) {
        // format found?
        if(document.getElementById("QuickShopFormat" + i)) {
            // unmark the found product
            document.getElementById("QuickShopFormat" + i).className = "Format";
        }
    }
    
    // mark the new format
    document.getElementById("QuickShopFormat" + formatId).className = "Format SelectedFormat";
    
    // get the motive selection
    getQuickShopMotives(null, formatId, productId);
}

// Adds the Decision Step
function getQuickShopMotives(responseText, formatId, productId)
{
    // get the step, or display it?
    if(responseText == null) {
        
        // build the url
        var url  = "./?do=shop&area=rpc&action=getmotiveselection&product=" + productId + 
                   "&format=" + formatId;
        
        // execute the httpRequest
        MakeHttpRequest(url, "getQuickShopMotives");
    }
    else {
        // assign the response text
        document.getElementById("QuickShopMotives").innerHTML = responseText;
        
        // show the text a cool way
        myFx = new Fx.Height("QuickShopMotives", {duration: 400, onComplete: function()
        {
            document.getElementById("QuickShopMotives").style.height = "200px";
        }});
        
        myFx.hide();
        myFx.toggle();
        
    }
}

// Selects a motive from the quick shop
function selectQuickShopMotive(motiveId, motiveCount) {
    // running upload? return
    if(isUploading()) {
        return;
    }
    
    // unmark all products first
    for(i=0; i <= motiveCount; i++) {
        // motive found?
        if(document.getElementById("QuickShopMotive" + i)) {
            // unmark the found motive
            document.getElementById("QuickShopMotive" + i).className = "Motive";
        }
    }
    
    // mark the new motive
    document.getElementById("QuickShopMotive" + motiveId).className = "Motive SelectedMotive";
    
    // disable some areas
    document.getElementById("QuickShopMotiveUpload").style.display = "none";
    document.getElementById("QuickShopMotiveGalleryStart").style.display = "none";
    document.getElementById("QuickShopMotiveClear2").style.display = "none";
    document.getElementById("QuickShopSubmit").style.display = "none";
    
    // enable the desired area
    if(motiveId == 1) {
        // motive upload
        document.getElementById("QuickShopMotiveUpload").style.display = "block";
        document.getElementById("SourceType").value = 1;
        document.getElementById("SourceId").value = null;
        document.getElementById("QuickShopSubmit").style.display = "block";
    }
    else if(motiveId == 2) {
        document.getElementById("QuickShopMotiveGalleryStart").style.display = "block";
        document.getElementById("SourceType").value = 2;
        document.getElementById("SourceId").value = null;
    }
    
    // enable the clear line
    document.getElementById("QuickShopMotiveClear2").style.display = "block";
}

// Shows the quick shop gallery
function showQuickShopGallery(productId, formatId) {
    // Show the gallery
    document.getElementById("QuickShopGallery").style.display = "block";
    
    // get the gallery
    getQuickShopGallery(null, productId, formatId);
}

// gets the quick shop gallery
function getQuickShopGallery(responseText, productId, formatId) {
    // make the request, or display it?
    if(responseText == null) {
        
        // build the url
        var url  = "./?do=shop&area=rpc&action=getgallery&product=" + productId + "&format=" + formatId;
        
        // execute the httpRequest
        MakeHttpRequest(url, "getQuickShopGallery");
    }
    else {
        // assign the response text
        document.getElementById("QuickShopGallery").innerHTML = responseText;
    }
}


// Updates the quick shop gallery
function updateQuickShopGallery(responseText, productId, formatId, page) {
    // make the request, or display it?
    if(responseText == null) {
        // get the selected motive category
        var motive_category = document.getElementById("MotiveCategory").value;
        
        // get the search keywords
        var motive_keywords = document.getElementById("MotiveKeywords").value;
        
        // build the url
        var url  = "./?do=shop&area=rpc&action=getgallery&motive_category=" + motive_category +
                   "&product=" + productId + "&format=" + formatId + "&keywords=" + motive_keywords +
                   "&page=" + page;
        
        // execute the httpRequest
        MakeHttpRequest(url, "getQuickShopGallery");
    }
    else {
        // assign the response text
        document.getElementById("QuickShopGallery").innerHTML = responseText;
    }
}

// Shows the quick shop gallery
function closeQuickShopGallery() {
    // Show the gallery
    document.getElementById("QuickShopGallery").style.display = "none";
}

// Selects a motive from the gallery
function selectQuickShopGalleryMotive(motiveId, motiveName, motiveUrl) {
    // close the gallery
    closeQuickShopGallery();
    
    // store the source id
    document.getElementById("SourceId").value = motiveId;
    
    // mark the action field as "uploading"
    document.getElementById("QuickShopNoAction").value = 1;
    
    // enable the loading field
    document.getElementById("QuickShopLoading2").style.display = "block";
    
    // submit the form
    document.getElementById("QuickShopForm").submit();
}

// Skips the gallery selection, and starts the upload
function startQuickShopUpload() {
    // only start, if there is a valid file
    if(document.getElementById("QuickShopMotiveUploadInput").value.length <= 0) {
        return;
    }
    
    // disable the button
    document.getElementById("QuickShopSubmit").style.display = "none";
    
    // mark the action field as "uploading"
    document.getElementById("QuickShopNoAction").value = 1;
    
    // enable the loading field
    document.getElementById("QuickShopLoading").style.display = "block";
    
    // start the upload
    document.getElementById("QuickShopForm").submit();
    
    // disable the file selection
    document.getElementById("QuickShopMotiveUploadInput").disabled = true;
}

// Checks if the upload process is running
function isUploading() {
    if(document.getElementById("QuickShopNoAction").value > 0) {
        return true;
    }
    else {
        return false;
    }
}

// Updates the shopping cart
function updateCart(itemId) {
    // get the new data from the article
    var formatId = document.getElementById("CartArticleFormat" + itemId).value;
    var quantity = document.getElementById("CartArticleQuantity" + itemId).value;
    var effect = document.getElementById("CartArticleEffect" + itemId).value;
    var rotation = document.getElementById("CartArticleRotation" + itemId).value;
    var cut = document.getElementById("CartArticleCut" + itemId).value;
    
    // build the url
    var url = "./?do=shop&area=cart&action=edititem&id=" + itemId + "&format_id=" + formatId +
              "&quantity=" + quantity + "&effect=" + effect + "&rotation=" + rotation +
              "&cut=" + cut + "#ItemAnchor" + itemId;
    
    // relocate
    document.location = url;
}

// Deletes an item from the cart
function deleteCartItem(itemId) {
    // build the url
    var url = "./?do=shop&area=cart&action=deleteitem&id=" + itemId;
    
    // relocate
    document.location = url;
}

// Copies an item from the cart
function copyCartItem(itemId) {
    // build the url
    var url = "./?do=shop&area=cart&action=copyitem&id=" + itemId;
    
    // relocate
    document.location = url;
}

// Checks a customer before he can proceed to the shipping selection
function checkCustomer(skipPassCheck, checkAGBMessage)
{
    // get the fields
    var last_name  = document.getElementById("last_name").value;
    var first_name  = document.getElementById("first_name").value;
    var address = document.getElementById("address").value;
    var streetnr = document.getElementById("streetnr").value;
    var zipcode = document.getElementById("zipcode").value;
    var town = document.getElementById("town").value;
    var country = document.getElementById("country").value;
    var email = document.getElementById("email").value;
    var phone = document.getElementById("phone").value;
    var bday = document.getElementById("bday").value;
    var bmonth = document.getElementById("bmonth").value;
    var byear = document.getElementById("byear").value;
    
    if(!skipPassCheck) {
        var password1 = document.getElementById("password1").value;
        var password2 = document.getElementById("password2").value;
    }
    
    if(checkAGBMessage) {
        var agb = document.getElementById("agb").checked;
    }
    
    // did errors happen?
    var noErrors = true;
    
    // check the last_name
    if(last_name.length <= 0) { // error
        noErrors = false;
        document.getElementById("last_name").className = "Text EmptyInput";
    }
    else {
        document.getElementById("last_name").className = "Text";
    }
    
    // check the first_name
    if(first_name.length <= 0) { // error
        noErrors = false;
        document.getElementById("first_name").className = "Text EmptyInput";
    }
    else {
        document.getElementById("first_name").className = "Text";
    }
    
    // check the address
    if(address.length <= 0) { // error
        noErrors = false;
        document.getElementById("address").className = "Text Address EmptyInput";
    }
    else {
        document.getElementById("address").className = "Text Address";
    }
    
    // check the streetnr
    if(streetnr.length <= 0) { // error
        noErrors = false;
        document.getElementById("streetnr").className = "Text StreetNr EmptyInput";
    }
    else {
        document.getElementById("streetnr").className = "Text StreetNr";
    }
    
    // check the zipcode
    if(zipcode.length <= 0 || isNaN(zipcode) || zipcode <= 0) { // error
        noErrors = false;
        document.getElementById("zipcode").className = "Text Zip EmptyInput";
    }
    else {
        document.getElementById("zipcode").className = "Text Zip";
    }
    
    // check the town
    if(town.length <= 0) { // error
        noErrors = false;
        document.getElementById("town").className = "Text EmptyInput";
    }
    else {
        document.getElementById("town").className = "Text";
    }
    
    // check the country
    if(country.length <= 0 || country <= 0 || isNaN(country)) { // error
        noErrors = false;
        document.getElementById("country").className = "EmptyInput";
    }
    else {
        document.getElementById("country").className = "";
    }
    
    // check the email
    if(email.length <= 0) { // error
        noErrors = false;
        document.getElementById("email").className = "Text EmptyInput";
    }
    else {
        document.getElementById("email").className = "Text";
    }
    
    // check the phone
    if(phone.length <= 0) { // error
        noErrors = false;
        document.getElementById("phone").className = "Text EmptyInput";
    }
    else {
        document.getElementById("phone").className = "Text";
    }
    
    // check the bday
    if(bday.length <= 0 || bday <= 0 || isNaN(bday)) { // error
        noErrors = false;
        document.getElementById("bday").className = "EmptyInput";
    }
    else {
        document.getElementById("bday").className = "";
    }
    
    // check the bmonth
    if(bmonth.length <= 0 || bmonth <= 0 || isNaN(bmonth)) { // error
        noErrors = false;
        document.getElementById("bmonth").className = "EmptyInput";
    }
    else {
        document.getElementById("bmonth").className = "";
    }
    
    // check the byear
    if(byear.length <= 0 || byear <= 0 || isNaN(byear)) { // error
        noErrors = false;
        document.getElementById("byear").className = "EmptyInput";
    }
    else {
        document.getElementById("byear").className = "";
    }
    
    
    // check the password1
    if(!skipPassCheck) {
        if(password1.length < 5 || password2.length < 5 || password1 != password2) { // error
            noErrors = false;
            document.getElementById("password1").className = "Text EmptyInput";
            document.getElementById("password2").className = "Text EmptyInput";
        }
        else {
            document.getElementById("password1").className = "Text";
            document.getElementById("password2").className = "Text";
        }
    }
    
    // check the agbs
    if(checkAGBMessage) {
        if(!agb) {
            
            if(noErrors) {
                alert(checkAGBMessage);
            }
            
            noErrors = false;
        }
    }
    
    // return the error var
    return noErrors;
}

// Goes to the customer selection
function gotoCustomer() {
    // build the url
    var url = "./?do=checkout&area=customer&action=show";
    
    // relocate
    document.location = url;
}

// shows the selected payment type
function setPaymentType(paymentType) {
    // hide both types
    document.getElementById("PaymentType1").style.display = "none";
    document.getElementById("PaymentType2").style.display = "none";
    document.getElementById("PaymentType3").style.display = "none";
    
    // show the selected type
    document.getElementById("PaymentType" + paymentType).style.display = "block";
}

// Checks the payment method 1 (bank), before processing
function checkPaymentBank()
{
    // get the fields
    var account_owner  = document.getElementById("account_owner").value;
    var account_number  = document.getElementById("account_number").value;
    var bank_name = document.getElementById("bank_name").value;
    var bank_number = document.getElementById("bank_number").value;
    var bank_country = document.getElementById("bank_country").value;
    
    // did errors happen?
    var noErrors = true;
    
    // check the account_owner
    if(account_owner.length <= 0) { // error
        noErrors = false;
        document.getElementById("account_owner").className = "Text AccountOwner EmptyInput";
    }
    else {
        document.getElementById("account_owner").className = "Text AccountOwner";
    }
    
    // check the account_number
    if(account_number.length <= 0) { // error
        noErrors = false;
        document.getElementById("account_number").className = "Text EmptyInput";
    }
    else {
        document.getElementById("account_number").className = "Text";
    }
    
    // check the bank_name
    if(bank_name.length <= 0) { // error
        noErrors = false;
        document.getElementById("bank_name").className = "Text EmptyInput";
    }
    else {
        document.getElementById("bank_name").className = "Text";
    }
    
    // check the bank_number
    if(bank_number.length <= 0) { // error
        noErrors = false;
        document.getElementById("bank_number").className = "Text Blz EmptyInput";
    }
    else {
        document.getElementById("bank_number").className = "Text Blz";
    }
    
    // check the bank_country
    if(bank_country.length <= 0 || bank_country <= 0 || isNaN(bank_country)) { // error
        noErrors = false;
        document.getElementById("bank_country").className = "EmptyInput";
    }
    else {
        document.getElementById("bank_country").className = "";
    }
    
    // return the error var
    return noErrors;
}

// Checks the payment method 1 (bank), before processing
function checkPaymentCC()
{
    // get the fields
    var cc_type  = document.getElementById("cc_type").value;
    var cc_owner  = document.getElementById("cc_owner").value;
    var cc_number  = document.getElementById("cc_number").value;
    var cc_check_number = document.getElementById("cc_check_number").value;
    
    // did errors happen?
    var noErrors = true;
    
    // check the cc_type
    if(cc_type.length <= 0 || cc_type <= 0 || isNaN(cc_type)) { // error
        noErrors = false;
        document.getElementById("cc_type").className = "EmptyInput";
    }
    else {
        document.getElementById("cc_type").className = "";
    }
    
    // check the cc_owner
    if(cc_owner.length <= 0) { // error
        noErrors = false;
        document.getElementById("cc_owner").className = "Text EmptyInput";
    }
    else {
        document.getElementById("cc_owner").className = "Text";
    }
    
    // check the cc_number
    if(cc_number.length <= 0) { // error
        noErrors = false;
        document.getElementById("cc_number").className = "Text EmptyInput";
    }
    else {
        document.getElementById("cc_number").className = "Text";
    }
    
    // check the cc_check_number
    if(cc_check_number.length <= 0) { // error
        noErrors = false;
        document.getElementById("cc_check_number").className = "Text CheckNumber EmptyInput";
    }
    else {
        document.getElementById("cc_check_number").className = "Text CheckNumber";
    }
    
    // return the error var
    return noErrors;
}

// Toggles the shipping address
function toggleShippingAddress()
{
    // toggle it!
    if(!document.getElementById("use_shipping_address").checked) {
        
        // hide the area
        document.getElementById("ShippingAddress").style.display = "none";
        
        document.getElementById("Button1").style.display = "inline";
        document.getElementById("Button2").style.display = "none";
    }
    else {
        // show the area
        document.getElementById("ShippingAddress").style.display = "block";
        document.getElementById("Button1").style.display = "none";
        document.getElementById("Button2").style.display = "block";
    }
    
}

// Checks the shipping address before proceeding to the payment section
function checkShippingAddress()
{
    // should we skip the check?
    if(!document.getElementById("use_shipping_address").checked) {
        return true;
    }
    
    // get the fields
    var last_name  = document.getElementById("last_name").value;
    var first_name  = document.getElementById("first_name").value;
    var address = document.getElementById("address").value;
    var streetnr = document.getElementById("streetnr").value;;
    var zipcode = document.getElementById("zipcode").value;
    var town = document.getElementById("town").value;
    var country = document.getElementById("country").value;
    
    
    // did errors happen?
    var noErrors = true;
    
    // check the last_name
    if(last_name.length <= 0) { // error
        noErrors = false;
        document.getElementById("last_name").className = "Text EmptyInput";
    }
    else {
        document.getElementById("last_name").className = "Text";
    }
    
    // check the first_name
    if(first_name.length <= 0) { // error
        noErrors = false;
        document.getElementById("first_name").className = "Text EmptyInput";
    }
    else {
        document.getElementById("first_name").className = "Text";
    }
    
    // check the address
    if(address.length <= 0) { // error
        noErrors = false;
        document.getElementById("address").className = "Text Address EmptyInput";
    }
    else {
        document.getElementById("address").className = "Text Address";
    }
    
    // check the streetnr
    if(streetnr.length <= 0) { // error
        noErrors = false;
        document.getElementById("streetnr").className = "Text StreetNr EmptyInput";
    }
    else {
        document.getElementById("streetnr").className = "Text StreetNr";
    }
    
    // check the zipcode
    if(zipcode.length <= 0 || isNaN(zipcode) || zipcode <= 0) { // error
        noErrors = false;
        document.getElementById("zipcode").className = "Text Zip EmptyInput";
    }
    else {
        document.getElementById("zipcode").className = "Text Zip";
    }
    
    // check the town
    if(town.length <= 0) { // error
        noErrors = false;
        document.getElementById("town").className = "Text EmptyInput";
    }
    else {
        document.getElementById("town").className = "Text";
    }
    
    // check the country
    if(country.length <= 0 || country <= 0 || isNaN(country)) { // error
        noErrors = false;
        document.getElementById("country").className = "EmptyInput";
    }
    else {
        document.getElementById("country").className = "";
    }
    
    // return the error var
    return noErrors;
}

// AGB check was moved to the CheckCustomer function, to nothing in here
function checkFinish()
{
    // disable the order buttons
    document.getElementById("OrderButtonBig").disabled = true;
    document.getElementById("OrderButtonSmall").disabled = true;
        
    // show the notice field
    document.getElementById("OrderFinish").style.visibility = "visible";
    
    // all ok
    return true;
}

// get the motive gallery
function getMotiveGallery() {
    updateMotiveGallery(null, 0);
}

// gets the motive shop gallery
function updateMotiveGallery(responseText, page) {
    // make the request, or display it?
    if(responseText == null) {
        // get the category
        var categoryId = document.getElementById("MotiveCategory") ? document.getElementById("MotiveCategory").value : 0;
        
        // get the search keywords
        var motive_keywords = document.getElementById("MotiveKeywords") ? document.getElementById("MotiveKeywords").value : "";
        
        // build the url
        var url  = "./?do=shop&area=rpc&action=getmotivegallery&motive_category=" + categoryId + 
                   "&keywords=" + motive_keywords + "&page=" + page;
        
        // execute the httpRequest
        MakeHttpRequest(url, "updateMotiveGallery");
    }
    else {
        // assign the response text
        document.getElementById("MotiveGallery").innerHTML = responseText;
    }
}
// shows the motive gallery
function showMotiveGallery() {
    // hide the preview
    document.getElementById("MotiveGalleryPreview").style.display = "none";
    
    // reset the selection
    document.getElementById("SourceId").value = 0;
    
    // create the gallery handle
    document.getElementById("MotiveGallery").style.display = "block";
    document.getElementById("MotiveShow").style.display = "none";
    document.getElementById("MotiveHide").style.display = "block";
}

// Hides the motive gallery
function hideMotiveGallery() {
    // create the gallery handle
    document.getElementById("MotiveGallery").style.display = "none";
    document.getElementById("MotiveShow").style.display = "block";
    document.getElementById("MotiveHide").style.display = "none";
    
    // is tehre a selected image?
    if(document.getElementById("SourceId").value > 0) {
        // update the motive preview
        updateMotiveGalleryPreview(null, document.getElementById("SourceId").value);
    }
}

function updateMotiveGalleryPreview(responseText, motiveId) {
    // make the request, or display it?
    if(responseText == null) {
        // build the url
        var url  = "./?do=shop&area=rpc&action=getmotivepreview&id=" + motiveId;
        
        // execute the httpRequest
        MakeHttpRequest(url, "updateMotiveGalleryPreview");
    }
    else {
        // assign the response text
        document.getElementById("MotiveGalleryPreview").innerHTML = responseText;
        document.getElementById("MotiveGalleryPreview").style.display = "block";
    }
}

// Selects an existing motive
function selectMotiveGalleryMotive(motiveId) {
    // save the motive
    document.getElementById("SourceId").value = motiveId;
    
    // hide the motive selection
    hideMotiveGallery();
    
    // update the format selection
    updateFormatSelection();
    
    // update the summary
    updateMotiveSummary();
}

// Updates the format selection, if a valid product was selected
function updateFormatSelection() {
    // get the value
    var productId = document.getElementById("ProductId").value;
    
    // valid product?
    if(productId > 0 && !isNaN(productId)) {
        showFormatSelection();
    }
}

// shows the format selection
function showFormatSelection() {
    // get the product
    var productId = document.getElementById("ProductId").value
    var motiveId = document.getElementById("SourceId").value;
    
    // get the format selection?
    if(productId > 0 && !isNaN(productId) && motiveId > 0 && !isNaN(motiveId)) {
        // reset the format id?
        if(document.getElementById("FormatId")) {
            document.getElementById("FormatId").selectedIndex = 0;
        }
        getFormatSelection(null, productId, motiveId);
    }
    
    document.getElementById("FormatSelection").style.display = "block";
    
    // update the summary
    updateMotiveSummary();
}

// gets the format selection
function getFormatSelection(responseText, productId, motiveId) {
    // make the request, or display it?
    if(responseText == null) {
        // build the url
        var url  = "./?do=shop&area=rpc&action=getgalleryformats&id=" + productId + "&motive=" + motiveId;
        
        // execute the httpRequest
        MakeHttpRequest(url, "getFormatSelection");
    }
    else {
        // assign the response text
        document.getElementById("FormatSelection").innerHTML = responseText;
    }
}

// updates the motive sumamry
function updateMotiveSummary() {
    // get the product
    var productId = document.getElementById("ProductId").value
    var motiveId = document.getElementById("SourceId").value;
    var formatId = document.getElementById("FormatId") ? document.getElementById("FormatId").value : 0;
    
    // is there a file?
    if(document.getElementById("UserMotive")) {
        var fileName = document.getElementById("UserMotive").value;
        var fileThere = true;
    }
    else {
        var fileName = null;
        var fileThere = false;
    }
    
    // get the format selection?
    if(productId > 0 && !isNaN(productId) && motiveId > 0 && !isNaN(motiveId) && formatId > 0 &&
       !isNaN(formatId) && (!fileThere || fileThere && fileName.length > 1)) {
        document.getElementById("MotiveSummary").style.display = "block";
    }
    else {
        document.getElementById("MotiveSummary").style.display = "none";
    }
}

// shows the motive file
function showMotiveFile() {
    document.getElementById("MotiveUploadField").style.display = "block";
    document.getElementById("MotiveFileShow").style.display = "none";
    document.getElementById("MotiveFileHide").style.display = "block";
}

// Updates the upload field
function updateUploadField() {
    // get the value
    var file = document.getElementById("UserMotive").value;
    
    // valid file
    if(file.length > 0) {
        // update the format selection
        updateFormatSelection();
        
        // update the summary
        updateMotiveSummary();
    }
    else { // show the file upload
        // hide the motive selection
        showMotiveFile();
        
        // update the summary
        updateMotiveSummary();
    }
}

// Enables the loading text
function enableLoadingText() {
    // enable the text
    document.getElementById("LoadingText").style.display = "block";
    
    // disable the button
    document.getElementById("UploadButton").disabled = true;
}

// Shows the cut help 
function openCutHelp()
{
        var wstat
        var ns4up = (document.layers) ? 1 : 0
        var ie4up = (document.all) ? 1 : 0
        var xsize = screen.width
        var ysize = screen.height
        var breite= 640;
        var hoehe= 460;
        var xpos=(xsize-breite)/2
        var ypos=(ysize-hoehe)/2
        
        window.open("index.php?do=staticpage&area=cuthelp&use_empty=1","","scrollbars=no,status=no,toolbar=no,location=no,directories=no,resizable=yes,menubar=no,width="+breite+",height="+hoehe+",screenX="+xpos+",screenY="+ypos+",top="+ypos+",left="+xpos)
}

// Shows the rotation help 
function openRotationHelp() {
        var wstat
        var ns4up = (document.layers) ? 1 : 0
        var ie4up = (document.all) ? 1 : 0
        var xsize = screen.width
        var ysize = screen.height
        var breite= 640;
        var hoehe= 190;
        var xpos=(xsize-breite)/2
        var ypos=(ysize-hoehe)/2
        
        window.open("index.php?do=staticpage&area=rotationhelp&use_empty=1","","scrollbars=no,status=no,toolbar=no,location=no,directories=no,resizable=yes,menubar=no,width="+breite+",height="+hoehe+",screenX="+xpos+",screenY="+ypos+",top="+ypos+",left="+xpos)
}

// shows the error reporting window
function showErrorReport(responseText) {
    // make the request, or display it?
    if(responseText == null) {
        // random var
        var curDate = new Date();
        
        // build the url
        var url  = "./?do=shop&area=rpc&action=geterrorreport&time=" + curDate.getSeconds();
        
        // execute the httpRequest
        MakeHttpRequest(url, "showErrorReport");
    }
    else {
        // assign the response text
        document.getElementById("QuickShopGallery").innerHTML = responseText;
        
        // display the template
        document.getElementById("QuickShopGallery").style.display = "block";
    }   
}

// Saves an error report
function saveErrorReport(responseText) {
    // make the request, or display it?
    if(responseText == null) {
        // get the input
        var errorDescription = document.getElementById("error_description").value;
        var errorCategory = document.getElementById("error_category").value;
        
        // invalid description?
        if(errorDescription.length <= 0) {
            return;
        }
        
        // build the url
        var url  = "./?do=shop&area=rpc&action=saveerrorreport&category=" + escape(errorCategory) +
                   "&description=" + escape(errorDescription);
        
        // execute the httpRequest
        MakeHttpRequest(url, "saveErrorReport");
    }
    else {
        // hide the error reporting window
        document.getElementById("QuickShopGallery").style.display = "none";
    }
}

// no submit on enter
function noEnterSubmit(myform, e) {
    var keycode;
    
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    
    if (keycode == 13) {
        return false;
    }
    else {
       return true;
    }
}

// Helper function for opening windows
function openWindow(url, w, h) {
   var padding;
   (navigator.appName == "Microsoft Internet Explorer") ? (padding = 0) : (padding = 0);

   var screenw = screen.availWidth;
   var screenh = screen.availHeight;
   var winw = (w + 15 + padding);
   var winh = (h + 15 + padding);
   var posx = (screenw / 2) - (winw / 2);
   var posy = (screenh / 2) - (winh / 2);

    window.open(url,"mywin","top="+posy+",left="+posx+",width="+winw+",height="+winh+",menubar=no,locationbar=no,statusbar=no,resizable=no,toolbar=no,scrollbars=yes");
}

// Shows the final size help 
function openFinalSizeHelp() {
        var wstat
        var ns4up = (document.layers) ? 1 : 0
        var ie4up = (document.all) ? 1 : 0
        var xsize = screen.width
        var ysize = screen.height
        var breite= 540;
        var hoehe= 335;
        var xpos=(xsize-breite)/2
        var ypos=(ysize-hoehe)/2
        
        window.open("index.php?do=staticpage&area=finalsizehelp&use_empty=1","","scrollbars=no,status=no,toolbar=no,location=no,directories=no,resizable=yes,menubar=no,width="+breite+",height="+hoehe+",screenX="+xpos+",screenY="+ypos+",top="+ypos+",left="+xpos)
}

// changes to the next field
function nextCouponField(thisField, nextField) {
    // current value
    var curVal = thisField.value
    
    // if the value is 4 -> next field
    if(curVal.length == 4) {
        // focus the next vield
        //document.getElementById(nextField).focus();
    }
}

// Logs the current click - used for analysing and debugging
function cL(responseText, currentDo, currentArea)
{
    // disabled
    return;
    
    // build the request, or execute it?
    if(null == responseText) {
        // get the resolution
        var screenX = screen.width;
        var screenY = screen.height;
        
        // no valid coordinates?
        if(globalCursorPosX <= 0 || globalCursorPosY <= 0) {
            // we don't want this
            return;
        }
        
        // testid = 8pe6nfk2700abr3hvg5p0409g2
        
        // build the url
        var url = "./?do=shop&area=rpc&action=jsclicklog&screenx=" + screenX + "&screeny=" + screenY +
                  "&pagex=" + globalCursorPosX + "&pagey=" + globalCursorPosY + "&lastdo=" + currentDo +
                  "&lastarea=" + currentArea;
                  
        // Execute the request
        MakeHttpRequest(url, "cL");
    }
}

// rates a single motive @ a gallery
function rateMotive(responseText, ratingValue, motiveId)
{
    // get the step, or display it?
    if(responseText == null) {
        
        // build the url
        var url  = "./?do=shop&area=rpc&action=ratemotive&id=" + motiveId + "&value=" + ratingValue;
        
        // execute the httpRequest
        MakeHttpRequest(url, "rateMotive", ratingValue);
    }
    else {
        // calculate the rating
        var ratingWidth = ratingValue * 25;
               
        // assign the width
        document.getElementById("current-rating").style.width = ratingWidth+'px';
        
        // assign a text
        document.getElementById("rate-links").style.display = 'none';
        
        // change the text
        document.getElementById("rating-text").innerHTML = "Danke f&uuml;r deine Bewertung!";
    }
}

// Shows the pickup help 
function openPickupHelp() {
    openWindow("index.php?do=staticpage&area=pickuphelp&use_empty=1", 800, 800);
}

// Shows the exit survey
function openExitSurvey() {
    // Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
            selects[i].style.visibility = "hidden";
    }
     
    // show the div   
    document.getElementById("ExitSurvey").style.display="block";
}

// closes the exit survey
function closeExitSurvey(responseText) {
    // get the step, or display it?
    if(responseText == null) {
        // hide the div
        document.getElementById("ExitSurvey").style.display="none";
        
        // make select boxes visible
    	selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
    		selects[i].style.visibility = "visible";
    	}
    	
    	// disabled the exit survey for this session
        var url  = "./?disable_exit_survey=1&use_empty=1";
        
        // execute the httpRequest
        MakeHttpRequest(url, "closeExitSurvey");
    }
    else {
        // everything ok - hide the div
        document.getElementById("ExitSurveyOpener").style.display="none";
    }
}

// submits the exit survey
function submitExitSurvey(responseText, closeWindow) {
    // get the step, or display it?
    if(responseText == null) {
        // get the comment
        var comment = document.getElementById("survey_comment").value;
        
        // get the options
        if(document.getElementById("option_1").checked) {
            var checkedOption = 1;
        }
        else if(document.getElementById("option_2").checked) {
            var checkedOption = 2;
        }
        else if(document.getElementById("option_3").checked) {
            var checkedOption = 3;
        }
        else if(document.getElementById("option_4").checked) {
            var checkedOption = 4;
        }
        else {
            var checkedOption = 0;
        }
        
        // no comment and checked option?
        if(checkedOption == 0 && comment.length == 0) {
            return;
        }
        
        // build the url
        var url  = "./?do=shop&area=rpc&action=updateexitsurvey&use_empty=1&result=" + escape(checkedOption) + "&comment=" +
                   escape(comment);
                   
        // execute the httpRequest
        MakeHttpRequest(url, "submitExitSurvey", closeWindow);
        
        // close the window?
        if(closeWindow) {
            closeExitSurvey();
        }
    }
}

// displays the Showroom
function showShowroom(one_pic_flag,one_pic_url,format_id,effect_id,cut) {
	var arrayPageSize = getPageSize(); //Funktion von Lightbox
	var arrayPageScroll = getPageScroll(); //Funktion von Lightbox
	var objOverlay = document.getElementById('overlay');
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	
	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}

	objOverlay.style.display = 'block';
    document.getElementById("loadingImage").style.display = "none";
	
	var so = new SWFObject("./../showroom/showroom.swf", "mymovie", "800", "500", "8", "#336699");
	//so.addParam("wmode", "transparent");
	
	if (one_pic_flag == true) {
		so.addParam("FlashVars","shop_id=maxposter&showroom_dir=../showroom/&sofa_id=couch_weiss&one_pic_flag=" + one_pic_flag + "&one_pic_url=" + one_pic_url + "&format_id=" + format_id + "&effect_id=" + effect_id + "&cut=" + cut);
	} else {
		so.addParam("FlashVars","shop_id=maxposter&showroom_dir=../showroom/&sofa_id=couch_weiss");
	}
	so.write("flashcontent");

	document.getElementById('swflightbox').style.display = "block";
}
// hides the Showroom
function hideShowroom(id) {
	// disableStartShowroom();
	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
	var objOverlay = document.getElementById('overlay');
    document.getElementById('swflightbox').style.display = "none";	
	objOverlay.style.display = 'none';
}
function disableStartShowroom(responseText) {
    if(responseText == null) {
		// disabled the start showroom for this session
        var url  = "./?disable_start_showroom=1&use_empty=1";
        
        // execute the httpRequest
        MakeHttpRequest(url, "disableStartShowroom");
    }
    else {
        // everything ok
    }
}