/*timestamp to avoid ajax cache - add at end of URL (+'/'+timestamp)*/
var day = new Date();
var timestamp = day.getTime();

/*to recognise the Enter button - invoke like onKeyPress="checkEnter(event) */
function checkEnter(e, how)
{ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable
	if(e && e.which)
	{ //if which property of event object is supported (NN4) e = e
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		if (how == 'dropdown')
		{
			set_showing();
		}
		else
			document.forms['login'].submit(); //submit the form
		return false;
	}
	else
	{
		return true;
	}
}

function checkSearchSubmit(e)
{ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which)
	{ //if which property of event object is supported (NN4) e = e
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		set_showing();
		return false;
	}
	else
	{
		return true;
	}
}

/* XML AjaX Objects. Begin */
var spanid;
var trashid;

function stateChangedVote()
{
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
	{
			document.getElementById(trashid).innerHTML=xmlHttp2.responseText
			var the_node = document.getElementById(trashid);
			execJS(the_node);
	}
}

function stateChangedALL()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
			document.getElementById(shows).style.display="block"
			if (xmlHttp.responseText != "")
				document.getElementById(shows).innerHTML=xmlHttp.responseText
			else
				document.getElementById(shows).style.display="none"
			var the_node = document.getElementById(shows);
			execJS(the_node);
			if(document.getElementById(trashid))
			{
				xmlHttp2.send(null);
			}
	}
	else
	{
			
	}
}

function GetXmlHttpObject()
{ 
    var req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) 
	{
		try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    }
	else if(window.ActiveXObject) 
	{
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	return req
}

//code to execute JS in innerHTML - borrowed from http://kratcode.wordpress.com/2006/03/07/javascript-script-execution-in-innerhtml-the-revenge/
function execJS(node)
{
  var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
  var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
  var bMoz = (navigator.appName == 'Netscape');

  if (!node) return;

  /* IE wants it uppercase */
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;

  for(var i=0;i<st.length; i++)
  {
    if (bSaf) {
      strExec = st[i].innerHTML;
      st[i].innerHTML = "";
    } else if (bOpera) {
      strExec = st[i].text;
      st[i].text = "";
    } else if (bMoz) {
      strExec = st[i].textContent;
      st[i].textContent = "";
    } else {
      strExec = st[i].text;
      st[i].text = "";
    }

    try {
      var x = document.createElement("script");
      x.type = "text/javascript";

      /* In IE we must use .text! */
      if ((bSaf) || (bOpera) || (bMoz))
        x.innerHTML = strExec;
      else x.text = strExec;

      document.getElementsByTagName("head")[0].appendChild(x);
    } catch(e) {
      alert(e);
    }
  }
};

function set_showing()
{
	if (document.forms['search_form'].qs.value != "")
	{
		if (document.search_form.type.value == "events" || document.search_form.type.value == "discounts")
		{
			document.search_form.showing.value = "Y"
		}
		else if (document.search_form.type.value == "eventsa")
		{
			document.search_form.showing.value = "B"
			document.search_form.type.value = "events"
		}
		else
		{
			var p2 = document.getElementsByTagName('input')[0];
			p2.parentNode.removeChild(p2); 
		}
		document.forms['search_form'].submit();
	}
}

function fc(){
document.search_form.qs.focus()
}

//for set neigbourhood

//some part of this code is in blocks/section/home/set_neighbourhood.php, since it required some php. read along with that for comprehension. moved whatever possible here to reduce size of the HTML pages. - kishore
var menu = new Array();
var menuwidth = 112;
var xxxx = 765;
var yyyy = 500;
var fcolor = '<font color="#FFFFFF" size="2">';
var defLength = 21;
menu[0] = new Array();
var xval,yval,diff,halfeddiff;
var webpagewidth=760;
diff=(screen.width-webpagewidth);
halfeddiff= 0.5 * diff;
xval=90+halfeddiff;
menu[0][1] = new Item('', '#', '', 0, 0, 1);
menu[1] = new Array();
menu[1][1] = new Item('<font color="#FFFFFF" size="2">Choose area...</font>', '#', '', defLength, 0, 0);
var alpha_range = new Array("A-H","I-M","M-Z");

