// JavaScript Document
var loading = "<img src=\"images/loading.gif\"/> Loading.";
var element = null;

function updateLeftbarMargin()
{
  urlStr = document.location.href;
  
  if( urlStr.match('shop.php') )
  {
    leftBarHeight = document.getElementById('shop_image').height;
  }
  else
  {
    itemRows = document.getElementsByName('item_row');
    numRows = Math.max( itemRows.length, 2 );
        
    rowHeight = 308;
    lastRowHeight = 234;
    
    leftBarHeight = ( (numRows-1) * rowHeight ) + lastRowHeight;
  }

  menuHeight = document.getElementById('shop_menu').offsetHeight;
  bottomContentHeight = 180;
  
  margin = leftBarHeight - bottomContentHeight - menuHeight;
  document.getElementById('message').style.marginTop = margin + "px";
  document.getElementById('message').style.visibility = "visible";
  document.getElementById('socialLinks').style.visibility = "visible";
  document.getElementById('mailing_list').style.visibility = "visible";    
}

function reload() {
  if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
  {
    window.location = xmlhttp.responseText;
  }
}

function update() {
  if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
  {
    options = xmlhttp.responseText;
    replaceInnerHTML( element, options );
  }
}

function replaceInnerHTML( object, options )
{
  if( navigator.userAgent.indexOf('MSIE') >= 0 && object.type == "select-one" )
  {
    old_options = object.innerHTML;
    new_options = object.outerHTML.replace( old_options + "</SELECT>", options + "</SELECT>");
    object.outerHTML = new_options;
  }
  else
  {
    object.innerHTML = options;
  }
}

