/**
 * Friendship popUps
 * @author Roy, Erick S
 * @version 1.1.3
 */
 
 /** constants **/
 var FRIENDSHIP_FRIENDREQUEST = 'friend_request';
 var FRIENDSHIP_FRIENDREMOVE = 'friend_remove';
 var FRIENDSHIP_RELATION = 'relation';
 var FRIENDSHIP_POKE = 'poke';
 var FIRENDSHIP_ADDTOMYCLASS = 'addtomyclass';

/** Global popup variable */
var popup = null;

if (typeof xhr == 'undefined') var xhr;
var friendshipAction;
var action1; // @deprecated
var arr=new Array();
var schoolAreaSelect=-1;
var idfSelect=-1;
var Myschool=-1;
var Classnum=-1;
var ClassOT=-1;
var Mador=-1;
var Wedates=-1;
var Mycity=-1;

/**
* interact with friends
*
* @author Eliko
* @param int action
* @param mixed param1
*/
function friendshipInteract(action, param1) {
	var url = '/srv/my/friends.php';
	if (!xhr) xhr = new XHR();

	switch (action) {
		case FRIENDSHIP_POKE:
			xhr.Post(url,'act='+FRIENDSHIP_POKE+'&memberId='+param1,response,action);
			return; // don't continue
		case FIRENDSHIP_ADDTOMYCLASS:
			xhr.Post(url,'act='+FIRENDSHIP_ADDTOMYCLASS+'&memberId='+param1,response,action);
			return; // don't continue
		case FRIENDSHIP_FRIENDREMOVE:
			if (!confirm('האם את/ה בטוח שברצונך למחוק חבר זה?'))
				return;
		case FRIENDSHIP_FRIENDREQUEST:
		case FRIENDSHIP_RELATION:
			url = url + '?act='+action+'&memberId='+param1;
			break;
	}
	
	friendshipAction = action;
	xhr.Get(url,popFriendshipInteract,param1);
}

function popFriendshipInteract(responseText,id) { 
	// if(action1!='popUserid') document.getElementById('loading').style.display='none';
	if (popup == null) {
		popup = new StaticPopup();
		popup.onShow = function() {
			if (friendshipAction == FRIENDSHIP_RELATION || friendshipAction=='popUserid') {
				var div=document.getElementById('div1');
				
				if (div.innerHTML.length>0)
					arr = eval('('+div.innerHTML+');');
				div.innerHTML='';

				for(i=0;i<arr.length;i++) {
					//get me the place they living or the family relation between them
					if((arr[i]['subcategory']>=18 && arr[i]['subcategory']<=20) || (arr[i]['subcategory']>=9 && arr[i]['subcategory']<=12) )
						document.getElementById('checkbox' + arr[i]['subcategory']).checked=true;
					  
					//get me the army unit text  
					if(arr[i]['data']!='-1' && arr[i]['data']!='' && arr[i]['category']=='3') {
						idfSelect=arr[i]['extraid'];
						// alert(idfSelect);
						document.getElementById('textfield' + arr[i]['category']).value=arr[i]['data']
					}
					
					//get me the school part 
					if(arr[i]['category']=='2') {
					  schoolAreaSelect=arr[i]['extraid2'];
					  Myschool=arr[i]['extraid'];

					  Classnum=arr[i]['data'].charAt(arr[i]['data'].length-1);
					  ClassOT=arr[i]['data'].substr(0,arr[i]['data'].length-1);
					}
					
					//get the city we live select field
					if(arr[i]['subcategory']==20)
					 Mycity=arr[i]['extraid'];
					
					//get mador selected
					if(arr[i]['subcategory']==22)
						Mador=arr[i]['extraid'];
						
					//get we use to date
					 if(arr[i]['subcategory']==25)
						Wedates=arr[i]['extraid'];
						
					//get inputs text
					if(arr[i]['subcategory']==23)
					 document.getElementById('textfield6').value=arr[i]['data'];
					else
					if(arr[i]['subcategory']==27)
					 document.getElementById('textfield8').value=arr[i]['data'];
					 
					 //get dates fields
					 //if(arr[i]['subcategory']==23)
					 
					document.getElementById('test'+arr[i]['category']).style.display='block';
					document.getElementById('img'+arr[i]['category']).src='../images/on_icon.gif';
				}
				getidf(idfSelect);
		    	getCities(schoolAreaSelect,Mycity);
				getMadorim(Mador);
				getWeDated(Wedates);
				//restart vars
				schoolAreaSelect = idfSelect = Myschool = -1;
				Classnum = ClassOT = Mador = Wedates = Mycity = -1;
			}
		}
		popup.onOk = function(id)
		{
			var url = '/srv/my/friends.php';
			switch (friendshipAction) {
				case FRIENDSHIP_FRIENDREQUEST:
					var vars=document.getElementById('friendRequestExtra').value;
					var postInfo = 'act='+friendshipAction+'&memberId=' + id+'&textarea1='+vars;
					xhr.Post(url,postInfo,askFriendResult);
					break;
				case FRIENDSHIP_FRIENDREMOVE:
				case FRIENDSHIP_POKE:
					var postInfo = 'act='+friendshipAction+'&memberId=' + id;
					xhr.Post(url, postInfo, resultFunc, friendshipAction);
					break;
				case FRIENDSHIP_RELATION:
					var postInfo='sendrequest=3&senderId=' + id;
					document.forms['M'].submit();
					break;
				case 'popUserid':
					var postInfo='sendrequest=4&senderId=' + id;
					document.forms['M'].submit();
				break;
			}
		}
	}

	popup.data = id;
	popup.setContent(responseText,500,260);
	popup.showCenter(StaticPopup.BUTTON_OK_CANCEL);
	popup.drawBorder();
}

