// 1mv script

/// GENERAL FUNCTIONS


	

 // TOGGLE DISPLAY OF AN ID
	function show(Id)
		{
		document.getElementById(Id).style.display = 'block';
		}
		
	function hide(Id)
		{
		document.getElementById(Id).style.display = 'none';
		}
	
	function tog(Id)
		{
		var t = document.getElementById(Id);	
		if(t.style.display == 'none')
			{show(Id);}
		else
			{hide(Id);}
		}	
	
	function showId(Id)
		{
		document.getElementById(Id).style.display = 'block';
		}	
	
	
	
	function more(Id) // Togs the name of the button src_[ID] aswell as [ID] display
		{
		tog(Id);
		var t = document.getElementById('src_' + Id);
		
		if(t.innerHTML == 'more')
			{ t.innerHTML = 'less'; }
		else
			{ t.innerHTML = 'more'; }
		
		}
		
		
///remove node/div id
    function removeObject(Id) {
        parentObj = document.getElementById(Id).parentNode;
        childObj = document.getElementById(Id); 

        parentObj.removeChild(childObj);
    }		
	
	
	
		
///////////// AJAX STUFF:	
	
  ///// Id represents the traget id 

	function ajaxOpen(url, Id, loading)
            {
			var t = document.getElementById(Id).innerHTML;
			var options = {
							method : 'get',
							onCreate: function() 
									{
										if(loading != '')
											{document.getElementById('loading').innerHTML = '<img src="img/loading.gif" />';}
									},										
							onSuccess : function(transport) {
										if(loading != '')
											{document.getElementById('loading').innerHTML = '';}
										
										var tTXT = transport.responseText;
										
										if(t != tTXT)
											{
											document.getElementById(Id).innerHTML = tTXT;
 											}		
											
											if(Id == 'newstuff' & t != document.getElementById(Id).innerHTML)
												{ new Effect.SlideDown('live0'); }											
											
											if(Id == 'newqbox' & t != document.getElementById(Id).innerHTML)
												{ new Effect.SlideDown('live1'); 	}
											
											
											if(Id == 'results')
											{ if(document.getElementById(Id).innerHTML != '')
													{ new Effect.SlideDown('live_com'); }	
												else
													{new Effect.SlideUp('live_com');}
												
											}
											
									
									

									}
						  };
			nocache = Math.random();			  
			new Ajax.Request(url+'&nocache='+nocache, options);	

            }


//// LIVE SEARCH
	function searchContacts()
		{	
			var q = document.getElementById('search-q').value;
			var url = 'index.php?ajax=contact&sq='+q;
			ajaxOpen(url, 'results', 'loading');
			var out = document.getElementById('results').innerHTML;
			if(out != '')
				{ document.getElementById('results').style.display = 'block'; }
			}

function callback(id, value)
			{ 
			var cb = document.getElementById('usr' + id)
			if(cb.checked != true){
				var a = '<a href="#" onclick="remtag(\'usr' + id + '\', \'usrdv' + id + '\')" id="usrdv' + id + '" title="Remove">';
				document.getElementById('tolist').innerHTML = document.getElementById('tolist').innerHTML + a + value +  '</a> '; 
				cb.checked = true;					
				}
				document.getElementById('search-q').value = '';	
				
			if(cb.checked == true)
				{var e = document.getElementById('results');
				e.style.display="none";
				}
			}
			
