//var rootpath="http://soundboard.com/";
//var rootpath="http://pcd-17/";
var winhandle =null;
var browser=null;


function getAJAX()
{
	var objxmlRequest=false;
	if (window.ActiveXObject)
	{	
		/******************
		Check jscript version
		When writing a script for a Web page, always place conditional compilation code 
		in comments. This allows hosts that do not support conditional compilation to 
		ignore it. 
		******************/
		//turn on conditional compilation
		/*@cc_on
		@if (@_jscript_version >= 5)
		try 
		{
			objxmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try
			{
				objxmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E)
			{
				objxmlRequest = false;
			}
		}
	
		@else
			objxmlRequest = false;
		@end @*/
		
		browser="IE";
	}
	
	else if(window.XMLHttpRequest)
	{
		
		/*if (window.netscape && netscape.security && netscape.security.PrivilegeManager.enablePrivilege) 
		{
		netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead UniversalBrowserWrite');
		}
		*/
		
		if (!objxmlRequest && typeof XMLHttpRequest != 'undefined')
		{
			try 
			{
				objxmlRequest = new XMLHttpRequest();
			}
			catch (e) 
			{
				objxmlRequest = false;
			}
		}			
		
		browser="NS";
	}

	return objxmlRequest;
}

function callXMLAsync(objAJAX,method,pageUrl)
{
	//window.alert(objAJAX);
	objAJAX.open(method, pageUrl, true);	
	objAJAX.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}

function callXMLSync(objAJAX,method,pageUrl)
{
	//window.alert(objAJAX);
	objAJAX.open(method, pageUrl, false);	
	objAJAX.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}

function addfavs(pageUrl)
{
//	alert(pageUrl);
	var objxmlRequest=getAJAX();

	if(objxmlRequest && objxmlRequest !=null)
	{		
		var rnd=getrandomnumber();

		//window.alert(pageUrl);
		
		callXMLAsync(objxmlRequest,"GET", pageUrl);
		objxmlRequest.onreadystatechange=function()
		{			
			if (objxmlRequest.readyState==4)
			{
				var StrResponse=objxmlRequest.responseText;				
				//window.alert(StrResponse);
				
				if(StrResponse.length>0){
					//document.getElementById('spfavs').innerHTML=StrResponse;				
					showmsg(StrResponse);
			    }
			}			
		}
		objxmlRequest.send(null);	
	}
}
function OpenShare(winpath,width,height,boolwinbars,boolscrollbars)
{
	if(boolwinbars=='undefined')
		boolwinbars=false;

	if(boolscrollbars=='undefined')
		boolscrollbars=false;

	var winname="";
	var winbars = 'directories='+boolwinbars+',location='+boolwinbars+',menubar='+boolwinbars+',status='+boolwinbars+',titlebar='+boolwinbars+',toolbar='+boolwinbars
	var winoptions = 'scrollbars='+boolscrollbars+',top=0,left='+(screen.width/2-(width/2))+',resizeable=yes,width='+width+',height='+height;
	var winfeatures = winbars + "," + winoptions;
	winhandle = window.open(winpath,winname,winfeatures);
	//if(window.focus)
	//	winhandle .focus();
}


