/*
*************************************
  Created by Laszlo Bejczi 2006
  Send me a mail if u could use this code. Anyway its free to reuse.
  fikusz01 at gmail dot com
*************************************
*/

var b64str   = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+"abcdefghijklmnopqrstuvwxyz"+"0123456789+/=";
var b64str_m = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+"abcdefghijklmnopqrstuvwxyz"+"0123456789-_=";
// - lett a + helyett és _ a / helyett

function base64enc(inp)
{

 m = 2;

 if (inp=="") return("");

 var b64ch=(m==1)?b64str:b64str_m;

 /*
 for (i=0;i<inp.length;i++)
 {
   //if (inp.charCodeAt(i)>255) alert("i:"+i+" code:"+inp.charCodeAt(i)+" ch:"+inp.charAt(i));
   if (inp.charCodeAt(i)>255) alert("i:"+i+" code:"+inp.charCodeAt(i)+" code:"+inp.charCodeAt(i+1));
   //alert(inp.charCodeAt(i)+inp.charAt(i));
 }
 */

 if (typeof(btoa)=="function")
  {
    alap=btoa(inp);
    if (m!=1)
    {
     alap=alap.replace(/\x2B/g,"-");
     alap=alap.replace(/\x2F/g,"_");
    }
    return alap;
  }

    var out = ""; //This is the output
    var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
    var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
    var i = 0; //Position counter

    do
    { //Set up the loop here
        chr1 = inp.charCodeAt(i++); //Grab the first byte
        chr2 = inp.charCodeAt(i++); //Grab the second byte
        chr3 = inp.charCodeAt(i++); //Grab the third byte

        //Here is the actual base64 encode part.
        //There really is only one way to do it.
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2))
        {
          enc3 = enc4 = 64;
        }
         else if (isNaN(chr3))
         {
            enc4 = 64;
         }

        //Lets spit out the 4 encoded bytes
        out = out + b64ch.charAt(enc1) + b64ch.charAt(enc2) + b64ch.charAt(enc3) + b64ch.charAt(enc4);

        // OK, now clean out the variables used.
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

    } while (i < inp.length); //And finish off the loop

    //Now return the encoded values.
    return out;
}

function is_numeric(s,mode) {
    var numeric = false;
    switch ( mode ) {
        case 'float':
            if ( s == parseFloat(s) ) numeric = true;
            break;
        case 'integer':
        default:
            if ( s == parseInt(s) ) numeric = true;
            break;
    }
    return numeric;
}

function ajax_search(id,kifejezes) {
    if ( kifejezes.length >= 3 ) {
        ajax_content(id,__prefix__+'/ajax/termek-kereso/'+base64enc(escape(kifejezes)));
    } else {
        $('#'+id+'_msg').html('Kérem adjon meg legalább 3 karaktert a keresés indításához ...');
    }
}

function ajax_search_with_0_char(id,kifejezes) {
    ajax_content(id,__prefix__+'/ajax/termek-gyorskereso/'+base64enc(escape(kifejezes)));
}

function ajax_kosarba_rak(id,tid,db) {
    if ( db == '' ) {
        var http_url = __prefix__+'/ajax/kosarba-rak/'+tid+'/0';
        $.ajax({url: http_url, success: function(){
        }});
    } else {
        if ( is_numeric(tid,'integer') ) {
            if ( is_numeric(db,'integer') ) {
                var http_url = __prefix__+'/ajax/kosarba-rak/'+tid+'/'+db;
                $.ajax({url: http_url, success: function(){
                }});
            } else {
                $('#'+id).val('');
                alert('A megadott darabszám nem szám!');
            }
        } else alert('Nincs ilyen termék!');
    }
}

function ajax_content(id,url) {
    $('#'+id+'_msg').html('Keresés ...');
    $('#'+id).load(url,'',function(){
        $('#'+id+'_msg').html('');
        $('#'+id).fadeIn({duration:500});
    });
    return false;
}

function edit_mezo_fokuszba(name) {
    $('#'+name).focus();
}

function showInClearbox(url,title,w,h) {
    CB_Open('href='+url+',,title='+title+',,width='+w+',,height='+h);
}

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=/";
}

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 bookmarksite(title, url){
    if (document.all) window.external.AddFavorite(url, title);
    else if (window.sidebar) window.sidebar.addPanel(title, url, "");
}

function go(url) {
    window.location = url;
}

function Confirm(msg, hova) {
    var response = window.confirm(msg);
    if (response) { go(hova); }
}

function ConfirmOnly(msg) {
    return window.confirm(msg);
}

