function checkThisForm(formname, submitbutton, errors) {
  if (errors == '') {
   // eval(formname+'.'+submitbutton+'.disabled=true');
    eval('document.'+formname+'.submit()');
  } else {
    alert(errors);
  }
}

function checkText(formname, textboxname, displaytext) {
  var localerror = '';
  if(Trim(eval('document.'+formname+'.'+textboxname+'.value'))=='') {
    localerror =  '- '+displaytext+' is Required.\n';
  } else localerror = '';
  return localerror;
}

function checkNum(formname, textboxname, displaytext) {
  var localerror = '';
  if(isNaN(eval('document.'+formname+'.'+textboxname+'.value'))) {
    localerror =  '- '+displaytext+' Should Be A Number With No Spaces.\n';
  } else localerror = '';
  return localerror;
}

function checkSpaces(formname, textboxname, displaytext) {
  var valid = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'; // define valid characters
  var localerror = '';
  if(!isValid(Trim(eval('document.'+formname+'.'+textboxname+'.value')), valid)) {
    localerror =  '- '+displaytext+' Should Not Contain Spaces.\n';
  } else localerror = '';
  return localerror;
}

function checkSelect(formname, selectboxname, displaytext) {
  var localerror = '';
  if(eval('document.'+formname+'.'+selectboxname+'.selectedIndex')==0) {
    localerror =  '- '+displaytext+' is Required.\n';
  } else localerror = '';
  return localerror;
}

function getRadio(formname, radioname, displaytext) {
  for (var i=0; i < eval('document.'+formname+'.'+radioname+'.length'); i++) {
    if (eval('document.'+formname+'.'+radioname+'[i].checked')) {
      var rad_val = eval('document.'+formname+'.'+radioname+'[i].value');
      return rad_val;
    }
  }
}

function checkRadio(formname, radioname, displaytext) {
  var localerror = '';
  var rad_val    = '';
  for (var i=0; i < eval('document.'+formname+'.'+radioname+'.length'); i++) { //check every radio button by that name
    if (eval('document.'+formname+'.'+radioname+'[i].checked'))  { //if it is checked
      rad_val += '-';
      }	else rad_val += '';
      }
    if (rad_val=='') {
      localerror =  '- '+displaytext+' is Required.\n';
    }
  return localerror;
}

function autoComplete (field, select, property) {
/*onKeyUp="autoComplete(this,this.form.selectboxname,'value',false)" - add this to textbox where you are typing*/
  var found = false;
  for (var i = 0; i < select.options.length; i++) {
    if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
      found=true; break;
    }
  }
  if (found) {
    select.selectedIndex = i;
  } else {
    select.selectedIndex = -1;
  }
  if (field.createTextRange) {
    if (!found) {
      field.value=field.value.substring(0,field.value.length-1);
      return;
    }
    var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
    if (cursorKeys.indexOf(event.keyCode+";") == -1) {
      var r1 = field.createTextRange();
      var oldValue = r1.text;
      var newValue = found ? select.options[i][property] : oldValue;
      if (newValue != field.value) {
        field.value = newValue;
        var rNew = field.createTextRange();
        rNew.moveStart('character', oldValue.length) ;
        rNew.select();
      }
    }
  }
}

function Trim(s) {
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) {
    s = s.substring(1,s.length);
  }
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function isValid(string,allowed) {
//  var valid = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; // define valid characters
    for (var i=0; i< string.length; i++) {
      if (allowed.indexOf(string.charAt(i)) == -1) return false;
    }
    return true;
}


function wdwpopup(url,name,w,h)
{
	window.open(url,'popup','width='+w+',height='+h+',scrollbars=no, location=no,statusbar=no');
}

function swapimg(img,what)
{
	document.getElementById(img).src = what
}

function playMediaClip(mediaClipId) {
 var winL = (screen.width - 300) / 2;
 var winT = (screen.height - 200) / 2;
 window.open( '/includes/playmedia.asp?id=' + mediaClipId , 'playMediaClip' , 'width=300, height=200, top=' + winT + ', left=' + winL + ', resizable=yes, scrollbars=no, toolbar=no, location=no, directories=no, status=no, menubar=no, copyhistory=no');
 return false;
}

// JavaScript File
function emailCheck (emailStr) {

var checkTLD=1;

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

var emailPat=/^(.+)@(.+)$/;

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

var validChars="\[^\\s" + specialChars + "\]";

var quotedUser="(\"[^\"]*\")";

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

var atom=validChars + '+';

var word="(" + atom + "|" + quotedUser + ")";

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
   }
}

if (user.match(userPat)==null) {

alert("The username doesn't seem to be valid.");
return false;
}

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
   }
}

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