/// END LIVE SEARCH




	function ajaxSaveQuestion(Id)
		{
			
			var v = document.getElementById('vote_'+Id).value, q = document.getElementById('qid_'+Id).value;
			if(v == '')
				{tog('vote_note_'+Id);}
			else
				{ajaxOpen('index.php?ajax=quickvote&qid='+q+'&vote='+v, Id, '');}
			
		}

        function ajaxSkipQuestion()
                {
                        q = document.getElementById('qid').value;
                        ajaxOpen('index.php?ajax=quickvote&qid='+q+'&vote=-1', 'tspan', 'q_loading');

                }

	function ajaxSaveQuestionBasic()
		{
			var v = document.getElementById('vote1').value, q = document.getElementById('qid1').value;
			ajaxOpen('index.php?ajax=quickvotebasic&qid='+q+'&vote='+v, 'abox2', 'loadbox2');
		}


	function verify_me(DivId, Ident)
		{
		var v = document.getElementById('verifyv_'+DivId).value, q = document.getElementById('verifyq_'+DivId).value;	
		var c = document.getElementById('verify_'+DivId).value;
		ajaxOpen('index.php?ajax=quickvote&qid='+q+'&ident='+Ident+'&vote='+v+'&code='+c, 'verifybox_'+DivId, 'loading');
		
		}



	// (id of textarea, targetId , qid, loading div )
	function ajaxComment(commId, Id, qId, loading)
            {
				var comtext = encodeURI(document.getElementById(commId).value);
                var options = {
                                method : 'post',
								onCreate: function() 
										{
											if(loading != '')
												{
												document.getElementById(loading).innerHTML = '<img src="img/results_load_small.gif" />';
												
												}
										},										
								onSuccess : function(transport) {
										document.getElementById(Id).innerHTML = transport.responseText;
											if(loading != '')
												{
												document.getElementById(loading).innerHTML = '';
												document.getElementById(commId).value = '';
												}
										}
                              };
                new Ajax.Request('index.php?ajax=comlist&qid=' + qId + '&addcom=' + comtext, options);
				
				
            }

	//remove comment
	 function remCom(Id, page) {
		 
		var target = 'com_' + page + '_' + Id;
		
		if(confirm("Are you sure you want to remove this comment?")==true)
			{ajaxOpen('index.php?ajax=delcom&delid='+Id, target, target); }
			
		 
	 }
	 
	 
	 /// monitor question
	 function monitor(Id, page, remove) {
		 			
		var target = 'target_' + page + '_' + Id;			
	ajaxOpen('index.php?ajax=monitor&q='+Id, target, target); 	
		 
	 }	 
	 
	 
	 
	//confirm friend
	 function confirmFriend(fid, ignore) {
		 			
		var target = 'friend_req_id_' + fid;
		
		//var comments = encodeURI(document.getElementById('friend_req_com'+fid).value);	
		
							  
		if(ignore == 'ignore')
			{ ajaxOpen('index.php?ajax=con_friend&ignore_f=yes&con_id=' + fid + '&comments=', target, target);
			new Effect.Highlight(target, { startcolor: '#fffbea', endcolor: '#fffdf4', duration: 3 }); }
		else
			{ ajaxOpen('index.php?ajax=con_friend&con_id=' + fid + '&comments=', target, target);
			new Effect.Highlight(target, { startcolor: '#fffbea', endcolor: '#fffdf4', duration: 3 }); }							  
							  
		//document.getElementById(target).innerHTML = '';	

	 }
 
	 
	 function viewCat(Id)
	 	{ajaxOpen('index.php?ajax=catview&cat='+Id, 'browse_box', 'loading');}
	 
	
	
	
	
	
	/// view profile elements
	
	
	function lounge(p, uid)
		{
			var url = 'index.php?ajax=lounge&page='+p+'&pid='+uid;
			
			ajaxOpen(url, 'lounge_content', 'loading');	
			
	
		}
	
	
	//QUESTION EXPLORER
	function togstyle(Id, Style){		
		document.getElementById(Id).className = Style;
	}

	function q_explore(Url){
		ajaxOpen('?ajax='+Url, 'page_content', '');
	}
	function show_load(){
		document.getElementById('pg_loader').style.display = 'block';
	}	
	
	


/// settings page cat order
  function updateHomeOrder()
						{
						
							var options = {
											method : 'post',
											parameters : Sortable.serialize('home_order'),
											onSuccess : function(transport) {
																			var n = document.getElementById('home_out');
																			n.innerHTML = transport.responseText;
																			}								
										  };
			 
										new Ajax.Request('index.php?ajax=sort&type=home', options); 
										 }
			 
						
			 
			 
				function updateOrder()
						{
						
							var options = {
											method : 'post',
											parameters : Sortable.serialize('cat_order'),
											onSuccess : function(transport) {
																			var n = document.getElementById('cat_out');
																			n.innerHTML = transport.responseText;
																			}								
										  };
			 
							new Ajax.Request('index.php?ajax=sort&type=cat', options); 
						
						}


function initDragables()
	{ Sortable.create('home_order', { onUpdate : updateHomeOrder }); 
		Sortable.create('cat_order', { onUpdate : updateOrder });
	}

/// end settings page cat order
	
	
	
	
	
	
	
	///homepage feed data
	function InsertFeedData(timestamp, lastitem)
	{
	
	var o = document.getElementById('oldstuff'), n = document.getElementById('newstuff');			
	document.getElementById('live0').style.display = 'none';
	
	ajaxOpen('index.php?ajax=getactivity&timestamp='+timestamp, 'newstuff', 'loading');
	var ran = Math.random();
			 
	if(n.innerHTML != '')
		{
		
			o.innerHTML = n.innerHTML + o.innerHTML;
			n.innerHTML = '';

		}	

	
	window.setTimeout('InsertFeedData('+timestamp+', "")',30000);
	}
	
	
	
	
	
	
	
	
	
	///// END AJAX

	 
	 
	 
	 
