/*
Last update ==> 10.11.09 16:38
*/

/*
Mail Adresse checken
*/
function EMail(s)  {
    var a = false;
    var res = false;
    if(typeof(RegExp) == 'function') {
        var b = new RegExp('abc');
        if(b.test('abc') == true){
            a = true;
        }
    }
    if(a == true) {
        reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
            '(\\@)([a-zA-Z0-9\\-\\.]+)'+
            '(\\.)([a-zA-Z]{2,4})$');
        res = (reg.test(s));
    } else {
        res = (s.search('@') >= 1 &&
            s.lastIndexOf('.') > s.search('@') &&
            s.lastIndexOf('.') >= s.length-5)
    }
    alert(res);
    return(res);
}

/*
Abruf der Daten vom Server
*/
function aktBasket(input,bestnr,preis,id) {
    var qty = 1;
    if (input == 'Remove') {
        if (confirm("Delete item# " + bestnr + " ?")) {
            addBasket('Remove',bestnr,preis,qty,id);
        }
    } else {
        var frm = document.forms['frmBasket'];
        qty = frm.elements[input].value;
        addBasket('Update',bestnr,preis,qty,id);
    }
    addBasket('Refresh',bestnr,preis,qty,id);

}
function addBasket(action) {
    showLoader();
    var bestnr = '';
    var preis = 0;
    var qty = 1;
    var id = 0;
    var test = "todo=" + action ;
    for (var i = 1; i < addBasket.arguments.length; i++) {
        switch (i) {
            case 1:
                bestnr = addBasket.arguments[i];
                test +="&bestnr=" + bestnr  ;
                break;
            case 2:
                preis = addBasket.arguments[i];
                test +="&preis=" + preis  ;
                break;
            case 3:
                qty = addBasket.arguments[i];
                test +="&qty=" + qty;
                break;
            case 4:
                id = addBasket.arguments[i];
                test +="&id=" + id;
                break;
        }
    }
    if (action != "Refresh" ) {
        $.ajax({
            url: '/rpcBasket.php',
            type: 'POST',
            data: {
                todo: action,
                bestnr: bestnr,
                preis: preis,
                qty: qty,
                id: id
            },
            success: function(rdata) {
                hideLoader();
                $('#rpcbasket').html(rdata);
            }
        });             
    } else {
        $.ajax({
            url: '/rpcBasket.php',
            type: 'POST',
            data: {
                todo: action,
                bestnr: bestnr,
                preis: preis,
                qty: qty,
                id: id
            },
            success: function(rdata) {
                hideLoader();
                $('#rpcShowCart').html(rdata);
            }
        });        
    }

}
function setAction(action) {
    showLoader();
    if (action == 'suche') {
//        new Ajax.Request('/rpcSearch.php',{
//            method: 'post',
//            onComplete: hideLoader,
//            onSuccess: searchHandler,
//            parameters: {
//                todo: action
//            }
//        }
//        );
        $.ajax({
            url: '/rpcSearch.php',
            type: 'POST',
            data: {
                todo: action
            },
            success: function(rdata) {
                hideLoader();
                document.getElementById('rowSearch').style.visibility = "visible";
                document.getElementById('rpcSuche').style.height = "30%";
                $('#rpcContent').html('');
                $('#rpcSuche').html(rdata);
            }
        });
    }
	
}


