function Ajax(){
    var ddr= null ;
    var browser = navigator.appName;

    window.XMLHttpRequest ? ddr = new XMLHttpRequest() : ( window.ActiveXObject ? ddr = new ActiveXObject("Microsoft.XMLHTTP") : ddr = new ActiveXObject("Msxml2.XMLHTTP") );
    return ddr;
}

function PostVal(Path,Param,xmlObj,destId){
    xmlObj =  Ajax();
    xmlObj.onreadystatechange=function(){
        if(xmlObj.readyState==4 && xmlObj.status==200)
        document.getElementById(destId).innerHTML = xmlObj.responseText;
    }
    xmlObj.open('POST', Path, true);
    xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlObj.setRequestHeader("Content-length", Param.length);
    xmlObj.setRequestHeader("Connection", "close");
    xmlObj.send(Param);
}

function PostVal1(Path,Param,xmlObj,destId,opt,val){
    xmlObj =  Ajax();
    xmlObj.onreadystatechange=function(){
        if(xmlObj.readyState==4 && xmlObj.status==200){
            document.getElementById(destId).innerHTML = xmlObj.responseText;
            //alert("Opt "+opt+" :: "+"Val "+val);
            if(opt == "match"){
                getMComments(''+val+'');
            }    
            else{
                getComments(location.pathname);
            }    
        }
    }
    xmlObj.open('POST', Path, true);
    xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlObj.setRequestHeader("Content-length", Param.length);
    xmlObj.setRequestHeader("Connection", "close");
    xmlObj.send(Param);
}

function appendOption(select,option){
    try {
        selectBox.add(option,null); // Standards compliant.
    } catch (e) {
        selectBox.add(option);      // IE only version.
    }
}

function PostValPlayer(Path,Param,xmlObj,destId){
    xmlObj =  Ajax();
    xmlObj.onreadystatechange=function(){
        if(xmlObj.readyState==4 && xmlObj.status==200){
            document.getElementById(destId).innerHTML = xmlObj.responseText;
            var size = document.getElementById("player1").options.length;
            var playersName = new Array();
            var playersId = new Array();

            for(j=2;j<12;j++){
                var selectBox = document.getElementById("player"+j);
                selectBox.length = 0;
                for(i=0;i<size;i++){
                    playersName[i] = document.getElementById("player1").options[i].text;
                    playersId[i] = document.getElementById("player1").options[i].value;
                    var option = document.createElement("option");
                    option.text = playersName[i];
                    option.value = playersId[i];
                    selectBox.add(option,null);
                }
            }
        }    
    }
    xmlObj.open('POST', Path, true);
    xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlObj.setRequestHeader("Content-length", Param.length);
    xmlObj.setRequestHeader("Connection", "close");
    xmlObj.send(Param);
}

function PostValBowler(Path,Param,xmlObj,destId){
    xmlObj =  Ajax();
    xmlObj.onreadystatechange=function(){
        if(xmlObj.readyState==4 && xmlObj.status==200){
            document.getElementById(destId).innerHTML = xmlObj.responseText;
            var size = document.getElementById("bowler1").options.length;
            var bowlersName = new Array();
            var bowlersId = new Array();

            for(j=2;j<12;j++){
                var selectBox = document.getElementById("bowler"+j);
                selectBox.length = 0;

                for(i=0;i<size;i++){
                    bowlersName[i] = document.getElementById("bowler1").options[i].text;
                    bowlersId[i] = document.getElementById("bowler1").options[i].value;
                    var option = document.createElement("option");
                    option.text = bowlersName[i];
                    option.value = bowlersId[i];
                    selectBox.add(option,null);
                    //alert(bowlersName[i]);
                }
            }
        }    
    }
    xmlObj.open('POST', Path, true);
    xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlObj.setRequestHeader("Content-length", Param.length);
    xmlObj.setRequestHeader("Connection", "close");
    xmlObj.send(Param);
}

function GetVal(Path,Param,xmlObj,destId){
    xmlObj =  Ajax();
    xmlObj.onreadystatechange=function()
    {
        if(xmlObj.readyState==4 && xmlObj.status==200)
            document.getElementById(destId).innerHTML = xmlObj.responseText;
    }
    xmlObj.open('get', Path+"?"+Param, true);
    xmlObj.send('null');
}

