// JavaScript Document
var event_= new Object();
event_.clear=false;

function noaction()
{
};


function getxmlhttpreq()
{ 
	var xhrobj;
	try
	{
    xhrobj= new window.XMLHttpRequest()
	}
	catch(e)
	{
		try
		{
			xhrobj= new window.ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){}
	}
	if (!xhrobj) 
	{
	alert("The browser doesn't support the XMLHttpRequest")
	return null;
	}
	else return xhrobj;
}

function createXML(rootname)
{
  text="<"+rootname+"></"+rootname+">";	
  try
  {	  parser=new DOMParser();
	  xmldoc=parser.parseFromString(text,"text/xml");
  }
  catch(e)
  {
	  xmldoc=new ActiveXObject("Microsoft.XMLDOM");
      xmldoc.async="false";
      xmldoc.loadXML(text); 
   }
return xmldoc;
}

function create_panel(width, parentID,id, instead_close)
{
	panel_cont=document.createElement("DIV");
	document.getElementById(parentID).appendChild(panel_cont);
	panel_cont.setAttribute("class","justdiv");
	panel_cont.setAttribute("id","panel"+id);
    panel_cont.style.marginBottom="5px";
	
	moc_cont=document.createElement("div");
	panel_cont.appendChild(moc_cont);
	moc_cont.style.position="relative";
	moc_cont.setAttribute("id","inf_panel"+id);
	moc_cont.style.marginLeft="auto";
	moc_cont.style.marginRight="auto";
	moc_cont.style.borderWidth="1px";
	moc_cont.style.borderStyle="solid";
	moc_cont.style.borderColor="#C2BDAA";
	moc_cont.style.backgroundColor="#F2F0EA";
	moc_cont.style.width=width;
	moc_cont.style.overflowX="hidden";
	
	moc_close=document.createElement("DIV");
	moc_close.style.backgroundImage="url('global/global_skins/close_button.gif')";
	moc_cont.appendChild(moc_close);
	moc_close.style.position="absolute";
	moc_close.style.width="10px";
	moc_close.style.height="10px";
	moc_close.style.backgroundColor="#C2BDAA"
	moc_close.style.top="3px";
	moc_close.style.left=(moc_cont.offsetWidth-16)+"px";
	if (!instead_close)
	moc_close.setAttribute("onClick","close_moc_panel(event)");
	else 
	moc_close.setAttribute("onClick",instead_close);
	
	moc_close.setAttribute("id","cls"+id);
 
  return moc_cont;
};
function close_moc_panel(event)
{
	
	try
	{
		moc_panel=event.target.parentNode.parentNode;	
	}
	catch(e)
	{
			moc_panel=event.srcElement.parentNode.parentNode;	
	};
	
	moc_panel.parentNode.removeChild(moc_panel);
}

//=====================================================================

