var xmlHttp;


function flagDeal(idToFlag,direction)
	{
	document.getElementById("flagID_" + idToFlag).innerHTML="";

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request");
		return;
		}
	var url="flagDeal.php?ID="+idToFlag + "&direction=" + direction;

	xmlHttp.onreadystatechange=flaggedStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	}

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}

function flaggedStateChanged()
	{
	//alert("statechanged called for element " + " " + " ready state: " + xmlHttp.readyState);
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ //alert(xmlHttp.responseText);
		if(xmlHttp.status == 200)
			{
			var breakChar=xmlHttp.responseText.indexOf(',');
			var dealID=xmlHttp.responseText.substr(0,breakChar);
			var votes=xmlHttp.responseText.substr(breakChar+1);
			document.getElementById("votesSpanID_" + dealID).innerHTML=" " + votes + " votes";
			}
		}
	}


function GetXmlHttpObject()
	{
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
		{
		objXMLHttp=new XMLHttpRequest();
		}
	else if (window.ActiveXObject)
		{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	return objXMLHttp;
	}
	
function toggleDiv(divid){
    if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
	  if(divid == 'filterSiteDiv') {
		document.getElementById('browseTagDiv').style.display = 'none';
	  }
	  if(divid == 'browseTagDiv') {
		document.getElementById('filterSiteDiv').style.display = 'none';
	  }
    }else{
      document.getElementById(divid).style.display = 'none';
    }
  }

function getDivStatus(elementID){
	if(document.getElementById(divid).style.display == 'none'){
      return 0;
    }else{
      return 1;
    }	
}

function toggleSpan(elementID){
    if(document.getElementById(elementID).style.display == 'inline'){
      document.getElementById(elementID).style.display = 'none';
    }else{
      document.getElementById(elementID).style.display = 'inline';
    }
  }

 function toggleSiteFilterDiv(divid){
    if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
       document.getElementById("siteFilterDivState").value = 'block';
    }else{
      document.getElementById(divid).style.display = 'none';
       document.getElementById("siteFilterDivState").value = 'none';
    }
  }

function refreshForm() {
  	document.FilterForm.submit();
  }

function jumpTo(formid, currpage, currview, hours, orderby){
	perPage = document.getElementById(formid).value;
	hours = document.getElementById(hours).value;

	window.location.href = 'index.php?perpage=' + perPage + '&page=' + currpage + '&view=' + currview + '&hours=' + hours + '&orderby=' + orderby;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function toggleSiteDeals(siteID)
	{
	var dealsToToggle=getElementsByClass("dealSite_" + siteID);
	for (i=0;i<dealsToToggle.length;i++)
		{
		if(dealsToToggle[i].style.display == 'none')
			dealsToToggle[i].style.display = 'inline';
		else
			dealsToToggle[i].style.display = 'none';
		}
	}

function hideJaveWarn()
	{
	var jWarn=document.getElementById("javaWarn");
	jWarn.style.display="none";
	jWarn.style.opacity=0;
	jWarn.style.visibility="hidden";
	jWarn.style.height="0";
	}

function passCheck()
	{

	if( document.getElementById("pass1").value == document.getElementById("pass2").value)
		{
		if(document.getElementById("pass1").value.length < 7)
			{
			document.getElementById("passCheck").innerHTML=" Password too short! ";
			document.getElementById("passCheck").style.color="red";
			}
		else
			{
			document.getElementById("passCheck").innerHTML=" Passwords match! ";
			document.getElementById("passCheck").style.color="green";
			}
		}
	else
		{
		document.getElementById("passCheck").innerHTML=" Passwords don't match! ";
		document.getElementById("passCheck").style.color="red";
		}
	}

function validSUForm() //validates the signup form
	{
	//validation here is just for ease of use. Full validation done in createAccount.php on form submit.
	var vaildForm=1;
	if(document.getElementById("username").value.length < 3)
		vaildForm=0;
	else if(document.getElementById("passCheck").style.color =="red")
		vaildForm=0;
	else if(document.getElementById("email").value.length < 6)
		vaildForm=0;
	if(vaildForm)
		document.getElementById("formSubmitButton").disabled=0;
	else
		document.getElementById("formSubmitButton").disabled=1;

	}

function setCookie(c_name,value,expiredays)
	{
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
	}

function quickLogOut()
	{
	xmlHttp=GetXmlHttpObject();

	xmlHttp.onreadystatechange=clearLogInCookie;
	xmlHttp.open("GET","logIn.php?logOut=logOut",true);
	xmlHttp.send(null);
	}

function clearLogInCookie()
	{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
		if(xmlHttp.status == 200)
			{
			setCookie("dealSitesSessionID","0");
			window.location.reload(true);
			}
		}
	}