//// WINDOWS FUNCTIONS
function openwin(Url, Title)
	{
	var win = new Window({className: "alphacube", title: Title, width:400, minimizable: false, maximizable: false, showEffectOptions: {duration:1} });
	win.setAjaxContent(Url);
	win.setZIndex(999999);
	win.showCenter();	
	WindowCloseKey.init();
	}

function openWindow(Url, Title, w, h)
	{
	var win = new Window({className: "alphacube", title: Title, width: w, height: h, minimizable: false, maximizable: false, showEffectOptions: {duration:1} });
	win.setAjaxContent(Url);
	win.setZIndex(999999);
	win.showCenter();	
	WindowCloseKey.init();
	}

function openDialog(Url)
	{
	     new Dialog.confirm({url: "dialog_ajax.html", options: {method: 'get'}}, 
                     {top: 10, width:250, className: "alphacube"})   
	}
	
function login()
	{
	var win = new Window({className: "alphacube", title: "Login to onemorevote", width:500, height: 200, minimizable: false, maximizable: false, showEffectOptions: {duration:1} });
	win.setAjaxContent('index.php?ajax=login');
	win.showCenter();
	WindowCloseKey.init();		
	}





   
///////
//
// General scripts



function check_for_new(timestamp)
	{
		var Id = 'new_feed';
		/*
		                var options = {
                                method : 'get',
								onCreate: function() 
										{
											if(loading != '')
												{document.getElementById('loading').innerHTML = '<img src="img/loading.gif" />';}
										},										
								onSuccess : function(transport) {
											if(loading != '')
												{document.getElementById('loading').innerHTML = '';}
											
											replace_if_new(Id, transport.responseText);
										
										
										if(Id == 'target')
											{ new Effect.Highlight(Id, { startcolor: '#fffbea', endcolor: '#ffffff', duration: 3 }); }
										}
                              };
							 
				 new Ajax.Request('index.php?ajax=check_new&timestamp='+timestamp, options);
				 */
				ajaxOpen('index.php?ajax=check_new&timestamp='+timestamp, Id, 'loading'); 					
				window.setTimeout('check_for_new('+timestamp+')',20000);
				
				
	}


//if(document.getElementById(Id).innerHTML != transport.responseText)		
											//document.getElementById(Id).innerHTML = transport.responseText;


function replace_if_new(Id, newhtml)
	{
	var t = document.getElementById(Id).innerHTML;
	if(t != newhtml)
		{document.getElementById(Id).innerHTML = newhtml;}
	}




function getvalue(Id)
	{
	return Id + '=' + encodeURI( document.getElementById(Id).value );
	}


function upval(Id, val)
	{document.getElementById(Id).value = val;}



//check if val if not then make border red
	function check_val(val, id)
		{
		var t = document.getElementById(id);
		if(t.value == '')
			{t.className = 'form_alert';}
		else
			{t.className = 'none';}
		}
		
		
function omvPop(ref)
	{window.open(ref,'omvpop_win','width=600,height=550,scrollbars=1,resizable=1');}

function openLink(ref)
	{window.open(ref,'omv_win','width=600,height=550,scrollbars=1,resizable=1');}
		
function print_q(ref)
	{window.open('index.php?ajax=print&qid='+ref,'omv_print','width=700,height=600');}		
		
//askq menu

function aqover(Act)
	{
			hide('aq0');
			
			for(i=1; i <= 3; i++)
				{
					if(Act == i)
						{ show('aq'+i); }
					else
						{ hide('aq'+i); }
				}
	}

		
function aqout()
	{
		show('aq0');
			
		for(i=1; i <= 3; i++)
			{ hide('aq'+i);}
						
	}
		