/*
Abruf der Daten vom Server
*/
function fillGrid(action) {
    showLoader();
    var s=0;
    var e=10;
    var p=1;
    var test = "todo=" + action ;
    for (var i = 1; i < fillGrid.arguments.length; i++) {
        switch (i) {
            case 1:
                s = fillGrid.arguments[i];
                test +="&s=" + s;
                break;
            case 2:
                p = fillGrid.arguments[i];
                test +="&p=" + p;
                break;
        }
    }
    $.ajax({
        url: '/rpcDataGrid.php',
        type: 'POST',
        data: {
            todo: action,
            s: s,
            p: p
        },
        success: function(rdata) {
            hideLoader();
            $('#rpcTotalIndex').html(rdata);
        }
    });
//    new Ajax.Request('/rpcDataGrid.php',
//    {
//        method: 'post',
//        onComplete: hideLoader,
//        onSuccess: gridHandler,
//        parameters: {
//            todo: action,
//            s: s,
//            p: p
//        }
//    }
//    );

}

	
function showLoader() {
    var posX = (Fensterweite() / 2) - 90 + 'px';
    var posY = 200 + 'px';
    document.getElementById('loaderDiv').style.top = posY;
    document.getElementById('loaderDiv').style.left = posX;
    document.getElementById('loaderDiv').style.visibility = "visible";
}
	
function hideLoader(){
    document.getElementById('loaderDiv').style.visibility = "hidden";
}

/*
* allgemeine Funktionen
*/
function Fensterweite () {
    if (window.innerWidth) {
        return window.innerWidth;
    } else if (document.body && document.body.offsetWidth) {
        return document.body.offsetWidth;
    } else {
        return 0;
    }
    return 0;
}

function Fensterhoehe () {
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (document.body && document.body.offsetHeight) {
        var y = document.body.offsetHeight;
        //alert ("y=" + y + " - >" + (screen.height));
        if (y < (screen.height - 200)) {
            return  (screen.height - 200);
        } else {
            return y;
        }
    } else {
        return 0;
    }
    return 0;
}

function printBasket() {
    basket = document.getElementById('ShoppingCartList');
    if (basket != '') {
        basket.style.height="100%";
        basket.style.overflow="auto";
        tab = document.getElementById('ShoppingCartTable');
        for (i=0;i < tab.rows.length-1;i++) {
            document.getElementById('imgdelete'+i).style.visibility = "hidden";
        }
        document.getElementById('rowButton1').style.visibility = "hidden";
        document.getElementById('rowButton2').style.visibility = "hidden";

        window.print();
        window.setTimeout("refreshBasket()", 10000)

    }
}
function refreshBasket() {
    basket = document.getElementById('ShoppingCartList');
    if (basket != '') {
        basket.style.height="120px";
        document.getElementById('rowButton1').style.visibility = "visible";
        document.getElementById('rowButton2').style.visibility = "visible";
        for (i=0;i < tab.rows.length-1;i++) {
            document.getElementById('imgdelete'+i).style.visibility = "visible";
        }

    }
}
function trim(str) {
    str = this != window? this : str;
    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
	
}
var isFrontorBack = false;
function changeCover(id) {
    var img = document.getElementById('showCover');
    if (!isFrontorBack) {
        img.src = "generateSmallFrontcover.php?ID=" + id + "&Size=back";
        isFrontorBack = true;
    } else {
        img.src = "generateSmallFrontcover.php?ID=" + id + "&Size=front";
        isFrontorBack = false;
    }

}
function Go (select, id) {
    var wert = select.options[select.options.selectedIndex].value;
    //alert(select.name  + " value = " + wert);
    if (wert != 'leer') {
        switch (select.name) {
            case "SubC":
                //location.href="/" + id + "/" + select.name + "/catalog/" + wert + ".html";
                location.href="/index.php?shopid=" + id + "&action=catalog&cat=" + wert + "&node=" + select.name;
                break;
            case "SubB":
                //location.href="/" + id + "/" + select.name + "/catalog/" + wert + ".html";
                location.href="/index.php?shopid=" + id + "&action=catalog&med=" + wert + "&node=" + select.name;
                break;
            case "SubD":
                //location.href="/" + id + "/" + select.name + "/sextoys/" + wert + ".html";
                location.href="/index.php?shopid=" + id + "&action=sextoys&grp=" + wert + "&node=" + select.name;
                break;
            case "SubE":
                //location.href="/" + id + "/" + select.name + "/magazin/" + wert + ".html";
                location.href="/index.php?shopid=" + id + "&action=magazin&grp=" + wert + "&node=" + select.name;
                break;
            case "SubI":
                //location.href="/" + id + "/" + select.name + "/sonstige/" + wert + ".html";
                location.href="/index.php?shopid=" + id + "&action=sonstige&grp=" + wert + "&node=" + select.name;
                break;
            case "SubP":
                //location.href="/" + id + "/" + select.name + "/sonstige/" + wert + ".html";
                location.href="/index.php?shopid=" + id + "&action=toys&grp=" + wert + "&node=" + select.name;
                break;                
        }
    }
}
function setSelected(liste,wert) {
    if (liste == "") {
        return;
    }
    var frm = document.getElementById(liste);
    //alert(frm.name +", "+  liste + ",  " +wert);
    for (i=0;frm.options.length;i++) {
        if (frm.options[i].value == wert) {
            frm.options[i].selected = true;
            break;
        }
    }
}
function Search(id) {
    var frm = document.forms['frmSearch'];
    if (frm.txtsearch.value.length > 0) {
        location.href="index.php?shopid=" + id +"&action=catalog&search=" + escape(frm.txtsearch.value);
    }
    return true;
}

