﻿var clearFields = function(fld, DefValue)
{
	if (document.getElementById(fld).value == DefValue)
	{
		document.getElementById(fld).style.fontStyle="normal";
		document.getElementById(fld).value="";
	}
};

//--

var resetFields = function(fld,DefValue)
{
	if(document.getElementById(fld).value == "")
	{
		document.getElementById(fld).style.fontStyle="italic";
		document.getElementById(fld).value=DefValue;
	}
};

//--	

function closeWindow()
{
    if (top.opener && !top.opener.closed) {top.opener.location.reload();}
	top.window.close();
}

//--

function coords()
{
	this.x;
	this.y;
	this.width;
	this.heigth;
	this.calculate = function(){
		var intWidth = this.width;	
		var intHeight = this.height;
		if (navigator.appName=="Netscape"){
			intTopDiff=(screen.height/10);
			intXPos=(screen.width/2)-(intWidth/2);
			intYPos=((screen.height/2)-(intHeight/2))-intTopDiff;
	
		}else{
			intXPos=(screen.availWidth/2)-(intWidth/2);
			intYPos=(screen.availHeight/2)-(intHeight/2);
		}
		this.x=intXPos>0?intXPos:0;
		this.y=intYPos>0?intYPos:0;
	}
}

//--

function validateDate(dateIn, monthIn, yearIn, strName) 
{
    var bValid = true
    if (parseInt(yearIn, 10) < 1753) {
        alert("You've entered a year earlier than 1753. Unfortunately we can only support dates after 1753. Please re-enter a more recent date.")
        return false;
    }
    if (isNaN(dateIn) || isNaN(monthIn) || isNaN(yearIn)) {
        alert("Part of " + strName + " is not in the right format (dd/mm/yyyy)");
        return false;
    }
    if (dateIn.length == 1) {
        dateIn = "0" + dateIn;
    }
    if (monthIn.length == 1) {
        monthIn = "0" + monthIn;
    }
    var textStr = dateIn + "/" + monthIn + "/" + yearIn;
    var chkDate = new Date();
    if (dateIn.indexOf("0") == 0) {
        dateIn = dateIn.substring(1);
    }
    if (monthIn.indexOf("0") == 0) {
        monthIn = monthIn.substring(1);
    }
    chkDate.setMonth((parseInt(monthIn) - 1), (parseInt(dateIn)));
    chkDate.setFullYear(parseInt(yearIn));
    var chkMonthInt = chkDate.getMonth() + 1;
    var chkMonthStr = chkMonthInt + "/";
    if (chkMonthStr.length == 2) {
        chkMonthStr = "0" + chkMonthStr;
    }
    var chkDateInt = chkDate.getDate();
    var chkDateStr = chkDateInt + "/";
    if (chkDateStr.length == 2) {
        chkDateStr = "0" + chkDateStr;
    }
    var cmpDate = chkDateStr + chkMonthStr + (chkDate.getFullYear());
    if (cmpDate.length != 10) bValid = false;
    else {
        if (textStr != cmpDate) bValid = false;
        else if (cmpDate == "NaN/NaN/NaN") bValid = false;
    }
    //handle 29th feb in a leap year
    if ((dateIn == 29) && (monthIn == 2) && (isLeapYear(yearIn))) bValid = true;
    if (!bValid) {
        alert("You've entered a " + strName + " that does not exist or used an invalid date format.\n\nPlease use the dd/mm/yyyy format.");
        return false;
    }
    else 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 loginPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		alert("The e-mail address seems to be incorrect!\n\nPlease check that you have included the correct @ and . symbols.");
		return false;
	}

	var login=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<login.length; i++) {
		if (login.charCodeAt(i)>127) {
		alert("The e-mail address contains invalid characters.");
		return false;
			}
	}

	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
		alert("The domain name part of your e-mail address\n (after the @ sign) contains invalid characters.");
		return false;
			}
	}

	if (login.match(loginPat)==null) {
		alert("The e-mail address does not 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("The IP address you have entered after the @ sign in your e-mail\naddress 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 part of your e-mail address (after the @ sign)\ndoes not seem to be valid.");
		return false;
			}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	alert("The e-mail address must end in a well-known domain\ne.g. domain.com, domain.net, domain.org etc.\nor two letter " + "country code e.g. domain.co.uk,\ndomain.org.uk etc.");
	return false;
	}

	if (len<2) {
	alert("The e-mail address does not have a valid domain name!");
	return false;
	}
	return true;
}

//--

function isEmpty(value2check){
	if (value2check == "" || value2check == null){
		return true;
	}
	//nice bit of regexp!
	var nonSpaceCharacters = /\w/;
	hasCharacters = nonSpaceCharacters.exec(value2check);
	
	if (hasCharacters == null){
		return( true );
	}
	
	return( false );
}

//--

function openWindow(url,height,width,winObj,blnReturn)
{
	var objCoords = new coords();
	objCoords.width = width;
	objCoords.height = height;
	objCoords.calculate();
	var intXPos = objCoords.x;
	var intYPos = objCoords.y;
	strProperties="location=no,menubar=no,resizable=yes,status=no,toolbar=no,titlebar=no,";
	strProperties+="directories=no,scrollbars=yes,toolbar=0,screenX="+intXPos+",left="+intXPos;
	strProperties+=",screenY="+intYPos+",top="+intYPos+",width="+width+",height="+height;
	winObj=window.open(url,"" + winObj + "",strProperties);
	winObj.focus();
	if(blnReturn) return winObj;
}

//--

function openWindow800(url,winObj)
{
	openWindow(url,500,795,winObj)
}

//--

function performAction(inStr)
{
	if (inStr != "null"){
		eval(inStr);
	}
}

//--

function Trim(sString)
{
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

//--

function isValidFileType(strFileName, arrExt) {
    if (!strFileName.length) {
        return (false);
    }
    strFileName = strFileName.toLowerCase();
    for (var j = 0; j < arrExt.length; j++) {
        if (strFileName.substring(strFileName.length, strFileName.indexOf(arrExt[j].toLowerCase())) == arrExt[j].toLowerCase()) {
            return (true);
        }
    }
    return (false);
}