////////////////////////////////		
		
		
	
	function open_q(Id)
		{
		open_url(Id, 'target');
		open_url(Id + '&f=e', 'target2');
		
		}
		
		
		
	function tab(Id, count, active)
		{
			
		for(i=0; i <= count; i++)
			{var d = document.getElementById(Id + i);
			var a = document.getElementById('a' + Id + i);
			if(a != null & d != null)
				{
				if(active == i)
					{d.style.display = 'block';
					if(Id == 'men')
						{a.style.background = '#ffffff';
						a.style.color = '#2a2a2a';
						}
						
					a.className = 'act';	
					}
				else
					{d.style.display = 'none';
					if(Id == 'men')
						{a.style.color = '#333333';
						a.style.background = 'none'
						}
					a.className = '';
					}
				}
			}
			
	
		}
   
   
   
   /// Set a given field value to a new value
 function setField(Id, val)
 	{
		document.getElementById(Id).value = val;
		
	}
   
 function setFieldHtml(Id, val)
 	{
		document.getElementById(Id).innerHTML = val;
		
	}   
   
   
   
   
   
 
 
 /// tags
 
 function tag(ckId, dvId, text, out)
	{
	var ckbox = document.getElementById(ckId), dvbox = document.getElementById(dvId), h = document.getElementById(out); 
	 
	 if(ckbox.checked == true)
	 	{
		var  curr = h.innerHTML;
		//alert(text + ' - ' + dvId + ' - ' + curr + ' - TRUE!!') 
		var a = '<a href="javascript:remtag(\'' + ckId + '\', \'' + dvId + '\')" id="' + dvId + '" title="Remove">';
		h.innerHTML = a + text + ', </a>' + curr;
		
		}
	
	}


	
function remtag(ckId, dvId)
	{
	var ckbox = document.getElementById(ckId), dvbox = document.getElementById(dvId); 	 
	 if(ckbox.checked == true)
	 	{
		ckbox.checked = false
		removeObject(dvId);		
		}
	
	}
   
   
 
   
   function clearall(Id)
	{
	var c = document.getElementById(Id + 'count').value
		for(i=1;i<=c;i++)
		{ document.getElementById(Id + i).value = ''; 
		
		}
	}


function clearfield(Id)
	{
		var c = document.getElementById(Id);
		c.value = '';
	}


//add extra answers
function addans(Id)
	{
	var o = '';
	var c = document.getElementById(Id + 'count'), d = document.getElementById(Id), v = document.getElementById(Id + '1');
	var n = c.value;
	
	var vs=new Array()
	var vl=new Array()
	var va=new Array()
	for(i=1;i<=n;i++)
		{ 
		v = document.getElementById(Id + i).value; 
		if(Id == 'lnk')
			{var vn = document.getElementById(Id + 'name' + i).value, vsp = document.getElementById(Id + 'sp' + i).checked;
			vs[i] = vsp;
			vl[i] = vn;}
		 
		  va[i] = v;
		}
	n++;

	c.value = n;
	
	if(Id == 'lnk')
		{var o = '<p><label><b>Link ' + n + '.</b>  Name: </label><input id="' + Id + 'name' + n + '" type="text" name="' + Id + 'name' + n + '" maxlength="250" /><br /> <label>Link:</label> <input id="' + Id + n + '" type="text" name="' + Id + n + '" maxlength="250" /> <br /><label for="' + Id + 'sp' + n + '">Sponsored</label><input type="checkbox" value="true" id="' + Id + 'sp' + n + '" name="' + Id + 'sp' + n + '" style="width:15px;" /> </p><hr />';}
	
	if(Id == 'ans' & n <= 15)
		{var o = '<p><label>Answer ' + n + ': </label> <input id="' + Id + n + '" type="text" size="50" name="' + Id + n + '" maxlength="250" /></p>';}	
	
	
	d.innerHTML = d.innerHTML + o ;
	
	for(i=1;i<n;i++)
		{ var t = document.getElementById(Id + i); 
			if(Id == 'lnk')
			{var tn = document.getElementById(Id + 'name' + i), tsp = document.getElementById(Id + 'sp' + i);
			tsp.checked = vs[i];
			tn.value = vl[i];}
			
		  t.value = va[i];
		  }
	
		
	return;
	
	}
	
	

	
	
	
	//////////////////////////////////////////////////////////
	/// new clean stuff under here ******************************
	//////////////////////////////////////////////////////////

	
	
	
	
	
	
		
