var ie = document.all?1:0;
var ns4 = document.layers?1:0;
var wwwhost;
var reghost;

if (wwwhost == undefined) {
  wwwhost = 'http://music.allofmp3.com/';
}

if (reghost == undefined) {
  reghost = 'http://account.allofmp3.com/';
}

var stathost = wwwhost;
var cathost  = wwwhost;
var myhost   = wwwhost;
var helphost    = wwwhost;
var previewhost = wwwhost;
var smshost  = wwwhost;

var linkhost = reghost;
var serhost  = reghost;
var reghost  = reghost;

function golink(action,h_value){ document.form1.action=action; document.form1.queryrepost.value=h_value; document.form1.submit();}

function rnd() {
    var ddd = "rnd="+Math.round(Math.random() * 1000000);
    return ddd;
}

function popup2(param1)                      { go_popup(stathost+'/order.shtml?id='+param1,'480','535','mp3_order','no') }
function popup_preview(param1,param2,param3,albref) { go_popup(previewhost+'/preview.shtml?group='+param1+'&album='+param2+'&song='+param3+'&albref=' + (albref == null ? '' : albref) + '&'+rnd(),'760','455','mp3_prev','yes');}
function popup_preview_album(group,album,lang) { go_popup(previewhost+'/share.shtml?group='+group+'&album='+album+'&lang='+lang + '&prevref=3','490','570','mp3_album_prev','yes');}
function popup_shareit(group,album,song,albref) { go_popup(previewhost+'/shareit_popup.shtml?group='+group+'&album='+album+(song==null?'':'&song='+song)+'&albref=' + (albref == null ? '' : albref) + '&'+rnd(),'760','455','mp3_shareit','yes');}
function popup_smsorder(param1,param2,param3) { go_popup(previewhost+'/sms/order.shtml?group='+param1+'&album='+param2+'&song='+param3+'&'+rnd(),'760','720','mp3_sms','yes');}
function popup_links()                       { go_popup(reghost +'/mylinks.shtml?'+rnd(),'640','400','mp3_mylinks','yes');}
function popup_legend(param1)                { go_popup(helphost+'/legend_nv.shtml?v='+param1,'760','410','mp3_legend','no');}
function popup_pass(param1)                  { go_popup(reghost +'/reg/pass.shtml?'+param1+'=on','400','270','mp3_pass','no');}
function popup_help(prm)                     { go_popup(helphost+'/help/help.shtml?prm='+prm+'&'+rnd(),1000,600,'mp3_help','yes');}
function popup_help2(gs, gd)                 { go_popup(helphost+'/help/help.shtml?gs='+gs+'&gd='+gd+'&'+rnd()+'&popup=yes',1000,600,'mp3_help','yes'); return false;}
function popup_tour(width,height)            { go_popup(helphost+'/tour/index.shtml',width,height,'tour','yes');}
function popup_SMSorder(param1,param2)       { go_popup(smshost +'/sms/smsorder.shtml?group='+param1+'&album='+param2+'&'+rnd()+'&action=first','730','460','mp3_smsorder','yes');}

function popup_WLadds(remove) {
    var s= myhost + '/getwait.shtml?';
    s = s+rnd()+'&group='+document.ADD['group'].value+'&album='+document.ADD['album'].value+'&albref='+document.ADD['albref'].value;
    if (remove) {
        s += '&remove=yes';
    }
    document.getElementById('toLoad').src = s;
}

function checkbuttons(CB) {
    var iname;
    var s = true;
    var st = 'big disabled';
    for (var i=0; i < document.getElementsByTagName("input").length; i++) {
        iname = document.getElementsByTagName("input")[i].name;
        if(iname) {
            if(!iname.indexOf("add_") && document.getElementsByTagName("input")[i].checked ) {
                s = false;
                st = 'big enabled';
                break;
            }
        }
    }
    document.ADD['submit'].disabled = s;
    document.ADD['submit'].className = st;
}

function changeGetItState() {
    var s = document.ADD['all'].checked;
    for(i = 0; i < 200; i++) {
        if(document.ADD[i].name == 'submit' || document.ADD[i].name == 'all') break;
        if(document.ADD[i].type == 'checkbox') {
            document.ADD[i].checked = s;
            CCB(document.ADD[i],'selected');
        }
    }
    checkbuttons();
}

