


// Pop Up Window

function openwindow(mywindow){

window.open(mywindow,"callback","status=no,toolbar=no,scrollbars=no,left=116,top=250,width=400,height=480");

}

// Centred Pop Up

var width = 400;
var height = 450;
var version4 = (navigator.appVersion.charAt(0) == "4"); 
var properties = 'height=' + height + ',width=' + width + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';

function displayPopup(evnt) {

var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;

if(navigator.appName == "Microsoft Internet Explorer") {
screenY = document.body.offsetHeight;
screenX = window.screen.availWidth;
}
else {
screenY = window.outerHeight
screenX = window.outerWidth
}
leftvar = (screenX - width - 670) / 2;
rightvar = (screenY - height + 63) / 2;
if(navigator.appName == "Microsoft Internet Explorer") {
leftprop = leftvar;
topprop = rightvar;
}
else {
leftprop = (leftvar - pageXOffset);
topprop = (rightvar - pageYOffset);
   }
if(evnt != null) {
properties = properties + ", left = " + leftprop;
properties = properties + ", top = " + topprop;
}
popupHandle = open('/nature-events/ssi/contact/call-back.html','popup3',properties);
}

// Call-back Form validation

var errors = "";

function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

function verify(f) {
    var msg;
    var empty_fields = "";
    var checked_count = 0;
    
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        if (((e.type == "text") || (e.type == "textarea") || (e.type == "select-one") || (e.type == "checkbox")) && !e.optional) {
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name.substr(9,1).toUpperCase() + e.name.substr(10);
                continue;
            }

            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field \"" + e.name.substr(9,1).toUpperCase() + e.name.substr(10) + "\" must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }

            }

	    if (e.email) {
		emailCheck(e.value);
            }

	     if (e.checked) {
                checked_count++;
	     }
        }
    }

    if ((f.checkbox_check != null) && (checked_count == 0) && (f.checkbox_check.value == 1)) errors += "- Please chose one or more checkbox options.\n";

    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    errors = "";
    return false;
}

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) {
	errors += "- The field \"Email address\" seems incorrect (check @ and .'s).\n";
	return false;
    }

    var user=matchArray[1];
    var domain=matchArray[2];

    for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
		errors += "- Email address username contains invalid characters.\n";
		return false;
  	 }
    }

    for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
		errors += "- The email address domain name contains invalid characters.\n";
		return false;
 	}
    }

    if (user.match(userPat)==null) {
	errors += "- The email address username doesn't seem to be valid.\n";
	return false;
    }

    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
	for (var i=1;i<=4;i++) {
		if (IPArray[i]>255) {
			errors += "- Email address destination IP address is invalid.\n";
			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) {
		errors += "- The email address domain name does not seem to be valid.\n";
		return false;
 	}
    }

    if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
	errors += "- There is a problem with the last part of the email address.\n";
	return false;
    }

    if (len<2) {
	errors += "- Email address is missing a hostname.\n";
	return false;
    }
    return true;

}

// check all checkboxes

function checkAll(box) {
for (var j = 0; j < box.length; j++) {
if (box[j].checked == false)
	box[j].checked = true;
else
	box[j].checked = false;
   }
}