function GetVal1(Path,Param,xmlObj,destId){
    xmlObj =  Ajax();
    xmlObj.onreadystatechange=function()
    {
        if(xmlObj.readyState==4 && xmlObj.status==200){
            document.getElementById(destId).innerHTML = xmlObj.responseText;
            window.location.reload();
        }    
    }
    xmlObj.open('get', Path+"?"+Param, true);
    xmlObj.send('null');
}

function getScheduleByDate(val1,val2){
    var param = 'date='+val1+'&name='+val2;
    document.getElementById("schedule").innerHTML = 'please wait ..';
    GetVal('getSchedule.jsp',param,'getSchedule','schedule');
}

function getTeams(val){

    var param = 'schId='+val+'&name=batting';
    document.getElementById("bat").innerHTML = 'please wait ..';
    GetVal('getTeams.jsp',param,'getTeams','bat');

    var param = 'schId='+val+'&name=bowling';
    document.getElementById("bowl").innerHTML = 'please wait ..';
    PostVal('getTeams.jsp',param,'getTeams','bowl');
}

function getPlayers(val){
    //alert(val);
    var param = 'teamId='+val+'&name=player1';
    //alert(param);
    PostValPlayer('getPlayers.jsp',param,'getPlayers','divPlayer1');
}

function getBowlers(val){
    //alert(val);
    var param = 'teamId='+val+'&name=bowler1';
    //alert(param);
    PostValBowler('getPlayers.jsp',param,'getBowlers','divBowler1');
}

function getPlayers1(val){
    //alert(val);
    var param = 'teamId='+val+'&name=playerId&op=set';
    //alert(param);
    PostVal('getPlayers.jsp',param,'getPlayers','players');
}

function setPlayerName(val){
    alert(val);
}

function getEditorial(val){
    var param = 'edid='+val;
    //alert(param);
    document.getElementById("editorial").innerHTML = '<tr height="100px"><td align="center"><img src="ajax-loader.gif"></td></tr>';
    //PostVal('getEditorial.jsp',param,'getEditorial','editorial');
    var time = setTimeout("PostVal('getEditorial.jsp','"+param+"','getEditorial','editorial')",500);
}

function delPlayer(val){
    param = 'player_id='+val;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/delPlayer.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/delPlayer.jsp";
    document.getElementById("loader"+val).innerHTML = '<img src="ajax-loader2.gif">';
    var time = setTimeout("GetVal('"+url+"',\""+param+"\",'delEditorial','loader"+val+"')",500);
}

function updatePlayer(val1,val2){
    param = 'player_id='+val1+'&player_name='+val2;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/updatePlayer.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/updatePlayer.jsp";
    document.getElementById("message").innerHTML = '<img src="ajax-loader2.gif">';
    var time = setTimeout("PostVal('"+url+"',\""+param+"\",'updateComment','message')",500);
}

function delEditorial(val){
    param = 'ed_id='+val;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/delEditorial.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/delEditorial.jsp";
    document.getElementById("loader"+val).innerHTML = '<img src="ajax-loader2.gif">';
    var time = setTimeout("GetVal('"+url+"',\""+param+"\",'delEditorial','loader"+val+"')",500);
}

function delSchedule(val){
    param = 'sch_id='+val;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/delSchedule.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/delSchedule.jsp";
    document.getElementById("loader"+val).innerHTML = '<img src="ajax-loader2.gif">';
    var time = setTimeout("GetVal('"+url+"',\""+param+"\",'delComment','loader"+val+"')",500);
}

/********************************* Article Comment section *************************************/

