var c2s_ref = document.referrer;
var jsessionid = null;
var uid = new Date().getTime();     // save in cookie

function initialize(){
    var savedUid = get_cookie("uid");
    if(savedUid == null){
        document.cookie = "uid=" + uid;
    }
    else{
        uid = savedUid;
    }
}

function redirectTo(buyURL){
    document.location = buyURL;
}

function generateBuyNowButton(buyURL, imageURL, serverName, productId){
    initialize();
    try{
        if(buyURL.indexOf("http://") >= 0){
            buyURL = buyURL.replace("http://", "https://");
        }
    }catch(Error){ /* just in case */ }
    //registering uid
    document.write("<img style=\"display:none\" src=\"http://" + serverName +"/ok.do?uid=" + uid + "&ref=" + encodeURI(c2s_ref) + "&product_id=" + productId + "\" >");
    document.write("<a href=\"javascript:redirectTo('" + buyURL + "&uid=" + uid + "')\" >");
    document.write("<img src=\""+ imageURL +"\" border=\"0\" title=\"Buy now!\" alt=\"Buy now!\" >");
    document.write("</a>");
}

function generateBuyNowLink(buyURL, serverName, productId){
    initialize();
    try{
        if(buyURL.indexOf("http://") >= 0){
            buyURL = buyURL.replace("http://", "https://");
        }
    }catch(Error){ /* just in case */ }
    document.write("<img style=\"display:none\" src=\"http://" + serverName +"/ok.do?uid=" + uid + "&ref=" + encodeURI(c2s_ref) + "&product_id=" + productId + "\" >");
    document.write("<a href=\"" + buyURL + "\" >");
    document.write("Order Now!");
    document.write("</a>");
}

function generateBuyLink(buyURL){
    document.write(buyURL);
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}