function sendcountry(event,path)
{ 

if(event_.clear)
{
	target=event_.target
	id=target.getAttribute('id');
}
else
	try
	{
		target=event.target;
		id=target.getAttribute('id');
	}
	catch(e)
	{
			target=event.srcElement;
			id=target.getAttribute('id');
	};

if(document.getElementById(id))
if(document.getElementById(id).selectedIndex!=0)
	{

	selected_country=document.getElementById(id).options[document.getElementById(id).selectedIndex].value;
	xhr=getxmlhttpreq();
	xhr.onreadystatechange=getcity;
	xhr.countriest_list_id=id;
	
	xhr.open("GET","../global/gain_city.php?country="+selected_country+"&id="+id, true);
	xhr.send(null);
	}
	
	if(document.getElementById(id+"details")!=null)
		{
			target.parentNode.parentNode.removeChild(document.getElementById(id+"details"));
		}
event_.clear=false;
}
function getcity()
{
	if(xhr.readyState==4)
	{
		new_element=document.createElement("div");
		document.getElementById(xhr.countriest_list_id).parentNode.parentNode.appendChild(new_element);
		new_element.setAttribute("class","justdiv");
		new_element.setAttribute("id",xhr.countriest_list_id+"details");
		new_element.innerHTML=xhr.responseText;
		
		selected_country=document.getElementById(xhr.countriest_list_id).options[document.getElementById(xhr.countriest_list_id).selectedIndex].value;
		if(selected_country=="United States")
		{
			states_cont=document.createElement("div");
			document.getElementById(xhr.countriest_list_id+"cities_cont").appendChild(states_cont);
			states_cont.setAttribute("class","justdiv");
			
			states_list='<select name="State" id="'+xhr.countriest_list_id+'states_list"><option value="state">State:</option><option value="AL">Alabama</option><option value="AK">Alaska</option><option value="AZ">Arizona</option><option value="AR">Arkansas</option><option value="CA">California</option><option value="CO">Colorado</option><option value="CT">Connecticut</option><option value="DE">Delaware</option><option value="DC">District Of Columbia</option><option value="FL">Florida</option><option value="GA">Georgia</option><option value="HI">Hawaii</option><option value="ID">Idaho</option><option value="IL">Illinois</option><option value="IN">Indiana</option><option value="IA">Iowa</option><option value="KS">Kansas</option><option value="KY">Kentucky</option><option value="LA">Louisiana</option><option value="ME">Maine</option><option value="MD">Maryland</option><option value="MA">Massachusetts</option><option value="MI">Michigan</option><option value="MN">Minnesota</option><option value="MS">Mississippi</option><option value="MO">Missouri</option><option value="MT">Montana</option><option value="NE">Nebraska</option><option value="NV">Nevada</option><option value="NH">New Hampshire</option><option value="NJ">New Jersey</option><option value="NM">New Mexico</option><option value="NY">New York</option><option value="NC">North Carolina</option><option value="ND">North Dakota</option><option value="OH">Ohio</option><option value="OK">Oklahoma</option><option value="OR">Oregon</option><option value="PA">Pennsylvania</option><option value="RI">Rhode Island</option><option value="SC">South Carolina</option><option value="SD">South Dakota</option><option value="TN">Tennessee</option><option value="TX">Texas</option><option value="UT">Utah</option><option value="VT">Vermont</option><option value="VA">Virginia</option><option value="WA">Washington</option><option value="WV">West Virginia</option><option value="WI">Wisconsin</option><option value="WY">Wyoming</option></select>';
 			
			states_cont.innerHTML=states_list;
			
		};
	};
};

function choosestate(event)
{	
	
	try
	{
		target=event.target;
		target.tagName;
	}
	catch(e)
	{
			target=event.srcElement;
			target.tagName;
	};
	
	states_list=target;
	prefix=target.getAttribute("id").slice(0,target.getAttribute("id").length-11);
	selected_country=document.getElementById(prefix).options[document.getElementById(prefix).selectedIndex].value;
	
	selected_city=document.getElementById(prefix+"cities_list").options[document.getElementById(prefix+"cities_list").selectedIndex];
	
	if(selected_country=="United States")
		{
			state_name=selected_city.getAttribute("state");
			if(state_name!="none")
			{
				for(i=0; i<document.getElementById(prefix+"states_list").options.length; i++) 
				if(state_name==document.getElementById(prefix+"states_list").options[i].value)i2=i;
				document.getElementById(prefix+"states_list").selectedIndex=document.getElementById(prefix+"states_list").options[i2].index;
			}
		}
			
	if(selected_city.text!="Other:")	
	document.getElementById(prefix+"city_box").disabled=true;
	else
	document.getElementById(prefix+"city_box").disabled=false;
		
};


