var baseURL = "http://www.pollthepeople.com";  // LIVE
// var baseURL = "http://78.129.143.43/~polltdev";  // DEV
function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!
    
    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser does not support this application, please use a different one");
                return false;
            }
        }
    }
    return ajaxRequest;
}

function peoples_poll(val,ppid){

    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('peoples_poll').innerHTML = ajaxRequest.responseText;
            }
        } 
    
    ajaxRequest.open("GET", "peoples_vote.php?val="+val+"&ppid="+ppid, true);
    ajaxRequest.send(null); 
}

function get_amazon(cat,field,div,div2){
    
    var keywords = document.getElementById(div).value;
    var artist = "";
    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById(div+'_div').innerHTML = ajaxRequest.responseText;
                document.getElementById(div+'_div').style.display = 'block';
            }
        } 
    if(field == 'Title'){
        artist = "&artist="+document.getElementById(div2).value;
    }
    
    ajaxRequest.open("GET", "../get_amazon.php?category="+cat+"&keywords="+keywords+"&field="+field+artist, true);
    ajaxRequest.send(null); 
}

function get_data(cat,div,pid,page){
    
    var keywords = document.getElementById(div).value;
    document.getElementById(div+'_uid').value = "";	
	// AG Added below
	// start
	if(keywords != "")
	{
	if(pid == '1'){	document.getElementById('vote_now_1').style.height = '680px';}
	if(pid == '2'){	document.getElementById('vote_now_2').style.height = '680px';}
	if(pid == '3'){	document.getElementById('vote_now_3').style.height = '680px';}
	if(pid == '4'){	document.getElementById('vote_now_4').style.height = '680px';}
	}
	// end
	
	if(keywords != ""){
        document.getElementById(div+'_div').innerHTML = "Searching..."
        document.getElementById(div+'_div').style.display = 'block';
        var ajaxRequest = ajaxFunction();
        
        ajaxRequest.onreadystatechange = function(){
                if(ajaxRequest.readyState == 4){
                    document.getElementById(div+'_div').innerHTML = ajaxRequest.responseText;
                    document.getElementById(div+'_div').style.display = 'block';
                }
            } 
    
        ajaxRequest.open("GET", baseURL + "/get_data.php?keywords="+keywords+"&div="+div+"&pid="+pid+"&page="+page, true);
        ajaxRequest.send(null); 
    }
}

function select_item(uid,name,div){
    document.getElementById(div).value = unescape(name);
    document.getElementById(div+'_uid').value = uid;
    document.getElementById(div+'_div').style.display = 'none';
	// Added by AG:
	// Start
	document.getElementById('vote_now_4').style.height = '244px';
	document.getElementById('vote_now_1').style.height = '244px';
	document.getElementById('vote_now_2').style.height = '244px';
	document.getElementById('vote_now_3').style.height = '244px';
	// End
}

function submit_search(action){
    var mysearch =  document.getElementById('navsearch').value;
    var url = baseURL + "/search/index.php?Submit=Search&action="+action;
    if(action == "user"){
        var s = "/ /";
        s = eval(s);
        var space = mysearch.search(s);
        var fname = escape(mysearch.substring(0,space));
        var sname = escape(mysearch.substring(space+1,mysearch.length));
        url += "&firstname="+fname+"&lastname="+sname;
    } 
    if(action == "title"){
        url += "&pid=1&search="+escape(mysearch);
    } 
    window.location = url;
}
function add_address(ucode){

    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                //document.getElementById('add_address').innerHTML = ajaxRequest.responseText;
                window.location = baseURL + "/mydetails/messages.php?mode=compose&to="+ucode;
            }
        } 
    
    ajaxRequest.open("GET", baseURL + "/addressbook.php?mode=add&u="+ucode, true);
    ajaxRequest.send(null); 
}

function rate_list(pid,lid,score){
    
    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('rate_list').innerHTML = ajaxRequest.responseText;
                get_rating(pid,lid);
            }
        } 
    
    ajaxRequest.open("GET", baseURL + "/rate_list.php?pid="+pid+"&lid="+lid+"&score="+score, true);
    ajaxRequest.send(null); 
    
}

