﻿// JScript File
// JScript File
var httpRequest=false;

function makeObject()
{
    var httpRequest;
    if(window.XMLHttpRequest)
    {
        httpRequest=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
        try
        {
            httpRequest=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
        }            
    }
    
    return httpRequest;
}

httpRequest = makeObject();

function _processRequest()
{
    if(httpRequest==null)httpRequest = makeObject();
    if(httpRequest.readyState == 4)
    {
        if(httpRequest.status==200)
        {
            var response=httpRequest.responseText;
            var update=new Array();
            if(response.indexOf('`' != -1))
            {
                update=response.split("`");
                if(update[0]=="search")
                {
                    document.getElementById("dvResult").innerHTML=update[1];
                    document.getElementById("hfId").value=update[2];
                  /*  document.getElementById("dvSubmit").style.display="block";*/
                }
                if(update[0]=="AddComment")
                {
                    switch(update[1])
                    {
                        case "T":
                        document.getElementById("dvResult").innerHTML="<p>Thanks, for giving your valuable comment.</p>";
                        
                        document.getElementById("txtNameForComment").value="";
                        document.getElementById("txtEmailForComment").value="";
                        document.getElementById("txtWebForComment").value="http://";
                        document.getElementById("txtCommentForComment").value="";
                        
                        
                        break;
                        
                        case "F":
                        document.getElementById("dvResult").innerHTML="<p>Some techical problem occurs while processing your comment. Please try after some time.</p>";
                        break;
                    }
                }
                if(update[0]=="searchMantras")
                {
                    document.getElementById("dvAjaxSearch").innerHTML=update[1];
                }
                if(update[0]=="searchEdit")
                {
                    document.getElementById("dvResult").innerHTML=update[1];
                    document.getElementById("hfId").value=update[2];
                    //document.getElementById("dvSubmit").style.display="block";
                }
                if(update[0]=="deleteItem")
                {
                    switch(update[1])
                    {
                        case "T":
                        var c="li"+update[2];
                        var a=document.getElementById(c).innerHTML.substring(0,document.getElementById(c).innerHTML.indexOf("<div"));
                        document.getElementById(c).innerHTML="<strike>"+a+"</strike>";
                        //document.getElementById(c).style.display="none";
                        break;
                        
                        case "F":
                        var c="li"+update[2];
                        var a=document.getElementById(c).innerHTML.substring(0,document.getElementById(c).innerHTML.indexOf("<div"));
                        document.getElementById(c).innerHTML=a+"<div+ id=\"spItem" + update[2] + "\"><a href=\"javascript:void(0);\"><img src=\"images/canEditMantra.gif\" alt=\"Remove\" title=\"Remove\" onclick=\"RemoveItems('" + update[2] + "')\"></a></div> [Unable to remove, Try again]";
                        break;
                    }
                }
                if(update[0]=="deleteTags")
                {
                    switch(update[1])
                    {
                        case "T":
                        var c="liTag"+update[2];
                        var a=document.getElementById(c).innerHTML.substring(0,document.getElementById(c).innerHTML.indexOf("<div"));
                        document.getElementById(c).innerHTML="<strike>"+a+"</strike>";
                        //document.getElementById(c).style.display="none";
                        break;
                        
                        case "F":
                        var c="liTag"+update[2];
                        var a=document.getElementById(c).innerHTML.substring(0,document.getElementById(c).innerHTML.indexOf("<div"));
                        document.getElementById(c).innerHTML=a+"<div id=\"spTags" + update[2] + "\"><a href=\"javascript:void(0);\"><img src=\"images/canEditMantra.gif\" alt=\"Remove\" title=\"Remove\" onclick=\"RemoveTags('" + update[2] + "')\"></a></div> [Unable to remove, Try again]";
                        break;
                    }
                }
            }
        }
    }
}