var area_list = new Array();
//change anything here in inc/variables.config.php too
area_list[2] = new Array ("Abids","Afzalgunj","Amberpet","Ameerpet","Banjara Hills","Begumpet","Bowenpally","Charminar","Dilsukhnagar", "Marredpally(E)","Himayatnagar");
area_list[3] = new Array ("Imliban BS","Jubilee BS","Kachiguda","Khairatabad","Koti","Kukatpally","Lakdi-ka-Pul","Langer House","Madhapur","Malakpet","Malkajgiri");
area_list[4] = new Array ("Mehdipatnam","Nampally","Osmania","Panjagutta","Ramanthapur","Ranigunj","Sanathnagar","Santosh Nagar","Secunderabad","Trimulgherry","Vanasthalipuram");
var area_coords = new Array(); 
area_coords[2] = new Array("18151913","2116379","18193735","13113835","15123910","1114128","09151611","22142814","20210514","1119191","1616303");//map+alphabet+number
area_coords[3] = new Array("20163516","11172734","18172536","15133611","18160416","05062712","16130931","19080531","12043923","20190722","11222818");
area_coords[4] = new Array("17113227","17140312","1620184","13120627","17211817","13160718","10123417","23182418","13183410","09183936","25262226");


var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);

function getRef(id) 
{
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
}

function getSty(id) 
{
	return (isNS4 ? getRef(id) : getRef(id).style);
} 

var popTimer = 0;
var litNow = new Array();

function popOver(menuNum, itemNum) 
{
	clearTimeout(popTimer);
	hideAllBut(menuNum);
	litNow = getTree(menuNum, itemNum);
	changeCol(litNow, true);
	targetNum = menu[menuNum][itemNum].target;
	if (targetNum > 0) {
		thisX = parseInt(menu[menuNum][0].ref.left) + parseInt(menu[menuNum][itemNum].ref.left);
		thisY = parseInt(menu[menuNum][0].ref.top) + parseInt(menu[menuNum][itemNum].ref.top);
		with (menu[targetNum][0].ref) {
			left = parseInt(thisX + menu[targetNum][0].x)+'px';
			top = parseInt(thisY + menu[targetNum][0].y)+'px';
			visibility = 'visible';
			zIndex = '1000';
		}
	}
}

function popOut(menuNum, itemNum) 
{
	if ((menuNum == 0) && !menu[menuNum][itemNum].target)
		hideAllBut(0)
	else
		popTimer = setTimeout('hideAllBut(0)', 000);//can put time here - e. g. 500 for 0.5s
}

function getTree(menuNum, itemNum) 
{
	itemArray = new Array(menu.length);
	while(1) {
		itemArray[menuNum] = itemNum;
		if (menuNum == 0) return itemArray;
		itemNum = menu[menuNum][0].parentItem;
		menuNum = menu[menuNum][0].parentMenu;
	}
}

function changeCol(changeArray, isOver) 
{
	for (menuCount = 0; menuCount < changeArray.length; menuCount++) {
		if (changeArray[menuCount]) {
			newCol = isOver ? menu[menuCount][0].overCol : menu[menuCount][0].backCol;
			with (menu[menuCount][changeArray[menuCount]].ref) {
				if (isNS4) bgColor = newCol;
				else backgroundColor = newCol;
			}
		}
	}
}

function hideAllBut(menuNum) {
	var keepMenus = getTree(menuNum, 1);
	for (count = 0; count < menu.length; count++)
	if (!keepMenus[count])
	menu[count][0].ref.visibility = 'hidden';
	changeCol(litNow, false);
}

function nothing(menuNum,itemNum)
{
	//setTimeout("popOver('"+menuNum+"','"+itemNum+"')",10);
	popOver(menuNum,itemNum);
	return;
}

function Menu(isVert, popInd, x, y, width, overCol, backCol, borderClass, textClass) {
	this.isVert = isVert;
	this.popInd = popInd
	this.x = x;
	this.y = y;
	this.width = width;
	this.overCol = overCol;
	this.backCol = backCol;
	this.borderClass = borderClass;
	this.textClass = textClass;
	this.parentMenu = null;
	this.parentItem = null;
	this.ref = null;
}

function Item(text, href, frame, length, spacing, target) {
	this.text = text;
	this.href = href;
	this.frame = frame;
	this.length = length;
	this.spacing = spacing;
	this.target = target;
	this.ref = null;
}

