var xmlhttp;

showPopup = function(theURL,jsname,id,width,height)
{
	var IpopTop = (document.body.clientHeight - height) / 2;
	var IpopLeft = (document.body.clientWidth - width) / 2;
	document.getElementById(id).innerHTML="<p> Please wait... </p>";
	document.getElementById(id).style.left=IpopLeft + 'px';
	document.getElementById(id).style.top='20px';
	document.getElementById(id).style.height=height + 'px';
	document.getElementById(id).style.width=width + 'px';
	document.getElementById(id).style.display = "block";
	loadURL(theURL,id);
	if(jsname != "NO_JS_NEEDED")
	{
	var scr = document.createElement('script'); 
	scr.setAttribute('type','text/javascript'); 
	scr.setAttribute('src',jsname); 
	document.body.appendChild(scr);                 
	}
}

function OCE_login_submit()
{
	deleteErrors('id_500_login');
	var count = 0;
	var ln = document.forms['td_login_form'].elements['username'].value.length;
	if (ln == 0 || ln > 50)
	{
		document.getElementById('id_500_login').innerHTML += "<p> * Username must be provided. </p>";
		count++;
	}
	ln = document.forms['td_login_form'].elements['password'].value.length;
	if (ln == 0 || ln > 50)
	{
		document.getElementById('id_500_login').innerHTML += "<p> * Password must be provided. </p>";
		count++;
	}
	if(count > 0)
	{
		document.getElementById('id_500_login').style.display="block";
		return false;
	}
	else
	{
		//loadPostURLWithParameters('/TestersDesk/login.do','id_200','no_input','id_500','td_login_form');
		document.forms['td_login_form'].action="/TestersDesk/login.do";
		document.forms['td_login_form'].submit();
	}
}

closeMessage = function(id)
{
	document.getElementById(id).style.display = "none";
}

loadURL = function(url,id)
{
	showIt('ProgressBar');
	document.getElementById(id).innerHTML="<p style='margin:10px;color:red;font-weight:bold;'>loading...</p>";
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{	
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
	xmlhttp.onreadystatechange=	function ()
									{
										if (xmlhttp.readyState==4)
										{
											hideIt('ProgressBar');
											if (xmlhttp.status==200)
											{
												document.getElementById(id).innerHTML=xmlhttp.responseText;
												document.getElementById(id).style.display='block';
											} 
											else
											{
												alert("a technical problem has occured\r\n");
											}
										}
									}
									;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","text/html;charset=UTF-8");
	xmlhttp.send('xnw=24');
	}
	else
	{
		hideIt('ProgressBar');
		alert("Your browser does not support XMLHTTP.");
	}
}
showIt = function(id)
{
	if(document.getElementById(id) != null)
	{
	document.getElementById(id).setAttribute("class","show");
	document.getElementById(id).setAttribute("className","show");
	}
}
hideIt = function(id)
{
	if(document.getElementById(id) != null)
	{
	document.getElementById(id).setAttribute("class","noshow");
	document.getElementById(id).setAttribute("className","noshow");
	}
}

