// GET ELEMENT BY ID
function getElement(id) {
	return document.getElementById(id);
}


function addLoadEvent(func) {
	var oldonload = window.onload;
	window.onload = (typeof window.onload != 'function') ? func : function () {
		oldonload();
		func(); 
	};
}
function getLoginWidgetMsg(mode) {
	if(mode == 'topNav') msg = 'To Save Presentations, Sign In';
	else if(mode == 'forgotPassWidget') msg = 'Forgot Your Password?';
	else msg = 'Existing users log in below. New users please Sign Up for a new account.';
	return msg;
}


function loginWidget(mode) {
	var iHTML = '';
	iHTML += '<a href="javascript:void(0);" onclick="this.parentNode.parentNode.removeChild(this.parentNode);return false;"><img src="images/b_close.gif" width="10" height="10" style="position: absolute; top: 0px; right: 0px; border: none" /></a><img src="images/corner_top-left.gif" width="10" height="10" style="position: absolute; top: 0px; left: 0px; border: none" /><img src="images/corner_bottom-left.gif" width="10" height="10" style="position: absolute; bottom: 0px; left: 0px; border: none" /><img src="images/corner_bottom-right.gif" width="10" height="10" style="position: absolute; bottom: 0px; right: 0px; border: none" />';
	iHTML += '<div style="margin:15px;padding:0;border:1px solid #788592;"><p class="msg">'+getLoginWidgetMsg(mode)+'</p>';
	iHTML += '<div class="loginWidget_right"><form method="post" action="#" style="margin:0;">';
	iHTML += '<input type="hidden" name="m" value="login" /><span class="loginWidget_message"></span>';
	iHTML += '<div style="margin-bottom: 5px;">E-Mail: <input type="text" name="userName" id="userName" style="width: 150px;height:15px"  runat="server" /></div>';
	iHTML += '<div style="margin-bottom: 5px;">Password: <input type="password" name="pword" id="pword" Runat="server" style="width: 150px;height:15px;" /></div>';
	iHTML += '<div style="color: #fff; font-size: 10px; font-weight:normal;margin-bottom: 5px;"><input type="checkbox" name="remember" id="rememberID" style="margin:0px;border:0;" runat="server" />&nbsp;&nbsp;Remember&nbsp;&nbsp;me</div>';
	iHTML += '<div style="margin-bottom: 5px;"><a href="javascript:void(0);" onclick="toggleForgotPass(this, true, \''+mode+'\');" style="font-size: 10px; font-weight:normal;">Forgot Password?</a></div>';
	iHTML += '<div><a id="fpHeader_logUser" class="signInButt" href="#"><span>sign in</span></a><div style="clear: both"></div></div>';
	iHTML += '</form></div><div style="clear: both"></div></div>';
	return iHTML;
}

function forgotPassWidget(mode) {
	var iHTML = '';
	iHTML += '<a href="javascript:void(0);" onclick="this.parentNode.parentNode.removeChild(this.parentNode);return false;"><img src="images/b_close.gif" width="10" height="10" style="position: absolute; top: 0px; right: 0px; border: none" /></a><img src="images/corner_top-left.gif" width="10" height="10" style="position: absolute; top: 0px; left: 0px; border: none" /><img src="images/corner_bottom-left.gif" width="10" height="10" style="position: absolute; bottom: 0px; left: 0px; border: none" /><img src="images/corner_bottom-right.gif" width="10" height="10" style="position: absolute; bottom: 0px; right: 0px; border: none" />';
	iHTML += '<div style="margin:15px;padding:0;border:1px solid #788592;"><p class="msg">Request New Password</p><p id="forgotPassEmailMessage">Enter your email address to receive a new password.</p>';
	iHTML += '<div class="loginWidget_right">';
	iHTML += '<input type="hidden" name="m" value="forgot" /><span class="loginWidget_message"></span>';
	iHTML += '<div style="margin-bottom: 5px">E-Mail Address:&nbsp;&nbsp;<input type="text" name="email" style="width: 120px; margin: 0px;height:15px;" /></div>';
	//iHTML += '<div id="backToLoginDiv"><a href="javascript:void(0);" onclick="toggleForgotPass(this, false, \''+mode+'\');" style="font-size: 10px; font-weight:normal;">Back to Login</a></div>'; 
	iHTML += '<div><asp:linkbutton id="logUser" Runat="server" CssClass="signInButt"></asp:linkbutton><span>Submit</span><div style="clear: both"></div></div>';
	iHTML += '</div><div style="clear: both"></div></div>';
	return iHTML;
	
	
}


function toggleForgotPass() {

	document.getElementById("logHolder").style.display="none";
	document.getElementById("forgotHolder").style.display="";
}