function response(response)
{
	MessageBox.alert(response);
}

function askFriendResult(response)
{
	if (response != 'ok')
	{
		MessageBox.alert(response);
		return;
	}

	var element = $('sendFriendRequest');
	if (element != null) // User page
		element.innerHTML = 'הצעת החברות נשלחה';
	else // Friends page
		MessageBox.alert('הצעת החברות נשלחה');
}

function resultFunc(postInfo,action1)
{
	if (postInfo != 'ok' || action1 =='delFriend')
	{
		//MessageBox.alert('הפעולה בוצעה');
		window.location=window.location.href;
	}
	//MessageBox.alert(postInfo);
}

function OpenCloseDiv(obj)
{
	var test='';//'test'+obj;
	var img='';//'img'+obj;

	for(var i=1;i<=8;i++)
	{
		//var radio='radio'+i;
		 test='test'+i;
         img='img'+i;
		
		if(obj==i)
		{
			  document.getElementById(test).style.display='block';
			  document.getElementById(img).src='../images/on_icon.gif';
		}
		else
		{
			document.getElementById(test).style.display='none';
 			document.getElementById(img).src='../images/off_icon.gif';
		}

		 
    }
  document.getElementById('RBcat').value=obj;

  popup.drawBorder();
}

function DblOpenClose(obj)
{	
	if(document.getElementById('test'+obj).style.display=='none')	
		document.getElementById('test'+obj).style.display='block';
	else
		document.getElementById('test'+obj).style.display=='none'

	//document.getElementById('radio'+obj).checked=false; 
	document.getElementById('RBcat').value=obj;   
	popup.drawBorder();
}

function selectYear()
{
	var date=new Date();
	var year=date.getYear();
	w='';
   // alert(year);
	for( i=1948;i<year;i++) document.write('<option value="'+i+'">'+i+'</option>');
}
	 
function selectMonth()
{
	for(i=1;i<=12;i++) document.write('<option value="'+i+'">'+i+'</option>');
}

function getidf(S)
{
	var idfUnit = Forms.getidfUnit();
	var html2 = new Array();
	for (var i=0;i<idfUnit.length;i++)
	{
		if(S!=null && S!=idfUnit[i][0])
			html2.push("<option value='"+idfUnit[i][0]+"'>"+idfUnit[i][1]+"</option>");
		else
			html2.push("<option value='"+idfUnit[i][0]+"' selected>"+idfUnit[i][1]+"</option>");
	}
	html2 = html2.join('');
	document.getElementById('selectUnit').innerHTML = "<select name='armyUnit'><option value='-1'>בחר</option>"+html2+"</select>";

	html2=null;
}
	      	
function getCities(S,L)
{	
	var yeshuvim = Forms.getYeshuvim();
	var html = new Array();
	var html2 = new Array();
	for (var i=0;i<yeshuvim.length;i++)
	{
		//if(S!=-1)
		if(S!=yeshuvim[i][0])
			html.push("<option value='"+yeshuvim[i][0]+"'>"+yeshuvim[i][1]+"</option>");
		else
			html.push("<option value='"+yeshuvim[i][0]+"' selected>"+yeshuvim[i][1]+"</option>");
		//if(L!=-1)
		if(L!=yeshuvim[i][0])
			html2.push("<option value='"+yeshuvim[i][0]+"'>"+yeshuvim[i][1]+"</option>");
		else
			html2.push("<option value='"+yeshuvim[i][0]+"' selected>"+yeshuvim[i][1]+"</option>");
	}
	html = html.join('');
	html2 = html2.join('');

	document.getElementById('selectCity').innerHTML = "<select name='City'><option value='-1'>בחר</option>"+html2+"</select>";
	document.getElementById('selectCity2').innerHTML = "<select name='schoolCity' onchange='getMyschool(this.value)'><option value='-1'>בחר</option>"+html+"</select>";

	if(Myschool!='' || Myschool!=null) getMyschool(S,Myschool);
	html=null;
	html2=null;
}

