var base_url = "http://www.granolabuzz.com/";
var login_type = '';
var ltype = '';

function $$(e){
	if(typeof e=='string')e=document.getElementById(e);
	return e;
};
function collect(a,f){
	var n=[];
	for(var i=0;i<a.length;i++){
		var v=f(a[i]);
		if(v!=null)n.push(v)
	}
	return n
};

ajax={};
ajax.x=function(){
	try{
		return new ActiveXObject('Msxml2.XMLHTTP')
	}catch(e){
		try{
			return new ActiveXObject('Microsoft.XMLHTTP')
		}catch(e){
			return new XMLHttpRequest()
		}
	}
};
ajax.serialize=function(f){
	var g=function(n){
		return f.getElementsByTagName(n)
	};
	var nv=function(e){
		if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);
		else return ''
	};
	var i=collect(g('input'),
	function(i){
		if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');
};
ajax.send=function(u,f,m,a){
	var x=ajax.x();
	x.open(m,u,true);
	x.onreadystatechange=function(){
		if(x.readyState==4)f(x.responseText)
	};
	if(m=='POST')
	x.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	x.send(a)
};
ajax.get=function(url,func){
	ajax.send(url,func,'GET')
};
ajax.gets=function(url){
	var x=ajax.x();
	x.open('GET',url,false);
	x.send(null);
	return x.responseText
};
ajax.post=function(url,func,args){
	ajax.send(url,func,'POST',args);
};
ajax.update=function(url,elm){
	var e=$$(elm);
	var f=function(r){
		e.innerHTML=r
	};
	ajax.get(url,f)
};
ajax.submit=function(url,elm,frm){
	var e=$$(elm);
	var f=function(r){
		e.innerHTML=r;
	};
	ajax.post(url,f,ajax.serialize(frm));
};

function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

	if (document.getElementsByTagName)
	{
		elements = form.getElementsByTagName('input');
		for( i=0, elm; elm=elements.item(i++); )
		{
			if (elm.getAttribute('type') == "text")
			{
				elm.value = '';
			}
		}
	}

	// Actually looking through more elements here
	// but the result is the same.
	else
	{
		elements = form.elements;
		for( i=0, elm; elm=elements[i++]; )
		{
			if (elm.type == "text")
			{
				elm.value ='';
			}
		}
	}
}

function dismissErrMsg(){
	document.getElementById('r_err_message').innerHtml='';
  	document.getElementById("r_err_message_container").style.display = "none";	
}

function captcha(){
	ajax.update('./handlers/register.php?action=captcha', 'captcha_div');
}

function new_group(){
	ajax.update('./handlers/new_group_form.php', 'new_group_div');
}

function new_buzz(){
	ajax.update('./handlers/new_buzz_form.php', 'new_buzz_div');
	setTimeout("tb_init('a.thickbox, area.thickbox, input.thickbox')", 500);
}

function edit_group(id){
	ajax.update('./handlers/new_group_form.php?action=edit&id=' + id, 'new_group_div');
}

function new_event(){
	ajax.update('./handlers/new_event_form.php', 'new_event_div');
}

function selectMailBox(box){
	if(box != ''){
		document.getElementById("box_contents_main").style.display="block";
		ajax.update('./handlers/mail.php?action=' + box, 'box_contents');
	}else{
		document.getElementById("box_contents_main").style.display="none";
	}
}

function hfields(){
	ajax.update('./handlers/register.php?action=hfields', 'hfields_div');	
}

function lMsg(message){
	document.getElementById('l_err_message').innerHTML=message;
}

function rMsg(message){
	document.getElementById('r_err_message').innerHTML=message;
  	document.getElementById("r_err_message_container").style.display = "block";
	var pattern=/SUCCESS!!/i;
	if(pattern.test(message)){
		document.getElementById('demo-all').style.display="none";
		clearForm('reg_form'); 
		scroll(0,0);
		pausecomp(4000);
		document.location.href="./process.php?txid=" + document.getElementById('txid').value;
	}	
}

function register(form) { 
	ajax.post('./handlers/register.php',rMsg,ajax.serialize(form));

	//document.getElementById('demo-all').style.display='none';
	//clearForm('reg_form');
	//scroll(0,0);	
}

function Login(form){
	ajax.post('./handlers/login.php',loginResponse,ajax.serialize(form) + '&login=true');
}