function CallAjax()
{
    if(document.getElementById("txtName").value=="")
    {
        alert("Please provide Mantra Name");
        document.getElementById("txtName").focus();
        document.getElementById("txtName").style.backgroundColor="#FFFFCC";
    }
    else
    {
        document.getElementById("txtName").style.backgroundColor="#FFFFFF";
        document.getElementById("dvResult").innerHTML="<p><strong>Searching Mantras </strong></p><img src=\"images/fm_loader.gif\" alt=\"Searching\" title=\"Searching\" />";
        document.getElementById("hfId").value="";
        document.getElementById("hfChecked").value="";
       /* document.getElementById("dvSubmit").style.display="none";*/
        var a = document.getElementById("txtName").value;
        a=encodeURIComponent(a);
        var b = document.getElementById("txtKeyWords").value;
        b=encodeURIComponent(b);
        httpRequest.open("POST","ajaxMantrasAdmin.aspx",true);
        httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpRequest.onreadystatechange=_processRequest;
        httpRequest.send("mode=search&text="+a+"&other="+b+"&m="+Math.random());
    }
}

function check(a)
{
    if(document.getElementById(a))
    {
        //alert(document.getElementById(a).checked);
        if(document.getElementById(a).checked)
        {
            document.getElementById(a).checked=false;
            AddMantrasItem(a,'0');
        }
        else
        {
            document.getElementById(a).checked=true;
            AddMantrasItem(a,'1');
        }
    }
    
    
    if(document.getElementById("chkAll").checked)
    {
        document.getElementById("chkAll").checked=false;
    }
    
            
}

function CheckAll()
{
    var a=document.getElementById("hfId").value;
    if(a!="")
    {
        var ids=new Array();
        ids=a.split(",");
        if(document.getElementById("chkAll").checked)
        {
            for(var i=0;i<ids.length;i++)
            {
                if(document.getElementById(ids[i]))
                {
                    document.getElementById(ids[i]).checked=true;
                }
            }
            document.getElementById("hfChecked").value=a;
        }
        else
        {
            for(var i=0;i<ids.length;i++)
            {
                if(document.getElementById(ids[i]))
                {
                    document.getElementById(ids[i]).checked=false;
                }
            }
            document.getElementById("hfChecked").value="";
        }
    }
}

function AddMantrasItem(Id,s)
{
    // 0 - Remove
    // 1 - Add
    switch(s)
    {
        case "0":
        Id=Id+",";
        //alert(Id);
        document.getElementById("hfChecked").value=replaceAll(document.getElementById("hfChecked").value,Id,"");
        break;
        
        case "1":
        document.getElementById("hfChecked").value+=Id+",";
        /*if(!CheckAvailable(Id))
        {
            alert("aaya");
            document.getElementById("hfChecked").value+=Id+",";
        }
        else
        {
            alert("aaya11");
        }*/   
        break;
    }
}

function CheckAvailable(Id)
{
    var f=false;
    var exId=new Array();
    if(document.getElementById("hfChecked").value!=null)
    {
        exId=document.getElementById("hfChecked").value.split(",");
        for(var i=0;i<exId.length;i++)
        {
            if(exId[i]==Id)
            {
                f=true;
                break;
            }
        }
    }
    return f;
}

function replaceAll(oldStr,findStr,repStr) {
  var srchNdx = 0;  // srchNdx will keep track of where in the whole line
                    // of oldStr are we searching.
  var newStr = "";  // newStr will hold the altered version of oldStr.
  while (oldStr.indexOf(findStr,srchNdx) != -1)  
                    // As long as there are strings to replace, this loop
                    // will run. 
  {
    newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
                    // Put it all the unaltered text from one findStr to
                    // the next findStr into newStr.
    newStr += repStr;
                    // Instead of putting the old string, put in the
                    // new string instead. 
    srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
                    // Now jump to the next chunk of text till the next findStr.           
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);
                    // Put whatever's left into newStr.             
  return newStr;
}