function writeMenus() 
{
	if (!isDOM && !isIE4 && !isNS4) return;

	for (currMenu = 0; currMenu < menu.length; currMenu++) with (menu[currMenu][0]) 
	{
		var str = '', itemX = 0, itemY = 0;
		for (currItem = 1; currItem < menu[currMenu].length; currItem++) with (menu[currMenu][currItem]) 
		{
			var itemID = 'menu' + currMenu + 'item' + currItem;
			var w = (isVert ? width : length);
			var h = (isVert ? length : width);
			
			if (isDOM || isIE4) 
			{
				str += '<div id="' + itemID + '" style="position: absolute; left: ' + itemX + 'px; top: ' + itemY + 'px; width: ' + w + 'px; height: ' + h + 'px; visibility: inherit;';
				if (backCol) str += 'background: ' + backCol + '; ';
				str += '" ';
			}
			if (isNS4) 
			{
				str += '<layer id="' + itemID + '" left="' + itemX + '" top="' + itemY + '" width="' +  w + '" height="' + h + '" visibility="inherit" ';
				if (backCol) str += 'bgcolor="' + backCol + '" ';
			}
			if (borderClass) 
				str += 'class="' + borderClass + '" ';

			str += 'onMouseOver="nothing(' + currMenu + ',' + currItem + ')" onMouseOut="popOut(' + currMenu + ',' + currItem + ')" onClick="popOut(' + currMenu + ',' + currItem + ')">';

			str += '<table width="' + (w - 8) + '" border="0" cellspacing="0" cellpadding="' + (!isNS4 && borderClass ? 3 : 0) + '"><tr><td align="left" height="' + (h - 7) + '">' + (itemID != "menu1item1" ? '<a style="display: block" class="' + textClass + '" href="' + href + '"' + (frame ? ' target="' + frame + '">' : '>') : '') + text + (itemID != "menu1item1" ? '</a>' : '') + '</td>';
			if (target > 0) 
			{
				menu[target][0].parentMenu = currMenu;
				menu[target][0].parentItem = currItem;
				if (popInd) str += '<td class="' + textClass + '" align="right">' + popInd + '</td>';
			}
			str += '</tr></table>' + (isNS4 ? '</layer>' : '</div>');
			if (isVert) itemY += length + spacing;
			else itemX += length + spacing;
		}
		if (isDOM) 
		{		
			var newDiv = document.createElement('div');
			document.getElementById('caldrop11').appendChild(newDiv);
			newDiv.innerHTML = str;
			ref = newDiv.style;
			ref.position = 'relative';
			ref.visibility = 'hidden';
		}
		if (isIE4) 
		{
			document.body.insertAdjacentHTML('beforeEnd', '<div id="menu' + currMenu + 'div" ' + 'style="position: absolute; visibility: hidden">' + str + '</div>');
			ref = getSty('menu' + currMenu + 'div');
		}
		if (isNS4) 
		{
			ref = new Layer(0);
			ref.document.write(str);
			ref.document.close();
		}
		for (currItem = 1; currItem < menu[currMenu].length; currItem++) 
		{
			itemName = 'menu' + currMenu + 'item' + currItem;
			if (isDOM || isIE4) menu[currMenu][currItem].ref = getSty(itemName);
			if (isNS4) menu[currMenu][currItem].ref = ref.document[itemName];
		}
	}
	with(menu[0][0]) 
	{
		ref.left = 0;
		ref.top = 0;
		ref.visibility = 'visible';
	}
}

//to set cookie
function setneighbourhood(loc,x,y,bx,by){
	SetCookie('LocName',loc,100);	
	SetCookie('XCord',x,100);	
	SetCookie('YCord',y,100);
	SetCookie('BlockX',bx,100);	
	SetCookie('BlockY',by,100);	
	document.forms['currneigh'].currlocation.value = loc;
}

function SetCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)
				 + ";expires="+expire.toGMTString()+";path=/";
}

