 
var popUpWin = false;

function closePopups()
{
	if (popUpWin) popUpWin.close();
}

function numbersOnly(e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if ((keycode == 13) || ((keycode >= 48) && (keycode <= 57))) { return true; }
	else return false;
}

function popUpPage(url)
{
	if (popUpWin.open) popUpWin.close();
	
	popUpWin = window.open(url, "popupwin", "width=650,height=500,scrollbars=1,toolbar=1")
}

function popUpImage(imagePath, width, height, windowTitle, scroll)
{
	windowTitle = windowTitle || "Balfour Beatty BRail";
	closePopups();
	popUpWin = window.open("", "popupwin", "width=" + width + ",height=" + height + ",left=" +( (screen.width / 2) - (width / 2)) + ",top=" + ((screen.height / 2) - (height / 2)) + ",scrollbars=" + ((scroll) ? 1 : 0));
	popUpWin.document.open();
	popUpWin.document.write("<html><head><title>" + windowTitle + "</title></head><body bottommargin=0 topmargin=0 leftmargin=0 rightmargin=0 marginwidth=0 marginheight=0><table border=0 cellspacing=0 cellpadding=0 width=100% height=100%><tr><td align=center valign=middle><img src='" + imagePath + "' alt='" + windowTitle + "'></td></tr></table></body></html>");
	popUpWin.document.close();
}
function isValidEmail(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}
var FieldList = "";
var checkboxMessage=""
var HighLightColour = '#cedae6';
var HighlightBorder = '1px solid #074b88';
var OrigBG = '#FFFFFF';
var OrigBorder = '1px solid #a7a6aa';
var OrigPadding =  '2px';
function checkCompulsory(form) {
    var keyword="_compulsory";
    if (document.images) {
        for (i=0;i<form.length;i++) {
            var tempobj=form.elements[i]; 
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString()) {
                shortFieldName=tempobj.name.substring(0,tempobj.name.length-keyword.length);
                shortFieldName=shortFieldName.slice(5);
                shortFieldName=shortFieldName.replace(/_/g," ")
                if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&tempobj.selectedIndex==0)) {
                    FieldList += shortFieldName + "\n";
                    tempobj.style.backgroundColor = HighLightColour;
                    tempobj.style.border = HighlightBorder;
                    tempobj.style.padding = OrigPadding;
                } else if (tempobj.type=="checkbox"&&!tempobj.checked){
                    if (checkboxMessage==''){
                        checkboxMessage='\nPlease ensure that you have selected:\n';
                    }
                    tempobj.style.backgroundColor=HighLightColour
                    checkboxMessage += shortFieldName+"\n";
                }  else  {
					tempobj.style.backgroundColor=OrigBG;
					tempobj.style.border=OrigBorder;
					tempobj.style.padding = OrigPadding;
                }
            }
        }
        
    }
}
function titlecase(form) {
    if (document.images) {
        for (i=0;i<form.length;i++) {
            var keyword = "_titlecase";
            var tempobj=form.elements[i];
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString()) {
                shortFieldName=tempobj.name.substring(0,tempobj.name.length-keyword.length);
                tempobj.name = shortFieldName;
                
                var newtempobj = (tempobj.value.substring(0,1)).toUpperCase() + tempobj.value.substring(1);
                tempobj.value = newtempobj;
            }
    }}
}
function uppercase(form) {
    if (document.images) {
        for (i=0;i<form.length;i++) {
            var keyword = "_uppercase";
            var tempobj=form.elements[i];
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString()) {
                shortFieldName=tempobj.name.substring(0,tempobj.name.length-keyword.length);
                tempobj.name = shortFieldName;
                
                var newtempobj = (tempobj.value.substring(0)).toUpperCase();
                tempobj.value = newtempobj;
            }
    }}
}
function lowercase(form) {
    if (document.images) {
        for (i=0;i<form.length;i++) {
			var keyword = "form_";
			var tempobj=form.elements[i];
			if(tempobj.type=="text"||tempobj.type=="textarea"){
			if (tempobj.name.substring(0,(keyword.length))==keyword.toString()) {
                    var tempobj=form.elements[i];
                          var newtempobj = (tempobj.value.substring(0)).toLowerCase();
                tempobj.value = newtempobj;   
			}}
    }}
}
function telephone(form) {
    if (document.images) {
        for (i=0;i<form.length;i++) {
            var keyword = "_Telephone_Number";
            var tempobj=form.elements[i];
            if (tempobj.name.substring((tempobj.name.length-keyword.length),(tempobj.name.length))==keyword.toString()) {
                var newtempobj = tempobj.value.substring(0,5)+" "+tempobj.value.substring(5,(tempobj.value.length));
                tempobj.value = newtempobj;
            }
    }}
}
function ValidateForm() {
    var emailMessage = "";
    alertMessage = "We did not get all the information we need from you.\n Please fill in the following field(s):\n\n";
    lowercase(document.contactForm);
	titlecase(document.contactForm);
    uppercase(document.contactForm);
	telephone(document.contactForm);
    checkCompulsory(document.getElementById('contactForm'));
    if (document.getElementById('form_Email_compulsory').value == ""){
        } else {
        validEmail = isValidEmail(document.getElementById('form_Email_compulsory').value);
        if (!validEmail) {
            document.getElementById('form_Email_compulsory').style.backgroundColor=HighLightColour;
            document.getElementById('form_Email_compulsory').style.border = HighlightBorder
            document.getElementById('form_Email_compulsory').style.padding = OrigPadding;
            emailMessage = "\nPlease enter a valid email address"; // Message to user.
        } else {
        	document.getElementById('form_Email_compulsory').style.backgroundColor=OrigBG;
			document.getElementById('form_Email_compulsory').style.border=OrigBorder;
			document.getElementById('form_Email_compulsory').style.padding = OrigPadding;
        }
    };
    if (FieldList == "" && emailMessage == ""){
        document.getElementById('contactForm').submit();
    } else {;
        if (FieldList != "") {
            alertMessage += FieldList+checkboxMessage;
        }
        if (emailMessage != "") {
            alertMessage += emailMessage;
        }
        FieldList = ''
        checkboxMessage = ''
        alert(alertMessage);
    };
}