function getpopular(cmd)
{
	var totrecs=0;
	var totpages=0;
	totrecs=document.getElementById('trecs').value;
	totpages=document.getElementById('tpages').value;
	
	var objxmlRequest=getAJAX();
	
	if(cmd == 'load')
		curpage=1;	

	else if(cmd=='prev')
		curpage--;

	else if(cmd=='next')
		curpage++;
	

	if(curpage==0  || totrecs==0)
	{
		document.getElementById('sp_or').style.visibility="hidden";
		document.getElementById('sp_next').style.visibility="hidden";
		document.getElementById('sp_prev').style.visibility="hidden";
		return;
	}

	//window.alert("totrecs == " + totrecs + "   " + " totpages == " + totpages + "  curpage == " + curpage);

	var pageUrl="getpopular.aspx?curpage=" + curpage + "&rnd=" + getrandomnumber();

	if(objxmlRequest && objxmlRequest !=null)
	{		
		//window.alert(pageUrl);
		callXMLAsync(objxmlRequest,"POST", pageUrl);
		objxmlRequest.onreadystatechange=function()
		{			
			if (objxmlRequest.readyState==4)
			{
				var StrResponse=objxmlRequest.responseText;
				//window.alert(StrResponse)
				document.getElementById('popdiv').innerHTML=StrResponse;
				objxmlRequest=null;

				if(curpage==1)
				{
					document.getElementById('sp_prev').style.visibility="hidden";
					document.getElementById('sp_or').style.visibility="hidden";

					if(totrecs && curpage<totpages)
						document.getElementById('sp_next').style.visibility="visible";
					else
						document.getElementById('sp_next').style.visibility="hidden";
				}
				else
				{
					if(totrecs && curpage==totpages)
					{
						document.getElementById('sp_or').style.visibility="hidden";
						document.getElementById('sp_next').style.visibility="hidden";
						document.getElementById('sp_prev').style.visibility="visible";
					}

					else if(totpages<=curpage)
					{
						document.getElementById('sp_or').style.visibility="hidden";
						document.getElementById('sp_next').style.visibility="hidden";
						document.getElementById('sp_prev').style.visibility="visible";
					}
					else
					{
						document.getElementById('sp_prev').style.visibility="visible";
						document.getElementById('sp_or').style.visibility="visible";
						document.getElementById('sp_next').style.visibility="visible";
					}
				}				
			}			
		}
		objxmlRequest.send(null);
	}
	return false;
}


function getcatpopular(cmd)
{
	var totrecs=0;
	var totpages=0;
	totrecs=document.getElementById('trecs').value;
	totpages=document.getElementById('tpages').value;
	
	
	var objxmlRequest=getAJAX();
	
	if(cmd == 'load')
		curpage=1;	

	else if(cmd=='prev')
		curpage--;

	else if(cmd=='next')
		curpage++;
	

	if(curpage==0  || totrecs==0)
	{
		document.getElementById('sp_or').style.visibility="hidden";
		document.getElementById('sp_next').style.visibility="hidden";
		document.getElementById('sp_prev').style.visibility="hidden";
		return;
	}

	var pageUrl="getcatpopular.aspx?id=" + document.getElementById('categoryid').value + "&curpage=" + curpage + "&rnd=" + getrandomnumber();


	if(objxmlRequest && objxmlRequest !=null)
	{		
		//window.alert(pageUrl);
		callXMLAsync(objxmlRequest,"POST", pageUrl);
		objxmlRequest.onreadystatechange=function()
		{			
			if (objxmlRequest.readyState==4)
			{
				var StrResponse=objxmlRequest.responseText;
				//window.alert(StrResponse)
				document.getElementById('popdiv').innerHTML=StrResponse;
				objxmlRequest=null;

				//window.alert(document.getElementById('favdiv').innerHTML);
				
				if(curpage==1)
				{
					document.getElementById('sp_prev').style.visibility="hidden";
					document.getElementById('sp_or').style.visibility="hidden";

					if(totrecs && curpage<totpages)
						document.getElementById('sp_next').style.visibility="visible";
					else
						document.getElementById('sp_next').style.visibility="hidden";
				}
				else
				{
					if(totrecs && curpage==totpages)
					{
						document.getElementById('sp_or').style.visibility="hidden";
						document.getElementById('sp_next').style.visibility="hidden";
						document.getElementById('sp_prev').style.visibility="visible";
					}

					else if(totpages<=curpage)
					{
						document.getElementById('sp_or').style.visibility="hidden";
						document.getElementById('sp_next').style.visibility="hidden";
						document.getElementById('sp_prev').style.visibility="visible";
					}
					else
					{
						document.getElementById('sp_prev').style.visibility="visible";
						document.getElementById('sp_or').style.visibility="visible";
						document.getElementById('sp_next').style.visibility="visible";
					}
				}
			}
			
		}
		objxmlRequest.send(null);
	}

	return false;

}


