// Copyright (C) 2005-2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
//     Instead, you may link to the homepage of this code:
//     http://www.php-development.ru/javascripts/popup-window.php
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as part of another product.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.

// USAGE
//
// function popup_show(id, drag_id, exit_id, position, x, y, position_id)
//
// id          - id of a popup window;
// drag_id     - id of an element within popup window intended for dragging it
// exit_id     - id of an element within popup window intended for hiding it
// position    - positioning type:
//               "element", "element-right", "element-bottom", "mouse",
//               "screen-top-left", "screen-center", "screen-bottom-right"
// x, y        - offset
// position_id - id of an element relative to which popup window will be positioned


// ***** Variables *************************************************************

var shpcrt_dragging = false;
var shpcrt_target = 'shpcrt';
var shpcrt_mouseX;
var shpcrt_mouseY;
var shpcrt_mouseposX;
var shpcrt_mouseposY;
var shpcrt_oldfunction;

function switchBaileyVideo(blvideo)	{
	var bvframe = document.getElementById('baileyvideofrm');
	bvframe.setAttribute('src','http://www.youtube.com/embed/'+blvideo+'?rel=0');
	
}

function submitForm_3()	{
	document.indexform.page.value='UpdateShoppingCart';
	document.indexform.submit();
}


function AddQuickProductToCart(anchAddProductsObj)	{
	var baileysku = trim(document.indexform.quickproductaddsku.value);
	var skuqty = trim(document.indexform.quickproductaddqty.value);
	if(baileysku.length<6 || skuqty.length<1 || isNaN(skuqty))	{
		alert('Please enter valid Bailey No and Quantity as number!');
		return false;
	}
	//var anchAddProducts='<%=BaileyConst.getConstantValue(BaileyConst.SSLSecUrl)+"index.php?dnfwd=1&page="+BaileyConst.ProductDetails%>'+'&baileyno='+baileysku+'&qty='+skuqty+'&flagquickadd=true';
	var anchAddProducts='';
	itemdetails=window.open(anchAddProducts,'itemdetails','width=450px,height=450px, resizable=yes, location=no, directories=no, status=no, menubar=no, scrollbars=yes, ');
	itemdetails.focus();
	return true;
}

function QuickLoadTopCart()	{
	var qlform = document.getElementById('quickloadcarttop');
	var ShoppingCartPage = document.getElementById('quickloadcart');
	var qlfrmser = $(qlform).serialize();
	if(ShoppingCartPage==null)	{
		txtlink = "index.php?dnfwd=1&topcart=1&page=quickcartloaditems&"+qlfrmser;
	}
	else	{
		txtlink = "index.php?dnfwd=1&topcart=1&screloadpage=1&page=quickcartloaditems&"+qlfrmser;
	}
   	ajax_loadContent('qlshopcartdiv',txtlink);
	return false;
}

function CloseTopCart()	{
	var element = document.getElementById('qlshpcrt');
	element.style.display = 'none';
	return false;
}

function VerifyQLCartItem(txbox)	{
	/*var txvalue = txbox.value;
	$.get("index.php?page=jQueryVerifyPartNo", { baileyno: txvalue }, function(data){
	alert(XMLtoString(data))
	});*/
	var txvalue = txbox.value;
	var html = $.ajax({
	  url: "index.php?page=jQueryVerifyPartNo",
	  global: false,
      type: "POST",
	  data: {baileyno : txvalue},
      dataType: "html",
	  async: false
	 }).responseText;
	if(html=='false') {
		alert('Invalid or discontinued part!');
		txbox.value = '';
		txbox.focus();
	}
}

function QuickLoadToCart()	{
	var qlform = document.getElementById('quickloadcart');
	var qlfrmser = $(qlform).serialize();
	txtlink = "index.php?dnfwd=1&page=quickcartloaditems&"+qlfrmser;
   	ajax_loadContent('shopcartdiv',txtlink);
	return false;
}



// ***** shpcrt_mousedown *******************************************************

function shpcrt_mousedown(e)
{
  var ie = navigator.appName == "Microsoft Internet Explorer";

  shpcrt_mouseposX = ie ? window.event.clientX : e.clientX;
  shpcrt_mouseposY = ie ? window.event.clientY : e.clientY;
}


// ***** shpcrt_mousedown_window ************************************************

