/*** ENewsletter Begin ***/
function enewsCheckItem(item, msg, regex) {
		var value = "";
		var itype = "r";
		
		if(typeof item.type != "undefined")
			itype = item.type.toLowerCase().substr(0,1);
		
		switch (itype)
		{
			case "r": // Radio Button
				for(var r = 0; r < item.length; r++)
				{
					if(item[r].checked)
					{
						value = item[r].value;
						break;
					}
				}
				break;
			case "c":
				value = item.checked ? "true" : "";
				break;
			case "s": // Select Box
				value = item.options[item.selectedIndex].value;
				break;
			case "t": // Textbox or Textarea
				value = item.value;
				break;
		}
		
		var fail = (value.length == 0 || ((typeof regex != "undefined") && !regex.test(value)));
		return (fail) ? "\n-Must specifiy " + msg : "";
    }	


	function validateENewsletter()
	{
		var form = document.frmEmailSignup;
		
		// Do some validation
		var errors = "";	
		errors += enewsCheckItem(form.txtFirstName, "First Name");
		errors += enewsCheckItem(form.txtLastName, "Last Name");
		errors += enewsCheckItem(form.txtEmail, "a valid Email", /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/);
		//errors += enewsCheckItem(form.chkConfirm, "that you agree with the terms and conditions");
		
		if(errors.length != 0)
		{
			alert("You may not register until the following issues have been resolved:\n" + errors);
		}
		else
		{			
			enewsletterSubmitted = true;
			
			var url = "/iccorp/ICBusEnewsletterProvider";
			
			//url = "success.txt";
			
			
			var query = "?email=" + $("#txtEmail").val() + "&first=" + $("#txtFirstName").val() + "&last=" + $("#txtLastName").val() + "&additional=" + $("#chkConfirm").attr("checked");
			$.get(url, {email: $("#txtEmail").val(), first: $("#txtFirstName").val(), last: $("#txtLastName").val(), source: "RightRail", additional: $("#chkConfirm").attr("checked")}, function(data){
				alert(data);	
				if(data.indexOf("success") >= 0)
					{
						alert('grow');
						emailSubmitBox.open(query);
					}
					else
					{
						//let them know it didn't work
						alert('There was an error when making your request.  Please wait a few moments and try again.');
					}
				});


				// Do your ajax magic here
		}
		return false;
	}
	
function SignupForENewsLetter(emailAddress)
{
	$(".enewsEmailSubmitBox").height("242px");
	emailSubmitBox.open("?email=" + emailAddress + "&origin=flash");
}

function growSubmitBox()
{
	$(".enewsEmailSubmitBox").animate({height: "763px"}, 1500);
}

function showThankyou()
{
	$(".enewsEmailSubmitBox").height("120px");
	$('html,body').animate({scrollTop: $(".enewsEmailSubmitBox").offset().top}, 1000);
}
	
var emailPreFilledSubmitBox = new function emailSubmitBoxClass()
{
	var _this = this;
	this.box = null;
	this.washout = null;
	this.open = function(query)
	{
		if(typeof query == "undefined")
			query = "";			
		
		var url = "/vgn-ext-templating/iccorp/webservices/enewsletter.jsp" + query;
		if(_this.box != null)
		{
			_this.box = null;
		}
			
		_this.washout = makeElement("div", {cn:"washout", style:{height:document.getElementById("all").offsetHeight + "px"}});
		$(".enewsEmailSubmitBox").height("430px");
		_this.box = makeElement("div", {cn:"enewsEmailSubmitBox enewsShortStack"});			
		
		_this.box.appendChild(makeElement("iframe", {src:url,frameborder:0,name:"specFrame",id:"specFrame",scrolling: "no"}));
		document.body.appendChild(_this.washout);			

		document.body.appendChild(_this.box);			
		
		
		zIndexMgr.setzIndexOf(_this.washout);
		zIndexMgr.setzIndexOf(_this.box);
		var offset = 40;
		if(getScrollTop() < 60)
			offset = 110 - getScrollTop();
		
		_this.box.style.top = (getScrollTop() + offset) + "px";
	};
	this.close = function()
	{
		_this.box.className = "hidden";
		_this.washout.className = "hidden";
		window.frames["specFrame"].location.href = "about:blank";
		$("#txtEmail").val("");
		$("#txtFirstName").val("");
		$("#txtLastName").val("");
		//window.location.reload();
	};
}

/*** ENewsletter End ***/

/*****************************************************************************
* Simple Make Element - Ratchet
*****************************************************************************/

function makeElement(name, attr, txt, cmt, data)
{
    var el = document.createElement(name);

    var u = "undefined";
    if(typeof attr != u && attr != null)
    {
        //Add attributes
        for(prop in attr)
        {
            if(prop.toString() == "cn")
            {
                el.className = attr[prop];
            }
            else if(prop.toString() == "style") 
            {
                for(style in attr[prop])
                {
                    if(style.toString() == "float")
                    {
                        if(typeof el.style.styleFloat != "undefined")
                            el.style.styleFloat = attr[prop][style];
                        else
                            el.style.cssFloat = attr[prop][style];
                    }
                    else
                    {
                        el.style[style.toString()] = attr[prop][style];
                    }
                }
            }
            else if (prop.toString() == "frameborder")
            {
                el.frameBorder = attr[prop];
            }
            else
            {
                el.setAttribute(prop.toString(), attr[prop]);
            }
        }
    }

    if(typeof txt != u && txt != null)
        el.appendChild(document.createTextNode(txt));
    
    if(typeof cmt != u && cmt != null)
        el.appendChild(document.createComment(cmt));

    if(typeof data != u && data != null)
		el.innerHTML = data;

    return el;
}

