﻿var httpRequest = makeRequestObject();

    // ------------------------- AJAX ---------------------------------
function makeRequestObject()
{

  var objRequest;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer") 
  {		
  	objRequest = new ActiveXObject("Microsoft.XMLHTTP");     	
  } 
  else 
  { 	
    objRequest = new XMLHttpRequest();
  }
  return objRequest;
}

//------Add Tag Starts--------------------------
function AddTag(RecType,TagStatus,hidItemId,txtTagName)
{
  
          if(document.getElementById(hidItemId)!=null && document.getElementById(txtTagName) !=null)
          {
                if(document.getElementById(hidItemId).value!="")
                {
                     if(document.getElementById(txtTagName).value!="")
                     {
                        var recId=document.getElementById(hidItemId).value;
                        var tagName=document.getElementById(txtTagName).value;
                       httpRequest.open("get", "http://www.spiritvoyage.com/AjaxTag.aspx?action=add&RecId="+recId+"&RecType="+RecType+"&TagText="+tagName+"&TagStatus="+TagStatus+"&m="+ Math.random(),true);  
                       // httpRequest.open("get", "http://localhost:1623/spiritvoyagenew/AjaxTag.aspx?action=add&RecId="+recId+"&RecType="+RecType+"&TagText="+tagName+"&TagStatus="+TagStatus+"&m="+ Math.random(),true);  
                        httpRequest.onreadystatechange = processRequestAddTag;
                        httpRequest.send(null);
                     }
                     else
                     {
                       if(document.getElementById("divTagmsg")!=null)
                       {
                       
                        document.getElementById("divTagmsg").innerHTML="<font color='red'>* Please provide tag name.</font><br/><br/>"
                       }
                     
                        
                        return false;
                     }
               }
               else
               {
                       if(document.getElementById("divTagmsg")!=null)
                       {
                        document.getElementById("divTagmsg").innerHTML="<font color='red'>* Item is not available!</font><br/><br/>"
                       }
                  
                  return false;
               }
         }  
          

 }

function processRequestAddTag()
{
            if(httpRequest.readyState < 4)
            {
              if(document.getElementById("divTagmsg")!=null)
              {
                document.getElementById("divTagmsg").innerHTML="<img src='http://www.spiritvoyage.com/Images/commentloader.gif' alt='Processing'><font color='#D63B02'>&nbsp;Please wait.</font>"
              }
            }
   
            if(httpRequest.readyState == 4)
            {
                        if(httpRequest.status==200)
                        {
                                    document.getElementById("divTagmsg").innerHTML="";
                                    var response=httpRequest.responseText;
                                    var update=new Array();
                                    if(response.indexOf('|' != -1))
                                    {
                                        update=response.split("|");
                                       if(update[0]=="Added")
                                       {
                                         if(document.getElementById("txtTagName")!=null)
                                         {
                                           document.getElementById("txtTagName").value="";
                                         }
                                          
                                           if(document.getElementById("divTagmsg")!=null)
                                           {
                                             document.getElementById("divTagmsg").innerHTML="<font color='red'>Tag added successfully!<br/>Thanks for adding the Tag. Once approved, it shall be displayed on this page.</font><br/><br/>"
                                           }
                                       }
                                       if(update[0]=="Not Added")
                                       {
                                           if(document.getElementById("divTagmsg")!=null)
                                           {
                                             document.getElementById("divTagmsg").innerHTML="<font color='red'>* Tag is not added!</font><br/><br/>"
                                           }
                                        
                                       }
                                       if(update[0]=="Error")
                                       {
                                           if(document.getElementById("divTagmsg")!=null)
                                           {
                                             document.getElementById("divTagmsg").innerHTML="<font color='red'>* There is some server error!</font><br/><br/>"
                                           }
                                        
                                       }
                                          
                                    }
                        }            
            }
}

//   ------Add Tag Ends-------------------



//   ----Approve Tag strts----------------