function shpcrt_mousedown_window(e)
{
  var ie = navigator.appName == "Microsoft Internet Explorer";

  if ( ie && window.event.button != 1) return;
  if (!ie && e.button            != 0) return;

  shpcrt_dragging = true;
  shpcrt_target   = this['target'];
  shpcrt_mouseX   = ie ? window.event.clientX : e.clientX;
  shpcrt_mouseY   = ie ? window.event.clientY : e.clientY;

  if (ie)
       shpcrt_oldfunction = document.onselectstart;
  else shpcrt_oldfunction = document.onmousedown;

  if (ie)
       document.onselectstart = new Function("return false;");
  else document.onmousedown   = new Function("return false;");
}


// ***** shpcrt_mousemove *******************************************************

function shpcrt_mousemove(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(shpcrt_target);
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;

  if (!shpcrt_dragging) return;

  element.style.left = (element.offsetLeft+mouseX-shpcrt_mouseX)+'px';
  element.style.top  = (element.offsetTop +mouseY-shpcrt_mouseY)+'px';

  shpcrt_mouseX = ie ? window.event.clientX : e.clientX;
  shpcrt_mouseY = ie ? window.event.clientY : e.clientY;
}

// ***** shpcrt_mouseup *********************************************************

function shpcrt_mouseup(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(shpcrt_target);

  if (!shpcrt_dragging) return;

  shpcrt_dragging = false;

  if (ie)
       document.onselectstart = shpcrt_oldfunction;
  else document.onmousedown   = shpcrt_oldfunction;
}

// ***** shpcrt_exit ************************************************************

function shpcrt_exit(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(shpcrt_target);

  shpcrt_mouseup(e);
  element.style.display = 'none';
  
  return false;
}


// ***** shpcrt_addtocart ************************************************************

function shpcrt_addtocart(frm)
{
   var shpcitem = frm.shpcartpopitem.value;
   var shpcqty = frm.shpcartpopqty.value;
   txtlink = "index.php?dnfwd=1&page=addtocartpopup&shpcaction=add&productno="+shpcitem+"&shpcqty="+shpcqty;
   ajax_loadContent('shopcartdiv',txtlink);
   return false;
}

// ***** shpcrt_addtocart ************************************************************

function shpcrt_reloadStatus()
{
   ajax_loadContentNoCache('CartStatusDV','CartStatus');
}


// ***** shpcrt_show ************************************************************

function shpcrt_show(id, drag_id, exit_id, position, x, y, position_id)
{
  var element      = document.getElementById(id);
  var drag_element = document.getElementById(drag_id);
  var exit_element = document.getElementById(exit_id);

  var width        = window.innerWidth  ? window.innerWidth  : document.documentElement.clientWidth;
  var height       = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

  element.style.position = "absolute";
  element.style.display  = "block";

  if (position == "element" || position == "element-right" || position == "element-bottom")
  {
    var position_element = document.getElementById(position_id);

    for (var p = position_element; p; p = p.offsetParent)
      if (p.style.position != 'absolute')
      {
        x += p.offsetLeft;
        y += p.offsetTop;
      }

    if (position == "element-right" ) x += position_element.clientWidth;
    if (position == "element-bottom") y += position_element.clientHeight;

    element.style.left = x+'px';
    element.style.top  = y+'px';
  }

  if (position == "mouse")
  {
    element.style.left = (document.documentElement.scrollLeft+shpcrt_mouseposX+x)+'px';
    element.style.top  = (document.documentElement.scrollTop +shpcrt_mouseposY+y)+'px';
  }

  if (position == "screen-top-left")
  {
    element.style.left = (document.documentElement.scrollLeft+x)+'px';
    element.style.top  = (document.documentElement.scrollTop +y)+'px';
  }

  if (position == "screen-center")
  {
    element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )/2+x)+'px';
    element.style.top  = (document.documentElement.scrollTop +(height-element.clientHeight)/2+y)+'px';
  }

  if (position == "screen-bottom-right")
  {
    element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )  +x)+'px';
    element.style.top  = (document.documentElement.scrollTop +(height-element.clientHeight)  +y)+'px';
  }

  drag_element['target']   = id;
  drag_element.onmousedown = shpcrt_mousedown_window;

  element.style.backgroundColor = '#f9f3d9'
  //exit_element.onclick     = shpcrt_exit;
}


// ***** Attach Events *********************************************************

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmousedown', shpcrt_mousedown);
else document.addEventListener('mousedown', shpcrt_mousedown, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmousemove', shpcrt_mousemove);
else document.addEventListener('mousemove', shpcrt_mousemove, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmouseup', shpcrt_mouseup);
else document.addEventListener('mouseup', shpcrt_mouseup, false);

