var origContent="";
function photopage(classname, id, imgfrom, imgto, imgcur, verticalmode) {
    var url = '../ajax/images.php';
    var pars = 'classname='+classname+'&id='+id+'&imgfrom='+imgfrom+'&imgto='+imgto+'&imgcur='+imgcur+'&verticalmode='+verticalmode;
	var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onFailure: reportError, onComplete: function(r){restoreimages(r)}});
    return false;
}
function restoreimages (r) {
    var ret=r.responseText;
    var imagesnav=$("imagesnav");
    imagesnav.innerHTML=ret;
}
function photoset (classname, id, imgcur) {
    var url = '../ajax/imageplace.php';
    var pars = 'classname='+classname+'&id='+id+'&imgcur='+imgcur;
	var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onFailure: reportError, onComplete: function(r){restoreimageplace(r)}});
    return false;
}
function restoreimageplace(r) {
    var ret=r.responseText;
    var imagesnav=$("imageplace");
    imagesnav.innerHTML=ret;
    jQuery('#imageplace a.lightbox').lightBox({
    	imageLoading: '/img/loading.gif',
    	imageBtnClose: '/img/lightbox-btn-close.gif',
    	imageBtnPrev: '/img/lightbox-btn-prev.gif',
    	imageBtnNext: '/img/lightbox-btn-next.gif'
    });    
}
function reportError(request) {
    alert(MESS["SORRY"]);
}
function myshow(field, id) {
    var reshow=1;
    if (show_desc) {
        if (show_desc==id) var reshow=0;
    }
    if (reshow) {
    	var posy=getPosition(field)[1];
    	var posx=getPosition(field)[0];
    	var div=$(id);
    	div.style.position="absolute";
    	div.style.top=eval(posy-120)+"px";
    	div.style.left=eval(posx-210)+"px";
    	div.style.display="block";
    	var h=div.getHeight();
    	var w=div.getHeight();
    	div.style.top=eval(posy-h+10)+"px";
    	show_desc=id;
    }
}
function myhide(id) {
    var div=$(id);
    div.style.display="none";
    show_desc=0;
}
function getPosition(obj) {
    var o=obj;
    var x=0, y=0;
    while(o) {
        x+=o.offsetLeft; 
        y+=o.offsetTop; 
        o=o.offsetParent; 
    }
    return [x,y];
}
function checkcartform(form) {    
    var error="";
    var ch=0;
    for (var i=0; i<form.elements.length; i++) {
        var el=form.elements[i];
        if (el.type=="select-one") {
            if (el.selectedIndex==0) {
                var t=$(el.name+"_name");
                error+=MESS["PLEASE_CHOOSE_SET"]+"\n";
            }
        }
    }
    if (isNaN(parseInt(form.qty.value)) || parseInt(form.qty.value)==0) error+=MESS["INCORRECT_QUANTITY"]+"\n";
    if (error!="")  {
        alert (MESS["ERROR"]+": "+error);
        return false;
    }
    else return true;
}
function checkcartmodifyform(form) {
    var error="";
    var ch=0;
    for (var i=0; i<form.elements.length; i++) {
        var el=form.elements[i];
        if (el.name.search("qty")!=-1) {
            if (isNaN(parseInt(el.value))) error+=el.value+" "+MESS["INCORRECT_QUANTITY"]+"\n";
        }
    }
    if (error!="")  {
        alert (MESS["ERROR"]+": "+error);
        return false;
    }
    else return true;
}
function checkcheckform(form) {
    var error="";
    var ch=0;
    for (var i=0; i<form.elements.length; i++) {
        var el=form.elements[i];
        if (el.name.search("address2")==-1 && el.name.search("promocode")==-1 && el.name.search("company")==-1 && el.name.search("s_company")==-1 && el.name.search("s_state")==-1 && el.name.search("state")==-1) {
            if (el.value=="") error+=el.name+" "+MESS["SHOULD_NOT_EMPTY"]+"\n";
        }
    }
    if (checkEmail(form.email.value)==false) error+=MESS["INCORRECT_EMAIL"]+"\n";
    if (error!="")  {
        alert (MESS["ERROR_ALL"]);
        return false;
    }
    else return true;
}
function makesubmit(form) {
    form.submit();
}
function reprice(form, subset) {
    var pice_place=$("current_price");    
    var index=subset.selectedIndex;    
    var id=subset[index].value;    
    if (id>0) {
        var price=$("subset_"+id);        
        pice_place.innerHTML=price.value;
    }
}
function copy_info(form, chck) {
    if (chck.checked==true) {
        for (var i=0; i<form.elements.length; i++) {
            var el=form.elements[i];
            var shname='s_'+el.name;
            var shelem=document.getElementsByName(shname);
            if (shelem[0]) {
                shelem[0].value=el.value;
            }
        }
    }
}
function doselectship(form, ship) {
    var pice_place=$("ship_price_only");
    var index=ship.selectedIndex;
    var id=ship[index].value;
    if (id>0) {
        var price=$("shipprice_"+id);
        if (price) {
            var t=price.value;
            pice_place.innerHTML=format_number_ex(t, 2, '.');
        }
        else pice_place.innerHTML="0";
    }
    else pice_place.innerHTML="0";
}
function format_number_ex(n, c, d, t) {
  c = Math.abs(c) + 1 ? c : 2;
  d = d || ",";
  t = t || "";
  var m = /^(\d+)(\.\d+)?$/.exec(n + "");
  if (!m) {
    return n;
  }
  var s;
  var x = m[1].length % 3;
  if (x) {
    s = m[1].substr(0, x);
    if (m[1].length > 3) {
      s += t;
    }
  }
  else {
    s = "";
  }
  s += m[1].substr(x).replace(/(\d{3})(?=\d)/g, "$1" + t);
  if (c) {
    s += d;
    if (m.length > 2 && typeof(m[2]) == "string" && m[2].length > 0) {
      s += parseFloat(m[2]).toFixed(c).substr(2);
    }
    else {
      for (var i = 0; i < c; ++i) {
        s += '0';
      }
    }
  }
  return s;
}
function dochargeship() {
    var ship_price_only=$("ship_price_only");
    var ship_price=$("ship_price"); 
    var subtotal=$("subtotal");
    var total_pay=$("total_pay");
    var s1=subtotal.innerHTML;
    var s2=ship_price_only.innerHTML;
    var tmp=eval(s1)+eval(s2);
    tmp=format_number_ex(tmp, 2, '.');
    ship_price.innerHTML=tmp;
    if (total_pay) total_pay.innerHTML=tmp;
}
function changebg(el, bg) {
	var e=$(el);	
	if (e) e.style.backgroundImage="url(/photos/document/big/"+bg+")";
}
function changesrc(t,n) {    
    t.src="/img/"+n;
}
function calculatepromo() {
    var promo=jQuery('input[name="promocode"]').val();
    jQuery.ajax({
        url: '/ajax/promo.php',
        data: ({promo: promo}),
        success: function (data, textStatus) {
            if (data) {
                var ship_price=jQuery("#ship_price");
                var ship_price_only=jQuery("#ship_price_only").html();
                var subtotal=jQuery("#subtotal");
                var t=subtotal.html();
                ship_price_only=parseFloat(ship_price_only) || 0;
                var dv=t*eval(data);
                var tmp=t-dv+eval(ship_price_only);
                tmp=format_number_ex(tmp, 2, '.');
                ship_price.html(tmp);
                jQuery("#promocode_block").hide();
                jQuery("#discount_per").html(format_number_ex(data*100, 2, '.'));
                jQuery("#discount_val").html(format_number_ex(dv, 2, '.'));
                jQuery("#promocode_info").show();
            }
        }
    });
}
function calculateshiping(do_shipping, form) {
    if (!do_shipping) alert(MESS["NO_SHIPING_OVER"]);
    if (do_shipping) {
        var c=jQuery('#country').val();
        var g=jQuery('#city').val();
        var z=jQuery('#zipcode').val();
        var s=jQuery('#state').val();
        var r=jQuery('#residential').attr('checked');        
        if (s=="" || c=="" || g=="" || z=="") {
            alert (MESS["ERROR_ALL"]);
        }
        else {
            var l = new Array()        
            var j=0;
            jQuery('.use_locations').each(function(index) {
                l[j]=jQuery(this).val();
                j++;
            });
            jQuery('#shipping_options').empty();
            jQuery('#ratesplase').html('');
            jQuery('#calc_bt').attr('disabled', 'disabled');
            jQuery.ajax({
                url: '/ajax/checkoutups.php',             
                data: ({country: c, city: g, zip: z, state: s, res: r, 'locations[]': l}),
                success: function (data, textStatus) {
                    if (data) {
                        var d=eval(data);
                        if (d) {
                            var selects=d[0];
                            for (var i=0; i<selects.length; i++) {
                                if (selects[i])
                                    form.shipping_options.options.add(new Option(selects[i].name, selects[i].num));
                            }
                            var hiddens=d[1];
                            for (var i=0; i<hiddens.length; i++) {
                                if (hiddens[i]) jQuery('#ratesplase').append('<input type="hidden" id="shipprice_'+hiddens[i].key+'" name="shipprice_'+hiddens[i].key+'" value="'+hiddens[i].value+'">');
                            }
                        }
                    }
                    jQuery('#shipingselect').show();
                    jQuery('#calc_bt').removeAttr('disabled');
                }
            });
        }
    }
}
function show_more_products() {
    var l=eval(jQuery('#limit').val());    
    var o=eval(jQuery('#offset').val())+l;
    var a=eval(jQuery('#cntall').val());
    if (a>o) {        
        jQuery('#more_link_container').remove();
        product_list_update(1, l, o);
        jQuery('#offset').val(o);
    }
    return false;
}
function product_list_update(addmode, limit, offset) { 
    jQuery('div.totop').show();
    if (!addmode) addmode=0;
    var param=new Array();
    var i=0;
    jQuery('div.forcheck div.item').each(function() {
        var inp=jQuery(this).children("input");
        if (inp.attr("disabled")==false) {
            param[i]=inp.attr("value");
            i++;
        }
    });
    jQuery('#bread_up').remove();
    jQuery('#back_list').hide();
    jQuery('#loading').show();
    var target="product_list";
    if (addmode) {
        target="more"+limit+"_"+offset;
        jQuery('#product_list').append("<div class='moreresult' id='"+target+"'></div>");
    }
    else {
        jQuery('#product_list').html("");
    }
    var keyword="";
    if (jQuery('#keyword').length) keyword=jQuery('#keyword').attr('value');
    var price="";
    if (jQuery('#price').length) price=jQuery('#price').attr('value');    
    var iid="";
    if (jQuery('#iid').length) iid=jQuery('#iid').attr('value');        
    
    jQuery('#'+target).load("/ajax/productsearch.php", { 'info[]': param, keyword: keyword, price: price, iid: iid, limit: limit, offset: offset}, function() {
        if (addmode) {
            //var gt=jQuery(".totop a").offset().top-800;
            var gt=jQuery(".footer").offset().top-800;
            jQuery('html, body').stop().animate({scrollTop: gt}, 1500);
        }
        jQuery('#loading').hide();
        if (!addmode) {
            jQuery('#offset').val(0);
            jQuery('#list_mode').val(1);
        }        
        origContent = jQuery('#product_list').html();
        //form to history
        //var url="sp_k~"+keyword+"!p~"+price+"!i~"+param.join('.');                
        //jQuery.history.load(url);        
        
    });
    return false;
}
function show_more_portfolio() {
    var l=eval(jQuery('#limit').val());    
    var o=eval(jQuery('#offset').val())+l;
    var a=eval(jQuery('#cntall').val());
    if (a>o) {        
        jQuery('#more_link_container').remove();
        portfolio_list_update(1, l, o);
        jQuery('#offset').val(o);        
    }    
    return false;    
}
function portfolio_list_update(addmode, limit, offset) {     
    if (!addmode) addmode=0;
    var param_system=new Array();
    var param_configuration=new Array();
    var param_organization=new Array();
    var i=0;
    var params_line=jQuery('#params_line');
    params_line.html("");
    var toline=new Array();
    var keyword=jQuery('#keyword').attr('value');
    if (keyword!="") toline[toline.length]=keyword;
    jQuery('div.forcheck div.item').each(function() {
        var inp=jQuery(this).children("input");
        if (inp.attr("disabled")==false) {
            toline[toline.length]=jQuery(this).text();
            if (inp.attr("name")=="system[]")           param_system[param_system.length]=inp.attr("value");
            if (inp.attr("name")=="configuration[]")    param_configuration[param_configuration.length]=inp.attr("value");
            if (inp.attr("name")=="organization[]")     param_organization[param_organization.length]=inp.attr("value");
            i++;
        }
    });
    jQuery('.subnav').show();
    jQuery('#bread_up').remove();
    jQuery('#back_list').hide();
    jQuery('#loading').show();
    var target="portfolio_list";
    if (addmode) {
        target="more"+limit+"_"+offset;
        jQuery('#portfolio_list').append("<div class='moreresult' id='"+target+"'></div>");
    }
    else {
        jQuery('#portfolio_list').html("");        
    }    
    var unt=1;
    if (jQuery('#unit2').attr('checked')==true) unt=2;
    var im=jQuery('#imagesmode').val();
    var surface=jQuery('#surface').attr('value');
    var height=jQuery('#height').attr('value');
    var country=jQuery('#country').attr('value');
    var state=jQuery('#state').attr('value');
    if (country!=0) toline[toline.length]=jQuery("#country option:selected").text();
    if (state!=0)   toline[toline.length]=jQuery("#state option:selected").text();
    if (surface!=0) toline[toline.length]=jQuery("#surface option:selected").text();
    if (height!=0)  toline[toline.length]=jQuery("#height option:selected").text();
    if (toline.length>0) {
        params_line.append("Search results for: ");
        params_line.append(toline.join(', '));
    }    
    jQuery('#'+target).load("/ajax/portfoliosearch.php", { 'system[]': param_system, 'configuration[]': param_configuration, 'organization[]': param_organization, keyword: keyword, surface: surface, height: height, country: country, state: state, unit: unt, limit: limit, offset: offset, imagesmode: im}, function() {
        if (addmode) {
            var gt=jQuery(".footer").offset().top-800;
            jQuery('html, body').stop().animate({scrollTop: gt}, 1500);
        }
        jQuery('#loading').hide();
        if (!addmode) {
            jQuery('#offset').val(0);
            jQuery('#list_mode').val(1);
        }
        jQuery('div.abc a.bold').removeClass('bold');
        origContent = jQuery('#portfolio_list').html();
    });
    return false;
}
//open product/portfolio details
function loadContent(hash) {
    var hash_a=hash.split("_");
    if (hash_a[0]=="top") hash_a[0]="";
    if (hash_a[0]=="p" || hash_a[0]=="i") {
        if (hash_a[0]=="p") {
            var update='product_list';
            var urlshow='/ajax/productdetail.php';
        }
        if (hash_a[0]=="i") {
            var update='portfolio_list';
            var urlshow='/ajax/portfoliodetail.php';
        }
        
        if (origContent == "") {
            origContent = jQuery('#'+update).html();
        }        
        var objname=jQuery("#productlnk_"+hash_a[1]).html();
        jQuery('#list_mode').val(0);
        jQuery('#adv_form').slideUp();
        jQuery('#adv_reset').hide();
        jQuery('div.totop').hide();
        jQuery('.adv_toggle').html(jQuery("#linktext").val());
        jQuery('.adv_blue').css("background-image", 'url('+jQuery("#backurl").val()+')');
        jQuery('#loading').show();
        jQuery('#'+update).html("");
        var unt=1;
        if (hash_a[0]=="i") {
            jQuery('.subnav').hide();            
            if (jQuery('#unit2').attr('checked')==true) unt=2;            
        }
        jQuery('#'+update).load(urlshow, {id: hash_a[1], unit: unt}, function() {
            jQuery('.bread tr td').append("<span id='bread_up'> &gt; "+objname+"</span>");
            jQuery('#loading').hide();
            jQuery('#back_list').show();
            jQuery('#imageplace a.lightbox').lightBox({
            	imageLoading: '/img/loading.gif',
            	imageBtnClose: '/img/lightbox-btn-close.gif',
            	imageBtnPrev: '/img/lightbox-btn-prev.gif',
            	imageBtnNext: '/img/lightbox-btn-next.gif'
            });
        });
    } else if(origContent != "") {
        if (!update) {
            if (jQuery('#product_list').length)   update='product_list';
            if (jQuery('#portfolio_list').length) update='portfolio_list';
        }
        jQuery('#'+update).html(origContent);
        jQuery('#back_list').hide();
        jQuery('#bread_up').remove();
        if (update=='portfolio_list') jQuery('.subnav').show();  
    }
    return false;
}
function show_more_project() {
    var l=eval(jQuery('#limit').val());    
    var o=eval(jQuery('#offset').val())+l;
    var a=eval(jQuery('#cntall').val());
    if (a>o) {        
        jQuery('#more_link_container').remove();
        project_list_update(1, l, o);
        jQuery('#offset').val(o);        
    }    
    return false;    
}
function project_list_update(addmode, limit, offset) {
    if (!addmode) addmode=0;
    jQuery('#loading').show();
    jQuery('div.totop').show();
    var target="project_list";
    if (addmode) {
        target="more"+limit+"_"+offset;
        jQuery('#project_list').append("<div class='moreresult' id='"+target+"'></div>");                
    }
    else {
        jQuery('#project_list').html("");
    }
    var mode=jQuery('#mode').val();
    jQuery('#'+target).load("/ajax/projectsearch.php", { mode: mode, limit: limit, offset: offset}, function() {
        if (addmode) {
            var gt=jQuery(".footer").offset().top-800;            
            jQuery('html, body').stop().animate({scrollTop: gt}, 1500);
        }
        jQuery('#loading').hide();        
        if (!addmode) {
            jQuery('#offset').val(0);
        }        
    });
    return false;
}
function show_more_person() {
    var l=eval(jQuery('#limit').val());    
    var o=eval(jQuery('#offset').val())+l;
    var a=eval(jQuery('#cntall').val());
    if (a>o) {        
        jQuery('#more_link_container').remove();
        person_list_update(1, l, o);
        jQuery('#offset').val(o);        
    }    
    return false;    
}
function person_list_update(addmode, limit, offset) { 
    if (!addmode) addmode=0;
    jQuery('#loading').show();
    var target="project_list";
    if (addmode) {
        target="more"+limit+"_"+offset;
        jQuery('#project_list').append("<div class='moreresult' id='"+target+"'></div>");
    }
    else {
        jQuery('#project_list').html("");
    }
    jQuery('#'+target).load("/ajax/personsearch.php", { limit: limit, offset: offset}, function() {
        if (addmode) {
            var gt=jQuery(".footer").offset().top-800;
            jQuery('html, body').stop().animate({scrollTop: gt}, 1500);
        }
        jQuery('#loading').hide();
        if (!addmode) {
            jQuery('#offset').val(0);
        }
    });
    return false;
}
function reloadstate(el, name, lang_code) {
    var ok=0;
    if (lang_code=="fr" && el.value==70) ok=1;
    if (lang_code=="gb" && el.value==2) ok=1;
    if (lang_code=="cn" && el.value==45) ok=1;
    if (lang_code=="de" && el.value==76) ok=1;
    if (lang_code=="nl" && el.value==140) ok=1;
    if (lang_code=="us" && (el.value==205 || el.value==1 || el.value==14)) ok=1;    
    if (ok==1) {
        jQuery("#outside_text").hide();
        jQuery("input[name=modify_bt]").removeAttr('disabled');
    }
    else {
        jQuery("#shipingselect").show();
        jQuery("#outside_text").show();
        jQuery("input[name=modify_bt]").attr('disabled', 'disabled');
    }
    var reg=jQuery("#"+name);
    if (reg) {
        var p_name='country';
        if (name=="iidstate") var p_name='country_id';
        reg.load("/ajax/state.php", {p_name: el.value});
    }
}
