﻿var MSG_INVALID_TIC = "Invalid Charity Studio Login ID \n\n Your login ID is your CharityMania \n sweepstakes ticket number.  This is not a \n recognized ticket number.  Check the number \n on your ticket and try again.";
 
 var MSG_ERROR = "Error!!!\n";
//var ServerPath = "/CMServices/Validations.asmx/ValidateTicket";
var ServerPath = "/CMServices/Validations.asmx/ValidateTicket";
var HostName=""; 
var TIC_MINI = 1;
var TIC_MAXI = 9999;
var ORG_LENGTH = 5;
var ticNumber;
var orgNumber;
function openPostedPage(url) {
    window.location = url + '?ON=' + orgNumber + '&TN=' + ticNumber;
 }
	
function fnOpenModal(url){
var sUrl=url;
var sFeatures="dialogHeight: 150px; dialogWidth: 200px;edge: raised; resizable: yes; status: no;";
window.showModalDialog(sUrl,sFeatures);
}

function _checkTicket2(ticNo, orgNo) {    
    if (!document.getElementById('chkTermsOfService').checked) {
        alert("You must first agree to the Terms of Service \n to enter the Charity Studio.");
        return false;
    }
     
    if (getCookie("CharityStudioAcceptedTerms") == undefined) {
        setCookie("CharityStudioAcceptedTerms", "1", 365);
    }
    
    //Check if ticno is not a number	
    if (isNaN(ticNo)) {

        alert(MSG_INVALID_TIC);
        return false;
    }
    try {
        var ticNo = parseInt(ticNo);
        if (ticNo >= TIC_MINI && ticNo <= TIC_MAXI) {
            if (orgNo.length == ORG_LENGTH) {
                ticNumber = ticNo;
                orgNumber = orgNo;
                _triggerValidation(ticNo, orgNo);
                //openPostedPage("clublogin.aspx");
            }
            else {
                alert(MSG_INVALID_TIC);
                return false;
            }
        }
        else {
            alert(MSG_INVALID_TIC);
            return false;
        }
        //Check if the term of service is checked or not

    }
    catch (e) {
        //alert(MSG_ERROR+e.description);
        openPostedPage("../clubloginvalidate.aspx");
        return false;

    }
}


function _triggerValidation(ticNo, orgNo)
{	
 
	GetDataViaAJAX(ServerPath, "TicketNum="+ticNo +"&OrgNum="+orgNo);//sendxmlDocToAjax(xmlPath, null);
	 
}

function GetDataViaAJAX(url, params)
{

 
    xmlhttp=null;
// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  
  xmlhttp=new XMLHttpRequest();
  if (xmlhttp.overrideMimeType) {
               xmlhttp.overrideMimeType('text/xml');
               }
   
  }
// code for IE
else if (window.ActiveXObject)
  {
  
            try {
                 xmlhttp= new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                 //implies browser does not support xmlhttp so post to the old asp page
				  openPostedPage("../clubloginvalidate.aspx");
                }
            }

  
  }
    
    if(xmlhttp!=null)
    { 
        xmlhttp.onreadystatechange = function() { ProcessResponse(xmlhttp); };
        xmlhttp.open("POST", ServerPath,true);
        xmlhttp.setRequestHeader("Host","");
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-Length","12");
        xmlhttp.send(params);
        
    }
    else
    {
    //implies browser does not support xmlhttp or for some reason unable to xmlhttp so post to the old asp page
    
    
       openPostedPage("../clubloginvalidate.aspx");
    }
 
}
function ProcessResponse(xmlhttp)
{ 
  if(xmlhttp.readyState == 4)
     {
         
      if( xmlhttp.status != 200)
      {        
             openPostedPage("../clubloginvalidate.aspx");
			//alert("Technical Error :\n\n Cannot find the redirect URL error. Please try later");
			return false;
         
      }
      
         var xmldoc = xmlhttp.responseXML;         
		var valid_ticket_node = xmldoc.getElementsByTagName('IsValidTicket').item(0);
		var TicketHolderPage_node=xmldoc.getElementsByTagName('TicketHolderPage').item(0);	
		var Error_node=xmldoc.getElementsByTagName('ErrorBasic').item(0);
		 
		  
			//check if the ticket is valid 
            if(valid_ticket_node.firstChild.data == "false")
            {
             
            if(valid_ticket_node.firstChild.data.indexOf("Technical Error")>= 0 || Error_node.firstChild.data.indexOf("Inactive Ticket")>= 0) {
                
				 openPostedPage("../clubloginvalidate.aspx");
				}
				else
				{

				        alert(Error_node.firstChild.data);
                   }

                 return false;
            }
            else {
                
				openPostedPage("../clubloginvalidate.aspx");
					//alert("Technical Error :\n\n Cannot find the redirect URL error. Please try later");
					return false;
				}
				
              
    }

}
function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}
function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
}



