var STTAFINTRO = {

	introDivCreated: false,
	introDivId: 'widgetContainer',
	name: '',
	emailId: '',
	rewardId: null,

	showIntroDiv: function(rewardId)
	{
		if(typeof(STTAFFUNC)=="undefined"){
			window.setTimeout('STTAFINTRO.showIntroDiv()',1000);
			return;
		}
		
		// initialise the custom variables
		STTAFINTRO.rewardId = rewardId;
		
		STTAFINTRO.createIntroDiv();
		
		STTAFINTRO.createOverlay();
		STTAFINTRO.positionDiv(STTAFINTRO.introDivId);
		document.getElementById(STTAFINTRO.introDivId).style.display= "block";
		
		// set focus on the name field
		STTAFINTRO.setFocus("nameText");
	},
	
	createIntroDiv: function()
	{
		if(STTAFINTRO.introDivCreated)
		{
			return;
		}
		
		var stIntroDiv = document.createElement('div');
		stIntroDiv.setAttribute('id', STTAFINTRO.introDivId);
		stIntroDiv.style.width='479px';
		stIntroDiv.style.height='544px';
		stIntroDiv.style.position='absolute';
		stIntroDiv.style.zIndex='99999';
		
		var introDivData =	'<div style = "background: #E9F5FF url(http://content.socialtwist.com/themes/2009092826129-14/images/header-img.gif) no-repeat left top; border:solid 1px #B7E6FF;"><p style = "float:right; margin:10px 10px 0 0; cursor:pointer"><img src = "http://content.socialtwist.com/themes/2009092826129-14/images/close.gif" onclick = "STTAFINTRO.hideIntroDiv();"/></p><p style = "text-align:center; font:bold 13px Arial, Helvetica, sans-serif; padding-top:100px; margin:0 0 10px 0; clear:both;">Enter your name and email to get started</p><table style="margin: 0 auto;" width = "240" border = "0" align = "center" cellpadding = "0" cellspacing = "10"><tr><td width = "51" style = "font:bold 12px Arial, Helvetica, sans-serif; color:#014282;">Name</td><td width = "234"><input tabindex=1 type = "text" id="nameText" onfocus = "STTAFINTRO.hideMessage()" onkeyup="STTAFINTRO.captureEnterKey(event)" name = "textfield" style = "width:175px; background-color:#F9FDFF; border:solid 1px #99CCFF; padding: 2px 0;" /></td></tr><tr><td style = "font:bold 12px Arial, Helvetica, sans-serif; color:#014282;">Email</td><td><input tabindex=2 onfocus = "STTAFINTRO.hideMessage()" onkeyup="STTAFINTRO.captureEnterKey(event)" type = "text" id="emailText" name = "textfield2" style = "width:175px; background-color:#F9FDFF; border:solid 1px #99CCFF; padding: 2px 0;" /></td></tr><tr><td colspan = "2"><div id="msgDiv" style="margin:5px 0; color:#ff0000; position:relative; display:none"></div><table width = "100%" border = "0" cellspacing = "5" cellpadding = "0"><tr><td onkeyup="STTAFINTRO.captureEnterKey(event);" width = "53%" align = "right"><img tabindex=3 style="cursor:pointer" src = "http://content.socialtwist.com/themes/2009092826129-14/images/continue.gif" width = "92" height = "29" onclick = "STTAFINTRO.continueAction();" /></td><td width = "47%" align = "center"><a tabindex=4 href = "javascript:void(0);" style = "font:bold 12px Arial, Helvetica, sans-serif; color:#014282; display:block; text-decoration:none; padding:3px 0;" onclick="javascript:STTAFINTRO.hideIntroDiv();" >No Thanks</a></td></tr></table></td></tr></table></div>';
		
		stIntroDiv.innerHTML = introDivData;
		document.body.appendChild(stIntroDiv);
		
		STTAFINTRO.introDivCreated = true;
	},

	createOverlay: function(){
		// lets create the overlay
		var overlay = document.createElement('div');
		overlay.setAttribute('id', 'st-overlay');
		var ocs = overlay.style;
		//var isIE6 = (window.external && typeof window.XMLHttpRequest == "undefined");
		if(navigator.appName == 'Microsoft Internet Explorer') {
			ocs.position = 'absolute';
			ocs.height = "2000px";
			ocs.overflow = "hidden";
		} else {
			ocs.position = 'fixed';
			ocs.height = '100%';
		}
		ocs.zIndex = 9990;
		ocs.top = 0;
		ocs.left = 0;
		ocs.width = '100%';
		ocs.backgroundColor = 'black';
		ocs.filter = 'alpha(opacity: 75)';
		ocs.MozOpacity = 0.75;
		ocs.opacity = 0.75;
		document.body.insertBefore(overlay, document.body.firstChild);
	},

	positionDiv: function(divId){
		var divObj = document.getElementById(divId);
		var h = (STTAFFUNC.GetHeight() / 2) - (546 / 2);
		var w = (STTAFFUNC.GetWidth() / 2) - (481 / 2);
		
		divObj.style.left = w + "px";
		divObj.style.top = h + "px";
	},
	
	hideIntroDiv: function()
	{
		document.getElementById(STTAFINTRO.introDivId).style.display= "none";
		document.getElementById('st-overlay').parentNode.removeChild(document.getElementById('st-overlay'));
	},
	
	continueAction: function()
	{
		if(!STTAFINTRO.verifyData())
		{
			return;
		}
		
		document.getElementById(STTAFINTRO.introDivId).style.display = "none";
		
		STTAFFUNC.cw(this, {id:'2009092826129-14', link: window.location, title: document.title, custom:{_rewardId:STTAFINTRO.rewardId, _userId:STTAFINTRO.emailId,_yourName: STTAFINTRO.name, _yourEmail:STTAFINTRO.emailId}});
		
		STTAFINTRO.setCloseEvent();
	},
	
	setCloseEvent: function() {
		STTAFFUNC.closeEventFunc = function() {
			document.getElementById('st-overlay').parentNode.removeChild(document.getElementById('st-overlay'));
		}
	},
	
	checkEmail: function (email) {
		var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(email)) {
			return false;
		}
		return true;
	},
	
	verifyData: function()
	{
		STTAFINTRO.name =  document.getElementById("nameText").value;
		STTAFINTRO.emailId =  document.getElementById("emailText").value;
		
		if(STTAFINTRO.name == "" )
		{
			STTAFINTRO.showMessage('Please enter your name');
			return false;
		}
		else if(STTAFINTRO.emailId == "" )
		{
			STTAFINTRO.showMessage('Please enter your email');
			return false;
		}
		else if (!STTAFINTRO.checkEmail(STTAFINTRO.emailId))
		{
			STTAFINTRO.showMessage('Please enter a valid email address');
			return false;
		}
		return true;
	},
	
	showMessage: function(msg)
	{
		document.getElementById("msgDiv").innerHTML = msg;
		document.getElementById("msgDiv").style.display = "block";
	},

	hideMessage: function()
	{
		document.getElementById("msgDiv").innerHTML = "";
		document.getElementById("msgDiv").style.display = "none";
	},
	
	setFocus: function(divId)
    {
		document.getElementById(divId).focus();
    },
	
	captureEnterKey: function(e)
	{
		var firefox = document.getElementById && !document.all;

		if (!firefox) {
			e = event;
		}	

		if (e.keyCode == 13) {
			if (e.target) {
				targ = e.target;
			} else if (e.srcElement) {
				targ = e.srcElement;
			}	

			STTAFINTRO.continueAction();
		}                        	
	}
}	