
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
	{
	    // Firefox, Opera 8.0+, Safari
	    xmlHttp=new XMLHttpRequest();
	}
    catch (e)
	{
	    // Internet Explorer
	    try
		{
		    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	    catch (e)
    {
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
	}
    return xmlHttp;
}


//function recordMsg(number, email, nickname, replyok, showemail, parentid)
function recordMsg(parentid, event, div)
{
    if (event !=0) {
	y=event.clientY;
	x=event.clientX;
    } else {
	x=0; y=0;
    }
    x=0;y=0
    pg = document.getElementById(div);    
    winW = document.body.offsetWidth;
    pg.style.width = (winW > 280) ? 280 : winW;
    if (pg.style.visibility=='visible') {
	pg.style.visibility='hidden';

	return;
    } 
    pg.innerHTML="<div style='float:right;border:1px solid #000000;' onclick=document.getElementById('"+div+"').style.visibility='hidden'>X</div><div id=SpokenBuzzCallBoxMain style='padding:2px;'></div>";
    pg.style.visibility="visible";
    pg.style.top=y;
    pg.style.left=x;
    pg = document.getElementById("SpokenBuzzCallBoxMain");  

    if (parentid) {
	//	pg.innerHTML+="text message (optional)</td><td><input type=text  id=SPtxt /></td></tr>";
	pg.innerHTML+="<b>Topic</b><br><input size=25 type=text  id=SPtxt /><br>";
    } else {
	//pg.innerHTML+="<table border=1><tr><td>";
	pg.innerHTML+="<b>Topic</b><br><input size=25 type=text  id=SPtxt /><br>";

	pg.innerHTML+="Secret<br><input type=text  size=10 id=SPsecret /><div class=smtext>(will be required to edit/moderate this topic)</div>";
    }
    pg.innerHTML+="<div style='float:right'>Tags<br><textarea  rows=5 cols=9 id=SPtags></textarea></div>";
    pg.innerHTML+="<b>Your Current Location</b><br><input class='big' type=text  id=SPloc /><br>";
    pg.innerHTML+="<b>Your email</b><br><input type=text  id=SPemail /><br>";
    pg.innerHTML+= "<b>Number to call you at to record message</b><br>";
    pg.innerHTML+="<input class='big' type=text  id='SPphonenum' size=15 onkeyup='validnum(this.value)' >";
    pg.innerHTML+=" <img src=/images/icons/telephone.png alt='Click to Call Now' onclick=recordMsgCall('"+parentid+"')></span><br>";
    pg.innerHTML+="<font size=1>Your number will not be shared with anyone</font><br>";

        //    pg.innerHTML+="</div>";

}

function recordMsgCall(parentid)
{
    number = document.getElementById("SPphonenum").value;
    email  = document.getElementById("SPemail").value;
    txt  = document.getElementById("SPtxt").value;
    //    alert(parentid);
    //    alert(document.getElementById("SPlatit").value);
    //    latit  = (document.getElementById("SPlatit").value) ? 1 : 0;
    try {
	latit  = document.getElementById("SPlatit").value ;
	longit  = document.getElementById("SPlongit").value ;
    } catch(err) {
	longit = 0;
	latit=0;
    }

    try {
	secret  = document.getElementById("SPsecret").value ;
    } 
    catch(err1) {
	secret =0;
    }
    try {
	loc  = document.getElementById("SPloc").value ;
    } 
    catch(err2) {
	loc ="";
    }
    try {
	tags  = document.getElementById("SPtags").value ;
    } 
    catch(err3) {
	tags ="";
    }
    try {
	private  = (document.getElementById("SPprivate").checked) ? 'Y' : 'N'  ;
    } 
    catch(err4) {
	private = 'N';
    }

    try {
	sendto  = document.getElementById("SPsendto").value ;
    } 
    catch(err5) {
	sendto = '';
    }

    if (!number) { return; }
    if (!email) { return; }
    url = "/cgi-bin/recordMsg.cgi";
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
	alert ("Your browser does not support AJAX!");
	return;
    }
    url+="?number="+number+"&parentid="+parentid+"&email="+email+"&txtmsg="+encodeURIComponent(txt)+"&secret="+encodeURIComponent(secret)+"&longit="+longit+"&latit="+latit+"&loc="+encodeURIComponent(loc)+"&tags="+encodeURIComponent(tags)+"&private="+private+"&sendto="+encodeURIComponent(sendto); //+"&replyok="+replyok+"&showemail="+showemail;
    //    document.write(url);
    //           alert(url);
    //     document.getElementById("comps").innerHTML+="Getting Current Data...please wait";
    document.getElementById("SpokenBuzzCallBoxMain").innerHTML+="<img src='/images/wait-anim.gif' height=40 align=middle/>";

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


function showResponse()
{
   if (xmlHttp.readyState==4)
	{
	    pg = document.getElementById("SpokenBuzzCallBoxMain");
	    pg.innerHTML="";
	    var xmlDoc=xmlHttp.responseXML.documentElement;
	    myurl = xmlDoc.getElementsByTagName("msgURL")[0];
	    if (myurl) {
		myurl = myurl.childNodes[0].nodeValue;
		pg.innerHTML+="You should be receiving a call very soon, please pick up the phone and record your message<br>";
		//		pg.innerHTML+="<br>Your message permalink is <a href="+myurl+">"+myurl+"</a>";	
		pg.innerHTML+="<br>BuzzID: <a href="+myurl+">"+xmlDoc.getElementsByTagName("buzzID")[0].childNodes[0].nodeValue+"</a>";
	    } else {
		pg.innerHTML+="ERROR:"+xmlDoc.getElementsByTagName("Error")[0].childNodes[0].nodeValue+"<br>";
	    }
	    //	    pg.innerHTML+="<span style='border:1px solid'  onclick=document.getElementById('SpokenBuzzCallBox').style.visibility='hidden'>Close</span>";
	}
}

function playMsg(buzzid,event,div) 
{
    //    window.location="/buzz/buzz/"+linkfn;
    //    document.getElementById("myplayer").autostart="1";
    if (event) {
	y=event.clientY;
	x=event.clientX;
    } else {
	y=0;
	x=0;
    }
    pg = document.getElementById(div);    
    pg.style.top=y;
    pg.style.left=x+30;
    if (pg.style.visibility=='visible') {
	pg.style.visibility='hidden';
	return ;
    }

    pg.innerHTML="<div style='float:right;border:1px solid #000000;' onclick=document.getElementById('"+div+"').style.visibility='hidden'>X</div>";
    pg.style.visibility="visible";
    pg.innerHTML+=buzzid+"<embed id='myplayer' src='/buzz/buzz/"+buzzid+".m3u' type='application/x-mplayer2' loop='0' autostart='1' height='45'></embed>";
    //    pg.innerHTML+= "<script type='text/javascript' src='http://spokenbuzz.com/cgi-bin/playmsg.cgi?buzzid="+buzzid+"'></script>";
    
    
}


function validnum(num) {
    newnum = num.replace(/[^0-9\-\(\)\.]/, "") ;
    document.getElementById("SPphonenum").value = newnum;
}
