

function toggle(a, b) {

    if (!document.getElementById) return true;
    if (b == 1) {
        for (var i = toggleList.length - 1; i >= 0; i--) {
            if (toggleList[i] != a) {
                document.getElementById("dvStep_" + toggleList[i]).style.display = 'none';
                var hrefStep = document.getElementById("hrefStep_" + toggleList[i]);
                hrefStep.className = "tgStep";
                /*hrefStep.style.backgroundImage = "url(../images/expand.jpg)";
                hrefStep.style.backgroundPosition = "right center";
                hrefStep.style.backgroundRepeat = "no-repeat";*/
            }
        }
    }

    /*var dvStep = document.getElementById("dvStep_" + a);
    dvStep.style.display = (dvStep.style.display == 'block') ? 'none' : 'block';

    var img = (dvStep.style.display == 'block') ? "collapse.jpg" : "expand.jpg";*/

    var link = document.getElementById("hrefStep_" + a);
    /*link.style.backgroundImage = "url(../images/" + img + ")";
    link.style.backgroundPosition = "right center";
    link.style.backgroundRepeat = "no-repeat";*/
    link.className = "tgStepActive";
    return false;
}

underscore = {
    position: function(element) {
        var p = { x: element.offsetLeft || 0, y: element.offsetTop || 0 };
        while (element = element.offsetParent) {
            p.x += element.offsetLeft;
            p.y += element.offsetTop;
        }
        return p;
    }
}

function ShowErrorContainer(csqid) {
    var ErrorDiv = document.getElementById("errorContainer_" + csqid)
    ErrorDiv.style.display = "block";
    window.scrollTo(0, underscore.position(ErrorDiv).y);
}

function HideErrorContainer(csqid) {
    document.getElementById("errorContainer_" + csqid).style.display = "none";
}

