// JavaScript Document

	var borderDef     = "";
	var backgroundDef = "";

	function trim(stringToTrim) {
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}

	function getElementsByClass(attributeValue)
	{
		var elementArray = new Array();
		var matchedArray = new Array();
		if (document.all) {
			elementArray = document.all;
		} else {
			elementArray = document.getElementsByTagName("*");
		}
		for (var i = 0; i < elementArray.length; i++) {
			var pattern = new RegExp("(^| )" + attributeValue + "( |$)");
			if (pattern.test(elementArray[i].className)) {
				matchedArray[matchedArray.length] = elementArray[i];
			}
		}
		return matchedArray;
	}

	highlight = function() {
	
		var sfEls = document.getElementById("promotion_questions").getElementsByTagName("input");
	    for (var i=0; i<sfEls.length; i++) {
			if (sfEls[i].type == "text") {
				if (sfEls[i].addEventListener) {
					sfEls[i].addEventListener("focus", function()
	                    {
	                        borderDef         = this.style.border;
	                        backgroundDef     = this.style.backgroundColor;
	                        this.style.border = "solid #FF9933 1px";
	                        this.style.backgroundColor = "#FFFF66";
	                    }, false);
					sfEls[i].addEventListener("blur", function()
	                    {
	                        this.style.border = borderDef;
	                        this.style.backgroundColor = backgroundDef;
	                    }, false);
				}
				else if (sfEls[i].attachEvent) {
					sfEls[i].attachEvent("onfocus", function()
	                    {
	                        borderDef         = this.style.border;
	                        backgroundDef     = this.style.backgroundColor;
	                        this.style.border = "solid #FF9933 1px";
	                        this.style.backgroundColor = "#FFFF66";
	                    });
					sfEls[i].attachEvent("onblur", function()
	                    {
	                        this.style.border = borderDef;
	                        this.style.backgroundColor = backgroundDef;
	                    });
				}
			}
			if ((sfEls[i].type == "radio") || (sfEls[i].type == "checkbox")) {
				
				if (sfEls[i].addEventListener) {
					sfEls[i].addEventListener("focus", function()
	                    {
	                    	var radioId = this.id;
			    			var divId = 'radio_' + radioId.substr(8);
	                        document.getElementById(divId).style.border = "solid #FF9933 1px";
	                        document.getElementById(divId).style.backgroundColor = "#FFFF66";
	                    }, false);
					sfEls[i].addEventListener("blur", function()
	                    {
	                    	var radioId = this.id;
			    			var divId = 'radio_' + radioId.substr(8);
	                        document.getElementById(divId).style.border = "solid #FFFFFF 1px";
	                        document.getElementById(divId).style.backgroundColor = "#FFFFFF";
	                    }, false);
				}
				else if (sfEls[i].attachEvent) {
					sfEls[i].attachEvent("onfocus", function()
	                    {
	                    	var radioId = this.id;
			    			var divId = 'radio_' + radioId.substr(8);
	                        document.getElementById(divId).style.border = "solid #FF9933 1px";
	                        document.getElementById(divId).style.backgroundColor = "#FFFF66";
	                    });
					sfEls[i].attachEvent("onblur", function()
	                    {
	                    	var radioId = this.id;
			    			var divId = 'radio_' + radioId.substr(8);
	                        document.getElementById(divId).style.border = "solid #FFFFFF 1px";
	                        document.getElementById(divId).style.backgroundColor = "#FFFFFF";
	                    });
				}
			}
	    }
	    
	    var sfEls2 = document.getElementById("promotion_questions").getElementsByTagName("select");
	    for (var i=0; i<sfEls2.length; i++) {

			if (sfEls2[i].addEventListener) {
				sfEls2[i].addEventListener("focus", function()
                    {
                        borderDef         = this.style.border;
	                    backgroundDef     = this.style.backgroundColor;
                        this.style.border = "solid #FF9933 1px";
                        this.style.backgroundColor = "#FFFF66";
                    }, false);
				sfEls2[i].addEventListener("blur", function()
                    {
                        this.style.border = borderDef;
                        this.style.backgroundColor = backgroundDef;
                    }, false);
			}
			else if (sfEls2[i].attachEvent) {
				sfEls2[i].attachEvent("onfocus", function()
                    {
                        borderDef         = this.style.border;
	                    backgroundDef     = this.style.backgroundColor;
                        this.style.border = "solid #FF9933 1px";
                        this.style.backgroundColor = "#FFFF66";
                    });
				sfEls2[i].attachEvent("onblur", function()
                    {
                        this.style.border = borderDef;
                        this.style.backgroundColor = backgroundDef;
                    });
			}
	    }
	}

	checkform = function() {
	    var formIsFilled  = true;
	    var radioArray    = new Array();
        var checkedInputs = getElementsByClass("permit_send");

        for (var i = 0; i < checkedInputs.length; i++) {
            //window.alert(checkedInputs[i].nodeName + " | " + checkedInputs[i].type);
            switch (checkedInputs[i].nodeName) {
                case "INPUT":
                	if (checkedInputs[i].type == "text") {
						if (trim(checkedInputs[i].value) == "") {
                            formIsFilled = false;
                            break;
						}
                    } else if (checkedInputs[i].type == "radio") {
						var radioSelected = false;
						for (var t = 0; t < checkedInputs.length; t++) {
						    if (checkedInputs[t].name == checkedInputs[i].name) {
                                if (checkedInputs[t].checked == true) {
                                    radioSelected = true;
                                    break;
								}
							}
						}
						if (radioSelected == false) {
                            formIsFilled = radioSelected;
                            break;
						}
					} else if (checkedInputs[i].type == "checkbox") {
					    if (checkedInputs[i].checked == false) {
                            formIsFilled = false;
						}
					}
                    break;
                case "SELECT":
				    var isSelected = false;
					for (var t = 0; t < checkedInputs[i].options.length; t++) {
						var element = checkedInputs[i].options[t];
						if ((element.selected == true) && (element.value != "")) {
							isSelected = true;
							break;
						}
					}
					if (isSelected == false) {
					    formIsFilled = isSelected;
                        break;
					}
			}
        }
        if (formIsFilled == true) {
			document.getElementById("sent_input").style.backgroundImage = "url(img/promotion-button-send-on.jpg)";
			document.getElementById("stage2").style.backgroundImage     = "url(img/promotion-stage2-on.jpg)";
			document.getElementById("sent_input").onclick               = function () { document.forms["promotion_form"].submit(); };
		} else {
            document.getElementById("sent_input").style.backgroundImage = "url(img/promotion-button-send-off.jpg)";
            document.getElementById("stage2").style.backgroundImage     = "url(img/promotion-stage2-off.jpg)";
            document.getElementById("sent_input").onclick               = function () { checkCondition(); };
		}
        //window.alert(formIsFilled);
        return formIsFilled;
	}

	loadfce = function() {
		var sfEls = getElementsByClass("permit_send");

	    for (var i=0; i<sfEls.length; i++) {
			if (sfEls[i].addEventListener) {
				sfEls[i].addEventListener("blur", checkform, false);
				sfEls[i].addEventListener("change", checkform, false);
			}
			else if (sfEls[i].attachEvent) {
				sfEls[i].attachEvent("onblur", checkform);
				sfEls[i].attachEvent("onchange", checkform);
			}
	    }
	    
	    var height = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
	    //var topPosition = Math.floor((window.innerHeight - 220) / 2);
	    var topPosition = Math.floor((height - 220) / 2);
	    
        var earOff = document.getElementById("promotion_ear_off");
		if (earOff != null) document.getElementById('promotion_ear_off').style.top =  topPosition + "px";

        var earOn = document.getElementById("promotion_ear_on");
		if (earOn != null) document.getElementById('promotion_ear_on').style.top =  topPosition + "px";
		
        var coverOn = document.getElementById("promotion_cover_on");
		if (coverOn != null) document.getElementById('promotion_cover_on').style.height =  document.body.clientHeight + "px";
		
        var coverOff = document.getElementById("promotion_cover_off");
		if (coverOff != null) document.getElementById('promotion_cover_off').style.height =  document.body.clientHeight + "px";
	}

	if (window.addEventListener) {
		window.addEventListener("load", loadfce, false);
		window.addEventListener("load", highlight, false);
		window.addEventListener("resize", loadfce, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", loadfce);
		window.attachEvent("onload", highlight);
		window.attachEvent("onresize", loadfce);
	}
	
	var sendbutton = document.getElementById("sent_input");
	if (sendbutton != null) {
	    if (sendbutton.addEventListener) {  // all browsers except IE before version 9
	        sendbutton.addEventListener ("focus", checkform, false);
	    } else {
	        if (sendbutton.attachEvent) {   // IE before version 9
	            sendbutton.attachEvent ("onfocus", checkform);
	        }
	    }
	}

    
	function getScrollY() {
		var scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
		} else if( document.body && document.body.scrollTop ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
		} else if( document.documentElement && document.documentElement.scrollTop ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
		}
		return scrOfY;
	}
    
    function showForm()
	{
        document.getElementById("promotion_center").style.marginTop = getScrollY() + 'px';

		var promotionCoverOn = document.getElementById("promotion_cover_on");
		if (promotionCoverOn != null) document.getElementById('promotion_cover_on').style.display  = 'block';

        var promotionCoverOff = document.getElementById("promotion_cover_off");
		if (promotionCoverOff != null) document.getElementById('promotion_cover_off').style.display  = 'block';

        var earOff = document.getElementById("promotion_ear_off");
		if (earOff != null) document.getElementById('promotion_ear_off').style.display  = 'none';

        var earOn = document.getElementById("promotion_ear_on");
		if (earOn != null) document.getElementById('promotion_ear_on').style.display  = 'none';
	}
	
	function hideForm()
	{
        var promotionCoverOn = document.getElementById("promotion_cover_on");
		if (promotionCoverOn != null) document.getElementById('promotion_cover_on').style.display  = 'none';

        var promotionCoverOff = document.getElementById("promotion_cover_off");
		if (promotionCoverOff != null) document.getElementById('promotion_cover_off').style.display  = 'none';

        var earOff = document.getElementById("promotion_ear_off");
		if (earOff != null) document.getElementById('promotion_ear_off').style.display  = 'block';
		
        var earOn = document.getElementById("promotion_ear_on");
		if (earOn != null) document.getElementById('promotion_ear_on').style.display  = 'block';
	}