function getMyschool(P_id,Sschool)
{
	var school = new Array();
	school=Forms.getSchool(P_id);

	var html3 = new Array();
	if(school)
	{
		for (var i=0;i<school.length;i++)
		{
			if(Sschool!=null && Sschool!=school[i][1])
				html3.push("<option value='"+school[i][1]+"'>"+school[i][2]+"</option>");
			else
				html3.push("<option value='"+school[i][1]+"' selected>"+school[i][2]+"</option>");
		}
		html3 = html3.join('');
		document.getElementById('selectSchool').innerHTML = "בית ספר:<select name='school' onchange='getMyClass()'><option value='-1'>בחר</option>"+html3+"</select>";

		if(ClassOT!=null && ClassOT!='') getMyClass(ClassOT);
		html3=null;
	}
	//return;
	//popup.drawBorder();
}

function getMyClass(ot)
{
	var values = ['א','ב','ג','ד','ה','ו','ז','ח','ט','י','יא','יב'];
	var html1 = new Array();
	for (var i=0;i<values.length;i++)
	{
		if(ot!=null && ot!=values[i])
			html1.push("<option value='"+values[i]+"'>"+values[i]+"</option>");
		else
			html1.push("<option value='"+values[i]+"' selected>"+values[i]+"</option>");
	}
	html1 = html1.join('');
	document.getElementById('selectClass').innerHTML = "כיתה:<select name='myclass' onchange='getClassnum()'><option value='-1'>בחר</option>"+html1+"</select>";

	if(Classnum!=null && Classnum!='') getClassnum(Classnum);
	html1=null;
	//popup.drawBorder();
}

function getClassnum(num)
{
	var values = ['1','2','3','4','5','6','7','8','9','10','11'];
	var html1 = new Array();
	for (var i=0;i<values.length;i++)
	{
		if(num!=null && num!=values[i])
			html1.push("<option value='"+values[i]+"'>"+values[i]+"</option>");
		else
			html1.push("<option value='"+values[i]+"' selected>"+values[i]+"</option>");
	}
	html1 = html1.join('');
	document.getElementById('selectClassnum').innerHTML = "מספר:<select name='myclassnum'><option value=''>בחר</option>"+html1+"</select>";

	html1=null;
	//popup.drawBorder();
}

function getMadorim(num)
{
	var values = 
	[
		[123,"פורומים"],
		[124,"היפים והאמיצים"],
		[125," לוח מודעות "],
		[126,"סקס משין"],
		[127,"פרוזה"],
		[128,"בדיחות"],
		[129,"עמוד משתמש"],
		[130,"חיזור גורלי"]
	];
	var html1 = new Array();
	for (var i=0;i<values.length;i++)
	{
		if(num!=null && num!=values[i][0])
			html1.push("<option value='"+values[i][0]+"'>"+values[i][1]+"</option>");
		else
			html1.push("<option value='"+values[i][0]+"' selected>"+values[i][1]+"</option>");
	}
	html1 = html1.join('');
	document.getElementById('selectMador').innerHTML = "<select name='selectA[0]' id='select5'><option value='-1'>בחר</option>"+html1+"</select>";

	html1=null;
	//popup.drawBorder();
}
		
function getWeDated(num)
{
	var values =
	[
		[123,"יצאנו פעם אחת ודי"],
		[124,"קצר וכיפי"],
		[125," היה רציני לקצת זמן"],
		[126,"זה מסובך"]
	];
	var html1 = new Array();
	for (var i=0;i<values.length;i++)
	{
		if(num!=null && num!=values[i][0])
			html1.push("<option value='"+values[i][0]+"'>"+values[i][1]+"</option>");
		else
			html1.push("<option value='"+values[i][0]+"' selected>"+values[i][1]+"</option>");
	}
	html1 = html1.join('');
	document.getElementById('selectWedated').innerHTML = "<select name='selectA[1]' id='select7'><option value='-1'>בחר</option>"+html1+"</select>";

	html1=null;

	//popup.drawBorder();
}