function CheckMandatory(csqid,cssqid, cIsNumericType,maxR,cMaxDecimal,CheckEqualPriority) {
    try {
        if (!CheckMaxResponses(csqid,maxR)) {
            return false;
        }
        if (cIsNumericType) {
            if (!CheckNumeric(csqid, cssqid,cMaxDecimal))
                return false;
        }
        var hasTextResponse = false;
        var frm = document.frmResults;
        var arrCount = -1;
        var arrName = new Array(); //holds the name of the elements requiring checking
        arrName[0] = new Array(); //name
        arrName[1] = new Array(); //type
        var gotName = false;

        var hasRelatedResponse = false;
        if (eval("document.frmResults.CampaignHasRelatedTextResponse_" + csqid + ".value") == "Y") {
            hasRelatedResponse = true;
        }

        var relatedRIC = eval("document.frmResults.CampaignRelatedTextResponseInternalCode_" + csqid + ".value");
        var relatedSelected = false;
        
        //Firstly, need to get an array of all the names on the form
        for (i = 0; i < frm.length; i++) {
            if (frm[i].name.indexOf("elab_" + csqid) == 0) {

                for (j = 0; j < arrName[0].length; j++) {
                    gotName = false;
                    if (arrName[0][j] == frm[i].name) //value already exists
                    {
                        gotName = true;
                        break;
                    }
                }
                if (!gotName) {

                    arrCount++;
                    arrName[0][arrCount] = frm[i].name;
                    arrName[1][arrCount] = frm[i].type;
                }
            }
        }

        for (i = 0; i < arrName[0].length; i++) {
            var elem = frm[arrName[0][i]];
            var mandatory = false;
            var type = arrName[1][i];
            switch (type) {
                case "radio": //single response

                    if (elem[0]) //If not an array then it's a single button
                    {
                        for (j = 0; j < elem.length; j++) {
                            if (elem[j].checked) {
                                mandatory = true;

                                if (hasRelatedResponse) {
                                    if (elem[j].value == relatedRIC) {
                                        relatedSelected = true;
                                    }
                                }
                                break;
                            }
                        }
                        if (mandatory) {
                            continue;
                        }
                    }
                    else {
                        if (elem.checked) {
                            mandatory = true;
                        }
                    }
                    break;

                case "checkbox": //multiple response

                    if (elem[0]) //An array
                    {
                        for (j = 0; j < elem.length; j++) {
                            if (elem[j].checked) {
                                if (hasRelatedResponse) {
                                    if (elem[j].value == relatedRIC) {
                                        relatedSelected = true;
                                    }
                                }
                                mandatory = true;
                                //break;
                            }
                        }
                        if (mandatory) {
                            continue;
                        }

                    }
                    else {
                        if (elem.selected) {
                            mandatory = true;
                        }
                    }

                    break;

                case "textarea": if (elem.value.length > 0) mandatory = true;  break; //open response
                case "text":
                    if (elem.value.length > 0 || hasTextResponse) {
                        hasTextResponse = true;
                        mandatory = true;
                    }

                    if (hasRelatedResponse && !relatedSelected) {
                        elem.value = "";
                        mandatory = true;
                    }
                    break;
            }
            // FRF0558 - exit for loop if current response not validated ;
            if (!mandatory)
                break;
        }

            //Question has not been answered
        if (!mandatory) {
            /*switch (type) {
            case "radio":
            if (arrName[0].length == 1) {
            alert("Please choose a response to the question.");
            }
            else {
            alert("Please choose a response to each question.");
            }
            break;
            case "checkbox":
            if (arrName[0].length == 1) {
            alert("Please ensure you select at least one response.");
            }
            else {
            alert("Please ensure you select at least one response from each question.");
            }
            break;
            case "textarea":
            case "text":
            document.getElementById("errorContainer_" + csqid).style.display = "block";;
            break;
            }*/
            ShowErrorContainer(csqid);
            return false;
        }
            /*
            <% if(cCampaignStepQuestionAllowEqualPriorities=="N"){ %>
            CheckEqualPriorities();
            <%}%>
            */
        if (CheckEqualPriority) {
            return CheckEqualPriorities(csqid);
        }
        HideErrorContainer(csqid);
        return true;

    } catch (e) {
    alert(e.description);
    }
		}

		function CheckRetype(csqid, cssqid, type) {
		var init = eval("document.frmResults.elab_" + csqid + "_" + cssqid + ".value");
		var checksame = eval("document.frmResults.elab_" + csqid + "_checksame.value");

		if (init != checksame || init.length == 0 || (type == "email address" && validateEmail(init) == false)) {
		    ShowErrorContainer(csqid); 
		   return false;
	    }
	    HideErrorContainer(csqid);
       return true;
	}

	function CheckRetypeNonMan(csqid, cssqid,dtype)
	{
	    var init = eval("document.frmResults.elab_" + csqid + "_" + cssqid +  ".value");
	    var checksame = eval("document.frmResults.elab_" + csqid + "_checksame.value");


	    if (init != checksame) {
	        //alert("Please ensure you have confirmed your " + type + " correctly.");
	        ShowErrorContainer(csqid);
	        return false;
	    }
	    HideErrorContainer(csqid);
	    return true;
	
	}
	
	function CheckAddress(csqid){
		if (eval("document.frmResults.elab_" + csqid + "_2.value") == "" || eval("document.frmResults.elab_" + csqid + "_5.value") == "" || eval("document.frmResults.elab_" + csqid + "_6.value") == ""){
		    ShowErrorContainer(csqid);
			return false;
        }
        HideErrorContainer(csqid);
		return true;
	}


	function CheckName(csqid) {
	    if (eval("document.frmResults.elab_" + csqid + "_2.value") == "" || eval("document.frmResults.elab_" + csqid + "_3.value") == "") {
		    ShowErrorContainer(csqid);
		    return false;
        }
        HideErrorContainer(csqid);
        return true;
    }
	

	var checkName = false;
	
	function CheckMaxResponses(csqid,maxR)
	{
		//var maxR = <% Response.Write(cMaxResponses); %>;
		var curR = 0;
		var frm = document.frmResults;
		var arrCount = -1;
		var arrName = new Array(); //holds the name of the elements requiring checking
			arrName[0] = new Array(); //name
			arrName[1] = new Array(); //type
		var gotName = false;
		
		if(maxR==0)
		{
			return true;
		}
		
		//Firstly, need to get an array of all the names on the form
		for (i = 0; i < frm.length; i++) {
		    if (frm[i].name.indexOf("elab_" + csqid) == 0) {
		        gotName = false;
				for(j=0;j<arrName[0].length;j++) {
				    if (arrName[0][j] == frm[i].name) { //value already exists
						gotName = true;
						break;
            		}
	            }
				if(!gotName) {
				    arrCount++;
					arrName[0][arrCount] = frm[i].name;
					arrName[1][arrCount] = frm[i].type;
				}				
			}
		}
		
		for(i=0;i<arrName[0].length;i++) {
			var elem = frm[arrName[0][i]];
			var mandatory = false;
			var type = arrName[1][i];
			curR = 0;
			if (type == "checkbox") { //multiple response
			    if (elem[0]) { //An array
			        for (j = 0; j < elem.length; j++) {
			            if (elem[j].checked) {
			                curR++;
			            }
        		}
            }
            else {
                if (elem.selected) {
                    curR++;
                }
            }

            if (curR > maxR) {
                //alert("Please ensure you select a maximum of " + maxR + " responses.");
                ShowErrorContainer(csqid);
                return false;
            }
		}
    }
    HideErrorContainer(csqid);
	return true;
}

    function CheckNumeric(csqid, cssqid, maxDecimal) {
	        var val = eval("document.frmResults.elab_" + csqid + "_" + cssqid + ".value");
		    var dec = val.indexOf(".");
		    if (maxDecimal == 0 && dec != -1) {
		        //alert("The number you enter must not contain any decimal places.");
		        ShowErrorContainer(csqid);
		        return false;
		    }
    		
		    if(dec!=-1) {
			    var s = val.substring(val.length-dec-1);
			    if(s.indexOf(".")==-1) {
			        //alert("The number you enter must not contain more than " + maxDecimal + " decimal places.");
			        ShowErrorContainer(csqid);
			        return false;
                }
            }
            if (IsNumeric(val)) {
                HideErrorContainer(csqid);
    	        return true;
		    }
		    else {
		        //alert("Please ensure you enter a number as a response.");
		        ShowErrorContainer(csqid);
			    return false;
		    }

	}
	
	function CheckEqualPriorities(csqid) {
		var frm = document.frmResults;
		var arrName = new Array(); //holds the value of the elements requiring checking
		var arrCount = -1;
		
		
		for(i=0;i<frm.length;i++) {
		    if (frm[i].name.indexOf("elab_" + csqid) == 0) {
		        if (frm[i].type == "radio") {
		            if (frm[i].checked == true) {
		                arrCount++;
		                arrName[arrCount] = frm[i].value;
		            }
		        }
		    }
		}
		
			for(a = 0;a<arrName.length;a++)
			{
				for(b = 0;b<arrName.length;b++)
				{
					//If values are equal the raise error!
					if (arrName[a]==arrName[b]&&a!=b)
					{
					    //alert('Answers to this question must be unique');
					    ShowErrorContainer(csqid);
						return false;
						break;
					}
				}
        }
        HideErrorContainer(csqid);
        return true;
	}
	
	function ReSubmitElab()
	{
		document.frmResults.CampaignResubmit.value = "Y";
		document.frmResults.submit();
	}
	

	function ReturnFromElab()
	{
		if(document.frmBreadcrumb_0)
		{

			if(document.frmBreadcrumb_0.action.indexOf("elab.aspx")<0){
				document.frmBreadcrumb_0.submit();
				return false;
			}
			else{
				window.opener = window.open('', '_self', '');
		 		window.opener.close();
				document.location.href = "about:blank";

			}
		}
		else{
			window.opener = window.open('', '_self', '');
	 		window.opener.close();
			document.location.href = "about:blank";	
		}
	}	
	
	
	
	/* tests to see if string is in correct UK style postcode: AL1 1AB, BM1 5YZ etc. */
    function isValidPostcode(p) {
        var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} ?[0-9][A-Z]{2}/i;
        return postcodeRegEx.test(p); ;
    }

    /*	formats a VALID postcode nicely: AB120XY -> AB1 0XY */
    function formatPostcode(csqid, cssqid, mandatory) {

        if (!mandatory && p == "") {
            HideErrorContainer(csqid);
            return true
         };
        
        p = eval("document.frmResults.elab_" + csqid + "_" + cssqid + ".value.toUpperCase()");
	    if (isValidPostcode(p)) {
	        var postcodeRegEx = /(^[A-Z]{1,2}[A-Z0-9]{1,2})([0-9][A-Z]{2}$)/i;
	        var obj = eval("document.frmResults.elab_" + csqid + "_" + cssqid);
	        obj.value = p.replace(postcodeRegEx, "$1 $2");
	        HideErrorContainer(csqid);
		    return true;
        }
        else{
            ShowErrorContainer(csqid);
		    return false;
	    }
	}

	function validateEmail(elementValue) {
	    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	    return emailPattern.test(elementValue);
	}

	function passwordStrength(password, minlength, maxlength, minnumeric, minalpha, minsymbol, minupper, minlower, minstrength) {
	    var desc = new Array();
	    desc[0] = "Very Weak";
	    desc[1] = "Weak";
	    desc[2] = "Better";
	    desc[3] = "Medium";
	    desc[4] = "Strong";
	    desc[5] = "Strongest";

	    var score = 0;
	    var helptext = "";
	    //if password bigger than minlength give 1 point
	    if (password.length >= minlength) score++;

	    //if password has both lower and uppercase characters give 1 point	
	    if ((password.match(/[a-z]/)) && (password.match(/[A-Z]/))) score++;

	    //if password has at least one number give 1 point
	    if (password.match(/\d+/)) score++;

	    //if password has at least one special caracther give 1 point
	    if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/)) score++;

	    //if password length equals or bigger than maxlength give another 1 point
	    if (password.length >= maxlength) score++;

	    if (!password.match(".[!,@,#,$,%,^,&,*,>,<,?,_,=,+,),(,£,¬,~,-,(,),.]{" + minsymbol + "}"))
	        helptext += "Symbols: " + minsymbol + "<br/>";

	    if (!password.match("[a-z]{" + minlower + "}"))
	        helptext += "Lowere Case Characters: " + minlower + "<br/>";

	    if (!password.match("[A-Z]{" + minupper + "}"))
	        helptext += "Upper case characters: " + minupper + "<br/>";

	    if (!password.match("\\d{" + minnumeric + "}"))
	        helptext += "Numeric Characters: " + minnumeric + "<br/>";

	    if (password.length < minlength)
	        helptext += "Minimum Length: " + minlength + "<br/>";

	    if (score < minstrength)
	        helptext += "Password Strength: " + desc[minstrength] + "<br/>";

	    if (helptext.length > 0)
	        helptext = "Please complete following requirements:<br/>" + helptext;

	    document.getElementById("passwordrequirements").innerHTML = helptext;
	    document.getElementById("passwordDescription").innerHTML = desc[score];
	    document.getElementById("passwordStrength").className = "strength" + score;

	    if (helptext.length > 0)
	        return false;
	    else
	        return true;
	}

	function validatenewpassword(minlength, maxlength, minnumeric, minalpha, minsymbol, minupper, minlower, minstrength) {

	    password = document.getElementById("newpassword").value;
	    confirmpassword = document.getElementById("confirmpassword").value;

	    if (!passwordStrength(password, minlength, maxlength, minnumeric, minalpha, minsymbol, minupper, minlower, minstrength)) {
	        alert("Please complete the password requiremetns");
	        return false;
	    }
	    else if (password != confirmpassword) {
	        alert("Password do not match.");
	        return false;
	    }
	    else
	        return true;


	}