//Vote interface
	
	function saveMyVote(vars, target){
		ajaxOpen('index.php?ajax=savemyvote&target='+target+'&'+vars, target, 'loading');
	}
	
	
	function homevote(v, q){
		
		saveMyVote('id='+q+'&aid='+v, 'homevote');
	}
	//load the video for a question
	function viewVid(qid, target, wh){
		ajaxOpen('index.php?ajax=viewvid&id='+qid+'&wh='+wh, target, 'loading');		
	}
	
	//load comments for a question
	function viewComments(qid, target){
		ajaxOpen('index.php?ajax=comlist&target='+target+'&qid='+qid, target, 'loading');		
	}
	
	
	//GROUPS related	
	//savegroup
	function saveGroup(target){
		var name = encodeURI(document.getElementById('group_name').value);
		var descr = encodeURI(document.getElementById('group_description').value);
		
		ajaxOpen('index.php?ajax=savegroup&target='+target+'&name='+name+'&desc='+descr, target, 'loading');		
	}
	//remove group
	function removeGroup(gid,target){
		ajaxOpen('index.php?ajax=removegroup&target='+target+'&gid='+gid, target, 'loading');		
	}
	//load members
	function selectGroup(gid,target){
		ajaxOpen('index.php?ajax=selectgroup&target='+target+'&gid='+gid, target, 'loading');	
	}
	//add member to group
	function addGroupMember(gid,uid,target){
		ajaxOpen('index.php?ajax=addgroupmember&target='+target+'&gid='+gid+'&uid='+uid, target, 'loading');	
	}	
	
	// alter permissions for a questions
	function alterPermissions(check, qid, uid, gid){
		if(check == true){
			var task = 'add';
		} else {
			var task = 'rem';
		}
		ajaxOpen('index.php?ajax=q_permissions&qid='+qid+'&task='+task+'&user='+uid+'&group='+gid, 'msg_area');
	}	

	function alterPermissionsType(button,qid){
			if(button == 1){
				ajaxOpen('?ajax=set_share_type&task=allomv&qid='+qid,'type_area','loading');
				//showId('allomv_extras');
				hide('permission_list');
				//hide('allfriends_list');
			}
			if(button == 2){
				ajaxOpen('?ajax=set_share_type&task=allfriends&qid='+qid,'type_area','loading');
				//showId('allfriends_list');
				hide('permission_list');
				//hide('allomv_extras');
			}
			if(button == 3){
				ajaxOpen('?ajax=set_share_type&task=specfriends&qid='+qid,'type_area','loading');
				showId('permission_list');
				//hide('allomv_extras');
				//hide('allfriends_list');
			}			
	}


	
///// CHARTS 5 simple load results
	
	
	function load_results(Id, view, target){	
		if(view == ''){
			ajaxOpen('?ajax=graph_standard&target='+ target +'&id='+ Id, target, 'loading');
		} else {
			ajaxOpen('?ajax=charts5&id='+ Id +'&target='+ target +'&view='+view, target ,'loading');
		}	
	}
	
	
	
	
	///// Homebox initial load
	function loadHomeQuestion()
            {
			var Id = 'question_area';
			var url = 'index.php?ajax=home_feature&tab=0';
			
			var t = document.getElementById(Id).innerHTML;
			var options = {
							method : 'get',
							onCreate: function() 
									{
										document.getElementById('loading').innerHTML = '<img src="img/loading.gif" />';
									},										
							onSuccess : function(transport) {										
										document.getElementById('loading').innerHTML = '';																				
										document.getElementById(Id).innerHTML = transport.responseText;																				
										//if(document.getElementById(Id).innerHTML != ''){
										//	new Effect.Appear('qa_slide'); 											
										//}								
								}
						};																			  
			nocache = Math.random();			  
			new Ajax.Request(url+'&nocache='+nocache, options);				
            }
	
	
	
	// JS calendar
		function checkDate(cal) {
			var date = cal.date;
			var time = date.getTime()			
			var date2 = new Date();			
			var now = date2.print("%Y%d%m%H%M");
			var cald = date.print("%s");			
			// use the _other_ field
			var field = document.getElementById("f_caldate");
			var field2 = document.getElementById("f_date_a");
			var tnow = date2.print("%s");			
			if (tnow >= cald) {				
			 field.innerHTML = 'No dates in the past';
			 field2.value = 0;
			} else {
				field.innerHTML = date.print("%Y/%d/%m %H:%M");
				field2.value = cald;
			}		 
		}	


		
	///////// resize text area
	function countLines(strtocount, cols) {
		var hard_lines = 1;
		var last = 0;
		while ( true ) {
			last = strtocount.indexOf("\n", last+1);
			hard_lines ++;
			if ( last == -1 ) break;
		}
		var soft_lines = Math.round(strtocount.length / (cols-1));
		var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
		if ( hard ) soft_lines = hard_lines;
		return soft_lines;
	}
	function cleanForm() {
		var the_form = document.forms[1];
		for ( var x in the_form ) {
			if ( ! the_form[x] ) continue;
			if( typeof the_form[x].rows != "number" ) continue;
			the_form[x].rows = countLines(the_form[x].value,the_form[x].cols) +1;
		}
		setTimeout("cleanForm();", 300);
	}
	
	///////////////////////