function set_bookmark() {
    //var s= myhost+'/setbookmark.shtml?set=on';
    var s= '/setbookmark.shtml?set=on';
    if(BookPresent != 'on' && IsAuth != 'off') {
        s = s+'&group='+document.BMark['group'].value+'&add=on';
        document.BMark['submit'].value = (CLang=='ENG'?'Delete from CheckList':'Удалить из Списка обновлений');
        BookPresent = 'on';
    } else {
        s = s+'&group='+document.BMark['group'].value+'&del=on';
        document.BMark['submit'].value = (CLang=='ENG'?'Add to Checklist':'Добавить в Список обновлений');
        BookPresent = 'off';
    }
    document.getElementById('toLoad').src = s;
}
function set_bookmark2(q) {
    //var s= myhost+'/setbookmark2.shtml?'+q;
    var s= '/setbookmark2.shtml?'+q;
    /*if(!in_bookmarks) {
    	s = s + '&set=yes';
    }*/
    /*if(BookPresent != 'on' && IsAuth != 'off') {
        s = s+'&group='+document.BMark['group'].value+'&add=on';
        document.BMark['submit'].value = (CLang=='ENG'?'Delete from CheckList':'Удалить из Закладок');
        BookPresent = 'on';
    } else {
        s = s+'&group='+document.BMark['group'].value+'&del=on';
        document.BMark['submit'].value = (CLang=='ENG'?'add to Checklist':'добавить в Закладки');
        BookPresent = 'off';
    }*/
    document.getElementById('toLoad').src = s;
}

function CCB(CB,trclass) {
    if (CB.checked) {
        hL(CB,trclass);
    } else {
        dL(CB);
    }
}

function hL(E,trclass) {
    if (ie) {
        while (E.tagName!="TR") {
            E=E.parentElement;
        }
    } else {
        while (E.tagName!="TR") {
            E=E.parentNode;
        }
    }
    if(E.className!='active') {
        E.className = trclass;
    }
}

function dL(E) {
    if (ie) {
        while (E.tagName!="TR") {
            E=E.parentElement;
        }
    } else {
        while (E.tagName!="TR") {
            E=E.parentNode;
        }
    }
    if(E.className!='active') {
        E.className = "";
    }
}

function go_popup(path, w, h, wname, scroll) {
    //p='width='+w+',height='+h+',resizable=yes,toolbar=no,location=no,directories=no,status=yes,scrollbars='+scroll+',menubar=no';
    p='width='+w+',height='+h+',resizable=yes,toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,menubar=no';
    xxx=window.open(path,wname,p);
    window.xxx.focus();
}

function SetLinkClass(param,ex) {
    for(var i = 0; i < document.links.length; i++) {
        if(document.links[i].name == param) {
            document.links[i].className = ex;
            break;
        }
    }
}

function caseOver(obj) {
    for(i=0; i<obj.childNodes.length; i++) {
        if((obj.childNodes[i].tagName == "DIV") && (obj.childNodes[i].className == "transparent")) {
            obj.childNodes[i].style.display = "none";
            break;
        }
    }
}
function caseOut(obj) {
    for(i=0; i<obj.childNodes.length; i++) {
        if((obj.childNodes[i].tagName == "DIV") && (obj.childNodes[i].className == "transparent")) {
            obj = obj.childNodes[i].style.display = "block";
            break;
        }
    }
}

function checkSel(obj, clas) {
    for(i=0;i<obj.childNodes.length;i++) {
        if(obj.childNodes[i].className == 'check') {
            for(j=0;j<obj.childNodes[i].childNodes.length;j++) {
                if(obj.childNodes[i].childNodes[j].tagName == 'INPUT') {
                    if(!obj.childNodes[i].childNodes[j].checked) {
                        obj.className = clas;
                    } else {
                        obj.className = "selected";
                    }
                    break;
                }
            }
        }
    }
}

function previewSong(id, group_id, album_id, song_id, albref) {
    //var tr_obj = document.getElementById("tr_"+id);
    var tr_obj = document.getElementById("div_"+id);
    //var td_obj = document.getElementById("td_"+id);
    var td_obj = document.getElementById("div_"+id);
    var tr0_obj = document.getElementById("tr0_"+id);
    if(tr_obj&&td_obj&&tr0_obj) {
        hideAllPreviews(id);
        if(tr_obj.style.display != '') {
            tr_obj.style.display = '';
            //var src = previewhost+'/previewsong.shtml?group='+group_id+'&album='+album_id+'&song='+song_id+'&albref=' + (albref == null ? '' : albref) + '&'+rnd();
            var src = '/previewsong.shtml?group='+group_id+'&album='+album_id+'&song='+song_id+'&albref=' + (albref == null ? '' : albref) + '&'+rnd();
            //getPage(src, 'td_'+id);
            getPage(src, 'div_'+id);
            tr0_obj.className = "active";
        } else {
            tr_obj.style.display = 'none';
            //clearPage('td_'+id);
            clearPage('div_'+id);
            tr0_obj.className = "";
            checkSel(tr0_obj, '');
        }
    }
}