function getnew(cmd)
{
	var totrecs=0;
	var totpages=0;
	totrecs=document.getElementById('trecs').value;
	totpages=document.getElementById('tpages').value;
	
	
	var objxmlRequest=getAJAX();
	
	if(cmd == 'load')
		curpage=1;	

	else if(cmd=='prev')
		curpage--;

	else if(cmd=='next')
		curpage++;
	

	if(curpage==0  || totrecs==0)
	{
		document.getElementById('sp_or').style.visibility="hidden";
		document.getElementById('sp_next').style.visibility="hidden";
		document.getElementById('sp_prev').style.visibility="hidden";
		return;
	}


	var pageUrl="getnewsoundboards.aspx?curpage=" + curpage + "&rnd=" + getrandomnumber();

	if(objxmlRequest && objxmlRequest !=null)
	{		
		//window.alert(pageUrl);
		callXMLAsync(objxmlRequest,"POST", pageUrl);
		objxmlRequest.onreadystatechange=function()
		{			
			if (objxmlRequest.readyState==4)
			{
				var StrResponse=objxmlRequest.responseText;
				//window.alert(StrResponse)
				document.getElementById('popdiv').innerHTML=StrResponse;
				objxmlRequest=null;

				if(curpage==1)
				{
					document.getElementById('sp_prev').style.visibility="hidden";
					document.getElementById('sp_or').style.visibility="hidden";

					if(totrecs && curpage<totpages)
						document.getElementById('sp_next').style.visibility="visible";
					else
						document.getElementById('sp_next').style.visibility="hidden";
				}
				else
				{
					if(totrecs && curpage==totpages)
					{
						document.getElementById('sp_or').style.visibility="hidden";
						document.getElementById('sp_next').style.visibility="hidden";
						document.getElementById('sp_prev').style.visibility="visible";
					}

					else if(totpages<=curpage)
					{
						document.getElementById('sp_or').style.visibility="hidden";
						document.getElementById('sp_next').style.visibility="hidden";
						document.getElementById('sp_prev').style.visibility="visible";
					}
					else
					{
						document.getElementById('sp_prev').style.visibility="visible";
						document.getElementById('sp_or').style.visibility="visible";
						document.getElementById('sp_next').style.visibility="visible";
					}
				}
			}
			
		}
		objxmlRequest.send(null);
	}

	return false;

}

function isBlank(searchBoxID)
{	
	if(document.getElementById(searchBoxID).value =="")
	{
		window.alert("Enter the keyword to search ");
		document.getElementById(searchBoxID).focus();
		// commented
		// return
		return false;
	}
	return true;
}

function addsong(pageUrl)
{
	
	var username=document.getElementById('hidText').value;
	var objxmlRequest=getAJAX();
	if( jQuery('#songslist').val() =="-1")
	{
		//window.alert("Please Select song ");
		showmsg("Please select a song ");
		document.getElementById('songslist').focus();
		return;
	}
	else
	{
		var id=document.getElementById('songslist').value;
	}
	
	if(objxmlRequest && objxmlRequest !=null)
	{		
		pageUrl=pageUrl+"&songid="+id;
		var rnd=getrandomnumber();	
		callXMLAsync(objxmlRequest,"GET", pageUrl);
		objxmlRequest.onreadystatechange=function()
		{
		if (objxmlRequest.readyState==4)
			{
				var StrResponse=objxmlRequest.responseText;				
				//window.alert(StrResponse);
				if(StrResponse.length>0){
					//document.getElementById('addEmsg').innerHTML=StrResponse;
					showmsg(StrResponse);
			    }
			}			
		}
		objxmlRequest.send(null);	
	}
	
}

function blank(){}


