var onload_functions = new Array();
var onunload_functions = new Array();
var onscroll_functions = new Array();
window.onload = function() { for (i = 0; i <= onload_functions.length-1; i++) eval(onload_functions[i]); }
window.onunload = function() { for (i = 0; i <= onunload_functions.length-1; i++) eval(onunload_functions[i]); }
window.onscroll = function() { for (i = 0; i <= onscroll_functions.length-1; i++) eval(onscroll_functions[i]); }
var base = "";
var to_call = null;
var top_products_delay 		= 6500;
var top_products_stop		= false;
var top_products_pause		= false;
var top_products_index 		= 0;
var top_products_max_index	= 0;
var group_index				= '';
var baseTag = document.getElementsByTagName("base");
for (var i=0; i<baseTag.length; i++)  base = baseTag[i].href;


function setOpacity(obj, opacity) {
    opacity = (opacity == 100)?99.999:opacity;
    // IE/Win
    obj.style.filter = "alpha(opacity:"+opacity+")";
     // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = opacity/100;
    // Older Mozilla and Firefox
    obj.style.MozOpacity = opacity/100;
    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity,step) {
    if (document.getElementById) {
		if (isNaN(step)) 		step = 10;		
        obj = document.getElementById(objId);
        if (opacity <= 100) {
            setOpacity(obj, opacity);
            opacity += step;
            window.setTimeout("fadeIn('"+objId+"',"+opacity+","+step+")", 25);
        }
    }
}

function fadeOut(objId,opacity,opacity_min,hide,step,call) {	
    if (document.getElementById) {
		if (isNaN(opacity_min)) opacity_min = 50;
		if (isNaN(hide)) 		hide = false;
		if (isNaN(step)) 		step = 10;
        obj = document.getElementById(objId);
		if (isNaN(call) && opacity == 0) {
			eval(call);
		}
        if (opacity >= opacity_min) {
            setOpacity(obj, opacity);
            opacity -= step;
            window.setTimeout("fadeOut('"+objId+"',"+opacity+","+opacity_min+","+hide+","+step+",'"+call+"')", 25);
        } else {
			if (hide == true) {
				obj = document.getElementById(objId);
		        setOpacity(obj, 0);
				obj.style.display = 'none';
			}
		}
    }
}

function validateEmail(s) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(s)) return true;
    else return false;
/*    
	var patn = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/;
    var patn = ^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if(patn.test(s)) return true;
	else return false;
    */
}

function addToBasket(id) {
    var qty = 1;
    var qty_obj = document.getElementById("qty_" + id);
    var size_obj = document.getElementById("combo_" + id);
    if (size_obj) id=parseInt(size_obj.value);
    if (!qty_obj) return false;
    qty = qty_obj.value;
    if (isNaN(parseInt(qty))) {
        alert (ln_error_number);
        qty_obj.value = 1;
        return false;
    } else if (qty < 1) {
        alert (ln_error_bigger_1);
        qty_obj.value = 1;
        return false;
    } else if (qty  > 999) {
        qty_obj.value = 1;
        alert (ln_error_smoler_1000);
        return false;
    }
    var asqwe12 = new AJAX(true);
    var url = "basket.php?action=add&id=" + id + "&qty=" + qty;
    asqwe12.callPage(base + "http/" + url, addToBasketR);
}

function addToBasketR(rPage, rAction, rText) {
    if (rAction == "ok" || rAction == "error")  alert(rText);
    else if (rAction == "go_to_basket") document.location = base + "basket.php";
    setTimeout("getBasketInfo()", 10);
}


function getBasketInfo() {
    var asq4we12 = new AJAX(true);
    var url = "basket.php?action=get_basket_info";
    asq4we12.callPage(base + "http/" + url, getBasketInfoR);
}
function getBasketInfoR(rPage, rAction, rText) {
    var d = document.getElementById("basket_info");
    if (d && rText) {
        d.innerHTML = rText;
    }
}

var images = Array();
var images_over = Array();

function over_pics() {
	if (!document.images) return false;
  	var images_src = new Array(THEME_DIR + "gfx/basket.gif", THEME_DIR + "gfx/details.gif");
    var images_src_over = new Array(THEME_DIR + "gfx/basket_over.gif", THEME_DIR + "gfx/details_over.gif");
    for (i = 0; i < images_src.length; i++) {
        var img = new Image;
        img.src = images_src[i];
        images.push(img);
        var img = new Image;
        img.src = images_src_over[i];
        images_over.push(img);
    }

  	for( i = 0; i < document.images.length; i++) {
        var img = document.images[i];
        for (j = 0; j < images_src.length; j++) {
            var a = img.src.substr(img.src.length-images_src[j].length, images_src[j].length);
            if (a == images_src[j]) {
                document.images[i].onmouseover = function() {
                    for (j = 0; j < images_src.length; j++) {
                        var a = this.src.substr(this.src.length-images_src[j].length, images_src[j].length);
                        if (a == images_src[j]) this.src=images_src_over[j];
                    }
                };

                document.images[i].onmouseout = function() {
                    for (j = 0; j < images_src.length; j++) {
                        var a = this.src.substr(this.src.length-images_src_over[j].length, images_src_over[j].length);
                        if (a == images_src_over[j]) this.src=images_src[j];
                    }
                };
            }

        }
	}
	var plink = document.getElementById("plink");
	if (plink) {
		plink.onclick = function () {
			show_hide("pub_info",plink);	
		}	
	}    
}