function hideAllPreviews(except) {
    for(i=1; i<200; i++) {
        if(except == i) continue;
        //var obj = document.getElementById("tr_"+i);
        var obj = document.getElementById("div_"+i);
        if(obj) {
            obj.style.display = "none";
            obj.style.visibility = "visible";
            //clearPage('td_'+i);
            clearPage('div_'+i);
        }
        var obj = document.getElementById("tr0_"+i);
        if(obj) if(obj.className != 'selected') obj.className = '';
    }
}

var tip_max_width = 300;
var tip_delay = 500;
var tip_timer = null;
var tip_popup_id;
var tip_text;
var newline_entity = '@n';
var reposx = 4;
var reposy = 4;

function labelShow() {
    if(tip_timer != null) {
        var tip = layer(tip_popup_id);
        tip.show();
        tip.write(tip_text);
        tip.style.width = ((tip_max_width) && (tip.style.width > tip_max_width)) ? tip_max_width + "px" : "auto";
    }
}

function labelM(e, text, obj, popup_id) {
    if(text != "") {
        tip_text = text;
        tip_text = tip_text.replace(eval("/" + newline_entity + "/g"), "<br />");
        if((popup_id == null) || (popup_id == "") || (popup_id == 0)) var popup_id = 'label_tip';
        tip_popup_id = popup_id;
        var tip = layer(popup_id);
        if(tip.exists()) {
            if((!tip.isVisible()) && (tip_timer == null)) {
            	tip.write(tip_text);
                tip_timer = setTimeout("labelShow();", tip_delay);
            }
            var x = e.clientX + document.documentElement.scrollLeft;
            var y = e.clientY + document.documentElement.scrollTop;
            //document.title = getWindowWidth() + ' | ' + x + ' | ' + tip.getWidth();
        	if(isOpera) {
        		var mod = 20;
        	} else if(ie) {
        		var mod = 0;
        	} else {
        		var mod = 20;
        	}
            if((tip.getWidth() + x + reposx+ mod) > getWindowWidth() ) {
            	x = getWindowWidth() - tip.getWidth() - reposx - mod;
            }
            tip.moveTo(x + reposx, y + reposy);
        }
    }
}

function labelSet(popup_id) {
    if((popup_id == null) || (popup_id == "") || (popup_id == 0)) var popup_id = 'label_tip';
    var tip = layer(popup_id);
    if(tip.exists()) {
    	//tip.setWidth(tip_max_width);
        tip.hide();
    }
}

function labelUM(popup_id) {
    clearTimeout(tip_timer);
    tip_timer = null;
    if((popup_id == null) || (popup_id == "") || (popup_id == 0)) var popup_id = 'label_tip';
    var tip = layer(popup_id);
    if(tip.exists()) tip.hide();
}

function clickSeeSample(id) {
    var obj = layer(id);
    if(obj.exists()) {
        var tmp = layer('ac-t');
        obj.appear();
        obj.show();
    }
}

function unclickSeeSample(id) {
    var obj = layer(id);
    if(obj.exists()) {
        obj.hide();
    }
}

function open_guide(url, w, h, flash_w, flash_h, t, header, is_popup, tc) {
	if((is_popup == '')||(is_popup == 'undefined')||(is_popup == null)) { is_popup = 0; }
	if((tc == '0')||(tc == '')||(tc == 'undefined')||(tc == null)) {
		tc = '&TC=1';
	} else {
		tc = '';
	}
	var add_params = '&flash_w=' + flash_w + '&flash_h=' + flash_h + '&t=' + t + '&header=' + header + '&ispopup=' + is_popup + tc;
	if(is_popup == 0) go_popup(stathost + '/guide_popup.shtml?load=' + url + add_params, w, h, 'flash_guide','no');
	if(is_popup == 1) document.location = stathost + '/guide_popup.shtml?load=' + url + add_params;
}
function popup_preview_song(group,album,song,lang) {
	go_popup(previewhost+'/share.shtml?group='+group+'&album='+album+'&song='+song+'&lang='+lang + '&prevref=3','490','570','mp3_album_prev','yes');
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
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 Get_Cookie( 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 Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function set_sourceref (sourceref) {

  Set_Cookie("sourceref", sourceref, 365, '/', cookiehost);

}

