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 changeimg(img)
{
document.getElementById('img').style.backgroundImage = "url('http://www.ocvision.co.uk/cms/img/500/"+img+"')";
}

function dispteam(skipper,boat)
{
document.getElementById('teamname').innerHTML = "<strong>" + unescape(boat) + "</strong>" + unescape(skipper);
}

function openconsole(num)
{
window.open('console/default.asp?z='+num,'console','width=700,height=560,toolbars=no,scrollbars=auto,location=no,status=no');
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function hidebubble(div) {
	if (document.getElementById) { 
		thecookie=getCookie('survey')
		
		if (thecookie.length==0) {
			thecookie='survey';
		}
		setCookie('survey',thecookie,365)
		
		document.getElementById(div).style.display='none';		
	} 
}

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 setCookie(c_name,value,expiredays) {
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires=" +exdate.toGMTString())
}
function checkCookie(div, chkbox) {
	if (document.getElementById(div) != null) {
		thecookie=getCookie('survey')
		if (thecookie !=null && thecookie != "" && thecookie.length != 0) {
			document.getElementById(div).style.display='none';
		} else {
			document.getElementById(chkbox).checked=false;
			document.getElementById(div).style.display='block';
		}
	}
}
