function photopage(classname, id, imgfrom, imgto, imgcur) {

    var url = '../ajax/images.php';
    var pars = 'classname='+classname+'&id='+id+'&imgfrom='+imgfrom+'&imgto='+imgto+'&imgcur='+imgcur;

    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;
}

function reportError(request) {
    alert('Sorry. There was an error.');
}

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-20)+"px";
        div.style.left=eval(posx-20)+"px";
        div.style.display="block";
        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];
}

var win;
function big_photo(f,w,h) {
    if (win) {
        win.close();
    }

    win=window.open('../photo.php?f='+f, null, "width="+w+",height="+h+",toolbar=0,scrollbars=no,resizable=no");
    return false;
}

function viewImg(image_href){
      var left = document.documentElement.clientWidth/2 - 250;
      var top = document.documentElement.clientHeight/2 - 100;

      var xstr = 'scrollbars=no,toolbar=no,status=no,menubar=no,directories=no,location=no,resizable=yes,width=500,height=200,left='+left+',top='+top;
      var prodWindow = window.open(image_href, 'window'+Math.round(Math.random()*1000), xstr);
      if (prodWindow) prodWindow.focus();
}

function backToSite(){
    var prodWindow = window.opener;
    if (prodWindow){
        prodWindow.focus();
        window.close();
    }
}

function housePrew(url, dir_image, image_name){
    var house_prev = document.getElementById('house');
    house_prev.innerHTML = '<a href="/view_img.php?dir_image='+dir_image+'&amp;image_name='+image_name+'" target="_blank" onclick="viewImg(this.href); return false;"><img src="'+url+dir_image+'/medium/'+image_name+'" alt="" /></a>';
}


/* Products Page Functions */

AjaxLoader = new Control.Modal(false,{
    width: 200,
    height: 130,
    overlayCloseOnClick: false,
    contents: '<div align="center"><img src="/img/ajax-loader.gif" ' +
            'width="54" height="55" vspace="15" alt=""><br><br><br>' +
            '<b><i>Loading...<\/i><\/b><\/div>'
});

function showLoader()
{
    if (Ajax.activeRequestCount < 1)
    {
        AjaxLoader.showWin();
    }
}

function hideLoader()
{
    if (Ajax.activeRequestCount <= 1)
    {
        window.setTimeout(hidewind, 500);
    }
}

function hidewind()
{
  AjaxLoader.hideWin();
}

function loadPopup(fname, popup_name, params)
{

  ajax = new Ajax.Updater(
        popup_name,
        "/ajax/" + fname,
        { method: 'post',
          parameters: params,
          onCreate: showLoader,
          onComplete: hideLoader,
          onFailure: reportError,
          onSuccess: displayPopup(popup_name)
        });
}

function get_model_image(model_id) {
  //if (!model_id) return;
  new Ajax.Request('/ajax/get_model_image.php?model_id='+model_id, {
    method: 'get',
      onSuccess: function(transport) {
          var img_src = transport.responseText;
          if (img_src != '') {
              $('model_img').src = img_src;
              $('model_img').style.display = "inline";
          }
          else {
              $('model_img').style.display = "none";
              $('model_img').src = '';              
          }
      }
   });

}

function displayPopup(popup_name)
{
    $(popup_name).style.display = 'block';
}

function hidePopup(popup_name)
{
    $(popup_name).style.display = 'none';
}

function hideModel()
{
    $('model_selector').innerHTML = '<table class="step"><tr><td><h3>Choose model:</h3></td><td class="step_number">2</td></tr></table>';
}

function hideSize()
{
    $('size_selector').innerHTML = '<table class="step"><tr><td><h3>Choose dimensions:</h3></td><td class="step_number">3</td></tr></table>';
}

function hideGlazing()
{
    $('glazing_selector').innerHTML = '<table class="step"><tr><td><h3>Choose glazing:</h3></td><td class="step_number">4</td></tr></table>';
}

function hidePitch()
{
    $('pitch_selector').innerHTML = '<table class="step"><tr><td><h3>Choose pitch of roof:</h3></td><td class="step_number">5</td></tr></table>';
}

function hideFinish()
{
    $('finish_selector').innerHTML = '<table class="step"><tr><td><h3>Choose finish:</h3></td><td class="step_number">6</td></tr></table>';
}

function hideAttr()
{
    $('attr_selector').innerHTML = '<table class="step"><tr><td><h3>Choose options:</h3></td><td class="step_number">7</td></tr></table>';
}