function addWatchBox()
	{
	var watches=document.getElementById("numWatches");
	var numWatches=watches.value;
	var watchList=document.getElementById("watchList");

	var newWatch = document.createElement('input');
	newWatch.setAttribute("name","watch_" + numWatches);
	newWatch.setAttribute("id","watch_" + numWatches);
	newWatch.setAttribute("type","text");
	newWatch.setAttribute("class","searchBox");
	newWatch.setAttribute("id","watch_" + numWatches);
	watchList.appendChild(newWatch);

	var newElememt = document.createElement('a');
	newElememt.setAttribute("href","javascript:void(null)");
	newElememt.setAttribute("onClick","testSearch(" + numWatches + ");");
	newElememt.innerHTML="Test search ";
	watchList.appendChild(newElememt);

	var newElement = document.createElement('br');
	watchList.appendChild(newElement);

	var newElememt = document.createElement('label');
	newElememt.setAttribute("for","watchTriggerEmail_"+numWatches);
	newElememt.innerHTML=" E-mail: ";
	watchList.appendChild(newElememt);

	var newElememt = document.createElement('input');
	newElememt.setAttribute("name","watchTriggerEmail_"+numWatches);
	newElememt.setAttribute("type","checkbox");
	newElememt.setAttribute("value","triggerEmail-" + numWatches );
	watchList.appendChild(newElememt);


	var newElememt = document.createElement('label');
	newElememt.setAttribute("for","watchTriggerRSS_"+numWatches);
	newElememt.innerHTML=" RSS: ";
	watchList.appendChild(newElememt);

	var newElememt = document.createElement('input');
	newElememt.setAttribute("name","watchTriggerRSS_"+numWatches);
	newElememt.setAttribute("type","checkbox");
	newElememt.setAttribute("value","triggerRSS-" + numWatches );
	watchList.appendChild(newElememt);


	var newElememt = document.createElement('label');
	newElememt.setAttribute("for","removeWatches[]"+numWatches);
	newElememt.innerHTML=" Remove Watch: ";
	watchList.appendChild(newElememt);


	var newCheckBox = document.createElement('input');
	newCheckBox.setAttribute("name","removeWatches[]");
	newCheckBox.setAttribute("type","checkbox");
	newCheckBox.setAttribute("value","new-" + numWatches + "-NULL");
	watchList.appendChild(newCheckBox);

	var newBreak = document.createElement('br');
	watchList.appendChild(newBreak);

	var newBreak = document.createElement('br');
	watchList.appendChild(newBreak);

	watches.value++;
	}


function testSearch(searchToTest)
	{
	var watches=document.getElementById("watch_" + searchToTest);
	if(watches)
		{
		window.open("index.php?terms=" + watches.value,"Test_search","width=800, height=600");
		}

	}

function openHelp()
	{
		window.open("feedhelp.php","Deal_Watch_Help","width=800, height=600, scrollbars=yes");
	}

function navBarLogin()
	{
	xmlHttp=GetXmlHttpObject();
	var username=document.getElementById("navBarUsername").value;
	var password=document.getElementById("navBarPassword").value;
	var toSend="username=" + username + "&password="+ password + "&rememberLogin=1";
	xmlHttp.onreadystatechange=navBarLogInDone;
	xmlHttp.open("POST","logIn.php",true);
	//alert("logging in... with: " + toSend);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader( "Content-length", toSend.length );
	xmlHttp.setRequestHeader( "Connection", "close" );
	xmlHttp.send(  toSend );
	}

function navBarLogInDone()
	{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
		if(xmlHttp.status == 200)
			{
			var regex = new RegExp("(Cannot log in.*?)<");
			var matches= regex.exec(xmlHttp.responseText);
			if(matches != null && matches.length > 1)
				alert(matches[1] + "\nPlease try again.");
			else
				window.location.reload(true);
			//alert( xmlHttp.responseText );
			}
		else { alert("Server error encountered while sending login information.\nPlease try again. If the problem persists, contact us."); }
		}
	}

function addTag(dealID,userID)
	{

	var tagToAdd=document.getElementById("addTags_" + dealID).value;

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{alert ("Browser does not support HTTP Request");	return;}
	var url="addTag.php?userID="+ userID + "&dealID=" + dealID+ "&tagName=" + tagToAdd;

	xmlHttp.onreadystatechange=dealTagged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}

function dealTagged()
	{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
		if(xmlHttp.status == 200)
			{
			//var elementToTag=document.getElementById("tagSpan_" + dealID);
			//elementToTag.innerHTML.=" <a href=\"?tagid=\">" + tag +" </a> ";

			//alert( xmlHttp.responseText );
			window.location.reload(true);
			}
		}
	}
	
	
	
function countClick(dealID)
	{
	xmlHttp=GetXmlHttpObject();

	xmlHttp.onreadystatechange=countClickDone;
	xmlHttp.open("GET","clickCounter.php?dealID=" + dealID,true);
	xmlHttp.send(null);
	}
	
function countTagClick(tagID)
	{
	xmlHttp=GetXmlHttpObject();

	xmlHttp.onreadystatechange=countClickDone;
	xmlHttp.open("GET","tagClickCounter.php?tagID=" + tagID,true);
	xmlHttp.send(null);
	}

function countClickDone()
	{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
		if(xmlHttp.status == 200)
			{
			}
		}
	}