function get_rating(pid,lid){
    
    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('current_rating').innerHTML = ajaxRequest.responseText;
            }
        } 
    
    ajaxRequest.open("GET", baseURL + "/get_rating.php?pid="+pid+"&lid="+lid, true);
    ajaxRequest.send(null); 
    
}
function send_item_request(div,pid){
    
    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('request_'+div).innerHTML = ajaxRequest.responseText;
            }
        } 
        
    var thing = escape(document.getElementById('item_'+div).value);
    ajaxRequest.open("GET", baseURL + "/send_item_request.php?pid="+pid+"&item="+thing, true);
    ajaxRequest.send(null); 
    
}
function set_spotlight(poll,lid){
    
    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('set_spotlight').innerHTML = ajaxRequest.responseText;
            }
        } 
        
    ajaxRequest.open("GET", baseURL + "/build_spotlight.php?poll="+poll+"&lid="+lid, true);
    ajaxRequest.send(null); 
    
}
function show_members(div,gid,page){
    
    //document.getElementById(div+'_div').innerHTML = "Searching..."
    //document.getElementById(div+'_div').style.display = 'block';
    var ajaxRequest = ajaxFunction();
    
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById(div).innerHTML = ajaxRequest.responseText;
            }
        } 

    ajaxRequest.open("GET", baseURL + "/groups/show_members.php?div="+div+"&gid="+gid+"&page="+page, true);
    ajaxRequest.send(null); 
}
function show_pending(div,gid,page){
    
    //document.getElementById(div+'_div').innerHTML = "Searching..."
    //document.getElementById(div+'_div').style.display = 'block';
    var ajaxRequest = ajaxFunction();
    
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById(div).innerHTML = ajaxRequest.responseText;
            }
        } 

    ajaxRequest.open("GET", baseURL + "/groups/show_pending.php?div="+div+"&gid="+gid+"&page="+page, true);
    ajaxRequest.send(null); 
}

function approve_pending(div,gid,page,ucode){
    
    //document.getElementById(div+'_div').innerHTML = "Searching..."
    //document.getElementById(div+'_div').style.display = 'block';
    var ajaxRequest = ajaxFunction();
    
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                //document.getElementById(div).innerHTML = ajaxRequest.responseText;
                show_pending(div,gid,page);
                show_members('show_members',gid,page);
            }
        } 

    ajaxRequest.open("GET", baseURL + "/groups/approve_pending.php?gid="+gid+"&ucode="+ucode, true);
    ajaxRequest.send(null); 
}

function join_group(gid){
    
    var ajaxRequest = ajaxFunction();
    
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('join_group').innerHTML = ajaxRequest.responseText;
            }
        } 

    ajaxRequest.open("GET", baseURL + "/groups/join_group.php?gid="+gid, true);
    ajaxRequest.send(null); 
}
function leave_group(gid){
    
    var ajaxRequest = ajaxFunction();
    
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('mygroups').innerHTML = ajaxRequest.responseText;
            }
        } 

    ajaxRequest.open("GET", baseURL + "/groups/leave_group.php?gid="+gid, true);
    ajaxRequest.send(null); 
}

function show_wall(div,gid,page){
    
    //document.getElementById(div+'_div').innerHTML = "Searching..."
    //document.getElementById(div+'_div').style.display = 'block';
    var ajaxRequest = ajaxFunction();
    
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById(div).innerHTML = ajaxRequest.responseText;
            }
        } 

    ajaxRequest.open("GET", baseURL + "/groups/show_wall.php?div="+div+"&gid="+gid+"&page="+page, true);
    ajaxRequest.send(null); 
}

function wall_add(gid){
    
    var ajaxRequest = ajaxFunction();
    var comment = escape(document.getElementById("comment").value);
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('show_wall').innerHTML = ajaxRequest.responseText;
            }
        } 

    ajaxRequest.open("GET", baseURL + "/groups/show_wall.php?gid="+gid+"&mode=add&comment="+comment, true);
    ajaxRequest.send(null); 
}
function wall_delete(gid,wid,page){
    
    var ajaxRequest = ajaxFunction();
    //var comment = escape(document.getElementById("comment").value);
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('show_wall').innerHTML = ajaxRequest.responseText;
            }
        } 

    ajaxRequest.open("GET", baseURL + "/groups/show_wall.php?gid="+gid+"&mode=delete&wid="+wid+"&page="+page, true);
    ajaxRequest.send(null); 
}