function updateColours( id ) 
{
  sname = "size_list_" + id;
  cname = "colour_list_" + id;
  qname = "quantity_list_" + id;

  slist = document.getElementById( sname );
  sindex = document.getElementById( sname ).selectedIndex;
  
  element = document.getElementById( cname );
  
  if( sindex != 0 )
  {
    sid = slist[sindex].value;
   
    try 
    {
      xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { }
  
    xmlhttp.onreadystatechange = update;
  
    xmlhttp.open('POST', 'stock.php', true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    
    // send POST variables
    xmlhttp.send("action=get_colours&id=" + id + "&sid=" + sid);

  }
  else
  {
    replaceInnerHTML( document.getElementById( cname ), "<option value=\"0\">Select Color</option><option value=\"select\">Select a size first</option>" );
    replaceInnerHTML( document.getElementById( qname ), "<option value=\"0\">Quantity</option><option value=\"select\">--Select a size and colour first--</option>" );
  }
}

function updateQuantity( id ) 
{
  sname = "size_list_" + id;
  cname = "colour_list_" + id;
  qname = "quantity_list_" + id;

  slist = document.getElementById( sname );
  sindex = document.getElementById( sname ).selectedIndex;
  
  clist = document.getElementById( cname );
  cindex = document.getElementById( cname ).selectedIndex;
  
  element = document.getElementById( qname );
  
  if( sindex != 0 && cindex != 0)
  {
    sid = slist[sindex].value;
    cid = clist[cindex].value;
    
    try 
    {
      xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { }
  
    xmlhttp.onreadystatechange = update;
  
    xmlhttp.open('POST', 'stock.php', true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    
    // send POST variables
    xmlhttp.send("action=get_stock&id=" + id + "&sid=" + sid + "&cid=" + cid);
  }
  else if( cindex == 0 )
  {
    replaceInnerHTML( document.getElementById( qname ), "<option value=\"0\">Quantity</option><option value=\"select\">--Select a size and colour first--</option>" );
  }
  
}

function validate( id ) 
{
  sname = "size_list_" + id;
  cname = "colour_list_" + id;
  qname = "quantity_list_" + id;  

  slist = document.getElementById( sname );
  sindex = document.getElementById( sname ).selectedIndex;
  sid = slist[sindex].value;
  
  if( sid == 0 )
  {
    alert("Please select a size first.");
    return false;
  }
  
  clist = document.getElementById( cname );
  cindex = document.getElementById( cname ).selectedIndex;
  cid = clist[cindex].value;

  if( cid == 0 || cid == 'select' )
  {
    alert("Please select a color first.");
    return false;
  }  
  
  qlist = document.getElementById( qname );
  qindex = document.getElementById( qname ).selectedIndex;
  qid = qlist[qindex].value;
  
  if( qid == 0 || qid == 'select' )
  {
    alert("Please select a quantity first.");
    return false;
  }  
  
  return true;
}

function validateUpdate()
{
  id = document.getElementsByName("item[]");
  
  for (var i = 0; i < id.length; i++)
  {
    itemId = id.item(i).value
  
    if( document.getElementById( "remove_" + itemId ).checked == false && !validate( itemId ) )
    {
      return false;
    }
  }
  
  return true;
}

function handleEnter(e) {
    var charCode;
    
    if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }

    return (charCode != 13);
}

function validateCoupon()
{
  coupon = document.getElementById("coupon").value;
  coupon = coupon.replace(/^\s+|\s+$/g,"");

  if( coupon.length == 0 )
  {
    alert( "Please enter a coupon code." );
  }
  else
  {
    window.location = "cart.php?coupon=" + coupon;
  }
}

function updateImage( id, image ) 
{
  element = document.getElementById( "item_image_panel" );
    
  try 
  {
    xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e) { }

  xmlhttp.onreadystatechange = update;

  xmlhttp.open('POST', 'image.php', true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  
  // send POST variables
  xmlhttp.send("action=get_next&id=" + id + "&image=" + image);
  
}

function removeItem( id )
{
  try 
  {
    xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e) { }
  
  xmlhttp.onreadystatechange = reload;
  
  xmlhttp.open('POST', 'cart.php', true);
  xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  
  // send POST variables
  xmlhttp.send("action=remove&id=" + id);

}

function mailingList(action)
{
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  var address = document.getElementById("email").value;
  if(reg.test(address) == false) 
  {
      alert('Invalid Email Address.');
  }
  else
  {
    try 
    {
      xmlhttp = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) { }
      
      xmlhttp.onreadystatechange = mailingListDone;
      
      xmlhttp.open('POST', 'email.php', true);
      xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
      
      // send POST variables
      xmlhttp.send("action=" + action + "&email=" + address);  
  }
}

function mailingListDone()
{
  div = document.getElementById("mailing_list");
  
  if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
  {
    div.innerHTML = xmlhttp.responseText;
  }
  else
  {
    div.innerHTML = "<img src=\"images/loading.gif\"/>";
  }
}

function updateShipping( shipping )
{
    swap = document.getElementById("swap").value

    if ( swap )
    {
        document.getElementById("total").value = shipping;
        document.getElementById("amount").value = shipping;

        document.getElementById("total_label").innerHTML = "CAD $" + shipping.toFixed(2);
        document.getElementById("shipping_label").innerHTML = "$" + shipping.toFixed(2);
    }
    else 
    {
        cur_shipping = parseFloat(document.getElementById("shipping").value);
        cur_total = parseFloat(document.getElementById("total").value);

        new_shipping = parseFloat(shipping);
        new_total = cur_total - cur_shipping + new_shipping;

        document.getElementById("total").value = new_total;
        document.getElementById("shipping").value = new_shipping;
        
        document.getElementById("total_label").innerHTML = "CAD $" + new_total.toFixed(2);
        document.getElementById("shipping_label").innerHTML = "$" + new_shipping.toFixed(2);
    }
}

function isFilled( field, msg )
{
    with ( field )
    {
        if ( value == null || value == "" )
        {
            alert( msg );
            field.focus();
            return false;
        }
        else
        {
            return true;
        }
    }
}

function isSelected( field, msg )
{
    sIndex = field.selectedIndex;

    if ( field[sIndex].value == 0 )
    {
        alert( msg );
        return false;
    }
    else
    {
        return true;
    }
}

function isValid( field, regexp, msg )
{
    if( !regexp.test( field.value ) )
    {
        alert( msg );
        field.focus();
        return false;
    }
    else
    {
        return true;
    }
}

function validateShipping( form )
{
    with( form )
    {
        if( !isFilled( last_name, "Please enter Last Name." ) ) return false;
        if( !isFilled( first_name, "Please enter First Name." ) ) return false;
        if( !isFilled( address1, "Please enter Address." ) ) return false;
        if( !isFilled( city, "Please enter City." ) ) return false;
		if( !isSelected( state, "Please select Province / State." ) ) return false; 		
        if( !isSelected( country, "Please select Country." ) ) return false;
        if( !isFilled( zip, "Please enter Postal Code / Zipcode." ) ) return false;
        //if( !isFilled( phone, "Please enter Telephone." ) ) return false;
        //if( !isValid( phone, new RegExp(/^[1-9]\d{9}$/), "Please enter valid 10 digits Telephone." ) ) return false;
    }
    
    return true;
}

