if (window) window.onError = null;            // suppress errors

function rand(n)  {
  x = Math.random()
  return (Math.floor(x*n) + 1);
}

var months = new Array (" ", "January", "February", "March", "April",
                        "May", "June", "July", "August", "September",
                        "October", "November", "December") 

function writeToday () {
  var today = new Date();
  var m = today.getMonth() + 1;
  var d = today.getDate();
  var y = today.getFullYear(); 

  document.write (d +  " " + months[m] +  " " + y)
}

function writeTodayShort () {
  var today = new Date();
  var m = today.getMonth() + 1;
  var d = today.getDate();

  document.write (d +  " " + months[m])
}

function writeDate (n) {
  var temp = n.split('-');
  var m = parseInt(temp[0], 10);
  var d = parseInt(temp[1] + "a", 10);
  var y = parseInt(temp[2], 10); 

  document.write (d +  " " + months[m] +  " " + y)
}

function writeDateCode (s) {
  var x = parseInt(s, 10);
  var m = parseInt(x/100);
  var d = x%100;
  document.write (d +  " " + months[m])
}

function showPrefixedContent(prefix, content) {
  if (content != "") {
    document.write (prefix + ": " + content + "<br>")
  }
}

// opening and closing popup windows

var remote = null;

function launchRemote (theFile) {
  remote = window.open("", "popup",
    "left=100,top=100,screenX=100,screenY=100,width=300,height=200");
  remote.location.href = theFile
  remote.focus()
}

function closeRemote () {
  if (remote && !remote.closed) {
    remote.close(); remote = null;
  }
}

function ShowRecommend(){
  var url = "http://www.icc-europe.org/scripts/" +  
            "csRecommend/csRecommend.cgi?command=showrecommend&page=" + 
            escape(window.location);
  h=500; w=500;
newWindow = window.open(url,'search','width='+w+',height='+h+',top='+(screen.height-h)/2+',left='+(screen.width-w)/2+',location=0,directories=0,status=no,menuBar=no,scrollBars=no,resizable=no');
  newWindow.focus();
}

// windows for feature displays

var CEDirectory = new Array();
CEDirectory["games"]     = "/CRICKETEUROPE/GENERAL/GAMES/";

var feature = null;

function launchFeature (featureType, theFile) {
  feature = window.open("", featureType,
           "left=5,top=5,screenY=5,screenX=5,width=650,height=540,status=yes");
  feature.location.href = CEDirectory[featureType] + theFile
  feature.focus()
}

var ECCDirectory = new Array();
ECCDirectory["quiz"]      = "/ECC/DATABASE/FEATURES/QUIZZES/";
ECCDirectory["games"]     = "/ECC/DATABASE/FEATURES/GAMES/";

function launchFeatureECC (featureType, theFile) {
  feature = window.open("", featureType,
           "left=10,top=10,screenY=10,screenX=10,width=650,height=480,status=yes");
  feature.location.href = ECCDirectory[featureType] + theFile
  feature.focus()
}

// loading pages selected from pull-down lists

function goPage(theuRL) {  
  window.top.location.href = theuRL;
}

function gotoPage(f) {  
  var selectedValue = f.options[f.selectedIndex].value;
  if (selectedValue != "") {
    window.top.location.href = selectedValue;
  }
  f.selectedIndex=0;
}

// Google Mapping

function launchGoogleMapWindow (theFile) {
  var width  = 650;
  var height = 510;
  var left = parseInt((screen.availWidth/2) - (width/2));
  var top  = parseInt((screen.availHeight/2) - (height/2));
  var windowFeatures = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top; 
  var remote = window.open("", "mapWindow", windowFeatures);
  remote.location.href = theFile
  remote.focus()
}


    function drawGoogleMap (latitude, longitude, annotation) {
      var map = new GMap(document.getElementById("map"));
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      map.centerAndZoom(new GPoint(longitude, latitude), 4);

      var point = new GPoint(longitude, latitude);
      var marker = new GMarker(point);
 
      // Show the ground details in the info window when the marker is clicked
      var html = "<b>" + annotation + "</b>";
      GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
      map.addOverlay(marker);
    }


// **** general-purpose cookie functions

var expiryDate = new Date();
expiryDate.setTime(expiryDate.getTime() + (24*60*60*1000*365)); 

function getCookie(name){
var cname = name + "=";               
var dc = document.cookie;             
    if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
        if (begin != -1) {           
        begin += cname.length;       
        end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
        } 
    }
  return "";
}

function setCookie(name, value) {
  document.cookie = name + "=" + escape(value) + "; path=/" +
                    "; expires=" + expiryDate.toGMTString();
}

function deleteCookie(name) {
  document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}

var userCookie    = "user_1";
var mailCookie    = "mail_1";

// e-mail address validation

function checkEmail(addressGiven) {
     if (addressGiven == "" ||
         addressGiven.indexOf("@")  == -1 ||
         addressGiven.indexOf(".")  == -1)  {
        alert("Please enter your email address correctly");
       return false
     }
    return true
 }

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;
}

return true;
}