function processRequest()
{
   // if(httpRequest==null)httpRequest = makeRequestObject();
    if(httpRequest.readyState == 4)
    {
        if(httpRequest.status==200)
        {
            var response=httpRequest.responseText;
            var update=new Array();
            if(response.indexOf('`' != -1))
            {
            
                update=response.split("`");
                switch(update[0])
                {
                    case "Approve":
                        if(update[1]=='T')
                        {
                            document.getElementById('L'+update[2]).style.color="#D63B02";
                            document.getElementById('L'+update[2]).innerHTML="Tag approved.";
                        }
                        else
                        {
                            document.getElementById('L'+update[2]).innerHTML="Error, Tag approved.";
                        }
                    break;
                    
                    case "Decline":
                        if(update[1]=='T')
                        {
                         
                           //document.location.href="ApproveTag.aspx";
                           document.location.href="http://www.spiritvoyage.com/ApproveTag.aspx";
                        }
                        else
                        {
                            document.getElementById('L'+update[2]).innerHTML="Error, Decline approved.";
                        }
                    break;
                    
                    case "Edit":
                        if(update[1]=='T')
                        {
                            document.getElementById('dvShowProcess'+update[2]).style.color="#D63B02";
                            
                            document.getElementById('dvShowProcess'+update[2]).style.marginBottom="0px"
                            document.getElementById('dvShowProcess'+update[2]).style.marginRight="0px"
                            document.getElementById('dvShowProcess'+update[2]).style.marginTop="-20px"
                            document.getElementById('dvShowProcess'+update[2]).style.marginLeft="50px"
                            
                            document.getElementById("dvShowProcess"+update[2]).innerHTML="Tag Saved.";
                            
                            document.getElementById("ed"+update[2]).style.display="block";
                            document.getElementById("sv"+update[2]).style.display="none";
                            document.getElementById("cn"+update[2]).style.display="none";
                            
                            document.getElementById("ed"+update[2]).disabled=false;
                        }
                        else
                        {
                            document.getElementById('dvShowProcess'+update[2]).style.color="#D63B02";
                            
                            document.getElementById('dvShowProcess'+update[2]).style.marginBottom="0px"
                            document.getElementById('dvShowProcess'+update[2]).style.marginRight="0px"
                            document.getElementById('dvShowProcess'+update[2]).style.marginTop="-20px"
                            document.getElementById('dvShowProcess'+update[2]).style.marginLeft="50px"
                            
                            document.getElementById("dvShowProcess"+update[2]).innerHTML="<p style=\"float:left; margin:4px 0 0 5px; width:500px;\">Error,occur on Tag Saved.</p>";
                            
                            document.getElementById("ed"+update[2]).style.display="block";
                            document.getElementById("sv"+update[2]).style.display="none";
                            document.getElementById("cn"+update[2]).style.display="none";
                            
                            document.getElementById("ed"+update[2]).disabled=false;
                        }
                    break;
                }
            }
        }            
    }
}


function decline(id)
{
    document.getElementById('app'+id).disabled=true;
    document.getElementById('dec'+id).disabled=true;
    if(document.getElementById('chk'+id))
        document.getElementById('chk'+id).disabled=true;
    document.getElementById('L'+id).style.display="block";
    
    // ------------------------------------------------------------------
    //var httpRequest = makeRequestObject();
//    httpRequest.open("POST","http://www.spiritvoyage.com/ajaxApprovTag.aspx",true);
    httpRequest.open("POST","ajaxApprovTag.aspx",true);
    httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    httpRequest.onreadystatechange=processRequest;
    httpRequest.send("mode=Decline&id="+id+"&m="+Math.random());
}

function approve(id,an)
{
   
    if(id!="")
    {
        document.getElementById('app'+id).disabled=true;
        document.getElementById('dec'+id).disabled=true;
        if(document.getElementById('chk'+id))
            document.getElementById('chk'+id).disabled=true;
      
        document.getElementById('L'+id).style.display="block";
        // ------------------------------------------------------------------
       // var httpRequest = makeRequestObject();
        //httpRequest.open("POST","http://www.spiritvoyage.com/ajaxApprovTag.aspx",true);
        httpRequest.open("POST","ajaxApprovTag.aspx",true);
        httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        httpRequest.onreadystatechange=processRequest;
        httpRequest.send("mode=Approve&id="+id+"&m="+Math.random());
    }
    else
    {
      alert("Tagid cannot be left blank.");
    }
}

function ShowTextBox(id)
{
    if(document.getElementById('chk'+id).checked){
        document.getElementById('dvEmailArtist'+id).style.display="block";
       
        }
    else
        document.getElementById('dvEmailArtist'+id).style.display="none";
}

function validateEmail(value) 
{
   
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    if (value.match(re)) 
    {
        return (false);
    }
    return(true);
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function MakeEditableTextArea(id)
{
   
    document.getElementById("dvShowProcess"+id).innerHTML="";
    document.getElementById("ed"+id).style.display="none";
    document.getElementById("sv"+id).style.display="block";
    document.getElementById("cn"+id).style.display="block";
    
    document.getElementById("cmt"+id).disabled=false;
    document.getElementById("cmt"+id).focus();
    
    document.getElementById("sv"+id).disabled=false;
    document.getElementById("cn"+id).disabled=false;
}

function cancelEdit(id)
{
    document.getElementById("ed"+id).style.display="block";
    document.getElementById("sv"+id).style.display="none";
    document.getElementById("cn"+id).style.display="none";
    
    document.getElementById("cmt"+id).disabled=true;
}

function SaveTextArea(id)
{
    document.getElementById("sv"+id).disabled=true;
    document.getElementById("cn"+id).disabled=true;
    document.getElementById("dvShowProcess"+id).style.display="block";
    
   
    var value=document.getElementById("cmt"+id).value;
    document.getElementById("cmt"+id).disabled=true;
    

    
    value=replaceAll(value,"?","%3F");
    value=replaceAll(value,"&","%26");
    
    //var httpRequest = makeRequestObject();
   // httpRequest.open("POST","http://www.spiritvoyage.com/ajaxApprovTag.aspx",true);
    httpRequest.open("POST","ajaxApprovTag.aspx",true);
    httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    httpRequest.onreadystatechange=processRequest;
    httpRequest.send("mode=Edit&text="+value+"&id="+id+"&m="+Math.random());
    //-------------------------------
}

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;
}

//   ----Approve Tag Ends--------------
