
//
// shop
// mattbestallningar
//

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


function eraseCookie(name) {
    createCookie(name,"",-1);
}


function cookie_set( str, sname, sval ) {
    tmp = str.split(';');
    for ( i = 0; i < tmp.length; i++ ) {
        if ( tmp[i].indexOf(sname) != -1 ) {
           tmp[i] = sname+'='+sval;
           break;
        }
    }
    if ( i == tmp.length ) {
       tmp.push( sname+'='+sval );
    }
    tmp = tmp.join( ';' );
    return tmp;
}



function cookie_get( cookie_name, sname ) {
    tmp = readCookie( cookie_name );
    tmp = unescape( tmp );
    tmp = tmp.split(';');
    for ( i = 0; i < tmp.length; i++ ) {
        if ( tmp[i].indexOf(sname)!=-1 ) {
           return (tmp[i].split('='))[1];
        }
    }
    return '';
}


function setCookie( strCook, sname, sval ) {
    cake = readCookie(strCook);
    ucake = unescape( cake );
    ucake = cookie_set( ucake, sname, sval );
    cake = escape(ucake);
    createCookie( strCook, cake, '' );
}


function set_fields_cookie(cookie_name) {
    for ( ii = 0; ii < document.forms[0].length; ii++ ) {
        tmp2 = document.forms[0].elements[ii];
        if ( "*checkbox*radio*".indexOf( tmp2.type.toLowerCase() ) != -1 ) {
            tmp3 = cookie_get(cookie_name, tmp2.name );
            if ( tmp3 != null && tmp2.value == tmp3 ) {
                tmp2.checked=true;
            }
         } else if ( "*button*reset*submit*".indexOf( tmp2.type.toLowerCase() ) == -1 ) {
            tmp3 = cookie_get( cookie_name, tmp2.name );
            tmp2.value = tmp3;
        } else {
          // alert('hej');
        }
    }
}


function set_load( cookie_name ) {
    if ( cookie_get(cookie_name, 'loaded' ) != "true" ) {
        return;
    }
    set_fields_cookie(cookie_name);
}


function add_fields_to_cookie(cookie_name) {
    tmp = readCookie(cookie_name);
    tmp = unescape( tmp );
    for ( ii = 0; ii < document.forms[0].length; ii++ ) {
        tmp2 = document.forms[0].elements[ii];
        if ( "*checkbox*radio*".indexOf( tmp2.type.toLowerCase() ) != -1 ) {
            if ( tmp2.checked == true ) {
                cookie_set( tmp, tmp2.name, tmp2.value );
            }
        } else if ( "*hidden*button*reset*submit*".indexOf( tmp2.type.toLowerCase() ) == -1 ) {
            cookie_set( tmp, tmp2.name, tmp2.value );

        } else {}
    }
    tmp = escape( tmp );
    createCookie(cookie_name, tmp, '');
}


function add_fields_to_cookie_and_go_to_(adress) {
    add_fields_to_cookie('monard_modell');
    location=adress;
}

function go_to( next_loc ) {
    if ( cookie_get('monard_modell', 'loaded' )!='true' ) {
        setCookie('monard_modell', 'loaded','true');
    }
    add_fields_to_cookie('monard_modell');
    location=next_loc;
    return false;
}


function email_check() {
    if ( cookie_get( 'monard_modell', 'Email' ) == '' ) {
        location='index.php';
    }
    return true;
}

function returnTo( prev_loc ) {
    add_fields_to_cookie('monard_modell');
    location = prev_loc;
    return false;
}


function save_session(cookie_name) {
    add_fields_to_cookie(cookie_name);
    document.location='save_page.php';
}


function load_session(cookie_name) {
    document.location = 'load_page.php?cookie_name='+cookie_name;
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}