// =================== Here I'm creating usercard =============================================================================
function create_usercard(parentid, id, fullname, photo_url,usertype)
{	
	
	
	card=document.createElement('div');
	document.getElementById(parentid).appendChild(card);
	card.setAttribute("id","card"+id);
	card.setAttribute("class","justdiv");
	card.style.width="162px";
	
	card.setAttribute("onmouseover","menushow(event)");
	card.setAttribute("onmouseout","menuhide(event)");
	
	menu=document.createElement("div");
	card.appendChild(menu);
	menu.setAttribute("class","usercard_menu");
	menu.setAttribute("id","menu"+id);

	
	
	photo_cont=document.createElement('div');
	card.appendChild(photo_cont);
	photo_cont.setAttribute("class","justdiv");
	photo_cont.style.height="160px";
	photo_cont.style.border="1px #666666 solid";
	
	
	photo=document.createElement("img");
	photo.setAttribute("id","photo"+id);
	photo.setAttribute("alt","NO IMAGE");
	photo.style.visibility="hidden";
	photo.setAttribute("onLoad","adjustphoto(event)");
	photo_cont.appendChild(photo);
	photo.setAttribute("src",photo_url);
	
	if(!usertype)
	{
		addf=document.createElement("div");
		menu.appendChild(addf);
		addf.setAttribute("class","menu_item");
		addf.setAttribute("id","addf"+id);
		
		menu_icon=document.createElement("div");
		addf.appendChild(menu_icon);
		menu_icon.setAttribute("class","usercard_menu_icons");
		menu_icon.style.backgroundPosition="0px -136px";
		
		msg=document.createElement("div");
		menu.appendChild(msg);
		msg.setAttribute("class","menu_item");
		msg.setAttribute("id","msg"+id);
		
		menu_icon=document.createElement("div");
		msg.appendChild(menu_icon);
		menu_icon.setAttribute("class","usercard_menu_icons");
		menu_icon.style.backgroundPosition="0px -56px";
		
		stop_float=document.createElement("div");
		stop_float.setAttribute("class","stop_floating");
		menu.appendChild(stop_float);
	}
	
	if(usertype=="nofriend")
	{
		addf=document.createElement("div");
		menu.appendChild(addf);
		addf.setAttribute("class","menu_item");
		addf.setAttribute("id","addf"+id);
		
		menu_icon=document.createElement("div");
		addf.appendChild(menu_icon);
		menu_icon.setAttribute("class","usercard_menu_icons");
		menu_icon.style.backgroundPosition="0px -136px";
				
		stop_float=document.createElement("div");
		stop_float.setAttribute("class","stop_floating");
		menu.appendChild(stop_float);
	}
	
	if(usertype=='friend')
	{
		msg=document.createElement("div");
		menu.appendChild(msg);
		msg.setAttribute("class","menu_item");
		msg.setAttribute("id","msg"+id);
		
		menu_icon=document.createElement("div");
		msg.appendChild(menu_icon);
		menu_icon.setAttribute("class","usercard_menu_icons");
		menu_icon.style.backgroundPosition="0px -56px";
		
		del=document.createElement("div");
		menu.appendChild(del);
		del.setAttribute("class","menu_item");
		del.style.backgroundPosition="0px -136px";
		del.setAttribute("id","del"+id);
		
		menu_icon=document.createElement("div");
		del.appendChild(menu_icon);
		menu_icon.setAttribute("class","usercard_menu_icons");
		menu_icon.style.backgroundPosition="0px -120px";
		
		stop_float=document.createElement("div");
		stop_float.setAttribute("class","stop_floating");
		menu.appendChild(stop_float);		
	};
		
	name_cont=document.createElement("div");
	card.appendChild(name_cont);
	name_cont.align="center";
	name_cont.style.paddingTop="7px";
	name_cont.style.height="30px";
	name_cont.style.overflow="hidden";
	
	name_=document.createElement("a");
	name_cont.appendChild(name_);
	name_.setAttribute('class','labels');
	name_.setAttribute('id','goto'+id);
	name_.setAttribute("href",'#');
	name_.style.textDecoration="none";
	name_.innerHTML=fullname;
	return card;
};
function adjustphoto(event)
{
	try
	{
		IMG_SRC=event.target;
		IMG_SRC.offsetWidth;
	}
	catch(e)
	{
			IMG_SRC=event.srcElement;
			IMG_SRC.offsetWidth;
	};
	if(IMG_SRC.offsetWidth==152) IMG_SRC.style.marginLeft="4px"
	else IMG_SRC.style.marginLeft=((160-IMG_SRC.offsetWidth)/2)+"px";
	
	if(IMG_SRC.offsetHeight==152) IMG_SRC.style.marginTop="4px"
	else IMG_SRC.style.marginTop=((160-IMG_SRC.offsetHeight)/2)+"px";
	IMG_SRC.style.visibility="visible";
	
};