function geturl()
{	
	if(document.getElementById('searchkeyword').value =="")
	{
		window.alert("Enter the keyword to search ");
		document.getElementById('searchkeyword').focus();
		return;
	}

	var objxmlRequest=getAJAX();	
	//var pageUrl=rootpath+"geturl.aspx";
	var pageUrl="http://soundboard.com/geturl.aspx";

	if(objxmlRequest && objxmlRequest !=null)
	{		
		//window.alert(pageUrl);
		callXMLAsync(objxmlRequest,"POST", pageUrl);
		objxmlRequest.onreadystatechange=function()
		{			
			if (objxmlRequest.readyState==4)
			{
				var StrResponse=objxmlRequest.responseText;
				//alert(StrResponse);
				window.setTimeout("blank();", 20000);
				//window.alert(StrResponse)
				//self.location.href=rootpath+"results.aspx?curpage=1&cnt="+StrResponse;
				self.location.href="http://soundboard.com/results.aspx?curpage=1&cnt="+StrResponse;
				
				objxmlRequest=null;				
			}
			
		}
		objxmlRequest.send("src=" + document.getElementById('searchkeyword').value + "&rnd=" + getrandomnumber());
	}
	return false;
}

function getsearchresults(cmd)
{
	var totrecs=0;
	var totpages=0;
	totrecs=document.getElementById('trecs').value;
	totpages=document.getElementById('tpages').value;
	
	var objxmlRequest=getAJAX();
	
	if(cmd == 'load')
		curpage=1;	

	else if(cmd=='prev')
		curpage--;

	else if(cmd=='next')
		curpage++;
	

	if(curpage==0  || totrecs==0)
	{
		document.getElementById('sp_or').style.visibility="hidden";
		document.getElementById('sp_next').style.visibility="hidden";
		document.getElementById('sp_prev').style.visibility="hidden";
		return;
	}

	//window.alert("totrecs == " + totrecs + "   " + " totpages == " + totpages + "  curpage == " + curpage);
	
	var pageUrl="getsearchresults.aspx?curpage=" + curpage + "&src=" + document.getElementById('srckeyword').childNodes[0].nodeValue + "&rnd=" + getrandomnumber();

	if(objxmlRequest && objxmlRequest !=null)
	{		
		//window.alert(pageUrl);
		callXMLAsync(objxmlRequest,"POST", pageUrl);
		objxmlRequest.onreadystatechange=function()
		{			
			if (objxmlRequest.readyState==4)
			{
				var StrResponse=objxmlRequest.responseText;
				//window.alert(StrResponse)
				document.getElementById('resdiv').innerHTML=StrResponse;
				objxmlRequest=null;

				if(curpage==1)
				{
					document.getElementById('sp_prev').style.visibility="hidden";
					document.getElementById('sp_or').style.visibility="hidden";

					if(totrecs && curpage<totpages)
						document.getElementById('sp_next').style.visibility="visible";
					else
						document.getElementById('sp_next').style.visibility="hidden";
				}
				else
				{
					if(totrecs && curpage==totpages)
					{
						document.getElementById('sp_or').style.visibility="hidden";
						document.getElementById('sp_next').style.visibility="hidden";
						document.getElementById('sp_prev').style.visibility="visible";
					}

					else if(totpages<=curpage)
					{
						document.getElementById('sp_or').style.visibility="hidden";
						document.getElementById('sp_next').style.visibility="hidden";
						document.getElementById('sp_prev').style.visibility="visible";
					}
					else
					{
						document.getElementById('sp_prev').style.visibility="visible";
						document.getElementById('sp_or').style.visibility="visible";
						document.getElementById('sp_next').style.visibility="visible";
					}
				}				
			}			
		}
		objxmlRequest.send(null);
	}
	return false;
}



