// JavaScript Document

function swapImages(img1, img2)
{
	src1 = document.getElementById(img1).src;
	src2 = document.getElementById(img2).src;

	document.getElementById(img1).src = src2;
	document.getElementById(img2).src = src1;

	document.getElementById('mainImageLink').href = document.getElementById('mainImage').src
	
}

function validateNumber(field)
{
  var val = field.value;
  if(!/^\d*$/.test(val)||val==0)
  {
      alert("Please enter a valid number");
      field.focus();
      field.select();
  }
}

function dec(quantity)
{
  document.getElementById(quantity).value--;
  validateNumber(document.getElementById(quantity));
  updateText();
}

function inc(quantity)
{
  document.getElementById(quantity).value++;
  validateNumber(document.getElementById(quantity));
  updateText();
}

function showEmail()
{
	window.location.href="/store/viewItemEmail.asp?idProduct=<%=pIdProduct%>";
}

function updateText()
{
  document.getElementById("textQuantity").innerHTML = document.getElementById("quantity").value
}

function xstooltip_findPosX(obj) 
{
  var curleft = 0;
  if (obj.offsetParent) 
  {
    while (obj.offsetParent) 
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function xstooltip_findPosY(obj) 
{
    var curtop = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function xstooltip_hide(id)
{
    it = document.getElementById(id); 
    it.style.visibility = 'hidden'; 
}


function xstooltip_show(tooltipId, parentId, posX, posY)
{
    it = document.getElementById(tooltipId);
    
    if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0))
    {
        // need to fixate default size (MSIE problem)
        it.style.width = it.offsetWidth + 'px';
        it.style.height = it.offsetHeight + 'px';
        
        img = document.getElementById(parentId); 
    
        // if tooltip is too wide, shift left to be within parent 
        if (posX + it.offsetWidth > img.offsetWidth) posX = img.offsetWidth - it.offsetWidth;
        if (posX < 0 ) posX = 0; 
        
        x = xstooltip_findPosX(img) + posX;
        y = xstooltip_findPosY(img) + posY;
        
        it.style.top = y + 'px';
        it.style.left = x + 'px';
    }
    
    it.style.visibility = 'visible'; 
}

function checkFormComplete()
{

	var customerName = document.getElementById('customerName').value;
	var email = document.getElementById('email').value;
	var phone = document.getElementById('phone').value;
	
	var customerNameFilter  = /^([a-zA-Z\s]){2,200}$/;
	var phoneFilter  = /^([0-9\s]){10,15}$/;
	var emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (!emailFilter.test(email)) { alert("Email address invalid"); return false; }
	if (!customerNameFilter.test(customerName)) { alert("Customer name too short"); return false;} 
	if (!phoneFilter.test(phone)) { alert("Phone number invalid"); return false;}

	return true;	
	
}

function validateNumber(field)
{
  var val = field.value;
  if(!/^\d*$/.test(val)||val==0)
  {
      alert("Please enter a valid number");
      field.focus();
      field.select();
  }
}

function dec(quantity)
{
  document.getElementById(quantity).value--;
  validateNumber(document.getElementById(quantity));
  document.getElementById("recalc").submit();
}

function inc(quantity)
{
  document.getElementById(quantity).value++;
  validateNumber(document.getElementById(quantity));
  document.getElementById("recalc").submit();
}

function checkSubmit(termsID)
{
if (document.getElementById(termsID).checked)
{
    return true; 
} 
else 
{
  alert("You must accept the terms & conditions by ticking the box");
  return false;
}

}

