var gBrowser;

function Browser() {
  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.isOPERA = false;  // Opera
  this.version = null;
  this.size = '';

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOPERA = true;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

gBrowser = new Browser();


function setfocus(el){
e=document.getElementById(el);
if (e) e.focus();}

function getOptionFromSelect(selectName){
  // Get the current selection from a select object
  Options = document.getElementById(selectName).options;
  for (var i=0; i<Options.length;i++){
 	if (Options[i].selected){
	 return (Options[i].value);}
  }
  return '';
}

var win= null;
function NewWindow(mypage,myname,w,h,scroll){
var winl = (screen.width-w)/2;
var wint = (screen.height-h)/2;
var settings ='height='+h+',';
settings +='width='+w+',';
settings +='top='+wint+',';
settings +='left='+winl+',';
if (scroll){settings +='scrollbars='+scroll+',';}
settings +='resizable=yes';
win=window.open(mypage,myname,settings);
setTimeout('raiseWindow(win)', 1);
return win;
} 

function raiseWindow(win){win.focus();}

function MM_openBrWindow(theURL,winName,features) { 
window.open(theURL,winName,features);
}

function stripInputToDigits(inp){
var outp, i, c;
outp="";
for (i=0; i<inp.length;i++){
  c = inp.charAt(i);
  if (c >= '0' && c <= '9')
    outp=outp+c;
}
return outp;
}

function SplitDate(Value) {
 Separators=['/', '-', '.'];
 for (i=0; i< Separators.length; i++) {
  parts=Value.split(Separators[i]);
  if (parts.length == 3) {return parts;}
 }
 return false;
}

function ValidateStrDate(Value){
var parts, d, m,y;
/* Check if the value passed in is valid */
parts=SplitDate(Value);
if (parts === false) {return false;}
d=parseInt(parts[0],10);
m=parseInt(parts[1],10);
y=parseInt(parts[2],10);
if (d == NaN || m == NaN || y == NaN) {return false;}
if (y < 100) { y = y + 2000;}
/* Check the d/m */
if (m < 1 || m > 12) {return false;}
if (d <1 || d > 31) {return false;}
if (d == 31 && (m==9||m==4||m==6|m==11)){return false;}
if (m == 2) {
    if (d>29) {return false;}
    if (d==29 && (y % 4 != 0)){return false;}
    if (d==29 && (y % 400 == 0)){return false;}
}
return true;
}

function FormatStrDate(Value){
var parts, d, m,y;
if (!ValidateStrDate(Value)){return Value;}
parts=SplitDate(Value);
d=parseInt(parts[0],10);
m=parseInt(parts[1],10);
y=parseInt(parts[2],10);
if (y < 100) { y = y + 2000;}
if (d < 10) {d='0'+d.toString()};
if (m < 10) {m='0'+m.toString()};
return d+'/'+m+'/'+y.toString();
}


function ValidateMobile(Value, Verbose, FieldPrompt, FieldMandatory){
    var MESSAGE = FieldPrompt + ' is a mobile phone number. \n\n You must enter your number complete with the prefix, e.g. 083, 085, 086, 087!';

    if (Value.length > 8){
        if (Value.charAt(0) == '+'){
           Value = '00'+Value.substring(1, Value.length);
           return Value;
        }
    }

    Value = stripInputToDigits(Value);

    if (Value.length == 0){
       if (FieldMandatory == 'Y'){
         rv = confirm(FieldPrompt + ' is required. Please enter a valid value');
         return false;
       }
       return Value;
    }

    if (Value.length < 10){
        if (Verbose){
            alert(MESSAGE);
            return false;
        }
        return Value;
    }
    if (Value.substring(0,5) == '00353'){
        Value = Value.substring(5, Value.length);
    } else if (Value.substring(0,2) == '00'){
        /* International code - assume correct */
        return Value;
    } else if (Value.substring(0,3) == '353') {
        Value = Value.substring(3, Value.length);
    }
    if (Value.charAt(0) != '0'){
       Value = '0'+Value;
    }
    prefix = Value.substring(0,3);
    if (prefix != '083' && prefix != '085' && prefix != '086' && prefix != '087' && prefix != '088' && prefix != '089'){
      if (Verbose){
        alert(MESSAGE);
        return false;
      }
      return Value;
    }
    if (Value.length < 10){
      if (Verbose){
        alert(MESSAGE);
        return false;
      }
      return Value;
   }
   return Value;
}


function ValidateTescoCC(Value, Verbose, FieldPrompt, FieldMandatory){
    var MESSAGE = 'Your TESCO ClubCard Number must be 16 digits long';

    Value = stripInputToDigits(Value);
    if (Value.length == 0){
       if (FieldMandatory == 'Y'){
         rv = confirm(FieldPrompt + ' is required. Please enter a valid value');
         return false;
       }
       return Value;
    }
    if (Value.length != 16){
        if (Verbose){
            alert(MESSAGE);
            return false;
        }
        return Value;
    }
   return Value;
}

// Log the client out if not touched for 20 minutes
var tID = null;
function resetTimer(e) {
  if (tID){
     clearTimeout(tID);} // reset the timer
  tID = setTimeout('executeTimer()',1200000);
}
function executeTimer() {
  location.href = 'https://www.newaddress.ie/logout.html';
}


//------------------------------------------------------------
// Please keep copyright lines in place without changes.
//   Copyright 2000 William and Mari Bontrager
//   Copyright 2003 Bontrager Connection, LLC
//
// For more information and instructions, please 
//    see the "Double Click Trapper" article at 
//    http://willmaster.com/possibilities/archives

TrapClicksForHowManySeconds = 5;

// If the user clicks the submit button and causes a 
//    trap, or clicks when a trip is set, you may spawn 
//    an alert box with a message. If you do want to 
//    spawn the alert box, specify the message between 
//    the quotation marks. Otherwise, leave it blank.
//(Blank is two consecutive quotation mark characters, 
//    with nothing between them.)
// Note: If you use any quotation marks in the message 
//       itself, those must be preceeded with a back-slash 
//       character. Example: "My name is \"tall\", okay?"

AlertBoxMessage = "One click is sufficient. The server should respond momentarily.\n\n\nClick the \"OK\" button.";

// If the user clicks the submit button and causes a trap, 
//    or clicks when a trip is set, you may spawn a popup 
//    box. If you want to spawn the popup box, specify 
//    the URL of the web page to insert into the popup. 
//    Otherwise, leave it blank. You may also specify 
//    the height of the popup and the width of the popup.

PopUpBoxURL = "";
PopUpBoxHeight = 300;
PopUpBoxWidth = 500;

// If you want the submit button to change its text when 
//    it is clicked, specify the name you assigned to the 
//    form, the name you assigned to the submit button 
//    form field, and the new text for the submit button. 
//    Otherwise, leave at least one of these blank.

//NameOfForm = "mainform";
//NameOfSubmitButton = "submitter";
//NewTextForSubmitButton = "Now Processing . . .";
NameOfForm = "";
NameOfSubmitButton = "submitter";
NewTextForSubmitButton = "Now Processing . . .";



// ////////////////////////////////////////// //
// NO CUSTOMIZATION REQUIRED BELOW THIS POINT //
// ////////////////////////////////////////// //


TrapTime = 0;
DoubleClickTrapperCounter = 0;


function DoubleClickTrapperAction() {
DoubleClickTrapperCounter++;
DoubleClickTrapperButton();
var trapTheClick = false;
if(DoubleClickTrapperCounter > 1 ) { trapTheClick = true; }
else{var tDate = new Date;TrapTime = tDate.valueOf();}
if(trapTheClick == true) {
	if(TrapTime == 0) {
		var tDate = new Date;
		TrapTime = tDate.valueOf();
		}
	else {
		var tDate = new Date;
		var localTrapTime = tDate.valueOf();
		if((localTrapTime - TrapTime) > (TrapClicksForHowManySeconds * 1000)) {
			TrapTime = 0;
			trapTheClick = false;
			DoubleClickTrapperCounter = 0;
			}
		}
	}
var valueToReturn = true;
if(trapTheClick == true) {
	valueToReturn = false;
	DoubleClickTrapperPopUp();
	DoubleClickTrapperAlert();
	}
return valueToReturn;
} // end of function DoubleClickTrapperAction()


function DoubleClickTrapperButton() {
var formname   = StripSpaces(NameOfForm);
var submitname = StripSpaces(NameOfSubmitButton);
var newtext    = StripSpaces(NewTextForSubmitButton);
if(formname.length > 0 && submitname.length > 0 && newtext.length > 0) {
	var s = 'document.' + NameOfForm + '.' + NameOfSubmitButton + ".value = '" + NewTextForSubmitButton + "'";
	eval(s);
	}
} // end of function DoubleClickTrapperButton()


function DoubleClickTrapperPopUp() {
var url = StripSpaces(PopUpBoxURL);
if(url.length > 0) {
	if(PopUpBoxHeight < 1) { PopUpBoxHeight = 200; }
	if(PopUpBoxWidth < 1) { PopUpBoxWidth = 300; }
	window.open(url,'',('height=' + PopUpBoxHeight + ',width=' + PopUpBoxWidth + ',resizable=yes,scrollbars=yes'));
	}
} // end of function DoubleClickTrapperPopUp()


function DoubleClickTrapperAlert() {
var message = StripSpaces(AlertBoxMessage);
if(message.length > 0) { alert(AlertBoxMessage); }
} // end of function DoubleClickTrapperAlert()


function StripSpaces(s) {
while(s.indexOf(' ') == 0) { s = s.substr(1); }
return s;
} // end of function StripSpaces()

function keyDown2() {
if(!gBrowser.isNS && window.event.keyCode == 13){
window.event.keyCode=9;}
return true;
}

function InitialiseTabs(){
for (var fi = 0; fi < document.forms.length; fi++){
for (var i = 0; i<document.forms[fi].elements.length; i++) {
el = document.forms[fi].elements[i];t = el.type;
if (t == 'text')el.onkeydown = keyDown2;}}
}
if(!gBrowser.isNS){
window.onload = InitialiseTabs;
}
function SetPageTimeout(){
 if (gBrowser.isIE == false){
    window.captureEvents(Event.MOUSEMOVE);}
 document.body.onmousemove = resetTimer;
if(!gBrowser.isNS){InitialiseTabs();}
}

function entsub(event,ourform) {
if (gBrowser.isIE){if (window.event && window.event.keyCode == 13){ourform.submit();}}
else if (event && event.which == 13){ourform.submit();}
else return true;}

function GetEl(id){return document.getElementById(id);}

