﻿var discLink, discImg, discTxt;

function checkDiscCode()
{
discLink = document.getElementById('but-disc');
discImg = document.getElementById('but-disc-img');
discTxt = document.getElementById('txtDiscount');

discTxt.readOnly = true;
discImg.src = "/images/but-loading.gif";
sendData(discTxt.value);
discLink.onclick = null;
return true;
}


var xmlHttp;

function sendData(discCode)
{
xmlHttp = GetXmlHttpObject();
var url='subscribe.asp';
var postvars = "aj=true&code=" + discCode
xmlHttp.onreadystatechange=ajaxBack;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8'); 
xmlHttp.send(postvars);
return true;
}

function ajaxBack()
{
if (xmlHttp.readyState==4)
    {
    if (xmlHttp.responseText=="yes")
        {
        //valid code;
        discTxt.readOnly = false;
        discLink.onclick = null;
        discTxt.onkeydown = showCheck;
        discImg.src = "/images/but-valid.gif";
        }
    else
        {
        //invalid code;
        discTxt.readOnly = false;
        discLink.onclick = null;
        discTxt.onkeydown = showCheck;
        discImg.src = "/images/but-invalid.gif";
        }    
    }
}

function showCheck()
{
discImg.src = "/images/but-check.gif";
discLink.onkeydown = null;
discLink.onclick = checkDiscCode;
}


function GetXmlHttpObject()
{var xmlHttp=null;
try  {xmlHttp=new XMLHttpRequest();}
catch (e) {
try {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}}
return xmlHttp;}