//remove extra whitespace
function remove_EXTRA_whitespace(item)
{
  item.value = item.value.replace(/\r/g, " ");
  item.value = item.value.replace(/[^ A-Za-z0-9`~!@#\$%\^&\*\(\)-_=\+\\\|\]\[\}\{'";:\?\/\.>,<]/g, "");
  item.value = item.value.replace(/'/g, "");
  item.value = item.value.replace(/ +/g, " ");  
  item.value = item.value.replace(/^\s/g, "");
  item.value = item.value.replace(/\s$/g, "");	
  if (item.value == ' '){item.value = ''; alert(error);}
}

function popUp(URL, id)
{
	eval("page" + timestamp + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=yes,location=0,statusbar=0,menubar=0,resizable=yes,width=500,height=375,left = 100,top = 50');");
}

function addlisting(type, from, orig_url)
{
	var f = document.forms["add_listing"];
	var params = "";
	if (f!=null)
	{
		for (i=0;i<(f.elements.length);i++) {
			params = params + f.elements[i].name +'='+ escape(f.elements[i].value) + '&'; 
		}
	}

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	shows = "addlisting";
	if (from=="footer")
		shows = "addlisting_f";
	var url = '/section/other/addlisting.php/'+type+'/'+timestamp+'/'+shows;
	if (orig_url!=null)
	{
		url = url + "?orig_url="+ orig_url + '&' 
	}
	else
	{
		url = url + "?"
	}
	url = url + params;
	xmlHttp.onreadystatechange=stateChangedALL;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

/*in multi-block roundups, if the blocks are coming from different tables, then if 2 (or more) records gave the same ID in their respective tables, there will be a problem with the save link, since there will be 2 forms with the same name*/
function savesubmit(formname, loc, noofvenues, which, mapid)
{
	var f = document.forms[formname];
	var params = "";
	for (i=0;i < (f.elements.length);i++) {
		params = params + f.elements[i].name +'='+ encodeURI(f.elements[i].value) + '&'; 
	}

	shows = loc;
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url = '/section/myspace/save.php/'+which+'/'+f.eventid.value+'/1/'+mapid+'/'+noofvenues
	var eventid = f.eventid.value;
	url = url + "?" + params;
	/*the following 3 lines have to be _before_ the 3 xmlHttp lines after that - originally they were after, and IE would flop*/
	var update_loc = eventid + 'save';
	var update_form = eventid;
	xmlHttp.onreadystatechange=stateChangedALL;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	updatesave(update_form, update_loc, noofvenues, which, mapid);
}

function updatesave(id, loc, noofvenues, which, mapid)
{
	trashid = loc;
	xmlHttp2=GetXmlHttpObject();
	if (xmlHttp2==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url2 = '/section/myspace/updatesave.php/'+which+'/'+id+'/1/'+mapid+'/'+noofvenues
	xmlHttp2.onreadystatechange=stateChangedVote;
	xmlHttp2.open("GET",url2,true);
	//xmlHttp2.send(null);
}

var trashid="";

function loginbox(path,type,profile,loc,more,tab)
{
	if(loc)
	{
		shows = loc;
	}
	else
	{
		shows = "loginbox";
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url='/blocks/profiles/common/user_rating.php?type='+type+'&profile='+profile+'&more='+more+'&tab='+tab+'&shows='+shows;
	if(more == 'true')
	{
		xmlHttp.onreadystatechange = showMoreRatings;
	}
	else
		xmlHttp.onreadystatechange = stateChangedALL;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showMoreRatings() {
	stateChangedALL();
	morerating("ratingform", false, "morerating");
}

function votesbox(id,loc,url2)
{
	trashid = loc;
	xmlHttp2=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	 alert ("Browser does not support HTTP Request");
	 return;
	}
	if(url2=="")
	{
		var url='/blocks/profiles/common/login.php';
	}
	url2 = url2 + "?review="+id;
	xmlHttp2.onreadystatechange=stateChangedVote;
	xmlHttp2.open("GET",url2,true);
}

function morerating(ratingform, checkflag, loc)
{
	var f = document.forms[ratingform];
	var rating_done_flag = 0;
	var params = "";
	if (checkflag == true)
		check = checkrating(ratingform);
	else 
		check = true;
	if(check == true)
	{
		f.submit_rating_done.value = "";
		for (i=0;i < f.elements.length;i++) { 
			if(f.elements[i].type == 'radio' || f.elements[i].type == 'checkbox')
			{
				if(f.elements[i].checked)
				{
					params = params + f.elements[i].name +'='+ encodeURIComponent(f.elements[i].value) + '&'; 
					if (f.elements[i].name == "rating_1" || f.elements[i].name == "rating_2" || f.elements[i].name == "rating_3" || f.elements[i].name == "rating_4" || f.elements[i].name == "rating_overall")
						rating_done_flag = 1;
				}
			}
			else if (f.elements[i].name && f.elements[i].value)
			{
				params = params + f.elements[i].name +'='+ escape(encodeURIComponent(f.elements[i].value)) + '&';
			}
		}
		shows = loc;
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}
		if(document.getElementById('morehide'))
		{
			document.getElementById('morehide').style.display = "none";
			document.getElementById('morehide').innerHTML = "";
		}
		if(shows == "rating_main_block" || shows == "rating_main_block_editorial" || shows == "loginbox" || shows == "editorial_loginbox")
		{
			params = params + "submit_rating_done=1&";
			params = params + "xmlrequest=1&";
		}
		else
		{
			if(!document.getElementById('morehide'))
			{
				f.submit_rating_done.value = "Submit";
			}
		}
		params = params + "shows="+shows;
		var url='/blocks/profiles/common/user_store.php';
		url=url + "?" + params;
		xmlHttp.onreadystatechange=stateChangedALL;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		return false;
	}
	else
	{
		return false;
	}
}
	
function deletevote(formname, loc, url)
{
	document.forms[formname].deleterating.value="delete";
	loginsubmit(formname, loc, url);
}

function loginsubmit(formname, loc, url)
{
	var f = document.forms[formname];
	var params = "";
	var reviewid="";
	var flag = 0;
	var validateradio ="";
	if(f.more)
	var more = f.more.value;
	else
	var more = 'false';
	for (i=0;i < f.elements.length;i++) { 
		if(f.elements[i].type == 'radio' || f.elements[i].type == 'checkbox')
		{
			if(f.elements[i].checked)
			{
				params = params + f.elements[i].name +'='+ encodeURIComponent(f.elements[i].value) + '&'; 
				flag =1;
			}
		}
		else
		{
			params = params + f.elements[i].name +'='+ encodeURIComponent(f.elements[i].value) + '&';
			if( f.elements[i].name == "review")
			{
				reviewid = f.elements[i].value;
			}
			if( f.elements[i].name == "validateradio")
			{
				validateradio = f.elements[i].value;
			}
		}
	 } 
	if(f)
	{
		if(validateradio == "check")
		{
			if(flag == 0)
			{
				alert("Please select an option. Very Helpful is 2 votes, Helpful is 1, Somewhat Helpful is 0.5 and Not Helpful is -1. You can change your vote at any time.");
				return false; 
				exit;
			}
		}
	}
	shows = loc;
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	if(url=="")
	{
		var url='/blocks/profiles/common/login_test.php';
	}
	url = url + "?" + params;
	if(more == 'true')
	{
		xmlHttp.onreadystatechange = showMoreRatings;
	}
	else
		xmlHttp.onreadystatechange = stateChangedALL;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	if(reviewid != "")
	{
		var vote='votes'+ reviewid;
		var url2 = '/blocks/profiles/common/updatevotes.php';
		votesbox(reviewid, vote, url2);
	}
}

function checkrating(ratingform)
{
	flag=0;
	var f = document.forms[ratingform];

	for (i=0;i < f.elements.length;i++) { 
			if(f.elements[i].type == 'radio')
			{
				if(f.elements[i].checked == true)
				{
					flag = 1;
				}
			}
	 } 
	if(flag==1)
		return true;
	else
	{
		alert("Please select a rating?");
		return false;
	}
	return false;
}

function generate_ads(section)
{
	var height = document.getElementById('core_table').clientHeight - 190; //subtracting top till 'ADVERTISEMENTS', and footer
	if (section == 'classifieds')
		height = height + 30;
	if (document.getElementById('ads_placeholder'))
	{
		document.getElementById('ads_placeholder').innerHTML = "<IFRAME FRAMEBORDER='0' WIDTH='125' HEIGHT='"+height+"' ALLOWTRANSPARENCY='true' MARGINWIDTH='0' MARGINHEIGHT='0' SCROLLING='NO' SRC='/blocks/section/common/ads_right_filler.php?height="+height+"'></IFRAME>";
	}
}

// restricting no of chars in form - by hscripts.com

function limiter(myform, comment, count)
{
	var tex = document.forms[myform][comment].value;
	var len = tex.length;
	if(len > count)
	{
        tex = tex.substring(0,count);
        document.forms[myform][comment].value = tex;
        return false;
	}
	document.getElementById('characters_left').innerHTML = count-len+" characters left";
}
