var MSG_INVALID_TIC = "Invalid Ticket Number:\n\nThis is not a recognized CharityMania ticket number.\nCheck the number on your ticket and try again.";
 
var MSG_ERROR = "Error!!!\n";
var ServerPath = "../CMServices/Validations.asmx/ValidateTicket";
//var ServerPath = "http://localhost:3799/CMServices/Validations.asmx/ValidateTicket";
var HostName=""; 
var TIC_MINI = 1;
var TIC_MAXI = 4960;
var ORG_LENGTH = 5;
var ticNumber;
var orgNumber;

function openPostedPage(url) {
    window.location = url + '?ON=' + orgNumber + '&TN=' + ticNumber;
    /*
    document.forms[0].method="post";
    document.forms[0].action=url;
    document.forms[0].submit();
    */
}

function openWinnerPage(url){
    if(document.forms[0].winner.value=="0") {
        alert("Winning tickets for this game have not yet been calculated and posted to the website.\nWinners are posted a day or two after the official dates posted on your ticket. ");
        return false;
    } else {
        openPostedPage(url);
    }
}

function _checkTicket(ticNo, orgNo) {
    //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);
            } else {
                alert(MSG_INVALID_TIC);
                return false;
            }
        } else {
            alert(MSG_INVALID_TIC);
            return false;
        }
    } catch(e) {
        //alert(MSG_ERROR+e.description);
        openPostedPage("../WhichFundraiser.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("../WhichFundraiser.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("../WhichFundraiser.aspx");
    }
}

function ProcessResponse(xmlhttp) { 
    if(xmlhttp.readyState == 4) {       
        if( xmlhttp.status != 200) {
            openPostedPage("../WhichFundraiser.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("../WhichFundraiser.aspx");
            } else {
                alert(Error_node.firstChild.data);
            }
            return false;
        } else {
            openPostedPage("../WhichFundraiser.aspx");
            //alert("Technical Error :\n\n Cannot find the redirect URL error. Please try later");
            return false;
        }
    }
}