/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function setCookie(NameOfCookie, value, expiredays)
{
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

    document.cookie = NameOfCookie + "=" + escape(value) +
    ((expiredays == null)?"" : ";expires=" + ExpireDate.toGMTString());
}
function getCookie(NameOfCookie)
{
    if (document.cookie.length > 0)
    {
        begin = document.cookie.indexOf(NameOfCookie+"=");
        if (begin != -1)
        {
            begin += NameOfCookie.length+1;
            end = document.cookie.indexOf(";", begin);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        }
    }
    return null;
}

function delCookie(NameOfCookie)
{
    if (getCookie(NameOfCookie))
    {
        document.cookie = NameOfCookie + "=" +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function checkCookie(NameOfCookie, value, expiredays)
{
    if(getCookie(NameOfCookie) == null)
    {
        setCookie(NameOfCookie,value,expiredays);
    }
}

function setCookieToAI(id, NameOfCookie)
{
    var value = getCookie(NameOfCookie);
    if(value != null){
        CheckSecondAffiliate(value, id);
    }
}

function SetAffiliateID(NameOfCookie, value){
    if(getCookie(NameOfCookie) == null)
    {
        CheckAffiliateID(NameOfCookie, value, 0);
    }else{
        CheckAffiliateID(NameOfCookie, value, 1);
    }
}

var xmlhttp;

function CheckAffiliateID(NameOfCookie, value, cookieExist)
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="AjaxCheckAffiliateID.php";
    url =url+"?value="+value;
    url =url+"&nameOfCookie="+NameOfCookie;
    url =url+"&cookieExist="+cookieExist;
    xmlhttp.open("post",url,false);
    xmlhttp.send(null);
    if (xmlhttp.readyState==4)
    {
        if (xmlhttp.status==200){
            xmlDoc = xmlhttp.responseXML;
            if(xmlDoc){
                var ack = GetXMLNode(xmlDoc, "ack", 0, 0);
                if(ack == 1){
                    if(cookieExist == 0){
                        setCookie(GetXMLNode(xmlDoc, "nameOfCookie", 0, 0),GetXMLNode(xmlDoc, "value", 0, 0),GetXMLNode(xmlDoc, "expiredays", 0, 0));
                    }
                }
            }
        }else{
            alert(xmlhttpRequestStatus(xmlhttp.status));
        }
    }
}

function CheckSecondAffiliate(value, id){
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="AjaxCheckSecondAffiliateID.php";
    url =url+"?value="+value;
    xmlhttp.open("post",url,false);
    xmlhttp.send(null);
    if (xmlhttp.readyState==4)
    {
        if (xmlhttp.status==200){
            xmlDoc = xmlhttp.responseXML;
            if(xmlDoc){
                document.getElementById(id).value = GetXMLNode(xmlDoc, "referralID", 0, 0);
            }
        }else{
            alert(xmlhttpRequestStatus(xmlhttp.status));
        }
    }
}