if (len<2) {
alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

//  End -->

function getHTTPObject(xd) {
var xd = false;
if (window.ActiveXObject){
	try{xd = new ActiveXObject("Msxml2.XMLHTTP");}
catch(e) {
	try{xd = new ActiveXObject("Microsoft.XMLHTTP");}
catch(e) {xd = false;}
	}
} 
else if (window.XMLHttpRequest) {try{xd = new XMLHttpRequest();}
	catch(e) {xd=false;}
}
return xd;
}

function grabFile(file,div,xd) {
var request = getHTTPObject(xd);
if (request) {
	request.onreadystatechange = function() {
		displayResponse(request,div);
	};
	request.open("GET",file,true);
	request.send(null);
	}
}

function displayResponse(request,div) {
//alert(request.readystate);
if (request.readyState == 4) {
	document.getElementById(div).innerHTML = request.responseText;
}
}


function rptAjax(file,div,tmr)
{
	grabFile(file,div,div);
	setTimeout("rptAjax('" + file + "','" + div + "','" + tmr + "')",tmr); // Time in ms - 30000 = 30 Seconds
}

function moverSel(wt)
{
document.getElementById(wt).style.display='';
document.getElementById('carouselContent').style.display='none';
}

function moutSel(wt)
{
document.getElementById(wt).style.display='none';
document.getElementById('carouselContent').style.display='';
}

function addprod(prod)
{

}

function showdeadcenterdiv(Xwidth,Yheight,divid) {
// First, determine how much the visitor has scrolled

var scrolledX, scrolledY;
if( self.pageYoffset ) {
scrolledX = self.pageXoffset;
scrolledY = self.pageYoffset;
} else if( document.documentElement && document.documentElement.scrollTop ) {
scrolledX = document.documentElement.scrollLeft;
scrolledY = document.documentElement.scrollTop;
} else if( document.body ) {
scrolledX = document.body.scrollLeft;
scrolledY = document.body.scrollTop;
}

// Next, determine the coordinates of the center of browser's window

var centerX, centerY;
if( self.innerHeight ) {
centerX = self.innerWidth;
centerY = self.innerHeight;
} else if( document.documentElement && document.documentElement.clientHeight ) {
centerX = document.documentElement.clientWidth;
centerY = document.documentElement.clientHeight;
} else if( document.body ) {
centerX = document.body.clientWidth;
centerY = document.body.clientHeight;
}

// Xwidth is the width of the div, Yheight is the height of the
// div passed as arguments to the function:
var leftoffset = scrolledX + (centerX - Xwidth) / 2;
var topoffset = scrolledY + (centerY - Yheight) / 2;
// The initial width and height of the div can be set in the
// style sheet with display:none; divid is passed as an argument to // the function
var o=document.getElementById(divid);
var r=o.style;
r.position='absolute';
r.top = topoffset + 'px';
r.left = leftoffset + 'px';
//r.display = "block";
} 

function tog(a,b)
{
document.getElementById(a).style.display='none';
document.getElementById(b).style.display='';
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function add2cart(p)
{
if (getCookie('country') == '')
	{ 
	showdeadcenterdiv(500,200,'country');
	$('#country').fadeIn('fast');
	}
	else
	{
	showdeadcenterdiv(200,200,'country');
	grabFile('/scripts/setcountry.asp?prod='+p,'country','country');
	$('#country').fadeIn('fast');
	}
}

function numbersOnly(el)
{
el.value = el.value.replace(/[^0-9]/g, "");
}

function checkoutRecalc(p,c)
{

var oPrice = 0
for (x=0;x<document.chk.totalitems.value;x++)
	{
		oPrice = oPrice + parseFloat(document.chk['item_price_'+(x+1)].value)*parseFloat(document.chk['item_quantity_'+(x+1)].value)
	}
	
	oPrice = oPrice + parseFloat(document.chk.shipping.value);
	 
	document.getElementById('subtotal').innerHTML = "Sub Total (Ex. VAT):<br />" + c + " " + oPrice.toFixed(2);
}

function minusqty(p,c)
{
if (document.chk['item_quantity_'+c].value > 1)
	{
	document.chk['item_quantity_'+c].value = (document.chk['item_quantity_'+c].value -1)
	document.chk['item_totalweight_'+c].value = document.chk['item_unitweight_'+c].value * document.chk['item_quantity_'+c].value
	checkoutRecalc(c,p)
	shipcalc(document.chk.totalitems.value,p)
	}
}

function plusqty(p,c)
{
if (document.chk['item_quantity_'+c].value < 99)
	{
	document.chk['item_quantity_'+c].value = (+document.chk['item_quantity_'+c].value +1)
	document.chk['item_totalweight_'+c].value = document.chk['item_unitweight_'+c].value * document.chk['item_quantity_'+c].value
	checkoutRecalc(c,p)
	shipcalc(document.chk.totalitems.value,p)
	}
}

function shipcalc(t,c)
{
var weight = 0

var ShipAry = new String(document.chk.shipband.value);
var ShipAry = ShipAry.split(",");
var PriceAry = new String(document.chk.shipprice.value);
var PriceAry = PriceAry.split(",");
var shiptop = ShipAry.length;

for (y=0;y<t;y++)
	{
		weight += parseFloat(document.chk['item_totalweight_'+(y+1)].value)
	}

weight = weight.toFixed(2);

var index = 0;

while (ShipAry[index] < weight)
	{
		index++;
	}

document.getElementById('shipping').innerHTML = "Shipping Cost <br />" + c + ' ' + PriceAry[index];
document.getElementById('shippin').value = PriceAry[index];
document.chk.shipping.value = PriceAry[index];
checkoutRecalc('',c)
}