function Validate()
{
    var f=false;
    var msg="";
    
    if(document.getElementById("txtName").value=="")
    {
        
        document.getElementById("txtName").focus();
        document.getElementById("txtName").style.backgroundColor="#FFFFCC"
        msg="Please provide Mantra Name";
        f=false;
    }
    else
    {
        f=true;
        
    }
    
    /*
    // Description
    if(document.getElementById("txtDesc").value=="" && f)
    {
        f=true;
        document.getElementById("txtDesc").style.backgroundColor="#FFFFFF"
    }
    else if(msg=="")
    {
        msg="Please provide the description for Mantra";
        document.getElementById("txtDesc").focus();
        document.getElementById("txtDesc").style.backgroundColor="#FFFFCC"
        f=false;
    }
    
    // Meditation
    if(document.getElementById("txtMeditation").value=="" && f)
    {
        f=true;
        document.getElementById("txtMeditation").style.backgroundColor="#FFFFFF"
    }
    else if(msg=="")
    {
        msg="Please provide the Meditation for Mantra";
        document.getElementById("txtMeditation").focus();
        document.getElementById("txtMeditation").style.backgroundColor="#FFFFCC"
        f=false;
    }
    
    // Teaching
    if(document.getElementById("txtTeaching").value=="" && f)
    {
        f=true;
        document.getElementById("txtTeaching").style.backgroundColor="#FFFFFF"
    }
    else if(msg=="")
    {
        msg="Please provide the Teaching for Mantra";
        document.getElementById("txtTeaching").focus();
        document.getElementById("txtTeaching").style.backgroundColor="#FFFFCC"
        f=false;
    }
    
    // Tags
    if(document.getElementById("txtTags").value=="" && f)
    {
        f=true;
        document.getElementById("txtTags").style.backgroundColor="#FFFFFF"
    }
    else if(msg=="")
    {
        msg="Please provide the Tags for Mantra";
        document.getElementById("txtTags").focus();
        document.getElementById("txtTags").style.backgroundColor="#FFFFCC"
        f=false;
    }
    */
    if(!f)
        alert(msg);
    return f;
}

//-----------------------------------------------------------------
// Edit Mantra Section
//-----------------------------------------------------------------
function CheckSelected(){
    var a=document.getElementById("ddlMantraName").value;
    if(a=="-1"){
        alert("Please select the Mantra Name");
        document.getElementById("ddlMantraName").focus();
        return false;}
    else
        return true;        
}

function CallAjaxForEditMantra()
{
    if(document.getElementById("txtName").value=="")
    {
        alert("Please provide Mantra Name");
        document.getElementById("txtName").focus();
        document.getElementById("txtName").style.backgroundColor="#FFFFCC";
        return false;
    }
    else
    {
        document.getElementById("txtName").style.backgroundColor="#FFFFFF";
        document.getElementById("dvResult").innerHTML="<img src=\"images/fbloader.gif\" alt=\"Searching\" title=\"Searching\" />";
        
        
        var a = document.getElementById("txtName").value;
        a=replaceAll(a,"'","");
        a=encodeURIComponent(a);
        var b = document.getElementById("txtKeyWords").value;
        b=replaceAll(b,"'","");
        b=encodeURIComponent(b);
        var c=document.getElementById("txtId").value;
        c=encodeURIComponent(c);
        
        
        
        httpRequest.open("POST","ajaxMantrasAdmin.aspx",true);
        httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpRequest.onreadystatechange=_processRequest;
        httpRequest.send("mode=searchEdit&text="+a+"&other="+b+"&Id="+c+"&m="+Math.random());
    }        
}
function checkAlert(a)
{
    alert("Sorry, Item "+a+" Already Selected");
}
 