function show_bulkpm(lid,pid){
    
    var ajaxRequest = ajaxFunction();
    //var comment = escape(document.getElementById("comment").value);
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('bulkpm').innerHTML = ajaxRequest.responseText;
            }
        } 

    ajaxRequest.open("GET", baseURL + "/mydetails/bulk_pm.php?lid="+lid+"&mode=show&pid="+pid, true);
    ajaxRequest.send(null); 
}

function send_bulkpm(lid,pid){
    
    //loop through form and get checked friends.
    var friends = "";       var message = document.getElementById('bulkpm_form').elements['message'].value;
    var myobj = document.getElementById('bulkpm_form').elements;
    for (var i = 0;i<myobj.length;i++){
     //build up query string for post   
     var obj = myobj[i];
     if (obj){
        if(obj.checked){
            friends += obj.value+",";
        }
     }
    }
    
    var ajaxRequest = ajaxFunction();
    //var comment = escape(document.getElementById("comment").value);
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('bulkpm').innerHTML = ajaxRequest.responseText;
                window.location = baseURL + "/features.php";
            }
        } 

    ajaxRequest.open("GET", baseURL + "/mydetails/bulk_pm.php?lid="+lid+"&mode=send&pid="+pid+"&f="+friends+"&message="+message, true);
    ajaxRequest.send(null); 
}

function send_bulkinvite(gid){
    
    //loop through form and get checked friends.
    var friends = "";       
	var message = document.getElementById('bulkinvite_form').elements['message'].value;
    var myobj = document.getElementById('bulkinvite_form').elements;
    for (var i = 0;i<myobj.length;i++){
     //build up query string for post   
     var obj = myobj[i];
     if (obj){
        if(obj.checked){
            friends += obj.value+",";
        }
     }
    }
    
    var ajaxRequest = ajaxFunction();
    //var comment = escape(document.getElementById("comment").value);
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('invite_friends').innerHTML = ajaxRequest.responseText;
                //window.location = baseURL + "/features.php";
            }
        } 

    ajaxRequest.open("GET", baseURL + "/groups/bulk_invite.php?gid="+gid+"&mode=send&f="+friends+"&message="+message, true);
    ajaxRequest.send(null); 
}

function mytop5form(mode){
    
    var ajaxRequest = ajaxFunction();
    //var comment = escape(document.getElementById("comment").value);
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('mytopfive').innerHTML = ajaxRequest.responseText;
            }
        } 

    ajaxRequest.open("GET", baseURL + "/mydetails/mytopfive.php?Submit="+mode, true);
    ajaxRequest.send(null); 
}

function rate_top5(aid,score){
    
    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('rate_top5').innerHTML = ajaxRequest.responseText;
                top5_rating(aid);
            }
        } 
    
    ajaxRequest.open("GET", baseURL + "/users/usertopfive.php?aid="+aid+"&Submit=vote&score="+score, true);
    ajaxRequest.send(null); 
    
}
function top5_rating(aid){
    
    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('top5_rating').innerHTML = ajaxRequest.responseText;
            }
        } 
    
    ajaxRequest.open("GET", baseURL + "/users/usertopfive.php?aid="+aid+"&Submit=rating", true);
    ajaxRequest.send(null); 
    
}
function set_top5(aid){
    
    var ajaxRequest = ajaxFunction();
    ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                document.getElementById('set_top5').innerHTML = ajaxRequest.responseText;
            }
        } 
        
    ajaxRequest.open("GET", baseURL + "/build_top5.php?aid="+aid, true);
    ajaxRequest.send(null); 
    
}

// Load iTunes options into reviews page
function load_iTunesOptions(div, asin, pid)
{
    document.getElementById(div).innerHTML = "Loading iTunes options..."
    document.getElementById(div).style.display = 'block';
    var ajaxRequest = ajaxFunction();
    
    ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
        {
            document.getElementById(div).innerHTML = ajaxRequest.responseText;
        }
    } 

    ajaxRequest.open("GET", baseURL + "/reviews/get_itunes_options.php?div=" + div + "&asin=" + asin + "&pid=" + pid, true);
    ajaxRequest.send(null); 
}

var popUpWin=0;

function launch_invite() {
  var URLStr = baseURL + "/importer/";
  var left = 20;
  var top = 20;
  var width = 780;
  var height = 500;
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function showDiv(div){
    document.getElementById(div).style.display = "";
}
function hideDiv(div){
    document.getElementById(div).style.display = "none";
}