function loginResponse(response){
	var pattern=/ERROR/i;
	if(pattern.test(response)){
		var message = '<font color="red"><b>' + response + '</b></font>';
		lMsg(message);
	}else{
		window.location = response;
	}
}

function transBody(){
	document.getElementById('main_body').style.filter='alpha(opacity=30)'; 
	document.getElementById('main_body').style.mozOpacity='0.3'; 
	document.getElementById('main_body').style.opacity='0.3';
	document.getElementById('main_body').style.khtmlOpacity='.3'; 

}

function registerClose() {
  	document.getElementById("register_div").style.display = "none";
	document.getElementById("pass_recovery_div").style.display = "none";
	document.getElementById("login_div").style.display = "none";
	document.getElementById('main_body').style.filter='alpha(opacity=100)'; 
	document.getElementById('main_body').style.mozOpacity='1'; 
	document.getElementById('main_body').style.opacity='1'; 

}

function passRecoverForm(rtype){
	document.getElementById('pass_recovery_div').style.display='block';
	document.getElementById('login_div').style.display='none';
	transBody();
	scroll(0,0);
		// Open PHP script for requests
   		httpu2.open('get', base_url + 'handlers/pass_recovery.php?rtype=' + rtype, false);
		httpu2.send(null);
		if(httpu2.status == 200){
			// Text returned FROM the PHP script
      		var response = httpu2.responseText;
			if(response) {
				document.getElementById("pass_recovery_div").innerHTML = response;
      		}
		}
}

function recoverPass(rtype,form){
	transBody();
	document.getElementById('pass_recovery_div').style.display='block';
	scroll(0,0);
	// Open PHP script for requests
   	httpu2.open('POST', base_url + 'handlers/pass_recovery.php', false);
    httpu2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     httpu2.send('remail=' + form.remail.value + '&rtype=' + rtype);
		if(httpu2.status == 200){
			// Text returned FROM the PHP script
      		var response = httpu2.responseText;
			if(response) {
				document.getElementById("pass_recovery_div").innerHTML = response;
      		}
		}
}

function editWindow(url, width, height){
var str="";
 str += "height=" + height;
  str += ",innerHeight=" + height;
  str += ",width=" + width;
  str += ",innerWidth=" + width;
 str += ",menubar=1";
  str += ",resizable=1";
  str += ",scrollbars=1";

 window.open (url,"editwindow",str);
}

//Forum Stuff

    function confirmDelete(start, del, mNr, tNr, show) {
      var check = confirm("<? echo $message[14]; ?>");
      if(check) {
        var param = '?start=' + start + '&delete=' + del;
        if(mNr) param += '&mNr=' + mNr + '&tNr=' + tNr;
        if(show) param += '&date_show=' + show;
        document.location.href = '<? echo $PHP_SELF; ?>' + param;
      }
    }

function insertSmilie(txt) {
  var el = document.f1.sfText;
  if(!el.value) el.value = txt + ' ';
  else el.value += ((el.value.charAt(el.value.length-1) == ' ') ? '' : ' ') + txt + ' ';
  el.focus();
}

function countdown(cnt) {
  var obj = 0;

  if(document.getElementById) obj = document.getElementById('divSubmit');
  else if(document.all) obj = document.all.divSubmit;

  if(obj) {
    if(cnt < 1) {
      obj.innerHTML = '[ <a href="javascript:document.f1.submit()" class="cssLink3">Submit</a> ]';
    }
    else {
      obj.innerHTML = '[ ' + cnt + ' ]';
      cnt--;
      setTimeout('countdown(' + cnt + ')', 1000);
    }
  }
}

function delete_mail(type, id)
{
	if (!confirm("\nAre you sure you wish to delete this mail?")){
	}
	else
	{
		location.href="./mail.php?action=delete_mail&type=" + type + "&id=" + id;
	}
}


function nl2br(text){

text = escape(text);

if(text.indexOf('%0D%0A') > -1){

var re_nlchar = /%0D%0A/g ;

}else if(text.indexOf('%0A') > -1){

var re_nlchar = /%0A/g ;

}else if(text.indexOf('%0D') > -1){

var re_nlchar = /%0D/g ;

}

return unescape( text.replace(re_nlchar,'<br />') );

}

function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
} 