function CheckInActive()
{
    var b=document.getElementById("ddlMantraName").value;
    if(b=="-1")
    {
        alert("Please select the Mantra");
        document.getElementById("chkInActive").checked=false;
        document.getElementById("ddlMantraName").focus();
    }
    else
    {
        if(document.getElementById("chkInActive").checked)
        {
            var c=document.getElementById("txtName").value;
            c= replaceAll(c,"'","");
            var a=confirm("Are you sure to InActive : "+c);
            if(a)
            {
                document.getElementById("chkInActive").checked=true;
            }
            else
            {
                document.getElementById("chkInActive").checked=false;
            }            
        }
    }        
}
function RemoveItems(a)
{
    var b=document.getElementById("txtId").value;
    document.getElementById("spItem"+a).innerHTML="<img src=\"images/fbloader.gif\" alt=\"Searching\" title=\"Searching\" />";
    
    httpRequest.open("POST","ajaxMantrasAdmin.aspx",true);
    httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    httpRequest.onreadystatechange=_processRequest;
    httpRequest.send("mode=deleteItem&a="+a+"&b="+b+"&m="+Math.random());
}
function RemoveTags(a)
{
    var b=document.getElementById("txtId").value;
    document.getElementById("spTags"+a).innerHTML="<img src=\"images/fbloader.gif\" alt=\"Searching\" title=\"Searching\" />";
    
    httpRequest.open("POST","ajaxMantrasAdmin.aspx",true);
    httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    httpRequest.onreadystatechange=_processRequest;
    httpRequest.send("mode=deleteTags&a="+a+"&b="+b+"&m="+Math.random());
}
//function loadjscssfile(filename, filetype)
//{
//    var f=false;
//    if (filetype=="js")
//    { //if filename is a external JavaScript file
//        var fileref=document.createElement('script')
//        fileref.setAttribute("type","text/javascript")
//        fileref.setAttribute("src", filename)
//        f=true;
//    }
//    else if (filetype=="css")
//    { 
//        //if filename is an external CSS file
//        var fileref=document.createElement("link")
//        fileref.setAttribute("rel", "stylesheet")
//        fileref.setAttribute("type", "text/css")
//        fileref.setAttribute("href", filename)
//        f=true;
//    }
//    if (typeof fileref!="undefined")
//    {
//        document.getElementsByTagName("head")[0].appendChild(fileref);
//        f=true;
//    }
//        
//    return f;
//}
//-----------------------------------------------------------------
// Mantra Landing Section
//-----------------------------------------------------------------
function PostComment()
{
    var f=false;
    if(document.getElementById("txtNameForComment").value=="")
    {
        alert("Please provide Name");
        document.getElementById("txtNameForComment").focus();
        document.getElementById("txtNameForComment").style.backgroundColor="#FFFFCC";
        f=true;
        
        document.getElementById("txtCommentForComment").style.backgroundColor="#FFFFFF";
    }
    else if(document.getElementById("txtCommentForComment").value=="" && !f)
    {
        alert("Please provide Comment");
        document.getElementById("txtCommentForComment").focus();
        document.getElementById("txtCommentForComment").style.backgroundColor="#FFFFCC";
        f=true;
        document.getElementById("txtNameForComment").style.backgroundColor="#FFFFFF";
    }
    else if (!f)
    {
        document.getElementById("txtNameForComment").style.backgroundColor="#FFFFFF";
        document.getElementById("txtCommentForComment").style.backgroundColor="#FFFFFF";
        document.getElementById("dvResult").innerHTML="Sending your comment.<img src=\"http://www.spiritvoyage.com/images/commentloader.gif\" alt=\"\" title=\"\" />";
        
        var a = document.getElementById("txtNameForComment").value;
        a=encodeURIComponent(a);
        
        var b = document.getElementById("txtEmailForComment").value;
        b=encodeURIComponent(b);
        
        var c=document.getElementById("txtWebForComment").value;
        c=encodeURIComponent(c);
        
        var d=document.getElementById("txtCommentForComment").value;
        d=encodeURIComponent(d);
        
        var e=document.getElementById("hidItemId").value;
        e=encodeURIComponent(e);
        
        httpRequest.open("POST","ajaxMantrasAdmin.aspx",true);
        httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpRequest.onreadystatechange=_processRequest;
        httpRequest.send("mode=comment&name="+a+"&email="+b+"&web="+c+"&comment="+d+"&ItemId="+e+"&m="+Math.random());
    }
}
//-----------------------------------------------------------------


//-----------------------------------------------------------------
// Mantra Home Page
//-----------------------------------------------------------------
function Search(srcTerm,id)
{
    if(document.getElementById('Anchor'+id))
    {
       document.getElementById("Anchor"+id).className="selectAlph";
    }
    for(var i=1;i<=26;i++)
    {
        if(i==id)
            continue;
        else
        {
            if(document.getElementById('Anchor'+i))
            {
                document.getElementById("Anchor"+i).className="";
            }
        }            
    }
    
    document.getElementById("dvAjaxSearch").innerHTML="<img src=\"http://www.spiritvoyage.com/pictures/loading.gif\" alt=\"Searching\" style=\"margin:0 auto; width:32px; height:32px; display:block\">"
    
    httpRequest.open("POST","ajaxMantrasAdmin.aspx",true);
    httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    httpRequest.onreadystatechange=_processRequest;
    httpRequest.send("mode=searchMantras&keyword="+srcTerm+"&m="+Math.random());
    
} 