function hideQuantity()
{
    $('quantity_selector').innerHTML = '<table class="step"><tr><td><h3>Choose quantity:</h3></td><td class="step_number">8</td></tr></table>';
}

function hideCalculate()
{
    $('calculate_info').innerHTML = '';
}

function choose_size()
{
    var params = new Array();

    elem = $A($$("input.dimension_field"));
    elem.each(function(s, index)
              {
                params[index] = s.id + "=" + s.value;
              });
    params = params.join ("&");

    var url = '/ajax/size_validate.php';
    var myAjax = new Ajax.Request(
        url,
        { method: 'get',
          parameters: params,
          onFailure: function(r) { alert("Error:" + r.responseText) },
          onException:function(r, err) { alert("Exception:" + r.responseText + err) },
          onSuccess: function(r)
          {
                if (r.responseText)
                {
                    var error_message = r.responseText;
                    if(error_message != "no_errors")
                    {
                        alert(error_message);
                    }
                    else
                    {
                        loadPopup('glazing_selector.php', 'glazing_selector');
                    }
                }
          }
    });
}

function choose_attr()
{
    var params = new Array();
    var validate = true;

    elem = $A($$("select.attr_field"));
    elem.each(function(s, index)
              {
                params[index] = s.id + "=" + s.value;
                if(s.value == 0) validate = false;
              });
    params = params.join ("&");

    if(validate == false)
    {
        alert("You must choose all attributes!");
    }
    else
    {
        loadPopup('quantity_selector.php', 'quantity_selector', params);
    }
}

function choose_quantity()
{
    error = "";
    qnt = $('quantity').value;

    if(qnt == "") error += "Please, enter quantity\n";

    if(error != "")
    {
        alert(error);
    }
    else
    {
        loadPopup('calculate.php', 'calculate_info', 'quantity=' + qnt);
    }
}

function on_address_change(address_id)
{
    showLoader();
        
    var url = '../ajax/get_freight.php';
    var pars = 'address='+address_id;
    
	var myAjax = new Ajax.Request(
        url, 
        {method: 'get', parameters: pars, onFailure: reportError, onComplete: function(r){renew_price(r)}});
    
    return false;
}




function renew_price(r) {
   
    hideLoader();
    /*alert('here');*/     
    var ret=r.responseText;    
     
    var delim = ret.search(':');
    var shipping = ret.substr(0, delim);   
    var total = ret.substr(delim+1, ret.length);
    
     
    var row = $('freight_row');
        
    if (shipping == 'n/a') 
    {
        shipping = 'Call for Shipping Estimate';
        row.style.display = "";
    }
    else if (shipping == '0.00')
    {
        shipping = '$0.00';
        row.style.display = "none";
    }
    else
    {
        shipping = '$'+shipping;
        row.style.display = "";
    }
    
    $('freight').update(shipping);
    $('total').update('$'+total);
    
    hideLoader();

}

function reportError(request) {
    alert('Sorry. There was an error.');
}


function render_sales_email() {
    document.write('<a href="mailto:');
    document.write('sales');
    document.write('@');
    document.write('starlightskylights.com');
    document.write('">');
    document.write('sales');
    document.write('@');
    document.write('starlightskylights.com');
    document.write('</a>');    
}


function render_keith_email() {
    document.write('<a href="mailto:');
    document.write('keith');
    document.write('@');
    document.write('starlightskylights.com');
    document.write('">');
    document.write('keith');
    document.write('@');
    document.write('starlightskylights.com');
    document.write('</a>');    
}

function get_window_width() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;    
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;    
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;    
  }
  return myWidth;  
}


function get_window_height() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE    
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'    
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible    
    myHeight = document.body.clientHeight;
  }  
  return myHeight;
}

function create_estimate_popup() {
    //alert(window.);
    
    var left = (get_window_width()-1000)/2;
    var top = (get_window_height()-600)/2;
    
    var xstr = 'scrollbars=yes,toolbar=yes,status=no,menubar=no,directories=no,location=no,resizable=yes,width=1000,height=700,left='+left+',top='+top;
    var myWindow = window.open("/catalog.html", 'window'+Math.round(Math.random()*1000), xstr);
    if (myWindow) myWindow.focus();
}

function close_estimate_popup() {    
    var opener = window.opener;
    if(opener != null) {
        opener.location.href = '/cart.html?order=succ';
        opener.focus();
        self.close();
    }    
}