function setLoginWidgetPosition(thisItem, newDiv, mode) {
		topItem = thisItem.parentNode;
		newDiv.style.top = (topItem.offsetTop+topItem.offsetHeight-1)+'px';
		newDiv.style.right = '0px';
}




function toggleLogin(onoff) {
	/*	var divId = 'loginWidget_'+mode+(id ? '_'+id : '');
	if(getElement(divId)) {
		getElement(divId).parentNode.removeChild(getElement(divId));
	} else {
		var newDiv = document.createElement('DIV');
		newDiv.id = divId;
		newDiv.className = 'loginWidget';
		newDiv.innerHTML = loginWidget(mode);
		newDiv.style.zIndex = 99999;
		
		setLoginWidgetPosition(thisItem, newDiv, mode);
		getElement('master').appendChild(newDiv);
		if(newDiv.getElementsByTagName('form').length > 0) {
			newDiv.getElementsByTagName('form')[0].elements.email.focus();
		}
	}*/
	try{
	    urchinTracker('/home/top/login/clickopen.html');
	}catch(err){
	
	}
	document.getElementById("logHolder").style.display="";
	document.getElementById("forgotHolder").style.display="none";
	
	if(onoff){
	
		document.getElementById("loginWidget").style.display="";
	}else{
		document.getElementById("loginWidget").style.display="none";
	}
}

function doWidgetForgotPass(thisItem) {
	items = thisItem.form.elements;
	msgDiv = thisItem.parentNode.parentNode.getElementsByTagName('SPAN')[0];
	if(!items.email.value.trim()) {
		msgDiv.innerHTML = 'Please enter your e-mail address.';
	} else {
		var domain = '';
		var email = items.email.value.trim();
		if(email.indexOf('@') == -1) {
			domain = '&domain='+encodeURIComponent(email);
			email = '@';
		}
		var url = '/_login/proxy?path=/servista/reset&t=cp&email='+encodeURIComponent(email)+domain+'&rd=#';
		doAjax(url);
		getElement('forgotPassEmailMessage').innerHTML = 'An e-mail has been sent to your Inbox. Please follow the instructions in the email to reset your password.';
		getElement('forgotPassButton').innerHTML = '<a href="javascript:void(0);" class="noborder" onclick="this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode.parentNode.parentNode.parentNode);return false;"><img src="images/b_closewindow.gif" /></a>';
		getElement('backToLoginDiv').style.display = 'none';
		getElement('forgotPassEmailDiv').style.display = 'none';
	}
}


function doWidgetLogin(thisItem) {
	items = thisItem.form.elements;
	msgDiv = thisItem.parentNode.parentNode.getElementsByTagName('SPAN')[0];
	if(!items.email.value.trim() || !items.pass.value.trim()) {
		msgDiv.innerHTML = 'No match. Please try again.';
	} else {
		msgDiv.innerHTML = 'Checking ...';
		var data = '';
		var email = items.email.value.trim();
		if(email.indexOf('@') == -1) {
			data = 'domain='+encodeURIComponent(email);
		} else {
			data = 'email='+encodeURIComponent(email);
		}
		data += '&pass='+encodeURIComponent(items.pass.value.trim());
		doAjax("/_action/user/doLogin", function(AJAX) {
			if(AJAX.responseText == 1) {
				msgDiv.innerHTML = 'Logging in ...';
				location.href = location.href;
			} else {
				msgDiv.innerHTML = 'No match. Please try again.';
			}
		}, data);
	}
	return false;
}



function forgotIsValidEmail(email) {		

	if (email.length==0){ 

		

			return false;			

	}			

	if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character

		return false;

	} else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @

		return false;

	} else if (email.indexOf("@") == email.length) {  // @ must not be the last character

		return false;

	} else if (email.indexOf("..") >=0) { // two periods in a row is not valid

	return false;

	} else if (email.indexOf(".") == email.length) {  // . must not be the last character

	return false;

	}

	return true;

}





function forgotValidate(){



	var valid=true;

	if(!forgotIsValidEmail(document.getElementById("forgotemail").value)){

		valid = false;	

		alert("Please enter a correct email address.");



	}	

	if(valid){

		forgot(document.getElementById("forgotemail").value);

		document.getElementById("fpHeader_logUser").style.display="none";	

	}

}



function forgotCreateRequestObject() {

	var request_o;

	//declare the variable to hold the object.

	value = false;

	while (!value) {

		if (document.getElementById) {

			try {

				request_o = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

				value = true;

			} catch (e) {

				//trace("ERROR: \n" + e);

				value = false;

			}

		}

	}

	return request_o;

	//return the object

}



