  var xmlhttp = "";

    function addEvent(obj, evt, fnc) {
        if (obj.addEventListener) {
            obj.addEventListener(evt, fnc, false);
        } else if (obj.attachEvent) {
            obj.attachEvent("on" + evt, fnc);
        } else {
            return false;
        }
        return true;
    }


    function removeEvent(obj, evt, fnc) {
        if (obj.removeEventListener) {
            obj.removeEventListener(evt, fnc, false);
        } else if (obj.detachEvent) {
            obj.detachEvent("on" + evt, fnc);
        } else {
            return false;
        }
        return true;
    }


    function appendElement(node, tag, id, htm) {
        var ne = document.createElement(tag);
        if (id) {
            ne.id = id;
        }
        if (htm) {
            ne.innerHTML = htm;
        }
        node.appendChild(ne);
    }


    function showPopup(p,dept) {
        greyout(true);
        document.getElementById(p).style.display = "block";
         var restoreContent = "<fieldset style=\"position:absolute;left:10px;top:10px;bottom:10px;bottom:5px\\9;right:10px;!border:none;\">"   //;background-color:#F0F0F0;
        restoreContent += "<legend style=\"*display:none;!display:block;\">Send a message to E. Rex Makin &amp; Co.</legend>";
        restoreContent += "<h2 style=\"visibility:hidden;\">Send a message to E. Rex Makin &amp; Co.</h2>";
        restoreContent += "<table style=\"position:absolute;left:10px;top:10px;bottom:10px;right:0px;\">";
        restoreContent += "<tr>";
        restoreContent += "<td width=\"290px\">";
        restoreContent += "<p style=\"padding-top:20px\\9;\"><div id=\"forenameLabel\">First Name:</div>";
        restoreContent += "<input class=\"text\" type=\"text\" id=\"name\" name=\"name\" size=\"25\" maxlength=\"30\"/>";
        restoreContent += "<div id=\"surnameLabel\">Surname:</div>";
        restoreContent += "<input class=\"text\" type=\"text\" id=\"surname\" name=\"surname\" size=\"25\" maxlength=\"30\"/></p>";
        restoreContent += "</td>";
        restoreContent += "<td width=\"200px\">";
        restoreContent += "<div><p style=\"padding-top:20px\\9;\"><div id=\"mailLabel\">E-Mail: </div>";
        restoreContent += "<input class=\"text\" type=\"text\" id=\"mail\" name=\"mail\" size=\"25\" maxlength=\"40\"/>";
        restoreContent += "<div id=\"numberLabel\">Phone Number:</div>";
        restoreContent += "<input class=\"text\" type=\"text\" id=\"number\" name=\"number\" size=\"25\" maxlength=\"40\"/></p></div>";
        restoreContent += "</td></tr><tr><td colspan=2>";
        restoreContent += "<div id=\"departmentLabel\">Department:</div>";
        restoreContent += "<select id=\"department\" name=\"department\" class=\"department\">";
       
       var depArr=new Array();
		depArr[0]="business";
		depArr[1]="crime";
		depArr[2]="employment";
		depArr[3]="family";
		depArr[4]="injury";
		depArr[5]="property";
		depArr[6]="public";
		depArr[7]="wills";
		depArr[8]="general";
		depArr[9]="management";
		
	var depArr1=new Array();
		depArr1[0]="Business and Civil Litigation";
		depArr1[1]="Crime";
		depArr1[2]="Employment";
		depArr1[3]="Family Law";
		depArr1[4]="Personal Injury";
		depArr1[5]="Property Law";
		depArr1[6]="Public Law";
		depArr1[7]="Wills, Probate and Trusts";
		depArr1[8]="General";
		depArr1[9]="Management";	
		
	for (var arrItem in depArr){
	
		if(dept==depArr[arrItem]){

		restoreContent +="<option value=\""+depArr[arrItem]+"\" selected=\"selected\">"+depArr1[arrItem]+"</option>";
				
		}else{
		restoreContent +="<option value=\""+depArr[arrItem]+"\">"+depArr1[arrItem]+"</option>";
		}
            
        }    
                     
        
        
        
        
        restoreContent += "</select>";
        restoreContent += "<p id=\"messageLabel\" style=\"padding:0px\\9;\">Message:";
        restoreContent += "<textarea rows=4 style=\"width:99%;height:95%;\" id=\"message\"></textarea></p>";
        restoreContent += "<div style=\"float:right;margin-right:2px\\9;margin-bottom:3px;\">";
        restoreContent += "<input type=\"button\" value=\"Cancel\" onClick=\"hidePopup('popup1',false)\" class=\"res\">";
        restoreContent += "<input style=\"margin-left:10px;\" type=\"button\" value=\"Send\" class=\"sub\" onclick=\"terms(document.getElementById('name').value,document.getElementById('surname').value,document.getElementById('mail').value,document.getElementById('number').value,document.getElementById('department').value,document.getElementById('message').value);\">";
        restoreContent += "</div>";
        restoreContent += "</td></tr></table>";
        restoreContent += "</fieldset>";
        document.getElementById(p).innerHTML = restoreContent;
    }


    function hidePopup(p) {
        greyout(false);
        document.getElementById(p).style.display = "none";
        
    }


    function greyout(d, z) {
        var obj = document.getElementById("greyout");
        if (!obj) {
            appendElement(document.body, "div", "greyout");
            obj = document.getElementById("greyout");
            obj.style.position = "absolute";
            obj.style.top = "0px";
            obj.style.left = "0px";
            obj.style.background = "#111";
            obj.style.opacity = ".3";
            obj.style.filter = "alpha(opacity=30)";
        }
        if (d) {
            var ch = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
            var cw = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
            var sh = document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;
            if (document.body.scrollHeight) {
                sh = Math.max(sh, document.body.scrollHeight);
            }
            var sw = document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth;
            if (document.body.scrollWidth) {
                sh = Math.max(sh, document.body.scrollWidth);
            }
            var wh = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
            if (!z) {
                z = 50;
            }
            obj.style.zIndex = z;
            obj.style.height = Math.max(wh, Math.max(sh, ch)) + "px";
            obj.style.width = Math.max(sw, cw) + "px";
            obj.style.display = "block";
            addEvent(window, "resize", greyoutResize);
        } else {
            obj.style.display = "none";
            removeEvent(window, "resize", greyoutResize);
        }
    }


    function greyoutResize() {
        var ch = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
        var cw = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
        var sh = document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight;
        if (document.body.scrollHeight) {
            sh = Math.max(sh, document.body.scrollHeight);
        }
        var sw = document.documentElement.scrollWidth ? document.documentElement.scrollWidth : document.body.scrollWidth;
        if (document.body.scrollWidth) {
            sh = Math.max(sh, document.body.scrollWidth);
        }
        var wh = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
        var obj = document.getElementById("greyout");
        obj.style.height = ch + "px";
        obj.style.width = cw + "px";
        obj.style.height = Math.max(wh, Math.max(sh, ch)) + "px";
        obj.style.width = Math.max(sw, cw) + "px";
    }

    var error = 0;

    function checkBlank(inDat, label) {
        if (inDat == "") {
            document.getElementById(label).style.color = "red";
            document.getElementById(label).style.fontWeight = "bold";
            error++;
        } else {
            document.getElementById(label).style.color = "black";
            document.getElementById(label).style.fontWeight = "normal";
        }
    }


    function validateEmail(field, label) {
        apos = field.indexOf("@");
        dotpos = field.lastIndexOf(".");
        if (apos < 1 || dotpos - apos < 2) {
            document.getElementById(label).style.color = "red";
            document.getElementById(label).style.fontWeight = "bold";
            error++;
        } else {
            document.getElementById(label).style.color = "black";
            document.getElementById(label).style.fontWeight = "normal";
        }
    }


    function checkNumber(numb, label) {
        var valid = "0123456789 -+";
        var char = "";
        var stat = true;
        if (numb.length < 6) {
            stat = false;
        }
        for (i = 0; i < numb.length; i++) {
            char = numb.charAt(i);
            if (valid.indexOf(char) == -1) {
                stat = false;
            }
        }
        if (stat == false) {
            document.getElementById(label).style.color = "red";
            document.getElementById(label).style.fontWeight = "bold";
            return stat;
            
            
        } else {
            document.getElementById(label).style.color = "black";
            document.getElementById(label).style.fontWeight = "normal";
            return stat;
        }
    }


    function terms(fnm, snm, mail, num, dep, msg) {
    
   
   	

   	msg = msg.replace(/['"]/g,'');
   
   	msg = msg.replace(new RegExp( "\\n", "g" )," ... ");
   
   
        checkBlank(fnm, "forenameLabel");
        checkBlank(snm, "surnameLabel");
        checkBlank(mail, "mailLabel");
        validateEmail(mail, "mailLabel");
        checkBlank(num, "numberLabel");
        //checkNumber(num, "numberLabel");
        
        var ch = checkNumber(num, "numberLabel");
        if (ch == false) {
            error++;
        }
        checkBlank(msg, "messageLabel");
        if (error >= 1) {
            alert("Please ensure that you have filled in all the fields and that you have entered a valid e-mail address and phone number.");
            error = 0;
        } else {
            var divText = "<fieldset style=\"position:absolute;left:10px;top:10px;bottom:10px;right:10px;margin-top:0px\9;\">";
            divText += "<legend>Terms &amp; Conditions</legend>";
            divText += "<textarea rows=8 overflow=\"auto\" style=\"width:95%;\">The term &#8216;E Rex Makin & Co&#8217; or &#8216;us&#8217; or &#8216;we&#8217; or &#8216;our&#8217; refers to the owner of the website. The term &#8216;you&#8217; refers to the user or viewer of our website.";
            divText += "The use of this website is subject to the following terms of use:\n\n";
            divText += "1. The content of the pages of this website does not constitute legal advice and is for your general information and use only. It is subject to change without notice.\n\n";
            divText += "2. Neither we nor any third parties provide any warranty or guarantee as to the accuracy, timeliness, performance, completeness or suitability of the information and materials found or offered on this website for any particular purpose. You acknowledge that such information and materials may contain inaccuracies or errors and we expressly exclude liability for any such inaccuracies or errors to the fullest extent permitted by law.\n\n";
            divText += "3. Your use of any information or materials on this website is entirely at your own risk, for which we shall not be liable. It shall be your own responsibility to ensure that any products, services or information available through this website meet your specific requirements.\n\n";
            divText += "4. This website contains material which is owned by or licensed to us. This material includes, but is not limited to, the design, layout, look, appearance and graphics. Reproduction is prohibited other than in accordance with the copyright notice, which forms part of these terms and conditions.\n\n";
            divText += "5. All trademarks reproduced in this website, which are not the property of, or licensed to the operator, are acknowledged on the website.\n\n";
            divText += "6. Unauthorised use of this website may give rise to a claim for damages and/or be a criminal offence.\n\n";
            divText += "7. You may not download or reproduce any part of this website except for those parts which are in a PDF format.\n\n";
            divText += "8. From time to time this website may also include links to other websites. These links are provided for your convenience to provide further information. They do not signify that we endorse the website(s). We have no responsibility for the content of the linked website(s). Where a link will navigate you away from our page we will warn you of this.\n\n";
            divText += "9. You may not create a link to this website from another website or document without our prior written consent.\n\n";
            divText += "10. We may capture and retain data relating to your use of our website and may use that data for research or marketing purposes.\n\n";
            divText += "11. Your use of this website and any dispute arising out of such use of the website is subject to the laws of England and Wales and the courts of those countries shall have exclusive jurisdiction.\n\n";
            divText += "</textarea>";
            divText += "<p><input type=\"checkbox\" id=\"checkout\"/> Tick box to confirm that you have read and understand the terms and conditions of this website.</p>";
            divText += "<table width=\"100%\"><tr><td>";
            divText += "<img src=\"../images/sec1.png\" alt=\"null\"/>";
            divText += "</td><td width=\"130px\" ><p style=\"font-size:x-small;\">In order to prevent malicious e-mail attack, please confirm the security code.</p></td>";
            divText += "<td><input style=\"border:1px solid black;padding-bottom:4px;\" id=\"sec\" class=\"formMand\" type=\"text\" alt=\"Security Input\" size=\"6\" maxlength=\"6\" tabindex=\"150\"></input></td>";
            divText += "<td align=\"right\"><input type=\"button\" value=\"Cancel\" class=\"res\" onclick=\"hidePopup('popup1')\"><input type=\"button\" value=\"Send\" class=\"sub\" style=\"margin-left:10px;margin-left:0px\9;\" onclick=\"runEmail('" + fnm + "','" + snm + "','" + mail + "','" + num + "','" + dep + "','" + msg + "',document.getElementById(\'checkout\').checked)\" /></td></tr></table>";
            divText += "</fieldset>";
            document.getElementById("popup1").innerHTML = divText;
        }
    }


    function switchContent() {
        document.getElementById("moreinfo").style.display = "block";
        document.getElementById("compliance").style.display = "none";
    }


    function switchBack() {
        document.getElementById("compliance").style.display = "block";
        document.getElementById("moreinfo").style.display = "none";
    }


    function GetXmlHttpObject1() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest;
        }
        if (window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
        return null;
    }


    function runEmail(fnn, snn, emm, nnn, dpp, mss,check) {
    
    
        var sec = document.getElementById('sec').value;
        sec = sec.toLowerCase();
        
        document.getElementById('sec').style.borderColor="black";
	document.getElementById('sec').style.borderWidth="1px";
        
        //alert (sec);
    	if(sec!='jksp4x'){

			alert("There was an error filling out the security code.");
			document.getElementById('sec').style.borderColor="red";
			document.getElementById('sec').style.borderWidth="2px";
			return false;
	}
	
        if (check == true){
        
        xmlhttp = GetXmlHttpObject1();
        if (xmlhttp == null) {
            alert("Browser does not support HTTP Request");
            return;
        }

        var url = "../php/mail2.php";
        url = url + "?forename=" + fnn;
        url = url + "&surname=" + snn;
        url = url + "&number=" + nnn;
        url = url + "&email=" + emm;
        url = url + "&department=" + dpp;
        url = url + "&message=" + mss;
        url = url + "&sid=" + Math.random();
        xmlhttp.onreadystatechange = stateCh;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
        
        }else{
           alert("You Must accept the terms and conditions of the website.");
        }
    }


    function stateCh() {
        if (xmlhttp.readyState == 4) {
            var rpn = xmlhttp.responseText;
            document.getElementById('popup1').innerHTML=rpn;
        }
    }