function getParentByPrefix(target,prefix)
{
	parent_=null;
	if(target.getAttribute("id"))
	if(target.getAttribute("id").slice(0,prefix.length)==prefix) 
	{
		parent_=target
		return parent_;
	}
	
		flag=false;
		parent_=target;
		while(!flag)
		{
			parent_=parent_.parentNode;
		   if(parent_.tagName)
			{
				if(parent_.getAttribute("id"))
				if(parent_.getAttribute("id").slice(0,prefix.length)==prefix)
				{
					flag=true
				}
			}
			else 
			{
				parent_=null;
				flag=true;
			};
		};

return parent_;
};

function menushow(event)
{  

	try
	{
		target=event.target;
		target.tagName;
	}
	catch(e)
	{
			target=event.srcElement;
			target.tagName;
	};
	parent_=getParentByPrefix(target,"card");
	if(parent_)
	id=parent_.getAttribute("id").slice(4);
	
	document.getElementById('menu'+id).style.visibility="visible";
	document.getElementById("goto"+id).style.textDecoration="underline";

};

function menuhide(event)
{
	try
	{
		target=event.target;
		target.tagName;
	}
	catch(e)
	{
			target=event.srcElement;
			target.tagName;
	};
	
	parent_=getParentByPrefix(target,"card");
	if(parent_)
	id=parent_.getAttribute("id").slice(4);
	document.getElementById('menu'+id).style.visibility="hidden";
	document.getElementById("goto"+id).style.textDecoration="none";
	
};

function stopfloating(parent_id)
{
	stop_floating=document.createElement("div")
	document.getElementById(parent_id).appendChild(stop_floating);
	stop_floating.setAttribute("class","stop_floating");
};
// =================== (Here I'm creating usercard ) End=============================================================================

function openuser_handler(status)
{
	xhr=getxmlhttpreq();
	param=document.getElementById("openusersate").innerHTML.toLowerCase();
	xhr.open("GET","../global/gain_openuser.php?state="+param,true);
	xhr.onreadystatechange=getopenuserstatus;
	xhr.send(null);
	document.getElementById("openusersate").innerHTML='<img src="../global/global_skins/wait_b.gif"/>';	
};

function getopenuserstatus()
{
	if(xhr.readyState==4)
	{
		document.getElementById("openusersate").innerHTML=xhr.responseText.toUpperCase();
	};
};

//=============================Here I create a scrollbar ===============================================================