function forgot(username) {

	urchinTracker("/login/forgotpassword/email/"+username+".html");

	var faker = new Date().getTime();

	var doc = forgotCreateRequestObject();

	doc.onreadystatechange = function() {

		if (doc.readyState == 4 && doc.status == 200) {

			//trace("hello");

			var respon = doc.responseText;

		

			document.getElementById("forgotalert").style.display="";

			

			

		}

	};

	doc.open("GET", "login.aspx?pwforgot="+username+"&faker=" + faker, true);

	doc.setRequestHeader("Content-Type", "text");

	doc.send(null);

}



	









function Wrangler() {
	
	this.URL = '/_login/proxy?path=/servista/ads';
	this.divRoot = 'wrangler_';
	this.frameId = 'wranglerFrame';

	this.root = null;
	this.log = null;
	this.collection = null;
	this.divs = new Array();
	
	this.add = function(divId) {
		this.divs[this.divs.length] = divId;
		document.write('<div id="'+this.divRoot+divId+'"></div>');
	};

	this.run = function() {
		if(this.divs.length > 0) {
			document.body.appendChild(this.createFrame());
			var query = '';
			for(var i=0; i < this.divs.length; i++) query += 'type='+this.divs[i]+'&';
			query += '&url='+location.href;
			doAjax(this.URL, function(ajax) {
				if(ajax && ajax.responseXML && ajax.responseXML.firstChild) {
					wrangler.root = ajax.responseXML.firstChild;
					if(wrangler.root.nodeName != 'AdCollection') { // for PCIE garbage
						wrangler.root = wrangler.root.nextSibling;
					}
					wrangler.doPoke();
					wrangler.collection = getChildNode(wrangler.root, 'ads');
					wrangler.loadDivs();
					wrangler.fixHistory();
				}
			}, query);
		}
	};

	this.loadDivs = function() {
		for(var i=0; i < this.divs.length; i++) {
			this.loadDiv(this.divs[i]);
		}
	}

	this.loadDiv = function(divId) {
		var node = getChildNode(this.collection, divId);
		if(node) {
			var htmlBody = getNodeValue(node, 'body');
			var htmlHead = getNodeValue(node, 'head');
			if(htmlBody || htmlHead) {
				var fNode = getChildNode(node, 'frame');

				// Create a new iFrame to write to, and append the iFrame to the divId
				if(fNode) {
					var width = getNodeValue(fNode, 'width');
					var height = getNodeValue(fNode, 'height');
					if(width && height) {
						var divFrame = this.createFrame(this.divRoot+divId+'_frame', width, height);
						getElement(this.divRoot+divId).appendChild(divFrame);
						divFrame = frames[divFrame.id];
						this.writeToFrame(divFrame, htmlHead, htmlBody);
					}

				// Write to the Wrangler iFrame, then insert the results into the divId
				} else {
					var wFrame = frames[this.frameId];
					this.writeToFrame(wFrame, htmlHead, htmlBody);
					getElement(this.divRoot+divId).innerHTML = wFrame.document.body.innerHTML;
					wFrame.document.body.innerHTML = '';
				}
			}
		}
	}

	this.createFrame = function(namedId, width, height) {
		if(!namedId) namedId = this.frameId;
		if(!width) width = 0;
		if(!height) height = 0;
		var frame = document.createElement('iframe');
		frame.id = namedId;
		frame.name = frame.id;
		frame.frameBorder = 0;
		frame.scrolling = 'no';
		frame.width = width;
		frame.height = height;
		frame.src = '/_util/blank.html';
		return frame;
	}

	this.writeToFrame = function(frame, htmlHead, htmlBody) {
		if(detect.browser == 'ie') {
			frame.document.open();
			frame.document.write('<html><head>');
			if(htmlHead) frame.document.write(htmlHead);
			frame.document.write('</head><body>');
			if(htmlBody) frame.document.write(htmlBody);
			frame.document.write('</body></html>');
		} else {
			frame.document.open();
			var src = '<html><head></head><body>';
			if(htmlHead) src += htmlHead;
			if(htmlBody) src += htmlBody;
			src += '</body></html>';
			frame.document.write(src);
			frame.document.close();
		}
	}
	
	this.fixHistory = function() {

		/************

		if (IFrameObj.contentDocument) { // For NS6
			IFrameDoc = IFrameObj.contentDocument; 
		} else if (IFrameObj.contentWindow) { // For IE5.5 and IE6
			IFrameDoc = IFrameObj.contentWindow.document;
		} else if (IFrameObj.document) { // For IE5
			IFrameDoc = IFrameObj.document;
		} else {
			return true;
		}
  
		IFrameDoc.location.replace(URL);
		return false;

		*************/


	}





// END WRANGLER
}