function changePayment() {
    var frm = document.getElementById('drpLand');
    var country = frm.options[frm.options.selectedIndex].value;
    //alert (frm.options[frm.options.selectedIndex].value);
    var activ = true;
    switch (country) {
        case "Deutschland":
        case "Österreich":
            activ=false;
            break;
    }
    document.getElementById('rbNN').disabled = activ;

}
function changeConfirmMail() {
    var frm = document.getElementById('checkout');
    var ismail = frm.chkConfirmMail.checked;
    frm.txtEmail.disabled = !frm.chkConfirmMail.checked;
}


function CheckOut(theForm) {
    if (theForm.txtVorname.value == "") {
        alert("Firstname expected.");
        theForm.txtVorname.focus();
        return (false);
    }
    if (theForm.txtVorname.value.length < 3) {
        alert("Values too short. At least 3 characters expected.");
        theForm.txtVorname.focus();
        return (false);
    }
    if (theForm.txtNachname.value == "") {
        alert("Surname expected.");
        theForm.txtNachname.focus();
        return (false);
    }
    if (theForm.txtNachname.value.length < 3) {
        alert("Values too short. At least 3 characters expected.");
        theForm.txtNachname.focus();
        return (false);
    }
    if (theForm.txtStrasse.value == "") {
        alert("Street expected.");
        theForm.txtStrasse.focus();
        return (false);
    }
    if (theForm.txtStrasse.value.length < 3) {
        alert("Values too short. At least 3 characters expected.");
        theForm.txtStrasse.focus();
        return (false);
    }
    if (theForm.txtPLZ.value == "") {
        alert("Zip-Code expected.");
        theForm.txtPLZ.focus();
        return (false);
    }
    if (theForm.txtPLZ.value.length < 3) {
        alert("Values too short. At least 3 characters expected.");
        theForm.txtPLZ.focus();
        return (false);
    }
    if (theForm.txtOrt.value == "") {
        alert("City expected.");
        theForm.txtOrt.focus();
        return (false);
    }
    if (theForm.txtOrt.value.length < 3) {
        alert("Values too short. At least 3 characters expected.");
        theForm.txtOrt.focus();
        return (false);
    }
    if (isNaN(theForm.chkConfirmMail)) {
        if (theForm.chkConfirmMail.checked) {
            if (!Email(theForm.txtEmail.value)) {
                alert("Wrong Mail-Address.");
                theForm.txtEmail.focus();
                return (false);
            }
        }
    }
    return (true);
}
function CheckSummary(theForm) {
    if (!theForm.chkAGB.checked) {
        alert("Please read the Terms and Conditons.");
        theForm.chkAGB.focus();
        return (false);
    }
    return (true);
}

function closeAdult(url) {
    //alert(url);
    $.GB_hide();
}