function create_scrollbar(parentID,id,list_func, back_func, forward_func)
{
	parent_=document.getElementById(parentID);
	
	mtable=document.createElement('table');
	parent_.appendChild(mtable);
	mtable.style.borderBottom='1px #C2BDAA solid';
	mtable.style.borderTop="1px #C2BDAA solid";
	mtable.width='525px';
	row=document.createElement('tr');
	mtable.appendChild(row);
	
	cell1=document.createElement('td');
	row.appendChild(cell1);
	cell1.width='20px';
	cell1.vAlign="center";
	
	cell2=document.createElement('td');
	row.appendChild(cell2);
	cell2.vAlign="center";
	
	cell3=document.createElement('td');
	row.appendChild(cell3);
	cell3.align='right';
	cell3.vAlign="center";
	
	cell4=document.createElement('td');
	row.appendChild(cell4);
	cell4.align="right";
	cell4.width='90px';
	cell4.vAlign="center";
	
	cell5=document.createElement('td');
	row.appendChild(cell5);
	cell5.width='20px';
	cell5.align="right";
	cell5.vAlign="center";
	
	gbackward=document.createElement('div');
	cell1.appendChild(gbackward);
	gbackward.setAttribute('class','justdiv');
	gbackward.setAttribute('id',"gbackward"+id);
	gbackward.setAttribute('onclick',back_func);
	gbackward.style.width='16px';
	gbackward.style.height='16px';
	gbackward.style.backgroundPosition="0px -152px";
	gbackward.style.backgroundImage="url(../global/global_skins/icons.png)";
	gbackward.style.cursor="pointer";
	gbackward.style.marginRight='5px';
	
	span=document.createElement('span');
	cell2.appendChild(span);
	span.setAttribute('class', 'labels');
	span.setAttribute('id', 'onpage'+id);
	span.innerHTML='On page: 12';
	
	gforward=document.createElement('div');
	cell5.appendChild(gforward);
	gforward.setAttribute('class','justdiv');
	gforward.setAttribute('id',"gforward"+id);
	gforward.setAttribute('onclick',forward_func);
	gforward.style.width='16px';
	gforward.style.height='16px';
	gforward.style.backgroundPosition="0px -168px";
	gforward.style.backgroundImage="url(../global/global_skins/icons.png)";
	gforward.style.cursor="pointer";
	
	list_label=document.createElement('label');
	cell3.appendChild(list_label);
	list_label.setAttribute('class','labels');
	list_label.innerHTML='Current page: ';
	
	
	pages_list=document.createElement('select');
	cell4.appendChild(pages_list);
	pages_list.setAttribute('id','pages_list'+id);
	pages_list.setAttribute('onchange',list_func);
	pages_list.style.width="90px";
	
	
	
};

function showXMLusersbypage(xmldoc, result_cont_id, page_number)
{
		var current_page;
		current_page=respXML.getElementsByTagName('page')[page_number-1];
			
			if(current_page.getElementsByTagName('user').length>0)
			for(i=1;i<=current_page.getElementsByTagName('user').length; i++)
			{
				user=current_page.getElementsByTagName('user')[i-1];
				
				user_id=user.getElementsByTagName('user_id')[0].firstChild.nodeValue;
				fullname=user.getElementsByTagName('first_name')[0].firstChild.nodeValue+" "+user.getElementsByTagName('last_name')[0].firstChild.nodeValue;
				url=user.getElementsByTagName('thumb_url')[0].firstChild.nodeValue;
				
				user_status="";
				
				if(user.getElementsByTagName('openuser').length==0) user_status="nofriend"
				else if(user.getElementsByTagName('openuser')[0].firstChild.nodeValue=="off") user_status="nofriend";
				
				usercard=create_usercard(result_cont_id,user_id, fullname,url,user_status);
				usercard.setAttribute("class","floated_ucard");
				document.getElementById(result_cont_id).appendChild(usercard);
			};
			
			stop_float=document.createElement("div");
			document.getElementById(result_cont_id).appendChild(stop_float);
			stop_float.setAttribute("class","stop_floating");
};

// here I create result statistic 
function createstatisticsbar(parentID, id, first_text, second_text)
{
	bar=document.createElement('div');
	document.getElementById(parentID).appendChild(bar);
	bar.setAttribute('class','justdiv');
	bar.setAttribute('id',id);
	bar.setAttribute('style','height:30px; border-bottom:1px #C2BDAA solid; border-top:1px #C2BDAA solid');
	
	bar.innerHTML='<label id="rndm_openusers" class="labels" style="display:block; float:left; margin-top:7px; margin-left:10px;">'+first_text+'</label><label id="total_openusers" class="labels" style="display:block; float:right; margin-top:7px; margin-right:10px">'+second_text+'</label><div class="stop_floating"></div>';
};