var numArr = new Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

function voteForBusiness(bid, vote) {
//document.getElementById('voteDone'+bid).style.display = 'inline';
sendVoteRequest(bid, vote);
}

function sendVoteRequest(id, vote){
    var req734 = null;
    if (window.XMLHttpRequest) {
        req734 = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req734 = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var url = "/company/vote/" + id + "/" + vote;
    req734.open("GET", url, true);
    req734.onreadystatechange = callback_sendvote( req734 );
    req734.send(null);
}

function callback_sendvote(req) {
    return function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                // do nothing :)
            }
        }
    }
}

function sendCalendarRequest(url){
    var req1 = null;
    if (window.XMLHttpRequest) {
        req1 = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req1 = new ActiveXObject("Microsoft.XMLHTTP");
    }

    req1.open("GET", url+'/0', true);
    req1.onreadystatechange = callback_sendcalendar( req1 );
    req1.send(null);
}

function callback_sendcalendar(req) {
    return function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                document.getElementById('datepicker').innerHTML = req.responseText;
            }
        }
    }
}

function submitLogin(){
    var req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var url = "/login";
    req.open("GET", url, true);
    req.onreadystatechange = callback_login( req );
    req.send(null);
}

function callback_login(req) {
    return function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                // do nothing :)
            }
        }
    }
}

function showdiv(id) {
    if (document.getElementById) {
        document.getElementById(id).style.display = 'block';
    }
}

function hidediv(id) {
    if (document.getElementById) {
        document.getElementById(id).style.display = 'none';
    }
}

function showspan(id) {
  if (document.getElementById) {
    document.getElementById(id).style.display = 'inline';
  }
} 

function hidespan(id) {
  if (document.getElementById) {
    document.getElementById(id).style.display = 'none';
  }
}

function SetCookie(name,value,expires,path,domain,secure){
    var today=new Date();
    today.setTime(today.getTime());
    if(expires) expires=expires*1000*60*60*24;
    var expires_date = new Date(today.getTime()+(expires));
    document.cookie=name+"="+escape(value)+((expires)?";expires="+expires_date.toGMTString():"")+((path)?";path="+path:"")+((domain)?";domain="+domain:"")+((secure)?";secure":"");
}
function GetCookie(name) {
    var start=document.cookie.indexOf(name+"=");
    var len=start+name.length+1;
    if((!start)&&(name!=document.cookie.substring(0,name.length))) return null;
    if(start==-1) return null;
    var end=document.cookie.indexOf(";",len);
    if(end==-1) end=document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}
function DeleteCookie(name,path,domain){
    var exp=new Date();
    exp.setTime(exp.getTime()-1);
    if(GetCookie(name)) document.cookie=name+"="+((path)?";path="+path:"")+
        ((domain)?";domain="+domain:"" )+";expires="+exp.toGMTString();
}
function checkCoockie(domain){
    if (document.cookie.length) return 1;
    else return 0;
}

function start_chat(receiver_id, sender_id){
    html = '<a style="color:red;" href="/company/chat/'+sender_id+'/'+receiver_id+'?TB_iframe=true&amp;height=490&amp;width=497&amp;modal=true" class="thickbox" title="Start Chat">Start chat</a>';
    document.getElementById('chat'+receiver_id).innerHTML = html;
}

function calcHeight() {
    //alert(document.getElementById('TB_iframeContent'));
  var the_height=document.getElementById('TB_iframeContent').contentWindow.document.body.scrollHeight;
  document.getElementById('TB_iframeContent').height=the_height;
}

function utf8_encode ( argString ) {
    var string = (argString+''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");

    var utftext = "";
    var start, end;
    var stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
}

function base64_encode (data) {

    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];

    if (!data) {
        return data;
    }

    data = this.utf8_encode(data+'');

    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;

        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
       tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);

    enc = tmp_arr.join('');

    switch (data.length % 3) {
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
}

function addToFavorites(url, title) {
    if (document.all)  // ie
        window.external.AddFavorite(url, title);
    else if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
}

function getSelLetter(letter) {
    //for(i = 0; i <= 27; i++){
    //    document.getElementById('l'+i).className = "";
    //}
    //document.getElementById('l'+letter).className = "lsel";
    loadXml(letter);
}

function getDirsCont() {
    letter =window.location.hash.substring(1);
    if(letter != '' && inArray(numArr, letter)){
        //document.getElementById('l'+letter).className = "lsel";
        loadXml(letter);
    }
}

function loadXml(param){
    //hidediv('dirsCont');
    showdiv('dirsContXml');
    //document.getElementById('dirsContXml').innerHTML = '<div class="loading"><img src="/resources/im/layout/loadingAnimation.gif" width="32" height="32" border="0"/><br/><br/>Loading ...</div>';
    var req89 = null;
    if (window.XMLHttpRequest) {
       req89 = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
       req89 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var url = "/category/xml/" + param;
    req89.open("GET", url, true);
    req89.onreadystatechange = callback( req89 );
    req89.send(null);
}
function callback( req ){
    return function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                html = '';
                dirshtml = '';
                name = '';
                short_name = '';
                path = '';
                letterCnt = 0;
                if(req.responseXML != null){
                    var xmlLength = req.responseXML.getElementsByTagName("name").length;
                    html += '<div class="top pngfix"></div><div class="content pngfix"><a href="#" onclick="hidediv(\'dirsContXml\')" class="close_cat"><span class="hidden">close</span></a>';
                    html += '<div class="dirsCat">';
                    for(var i = 0; i < xmlLength; i++){
                        name = req.responseXML.getElementsByTagName("name")[i].firstChild.nodeValue;
                        short_name = (req.responseXML.getElementsByTagName("short_name")[i].firstChild != null) ?
                        req.responseXML.getElementsByTagName("short_name")[i].firstChild.nodeValue : '';
                        path = (req.responseXML.getElementsByTagName("path")[i].firstChild != null) ?
                        req.responseXML.getElementsByTagName("path")[i].firstChild.nodeValue : '';
                        if(i <= 50){
                            if(short_name != '' && path != ''){
                                html += '<a href="/category/show/' + path + '"';
                                html += ' title="' + name + '" alt="' + name + '">';
                                html += short_name + '</a><br/>';
                            }else{
                                html += '<div class="letter">' + name + '</div>';
                                letterCnt++;
                            }
                        }
                        if(i == 16)
                            html += '</div><div class="dirsCat">';
                        if(i == 33)
                            html += '</div><div class="dirsCat last">';
                    }
                    html += '</div>';
                    html += '<div class="clear"></div></div><div class="btm pngfix"></div>';
                }
                document.getElementById('dirsContXml').innerHTML = html;
            }
        }
    }
}