loadPostURLWithParameters = function(url,id_200,id_input,id_500,formName)
{
	showIt('ProgressBar');
	url = url + "?NoCache=" + Math.floor(Math.random()*10000); // to prevent caching
	xmlhttp=null;
	document.getElementById(id_500).style.display="none";
	if (window.XMLHttpRequest)
	{
	xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null)
	{
	xmlhttp.onreadystatechange=	function ()
									{
										if (xmlhttp.readyState==4)
										{
											hideIt('ProgressBar');
											if (xmlhttp.status==200)
											{
												document.getElementById(id_200).style.display="block";
												document.getElementById(id_200).innerHTML=xmlhttp.responseText;
												if(formName == "td_login_form")
												{
													setTimeout('successfulLogin()', 5000);
												}
											} 
											else if (xmlhttp.status==500)
											{
												document.getElementById(id_500).style.display="block";
												document.getElementById(id_500).innerHTML=xmlhttp.responseText;
											}
											else if (xmlhttp.status==413)
											{
												document.getElementById(id_500).style.display="block";
												document.getElementById(id_500).innerHTML="<p style='font-weight:bold;'>Request Entity Too Large</p><p>TestersDesk.com does not process requests that have more than 100KB</p> <p> If you really have data more than 100KB, please send it to ops@testersdesk.com and we will process it for FREE and send you back the results. Thanks, Ops Team. </p>";
											}
											else
											{
												alert("a technical unknown problem has occured\r\n");
											}
										}
									}
									;
		xmlhttp.open("POST",url,true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		var paramData = extractFormParameters(formName);
		xmlhttp.send(paramData);
		}
		else
		{
			alert("Your browser does not support XMLHTTP.");
			hideIt('ProgressBar');
		}
}

extractFormParameters = function(theForm){
var i = 0;
var returnValue = "";
var theFormDOM = document.getElementById(theForm);
var theElementType;
for(i=0; i < theFormDOM.elements.length; i++)
{
	theElementType = theFormDOM.elements[i].type;
	if(theElementType != "button" && theElementType != "submit" && theElementType != "image" && theElementType != "file" && theElementType != "reset")
	{
		if(theElementType == "radio" || theElementType == "checkbox")
		{
			if(theFormDOM.elements[i].checked)
			{
				returnValue+= theFormDOM.elements[i].name + "=" + encodeURIComponent(theFormDOM.elements[i].value);
			}
		}
		else
		{
			returnValue+= theFormDOM.elements[i].name + "=" + encodeURIComponent(theFormDOM.elements[i].value);
		}
	}
	if((i+1) < theFormDOM.elements.length)
	{returnValue+="&";}
}
return returnValue.replace(/&&$/,"");
}
dynamicPopupClose = function() {
  document.getElementById("dynamicPopup").style.display = "none";
}
successfulLogin = function()
{
	window.location.replace("/TestersDesk/TestersDesk.jsp");
}
hideProgressBars = function ()
{
	hideIt('ProgressBar');
}
function deleteErrors(theID)
{
	document.getElementById(theID).innerHTML='';
	document.getElementById(theID).style.display='none';
}

function registerUser()
{
	var errCount = 0;

	deleteErrors("id_500_register");

	if(document.forms['RegistrationForm'].elements['username'].value == '')
	{
		document.getElementById('id_500_register').innerHTML += "<p> * Looks like you have not entered a Username?</p>";
		errCount++;
	}
	var ln = document.forms['RegistrationForm'].elements['username'].value.length;
	
	if (ln < 3 || ln >15)
	{
		document.getElementById('id_500_register').innerHTML += "<p> * Username must be between 3 and 15 characters.</p>";
		errCount++;
	}

	if (document.forms['RegistrationForm'].elements['password'].value == '')
	{
		document.getElementById('id_500_register').innerHTML += "<p> * Please enter a password.</p>";
		errCount++;
	}

	ln = document.forms['RegistrationForm'].elements['password'].value.length;
	
	if (ln < 3 || ln >15)
	{
		document.getElementById('id_500_register').innerHTML += "<p> * Password must be between 3 and 15 characters.</p>";
		errCount++;
	}

	if (document.forms['RegistrationForm'].elements['email'].value == '')
	{ 
	  document.getElementById('id_500_register').innerHTML += "<p> * Please enter a valid E-mail ID.</p>"; 
	  errCount++;
	}

	if(!document.forms['RegistrationForm'].elements['tc'].checked)
	{
		document.getElementById('id_500_register').innerHTML += "<p> * We cannot register you unless you agree to the terms and conditions. </p>"; 
		errCount++;
	}

	if(errCount > 0)
	{
		document.getElementById('id_500_register').style.display="block";
		return false;
	}
	else
	{
		document.forms['RegistrationForm'].submit();
	}
}