﻿// JScript File
function Check(id)
{
    var flag=true;
    var an="";
    var itemId="";
    var itemname="";
    
        if(document.getElementById("hfItem").value=='')
        {
            document.getElementById("err").style.display="block";
            document.getElementById("errMsg").innerHTML="<font color='red'>* Please select artist/item.</font>";
            flag=false;
        }
        else
        {
            document.getElementById("err").style.display="none";
            an=document.getElementById("hfArtist").value;
            itemId=document.getElementById("hfItem").value;
            itemname=document.getElementById("hfItemName").value;
        }
   
    
    if(document.getElementById("txtComment")&& flag)
    {
        if(document.getElementById("txtComment").value=='')
        {
            document.getElementById("err").style.display="block";
            document.getElementById("errMsg").innerHTML="<font color='red'>* Please provide comment.</font>";
            document.getElementById("txtComment").focus();
            flag=false;
        }
        else
        {
            document.getElementById("err").style.display="none";
        }
    }
    
    if(document.getElementById("txtCustomerName")&& flag)
    {
        if(document.getElementById("txtCustomerName").value=='')
        {
            document.getElementById("err").style.display="block";
            document.getElementById("errMsg").innerHTML="<font color='red'>* Please provide your name.</font>";
            document.getElementById("txtCustomerName").focus();
            flag=false;
        }
        else
        {
            document.getElementById("err").style.display="none";
        }
    }
    
    if(document.getElementById("txtCustomerEmail")&& flag)
    {
        if(document.getElementById("txtCustomerEmail").value!='')
        {
            if(validateEmail(document.getElementById("txtCustomerEmail").value))
            {
                document.getElementById("txtCustomerEmail").focus();
                document.getElementById("errMsg").innerHTML="<font color='red'>* Please provide correct email.</font>";
                flag=false;
            }
        }
    }
    
    //alert(flag);
    if(flag)
    {
        document.getElementById("errMsg").innerHTML="<img src='http://www.spiritvoyage.com/Images/commentloader.gif' alt='Processing'><font color='#D63B02'>&nbsp;Sending your comment.</font>";
        CallAjax(id,an,itemId,itemname);
    }        
}

function validateEmail(value) 
{
    //Validating the email field
    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 validateURL(value)
{
    var regex = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/
    // /^http(s)?:/\/\([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?/
    if(value.match(regex))
    {
        alert("nahi..............");
    }
    else
    {
        alert("Yupeee.........");
    }
    
}

function CallAjax(id,an,itemId,itemname)
{
    var comment=document.getElementById("txtComment").value;
    var name=document.getElementById("txtCustomerName").value;
    var email=document.getElementById("txtCustomerEmail").value;
    var website=document.getElementById("txtCustomerWebsite").value;
    
    comment=replaceAll(comment,"&","-XXXCXXX-");
    
    var httpRequest = makeRequestObject();
    httpRequest.open("POST","http://www.spiritvoyage.com/ajaxItemCommentNew.aspx",true);
    //httpRequest.open("POST","http://localhost:2809/spiritvoyagenew/ajaxItemCommentNew.aspx",true);
    //httpRequest.open("POST","ajaxItemCommentNew.aspx",true);
    httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    httpRequest.onreadystatechange=processRequest;
    var mode="ip";
    if(an.length==0 && itemId.length>0)
    {
          if(itemId.indexOf("COL-")>=0)
          {
            mode="cip";
          }
          else
          {
            mode="ip";
          }
    }
    if(an.length>0)
    {
       mode="ap";
    }
    httpRequest.send("mode="+mode+"&aname="+an+"&i="+id+"&c="+comment+"&n="+name+"&e="+email+"&w="+website+"&itemId="+itemId+"&en="+itemname+"&m="+Math.random());
    
    // ------------------------- AJAX ---------------------------------


function makeRequestObject()
{
    var httpRequest = false;
    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;
}



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("`");
                if(update[0]=="T")
                {
                    document.getElementById("errMsg").style.textAlign="left";
                    document.getElementById("errMsg").style.color="#D63B02";
                    //document.getElementById("errMsg").innerHTML = "Thanks for giving your valuable comment."

                    if(update[1]!=null)
                    {

                         document.getElementById("errMsg").innerHTML =update[1]
                      
                    }
                    else
                    {

                      if (an=="Snatam kaur") 
                      {
                         document.getElementById("errMsg").innerHTML = "Thanks for giving your valuable comment. If you like to subscribe for Snatam Kaur's Newsletter, please <span class=\"moreTxt\"><a href=\"http://www.snatamkaur.com/NewsletterSignUp.html\" target=\"_blank\" ><strong>click here</strong></a>.</span>"

                      }
                      else 
                      {
                         document.getElementById("errMsg").innerHTML = "Thanks for giving your valuable comment."
                      }
                    }

                    if(document.getElementById("txtComment"))
                    {
                        document.getElementById("txtComment").value="";
                    }
                    if(document.getElementById("txtCustomerName"))
                    {
                        document.getElementById("txtCustomerName").value="";
                    }
                    if(document.getElementById("txtCustomerEmail"))
                    {
                        document.getElementById("txtCustomerEmail").value="";
                    }
                    if(document.getElementById("txtCustomerWebsite"))
                    {
                        document.getElementById("txtCustomerWebsite").value="http://";
                    }
                }
                else if(update[0]=="F")
                {
                    document.getElementById("errMsg").style.textAlign="left";
                    document.getElementById("errMsg").style.color="#D63B02";
                    document.getElementById("errMsg").innerHTML="Error, Occured please try again."
                }
            }
        }            
    }
}


    
    
}

function EventAction(event)
{
    //alert(event.keyCode);
    if(event.keyCode=='13') 
    {
        //document.getElementById("btnSubmit").focus();
        Check();
        return false;
    }
    else
    {
        return true;
    }
    
}

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 savename(a,b,c)
{
    document.getElementById("hfArtist").value=a;
    document.getElementById("hfItem").value=b;
    document.getElementById("hfItemName").value=c;
}
function savenameforitem(a,b)
{
    document.getElementById("hfArtist").value="";
    document.getElementById("hfItem").value=b;
    document.getElementById("hfItemName").value=a;
}

