
$(function(){
    var pcoptions = {
      beforeSubmit: beforeSubmit,
      success:      afterSubmit,
      url:          "/index.php?l=addtocart&fromajax=1",
      type:         "POST",
      dataType:     "html",
      timeout:      3000
    };
    $('.ajaxOrderForm').ajaxForm(pcoptions); 
});

function beforeSubmit(formData, jqForm, options) {

}

function afterSubmit(responseText, statusText, xhr, $form) {
  var ccontents,stotal, code,pid,html,id,o = eval('(' + responseText + ')');    
  id = 'status' + o.sku;
  html = o.message;
  xhtml = o.html;
  code = o.code;
  stotal=o.subtotal;
  ccontents=o.ccontents;
  var itemscount=o.itemscount;
  $('#status').show();
  if(code) {
    if(o.sku == "PC" || o.sku == "Laptop") {
      xacknowledge(html,o.sku);
      $('#statusmessage').html(xhtml).hide();
    } else {
      $('#btn' + o.sku).hide();  
      $('#added' + o.sku).show();  
      $('#tadded' + o.sku).show('fast');  
    }
    $('#blog').hide();
  }
  $('#subtotal').html(stotal);
  $('#ccontents').html('<table>'+ccontents+'</table>');
  $('#itemscount').html(itemscount);
}

function showpage2(id) {
      $('#frontpage').hide();
      $('.page1').hide();
      $('.page2').show();
      var pid = "Accessory" + id;
      $('.'+pid).show();
      $('#barwrapper').hide();
      window.scrollTo(0,0);  
      $('#statusmessage').show('slow').fadeIn('slow');
}

function show_detail(id) {
   $('#xtraseals').hide();
   $('.wrapperdetail').hide();
   $('#wrapperX'+id).toggle();
   $('html, body').animate({scrollTop: $("#featurewrapper").offset().top+400}, 800);
}

// survey form
var _survey_serial=0;
var last_dialog=null;
function surveyform() { 
  var x=$('#surveyform').html();
  var y = (x||"").replace(/xxxx/g,""+_survey_serial);
  var $dialog = $('<div class="thedialog"></div>')
  .html(y)
  .dialog({
        modal: true,
	width:  600,
	height: 360,
	autoOpen: false,
	resizable: false,
	draggable: true,
	position: 'center',
	title: ''
	});
  $dialog.dialog('open');
  _survey_serial++;
  last_dialog = $dialog;
}

function closesurveyform() {
try {
  if(last_dialog != null) {
    last_dialog.dialog('destroy');
    last_dialog=null;
  }
 } catch(err) {
 }
}

function tvchannel(v) {
   document.xform.source.value=v;
};
function tvchannelx(v) {
   document.xform.channel.value=v;
}

function hidecartcontents() {
 $('#showcartbox').hide();
 return true;
}

function isDescendOf(a,b) {
    try{
        while(a.parentNode){
            if(a.parentNode == b) return true;
            a=a.parentNode;
        }
    }
    catch(e){}
    return false; 
};
            
function hidecartcontents0(a) {
 var a = a || window.event;
 var b=a.target||a.srcElement;  
 var p = document.getElementById("showcartbox");
 if(p && isDescendOf(b,p)) {
  return false;
 }
 hidecartcontents();
 return true;
}

function showcart1(data){
 $('#showcartcontents').html(data);
 $('#showcartbox').show('fast');
 document.body.onmousedown=hidecartcontents0;
}

function showcart() {
  $.ajax({
	url: "/index.php",
	data: {l: 'show_cart_contents'},
	dataType: "html",
	success: showcart1
	});
}

var last_xdialog=null;
function close_xdialog() {
  if(last_xdialog) {
   try {
      last_xdialog.dialog('close');
      last_xdialog = null;
    } catch(error) {}
 } 
}       
function xacknowledge(m,pid) { 
  var $dialog = $('<div class="xdialog"></div>')
  .html(m + '<span class="config">on the next page, you can customize your order, then checkout!</span>'+
'<div class="right xcontinue"><a href="javascript:void(0)" onclick="close_xdialog()"><img src="/images/xcontinue.png" width="168" alt="" title=""></a></div>')
  .dialog({
        modal: true,
	width:  800,
	height: 160,
	autoOpen: false,
	resizable: false,
	draggable: true,
	position: 'center',
	dialogClass: 'ydialog', 
	close: function(){showpage2(pid);},
	title: ''
	});
  $dialog.dialog('open');
  last_xdialog= $dialog;
}