function getpassword()
{
	if(document.frm.username.value=="")
	{
		window.alert("Enter user name");
		document.frm.username.focus();
		return;								
	}	
	
	if(document.frm.email.value == "")
	{
		window.alert("enter email address !!");
		document.frm.email.focus();
		return;
	}
	
	if(document.frm.email.value != "")
	{
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.frm.email.value)))
		{	
			window.alert("Invalid email address !!");
			document.frm.email.focus();
			return;
		}
	}

	var objxmlRequest=getAJAX();

	var pageUrl="checkforgotpassword.aspx?username=" + document.getElementById('username').value + "&email=" + document.getElementById('email').value + "&rnd=" + getrandomnumber();

	if(objxmlRequest && objxmlRequest !=null)
	{		
		//window.alert(pageUrl);
		callXMLAsync(objxmlRequest,"POST", pageUrl);
		objxmlRequest.onreadystatechange=function()
		{			
			if (objxmlRequest.readyState==4)
			{
				var StrResponse=objxmlRequest.responseText;
				//window.alert(StrResponse)
				document.getElementById('lblMessage').innerHTML=StrResponse;
				objxmlRequest=null;				
			}
		}
		objxmlRequest.send(null);
	}	
}

function checkregavail()
{
	var objxmlRequest=getAJAX();

	var pageUrl="checkregavailability.aspx?username=" + document.getElementById('username').value + "&email=" + document.getElementById('email').value + "&rnd=" + getrandomnumber();

	if(objxmlRequest && objxmlRequest !=null)
	{		
		//window.alert(pageUrl);
		callXMLSync(objxmlRequest,"POST", pageUrl);
		objxmlRequest.send(null);
		objxmlRequest.onreadystatechange=function()
		{			
			if (objxmlRequest.readyState==4)
			{
				var StrResponse=objxmlRequest.responseText;
				//window.alert(StrResponse)
				objxmlRequest=null;

				if(StrResponse !="y")
				{
					document.getElementById('lblMessage').innerHTML=StrResponse;
					return false;
				}
				else if(StrResponse =="y")
					return true;				
			}
		}
		
	}
}


function getrandomnumber()
{
	return Math.floor(Math.random()*1500)
}

function topcenterwin(winpath,width,height,boolwinbars,boolscrollbars)
{
	if(boolwinbars=='undefined')
		boolwinbars=false;

	if(boolscrollbars=='undefined')
		boolscrollbars=false;

	var winname="";
	var winbars = 'directories='+boolwinbars+',location='+boolwinbars+',menubar='+boolwinbars+',status='+boolwinbars+',titlebar='+boolwinbars+',toolbar='+boolwinbars
	var winoptions = 'scrollbars='+boolscrollbars+',top=0,left='+(screen.width/2-(width/2))+',resizeable=yes,width='+width+',height='+height;
	var winfeatures = winbars + "," + winoptions;
	winhandle = window.open(winpath,winname,winfeatures);
	//if(window.focus)
	//	winhandle .focus();
}

function closewin()
{
	if(winhandle !=null)
	{
		if(!winhandle.closed)
			winhandle.close();
		winhandle=null;		
	}
}

function ShowLogin()
{
   var win = new Window({
        className: "bluelighting",
        width:320, minWidth:250,height:300, minHeight:200,zIndex: 1000, resizable: false, maximizable: false,
        title: "SoundBoard.com", showEffect:Element.show, hideEffect: Element.hide, draggable:true, 
        //wiredDrag: true,
        destroyOnClose: true
        //,recenterAuto: true
        , bottom:50, left:300
    }); 

    win.updateHeight();
    var bodyhtml='<div id="popuploginbox" style="border:solid 1px #e5e5e5;padding:8px;width:300px; margin:auto" >'+
                '<h3 style="text-align:center">Login</h3>'+                    
                '<table cellpadding="2" cellspacing="3" style="width:290px; border-collapse:collapse; align="center">'+
                    '<tr><td style="width:30%">Username</td><td><input name="userlogin" type="text" maxlength="50" id="userlogin" style="width:200px;" /></td></tr>'+
                    '<tr><td>Password</td><td><input name="userpass" type="password" maxlength="50" id="userpass" style="width:200px;" /></td></tr>'+
                    '<tr><td style="height:15px;"></td><td align="left"><div id="popupmsg" class="error"></div></td></tr>'+
                    '<tr><td></td><td><input type="button" name="btnLogin" value="Log In" id="btnLogin" class="button" onclick="doLogin();"/> Or <strong><a href="register.aspx" title="Signup for soundboard">Signup</a></strong><br /><br /><a href="forgotpassword.aspx">Forgot your password?</a></td></tr>'+
                '</table>'+
                '</div>'+
                '</div>';

    win.getContent().update(bodyhtml);     
    win.setDestroyOnClose();
    win.show();
}

