function afproGetTrackParam() { 
    get = new String(window.location);
    var res = null;
    var x = get.indexOf('?');
    if(x!=-1) {
        var tmp = get.substr(x+1, get.length-x);  
        var getparam = tmp.split('&');
        for(var i=0; i<getparam.length; i++)
        {
          tmp = getparam[i].split('=');
          if (tmp[0]=='affiliate_pro_tracking_id') 
          {
            var dateObj = new Date(2130, 12, 1);
            setCookie("afprotrackparam",tmp[1], null, dateObj, "."+window.location.hostname, false);
            res=tmp[1];
          }
        }

    }
    return res;    
}

function afproDocumentLoad() {
	afpro_tracking_code=afproGetTrackParam();  
    if (afpro_tracking_code == null) {
        afpro_tracking_code=getCookie("afprotrackparam");
    }    
	if (afpro_tracking_code != null) {
		afproInitFormHandlers();
	}
}

function afproInitFormHandlers() {
  var forms = document.forms;
  for(var i=0; i<forms.length; i++) {
    var className = " " + forms[i].action + " ";
    if((className.indexOf(' https://www.paypal.com/cgi-bin/webscr ') != -1) || (className.indexOf(' https://www.sandbox.paypal.com/cgi-bin/webscr ') != -1)) {
	  afpro_fc_paypal(forms[i]);
    }
    if(className.indexOf(' https://merchant.webmoney.ru/lmi/payment.asp ') != -1) {
	  afpro_fc_webmoney(forms[i]);
    }    
  }
}

function afpro_fc_paypal(form) {  
    var el = form.elements['notify_url'];
    if ("undefined" == typeof(el)) {
        tmpInput = document.createElement("input");
        tmpInput.setAttribute("type", "hidden");
        tmpInput.setAttribute("name", "notify_url");
        tmpInput.setAttribute("value", AFFPRO_BaseURL + "callbacks/callback_paypal_ipn.php");
        form.appendChild(tmpInput);
    }    
    el = form.elements['custom'];
    if ("undefined" == typeof(el)) {
        tmpInput = document.createElement("input");
        tmpInput.setAttribute("type", "hidden");
        tmpInput.setAttribute("name", "custom");
        tmpInput.setAttribute("value", afpro_tracking_code);
        form.appendChild(tmpInput);
    }    
    return;
}


function afpro_fc_webmoney(form) {  
    var el = form.elements['LMI_RESULT_URL'];
    if ("undefined" == typeof(el)) {
        tmpInput = document.createElement("input");
        tmpInput.setAttribute("type", "hidden");
        tmpInput.setAttribute("name", "LMI_RESULT_URL");
        tmpInput.setAttribute("value", AFFPRO_BaseURL + "callbacks/callback_webmoney.php");
        form.appendChild(tmpInput);
    }    
    el = form.elements['AFPRO_TRACKING_CODE'];
    if ("undefined" == typeof(el)) {
        tmpInput = document.createElement("input");
        tmpInput.setAttribute("type", "hidden");
        tmpInput.setAttribute("name", "AFPRO_TRACKING_CODE");
        tmpInput.setAttribute("value", afpro_tracking_code);
        form.appendChild(tmpInput);
    }        
    return;
}