function show_hide(id) {
	var obj = document.getElementById(id);
	if (obj) {
		if (obj.style.display == "none" || obj.style.display == "") {
			obj.style.display = "block"
		} else {
			obj.style.display = "none";			
		}
	}
}
	
function check_value(id) {
	var obj = document.getElementById(id);
	if (obj) {
        if (obj.value.trim()) {
            return true; 
        } else { 
            alert (ln_error_reason);
            return false;
        }
    } else {
        return false;
    }
}        

function load_top_products() {
    var asqwe13 = new AJAX(true);
    var url = "top_products.php?";
    asqwe13.callPage(base + "http/" + url, load_top_products_ret);
}

function load_top_products_ret(rPage, rAction, rText) {
	var obj = document.getElementById("scrollpanel");
	if (obj) {
		obj.className = "";		
		obj.innerHTML = rText;
		fadeIn("scrollpanel_cont_"+rPage, 0, 5);
		top_products_index = parseInt(rPage);
		top_products_max_index = parseInt(rAction);
	    setTimeout("change_top_products()", top_products_delay);
	}
}

function change_top_products() {
	if (!top_products_stop && !top_products_pause) {
		var next = top_products_index + 1;
		if (next > top_products_max_index) next = 0;		
		fadeOut("scrollpanel_cont_"+top_products_index, 100, 1, true, 5, 'document.getElementById("scrollpanel_cont_'+next+'").style.display="block"; fadeIn("scrollpanel_cont_'+next+'", 0, 5)');
		to_call = setTimeout("change_top_products()", top_products_delay);
		++top_products_index;
		if (top_products_index > top_products_max_index) top_products_index = 0;		
	} else {
		clearTimeout(to_call);
	}
}

function show_hide_tp(open_text,close_text) {
	clearTimeout(to_call);
	var obj = document.getElementById('scrollpanel_expand');
	if (top_products_stop == false) {
		top_products_stop = true;
		var display 	= 'block';
		var position 	= 'relative';
		var opacity 	= 100;
		obj.innerHTML	= close_text;
	} else {
		top_products_stop = false;
		var display 	= 'none';
		var position 	= 'absolute';
		var opacity 	= 0;	
		load_top_products();
		obj.innerHTML	= open_text;		
	}
	childs = document.getElementById("scrollpanel").childNodes.length;
	for(x=0;x<childs;x++) {
		obj = document.getElementById("scrollpanel_cont_"+x);
		obj.style.display = display;
		obj.style.position = position;
		setOpacity(obj, opacity);
	}
}

function expand(id,direction,min_height) {
	var obj = document.getElementById(id);
	if (obj) {
		var height = parseInt(obj.style.height);
		var max_height = parseInt(obj.firstChild.offsetHeight)+5;
		if (direction != "down") {
		 	window.setTimeout("slideUp('"+id+"',"+height+","+parseInt(min_height)+")", 50);		
		} else {
			if (id != group_index) {
			 	window.setTimeout("slideDown('"+id+"',"+height+","+max_height+")", 50);	
				obj = document.getElementById(group_index);
				if (obj) {
					var height = parseInt(obj.style.height);			
			 		window.setTimeout("slideUp('"+group_index+"',"+height+","+parseInt(min_height)+")", 50);
				}
				group_index = id;
			}
		}
	}
}

function slideDown(objId,height,max_height) {
    if (document.getElementById) {
        obj = document.getElementById(objId);
		height += 10;		
        if (height <= max_height) {
			obj.style.height = height+"px";
            window.setTimeout("slideDown('"+objId+"',"+height+","+max_height+")", 10);
        }
    }
}

function slideUp(objId,height,min_height) {
    if (document.getElementById) {
        obj = document.getElementById(objId);
		height -= 10;		
        if (height >= min_height) {
			obj.style.height = height+"px";
            window.setTimeout("slideUp('"+objId+"',"+height+","+min_height+")", 10);
        }
    }
}

function reloadCaptcha(id) {
    var cimg = document.getElementById(id);
    var rnd = randomText(50);
    cimg.src = "captcha.jpg?" + rnd;
    return true;
}

function randomText(len) {
    var available_chars = [];
    for(var i = 48; i < 58; i++) available_chars[available_chars.length] = String.fromCharCode(i);    
    for(var i = 65; i < 91; i++) available_chars[available_chars.length] = String.fromCharCode(i);    
    for(var i = 97; i < 123; i++) available_chars[available_chars.length] = String.fromCharCode(i);    
    if(0==available_chars.length) return false;
    var result="";   
    for(var j=0; j<len; j++) {var ch=available_chars[Math.floor(Math.random()*available_chars.length)]; result+=ch;}
    return result;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

onload_functions.push("load_top_products()");
onload_functions.push("over_pics()");