function doLogin()
{   
   
    if(jQuery('#userlogin').val()=="")
    {  	       
        jQuery('#popupmsg').html("Enter UserName");
    }
    else
    if(jQuery('#userpass').val()=="")
    {  	       
        jQuery('#popupmsg').html("Enter Password");
    }
    
    else
    {
        jQuery.post("../LoginHandler.ashx", { Auth:"yes", UNmae: jQuery('#userlogin').val(), Passwd: jQuery('#userpass').val()},
         function(data){
         
         var result=data.split("/");
             if(result[0]=="true")
             {
                 if(result[1]!="loggedin")
                 {
                     jQuery('#popupmsg').html("Login Successful");
                     Windows.getFocusedWindow().close();
                     jQuery('.loginbox').css("display","block");
                     jQuery('.notloginbox').css("display","none");
                     var logintxt=jQuery('.loginbox').html();
                     jQuery('.loginbox').html("Welcome "+result[1]+", "+logintxt);
                     jQuery('#btnLogin').attr("disabled","disabled");  
                     return true;                    
                  }
             }
             else
             {
                 jQuery('#popupmsg').html(result[0]);
                 jQuery('.notloginbox').css("display","block");
                 jQuery('.loginbox').css("display","none");     
                 return false;           
             }
        });                
                
    }
}

/* *********************** LoginClass ********************* */
jQuery.extend(
{
  context: function (context)
  {
    var co = 
    {
      callback: function (method)
      {
        if (typeof method == 'string') method = context[method];
        var cb = function () { method.apply(context, arguments); }
        return cb;
      }
    };
    return co;
  }
}); 

function LoginClass ()
{
  this.logged=false;
  this.callbackFuncSuccess='';
  
  this.cheakAuth = function(_callbackFunc)
  {
     this.callbackFuncSuccess = _callbackFunc;
     
        jQuery.ajax({
        type: 'post',
        url: '../LoginHandler.ashx',
        data: {Auth:"ckeck"},
        success: jQuery.context(this).callback('cheakAuth_callback')
        });
    //$.post('time.php', {}, $.context(this).callback('cheakAuth_callback'), 'text');
  }
  this.cheakAuth_callback = function (data)
  {
    if(data=="true")
    {
        this.logged=true;
        eval(this.callbackFuncSuccess);
    }
    else
    {
        this.logged=false;
        ShowLogin();
    }
  }
  this.test=function()
  {
    alert('ok');
  }
  this.addToFriend = function(username)
  {
        jQuery.ajax({
          type: "GET",url: "../account/addfavs.aspx",
          data:{rnd:getrandomnumber(),cnt:username},
          success: function(msg)
          {
             showmsg(msg);
          }
        });
  }
  this.addsongtoboard=function(username,_songid)
  {
    jQuery.ajax({
          type: "GET",url: "../account/addlist.aspx",
          data:{rnd:getrandomnumber(),cnt:username,songid:_songid},
          success: function(msg)
          {
             showmsg(msg);
          }
        });
  }
}
/* ************************************************************ */
jQuery(document).ready(function(){
    
});





function addtofriend(username)
{
    var obj=new LoginClass();
    obj.cheakAuth('this.addToFriend("'+username+'")');    
}

function addsongtoboard(username)
{
    if( jQuery('#songslist').val() =="-1")
	{
		//window.alert("Please Select song ");
		showmsg("Please select a song ");
		document.getElementById('songslist').focus();
		return;
	}
	else
	{
		var id=document.getElementById('songslist').value;
		var obj=new LoginClass();
        obj.cheakAuth('this.addsongtoboard("'+ username +'","'+ id +'")'); 
	}   
}