function postComment(val){
    var name = document.getElementById("name").value;
    var email = document.getElementById("email").value;
    var comment = document.getElementById("comment").value;

    var param = 'name='+name+'&email='+email+'&comment='+comment+'&path='+val;
    param = (param+'').replace(/([\\"'])/g, "\\$1").replace(/\u0000/g, "\\0");
    //alert(param);

    var url = "http://headlinesindia.mapsofindia.com/adminpanel/postComment.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/postComment.jsp";

    document.getElementById("message").innerHTML = '<img src="/adminpanel/ajax-loader1.gif">';
    var time = setTimeout("PostVal1('"+url+"',\""+param+"\",'postComment','message','news','0')",500);

    document.getElementById("name").value = "";
    document.getElementById("email").value = "";
    document.getElementById("comment").value = "";
}

function getComments(val){
    //alert("Path "+val);
    var param = 'path='+val;
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/getComments.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/getComments.jsp";
    var time = setTimeout("PostVal('"+url+"',\""+param+"\",'getComments','comments')",0);
}

function delComment(val){
    param = 'commentid='+val;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/delComment.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/delComment.jsp";
    document.getElementById("loader"+val).innerHTML = '<img src="ajax-loader2.gif">';
    var time = setTimeout("GetVal('"+url+"',\""+param+"\",'delComment','loader"+val+"')",500);
}

function updateComment(val1,val2){
    param = 'commentid='+val1+'&comment='+val2;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/updateComment.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/updateComment.jsp";
    document.getElementById("message").innerHTML = '<img src="ajax-loader2.gif">';
    var time = setTimeout("PostVal('"+url+"',\""+param+"\",'updateComment','message')",500);
}

/********************************* Match Comment section *************************************/

function postMComment(val,val1){
    var name = document.getElementById("name").value;
    var email = document.getElementById("email").value;
    var comment = document.getElementById("comment").value;

    var param = 'name='+name+'&email='+email+'&comment='+comment+'&sch_id='+val+'&path='+val1;
    param = (param+'').replace(/([\\"'])/g, "\\$1").replace(/\u0000/g, "\\0");
    //alert(param);

    var url = "http://headlinesindia.mapsofindia.com/adminpanel/postMComment.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/postMComment.jsp";

    document.getElementById("message").innerHTML = '<img src="/adminpanel/ajax-loader1.gif">';
    var time = setTimeout("PostVal1('"+url+"',\""+param+"\",'postComment','message','match','"+val+"')",500);

    document.getElementById("name").value = "";
    document.getElementById("email").value = "";
    document.getElementById("comment").value = "";
}

function getMComments(val,val1){
    var param = 'sch_id='+val+'&path='+val1;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/getMComments.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/getMComments.jsp";
    var time = setTimeout("PostVal('"+url+"',\""+param+"\",'getComments','comments')",0);
}

function delMComment(val){
    param = 'commentid='+val;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/delMComment.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/delMComment.jsp";
    document.getElementById("loader"+val).innerHTML = '<img src="ajax-loader2.gif">';
    var time = setTimeout("GetVal('"+url+"',\""+param+"\",'delComment','loader"+val+"')",500);
}

function updateMComment(val1,val2){
    param = 'commentid='+val1+'&comment='+val2;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/updateComment.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/updateMComment.jsp";
    document.getElementById("message").innerHTML = '<img src="ajax-loader2.gif">';
    var time = setTimeout("PostVal('"+url+"',\""+param+"\",'updateComment','message')",500);
}

/********************************* Tweet and retweet section *************************************/

function getTButton(val){
    //alert("Path "+val);
    var param = 'path='+val;
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/getTButton.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/getTButton.jsp";
    var time = setTimeout("PostVal(\""+url+"\",\""+param+"\",'getTButton','tweet_this')",0);
}

function tweetThis(val){
    //alert("Path "+val);
    var param = 'path='+val;
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/tweet.jsp?path="+val+"&action=tweetThis";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/tweet.jsp?path="+val+"&action=tweetThis";
    window.open(url,"Login to twitter", "height=370,width=800");
}
        
function retweetThis(val){
    //alert("Path "+val);
    var param = 'path='+val;
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/tweet.jsp?path="+val+"&action=retweetThis";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/tweet.jsp?path="+val+"&action=retweetThis";
    window.open(url,"Login to twitter", "height=370,width=800");
}


/********************************* Tweet and retweet section for match *************************************/

function getMTButton(val,val1){
    var param = 'sch_id='+val+'&path='+val1;
    //alert("Param "+param);
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/getMTButton.jsp";
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/getMTButton.jsp";
    var time = setTimeout("PostVal(\""+url+"\",\""+param+"\",'getTButton','tweet_this')",0);
}

function tweetMThis(val,val1){
    var url = "http://headlinesindia.mapsofindia.com/adminpanel/tweet.jsp?path="+val1+"&action=tweetMThis&sch_id="+val;
    //var url = "http://localhost:8084/headlinesindia2/adminpanel/tweet.jsp?path="+val1+"&action=tweetMThis&sch_id="+val;
    //alert("URL "+url);
    window.open(url,"Login to twitter", "height=370,width=800");
}
