function $get(object) { return document.getElementById(object); }

function saveFieldsPath()
{
	fieldsArray = new Array();
	var current=0;
	var continueLoop=1;
	while (continueLoop!=0)
	{
	    var fieldsOfferArray = new Array();
	    var currentOffer = 0;
	    var continueLoopOffer = 1;
	    while (continueLoopOffer)
	    {
            var idField = $get("hidOfferID" + current + "_" + currentOffer);
		    var offerNameField = $get("txtCampaignOffer" + current + "_" + currentOffer);
		    var offerUrlField = $get("txtCampaignOfferURL" + current + "_" + currentOffer);
		    var offerPayoutField = $get("txtPayout" + current + "_" + currentOffer);
		    var offerShareField = $get("txtOfferShare" + current + "_" + currentOffer);
		    var networkField = $get("ddlNetwork" + current + "_" + currentOffer);
            var inactiveOffField = $get("chkInactiveOff" + current + "_" + currentOffer);
		    if (idField!=null && offerNameField!=null && offerUrlField!=null && offerPayoutField!=null &&
                offerShareField!=null && networkField!=null && inactiveOffField!=null)
		    {
		        networkValue = networkField.options[networkField.selectedIndex].value;
			    fieldsOfferArray[currentOffer] = {'id':idField.value, 'offer':offerNameField.value, 'url':offerUrlField.value, 
			        'payout': offerPayoutField.value, 'share':offerShareField.value, 'network':networkValue, 'inactive':inactiveOffField.checked};
	            currentOffer++;
	        }
	        else
	            continueLoopOffer = 0;
	    }

	    var fieldsLandingArray = new Array();
	    var currentLanding = 0;
	    var continueLoopLanding =1;
	    while (continueLoopLanding!=0)
	    {
		    var idField = $get("hidLandingID" + current + "_" + currentLanding);
	        var landingNameField = $get("txtLanding" + current + "_" + currentLanding);
	        var landingIDField = $get("txtLandingPageID" + current + "_" + currentLanding);
	        var landingUrlField = $get("txtLandingUrl" + current + "_" + currentLanding);
	        var landingShareField = $get("txtShare" + current + "_" + currentLanding);
		    var inactiveField = $get("chkInactive" + current + "_" + currentLanding);
	        if (idField!=null && landingNameField!=null && landingIDField!=null && 
	            landingUrlField!=null && landingShareField!=null && landingShareField!=null)
	        {
	            fieldsLandingArray[currentLanding] = {'id':idField.value, 'name':landingNameField.value, 'landingid':landingIDField.value,
	                'url':landingUrlField.value, 'share':landingShareField.value, 'inactive':inactiveField.checked };
	            currentLanding++;
	        }
	        else
	            continueLoopLanding = 0;
	    }
	    if (currentOffer>0 && currentLanding>0)
        {
            var sharePathField = $get("txtSharePath" + current);
        	fieldsArray[current] = {'offer':fieldsOfferArray, 'landing':fieldsLandingArray, 'sharepath':sharePathField.value};
		    current++;
		}
		else
			continueLoop = 0;
	}
}

function saveFieldsCta()
{
    var fieldsLandingArray = new Array();
    var currentLanding = 0;
    var continueLoopLanding =1;
    while (continueLoopLanding!=0)
    {
        var idField = $get("hidLandingID" + currentLanding);
        var landingNameField = $get("txtLanding" + currentLanding);
        var landingIDField = $get("txtLandingPageID" + currentLanding);
        var landingUrlField = $get("txtLandingUrl" + currentLanding);
        var landingShareField = $get("txtShare" + currentLanding);
        var inactiveField = $get("chkInactive" + currentLanding);
        if (idField!=null && landingNameField!=null && landingIDField!=null && 
            landingUrlField!=null && landingShareField!=null && inactiveField!=null)
        {
            fieldsLandingArray[currentLanding] = {'id':idField.value, 'name':landingNameField.value, 'landingid':landingIDField.value, 
                'url':landingUrlField.value, 'share':landingShareField.value, 'inactive':inactiveField.checked };
            currentLanding++;
        }
        else
            continueLoopLanding = 0;
    }
	fieldsArray = new Array();
	fieldsArray["offer"] = new Array();
	var current=0;
	var continueLoop=1;
	while (continueLoop!=0)
	{
	    var fieldsOfferArray = new Array();
	    var currentOffer = 0;
	    var continueLoopOffer = 1;
	    while (continueLoopOffer)
	    {
		    var idField = $get("hidOfferID" + current + "_" + currentOffer);
		    var offerNameField = $get("txtCampaignOffer" + current + "_" + currentOffer);
		    var offerUrlField = $get("txtCampaignOfferURL" + current + "_" + currentOffer);
		    var offerPayoutField = $get("txtPayout" + current + "_" + currentOffer);
		    var offerShareField = $get("txtOfferShare" + current + "_" + currentOffer);
		    var networkField = $get("ddlNetwork" + current + "_" + currentOffer);
		    var inactiveOffField = $get("chkInactiveOff" + current + "_" + currentOffer);
		    if (idField!=null && offerNameField!=null && offerUrlField!=null && offerPayoutField!=null && 
		        offerShareField!=null && networkField!=null && inactiveOffField!=null)
		    {
		        networkValue = networkField.options[networkField.selectedIndex].value;
			    fieldsOfferArray[currentOffer] = {'id':idField.value, 'offer':offerNameField.value, 'url':offerUrlField.value, 
			        'payout': offerPayoutField.value, 'share':offerShareField.value, 'network':networkValue, 'inactive':inactiveOffField.checked};
	            currentOffer++;
	        }
	        else
	            continueLoopOffer = 0;
	    }
	    if (currentOffer>0)
        {
        	fieldsArray["offer"][current] = fieldsOfferArray;
		    current++;
		}
		else
			continueLoop = 0;
	}
	fieldsArray["landing"] = fieldsLandingArray;
}

function saveFieldsSequence()
{
	fieldsArray = new Array();
	fieldsArray["offer"] = new Array();
	fieldsArray["landing"] = new Array();
	var current=0;
    var continueLoop=1;
	while (continueLoop!=0)
	{
        var fieldsOfferArray = new Array();
        var currentOffer = 0;
        var continueLoopOffer=1;
        while(continueLoopOffer!=0)
        {
            var idField = $get("hidOfferID" + current + "_" + currentOffer);
		    var offerNameField = $get("txtCampaignOffer" + current + "_" + currentOffer);
            var offerUrlField = $get("txtCampaignOfferURL" + current + "_" + currentOffer);
            var offerPayoutField = $get("txtPayout" + current + "_" + currentOffer);
            var offerShareField = $get("txtOfferShare" + current + "_" + currentOffer);
            var networkField = $get("ddlNetwork" + current + "_" + currentOffer);
            var inactiveOffField = $get("chkInactiveOff" + current + "_" + currentOffer);
		    if (idField!=null && offerNameField!=null && offerUrlField!=null && offerPayoutField!=null && 
		        offerShareField!=null && networkField!=null && inactiveOffField!=null)
            {
                networkValue = networkField.options[networkField.selectedIndex].value;
	            fieldsOfferArray[currentOffer] = {'id':idField.value, 'offer':offerNameField.value, 'url':offerUrlField.value, 
	                'payout': offerPayoutField.value, 'share':offerShareField.value, 'network':networkValue, 'inactive':inactiveOffField.checked};
                currentOffer++;
            }
            else
                continueLoopOffer = 0;
        }
    	if (currentOffer>0)
        {
        	fieldsArray["offer"][current] = fieldsOfferArray;
		    current++;
		}
		else
			continueLoop = 0;
	}

    var fieldsLandingArrayTr = new Array();
	var currentTr=0;
	var continueLoopTr=1;
	while (continueLoopTr!=0)
	{
        var idField = $get("hidLandingIDtr" + currentTr);
        var landingNameField = $get("txtLandingtr" + currentTr);
        var landingIDField = $get("txtLandingPageIDtr" + currentTr);
        var landingUrlField = $get("txtLandingUrltr" + currentTr);
        var landingShareField = $get("txtSharetr" + currentTr);
        var inactiveField = $get("chkInactivetr" + currentTr);
        if (idField!=null && landingNameField!=null && landingIDField!=null && 
            landingUrlField!=null && landingShareField!=null && inactiveField!=null)
        {
            fieldsLandingArrayTr[currentTr] = {'id':idField.value, 'name':landingNameField.value, 'landingid':landingIDField.value,
                'url':landingUrlField.value, 'share':landingShareField.value, 'inactive':inactiveField.checked };
            currentTr++;
        }
        else
            continueLoopTr = 0;
	}
	fieldsArray['landingtr'] = fieldsLandingArrayTr;
	var current=0;
	var continueLoop=1;
	while (continueLoop!=0)
	{
	    var fieldsLandingArray = new Array();
	    var currentLanding = 0;
	    var continueLoopLanding =1;
	    while (continueLoopLanding!=0)
	    {
	        var idField = $get("hidLandingID" + current + "_" + currentLanding);
            var landingNameField = $get("txtLanding" + current + "_" + currentLanding);
	        var landingIDField = $get("txtLandingPageID" + current + "_" + currentLanding);
	        var landingUrlField = $get("txtLandingUrl" + current + "_" + currentLanding);
	        var landingShareField = $get("txtShare" + current + "_" + currentLanding);
	        var inactiveField = $get("chkInactive" + current + "_" + currentLanding);
            if (idField!=null && landingNameField!=null && landingIDField!=null && 
                landingUrlField!=null && landingShareField!=null && inactiveField!=null)
	        {
	            fieldsLandingArray[currentLanding] = {'id':idField.value, 'name':landingNameField.value, 'landingid':landingIDField.value,
	                'url':landingUrlField.value, 'share':landingShareField.value, 'inactive':inactiveField.checked };
	            currentLanding++;
	        }
	        else
	            continueLoopLanding = 0;
	    }
	    if (currentLanding>0)
        {
        	fieldsArray["landing"][current] = fieldsLandingArray;
		    current++;
		}
		else
			continueLoop = 0;
	}
}

function saveFieldsEmail()
{
	fieldsArray = new Array();
	var current=0;
	var continueLoop=1;
	while (continueLoop!=0)
	{
	    var fieldsLandingArray = new Array();
	    var currentLanding = 0;
	    var continueLoopLanding =1;
	    while (continueLoopLanding!=0)
	    {
	        var idField = $get("hidLandingID" + current + "_" + currentLanding);
            var landingNameField = $get("txtLanding" + current + "_" + currentLanding);
	        var landingIDField = $get("txtLandingPageID" + current + "_" + currentLanding);
	        var landingUrlField = $get("txtLandingUrl" + current + "_" + currentLanding);
	        var landingShareField = $get("txtShare" + current + "_" + currentLanding);
	        var inactiveField = $get("chkInactive" + current + "_" + currentLanding);
            if (idField!=null && landingNameField!=null && landingIDField!=null && 
                landingUrlField!=null && landingShareField!=null && inactiveField!=null)
	        {
	            fieldsLandingArray[currentLanding] = {'id':idField.value, 'name':landingNameField.value, 'landingid':landingIDField.value,
	                'url':landingUrlField.value, 'share':landingShareField.value, 'inactive':inactiveField.checked };
	            currentLanding++;
	        }
	        else
	            continueLoopLanding = 0;
	    }
	    var fieldsOfferArray = new Array();
	    var currentOffer = 0;
	    var continueLoopOffer = 1;
	    while (continueLoopOffer)
	    {
		    var idField = $get("hidOfferID" + current + "_" + currentOffer);
		    var offerNameField = $get("txtCampaignOffer" + current + "_" + currentOffer);
		    var offerUrlField = $get("txtCampaignOfferURL" + current + "_" + currentOffer);
		    var offerPayoutField = $get("txtPayout" + current + "_" + currentOffer);
		    var offerShareField = $get("txtOfferShare" + current + "_" + currentOffer);
		    var networkField = $get("ddlNetwork" + current + "_" + currentOffer);
		    var inactiveOffField = $get("chkInactiveOff" + current + "_" + currentOffer);
		    if (idField!=null && offerNameField!=null && offerUrlField!=null && offerPayoutField!=null &&
		        offerShareField!=null && networkField!=null && inactiveOffField!=null)
		    {
		        var offerSentField = $get("txtSent" + current);
		        networkValue = networkField.options[networkField.selectedIndex].value;
			    fieldsOfferArray[currentOffer] = {'id':idField.value, 'offer':offerNameField.value, 
			        'url':offerUrlField.value, 'payout': offerPayoutField.value, 'share':offerShareField.value, 
			        'network':networkValue, 'sent' : offerSentField.value, 'inactive':inactiveOffField.checked};
	            currentOffer++;
	        }
	        else
	            continueLoopOffer = 0;
	    }

	    if (currentOffer>0 && currentLanding>0)
        {
        	fieldsArray[current] = {'offer':fieldsOfferArray, 'landing':fieldsLandingArray};
		    current++;
		}
		else
			continueLoop = 0;
	}
}

function saveFieldsVs()
{
	fieldsArray = new Array();
    var fieldsOfferArray = new Array();
    var currentOffer = 0;
    var continueLoopOffer=1;
    while(continueLoopOffer!=0)
    {
        var idField = $get("hidOfferID" + currentOffer);
		var offerNameField = $get("txtCampaignOffer" + currentOffer);
        var offerUrlField = $get("txtCampaignOfferURL" + currentOffer);
        var offerPayoutField = $get("txtPayout" + currentOffer);
        var offerShareField = $get("txtOfferShare" + currentOffer);
        var networkField = $get("ddlNetwork" + currentOffer);
        var inactiveOffField = $get("chkInactiveOff" + currentOffer);
		if (idField!=null && offerNameField!=null && offerUrlField!=null && offerPayoutField!=null &&
		    offerShareField!=null && networkField!=null && inactiveOffField!=null)
        {
            networkValue = networkField.options[networkField.selectedIndex].value;
	        fieldsOfferArray[currentOffer] = {'id':idField.value, 'offer':offerNameField.value, 'url':offerUrlField.value, 
	            'payout': offerPayoutField.value, 'share':offerShareField.value, 'network':networkValue, 'inactive':inactiveOffField.checked};
            currentOffer++;
        }
        else
            continueLoopOffer = 0;
    }
    var fieldsLandingArray = new Array();
    var currentLanding = 0;
    var continueLoopLanding =1;
    while (continueLoopLanding!=0)
    {
        var idField = $get("hidLandingID" + currentLanding);
        var landingNameField = $get("txtLanding" + currentLanding);
        var landingIDField = $get("txtLandingPageID" + currentLanding);
        var landingUrlField = $get("txtLandingUrl" + currentLanding);
        var landingShareField = $get("txtShare" + currentLanding);
        var inactiveField = $get("chkInactive" + currentLanding);
        if (idField!=null && landingNameField!=null && landingIDField!=null && 
            landingUrlField!=null && landingShareField!=null && inactiveField!=null)
        {
            fieldsLandingArray[currentLanding] = {'id':idField.value, 'name':landingNameField.value, 'landingid':landingIDField.value,
                'url':landingUrlField.value, 'share':landingShareField.value, 'inactive':inactiveField.checked };
            currentLanding++;
        }
        else
            continueLoopLanding = 0;
    }
    fieldsArray = { 'landing':fieldsLandingArray, 'offer':fieldsOfferArray};
}

function saveFieldsLead()
{
	fieldsArray = new Array();
    var fieldsOfferArray = new Array();
    var currentOffer = 0;
    var continueLoopOffer=1;
    while(continueLoopOffer!=0)
    {
        var idField = $get("hidOfferID" + currentOffer);
        var offerNameField = $get("txtCampaignOffer" + currentOffer);
        var offerUrlField = $get("txtCampaignOfferURL" + currentOffer);
        var offerPayoutField = $get("txtPayout" + currentOffer);
        var offerShareField = $get("txtOfferShare" + currentOffer);
        var networkField = $get("ddlNetwork" + currentOffer);
        var inactiveOffField = $get("chkInactiveOff" + currentOffer);
        if (idField!=null && offerNameField!=null && offerUrlField!=null && offerPayoutField!=null &&
            offerShareField!=null && networkField!=null && inactiveOffField!=null)
        {
            networkValue = networkField.options[networkField.selectedIndex].value;
	        fieldsOfferArray[currentOffer] = {'id':idField.value, 'offer':offerNameField.value, 'url':offerUrlField.value, 
	            'payout': offerPayoutField.value, 'share':offerShareField.value, 'network':networkValue, 'inactive':inactiveOffField.checked};
            currentOffer++;
        }
        else
            continueLoopOffer = 0;
    }
    var fieldsLandingArrayTr = new Array();
	var currentTr=0;
	var continueLoopTr=1;
	while (continueLoopTr!=0)
	{
        var idField = $get("hidLandingIDtr" + currentTr);
        var landingNameField = $get("txtLandingtr" + currentTr);
        var landingIDField = $get("txtLandingPageIDtr" + currentTr);
        var landingUrlField = $get("txtLandingUrltr" + currentTr);
        var landingShareField = $get("txtSharetr" + currentTr);
        var inactiveField = $get("chkInactivetr" + currentTr);
        if (idField!=null && landingNameField!=null && landingIDField!=null && 
            landingUrlField!=null && landingShareField!=null && inactiveField!=null)
        {
            fieldsLandingArrayTr[currentTr] = {'id':idField.value, 'name':landingNameField.value, 'landingid' : landingIDField.value,
                'url':landingUrlField.value, 'share':landingShareField.value, 'inactive':inactiveField.checked };
            currentTr++;
        }
        else
            continueLoopTr = 0;
	}
	fieldsArray = { 'landing':new Array(), 'landingtr':fieldsLandingArrayTr, 'offer':fieldsOfferArray};
    var currentLanding = 0;
    var continueLoopLanding =1;
    while (continueLoopLanding!=0)
    {
        var idField = $get("hidLandingID" + currentLanding);
        var landingNameField = $get("txtLanding" + currentLanding);
        var landingIDField = $get("txtLandingPageID" + currentLanding);
        var landingUrlField = $get("txtLandingUrl" + currentLanding);
        var landingShareField = $get("txtShare" + currentLanding);
        var inactiveField = $get("chkInactive" + currentLanding);
        if (idField!=null && landingNameField!=null && landingIDField!=null && 
        landingUrlField!=null && landingShareField!=null && inactiveField!=null)
        {
            fieldsArray["landing"][currentLanding] = {'id':idField.value, 'name':landingNameField.value, 'landingid':landingIDField.value,
                'url':landingUrlField.value, 'share':landingShareField.value, 'inactive':inactiveField.checked };
            currentLanding++;
        }
        else
            continueLoopLanding = 0;
    }
}

function saveFieldsExtraTokens()
{
	fieldsArray = new Array();
	var current=1;
	var continueLoop=1;
	while (continueLoop!=0)
	{
        var nameField = $get("txtExtraTokenName" + current);
	    var urlField = $get("txtExtraTokenUrl" + current);
	    var paramField = $get("txtExtraTokenParam" + current);
	    if (nameField != null && urlField != null && paramField != null)
	    {
	        fieldsArray[current] = {'name':nameField.value, 'url':urlField.value, 'param':paramField.value };
		    current++;
	    }
		else
			continueLoop = 0;
	}
}

function addDestinationPath(incrDest, incrOffer, incrPath)
{
	saveFieldsPath();
	var hidControl = $get("hidDestinationToShow");
	var destinationToShow = hidControl.value;
	var hidControlOffer = $get("hidOffersToShow");
	var offersToShow = hidControlOffer.value;
	var hidControlPath = $get("hidPathsToShow");
	var pathsToShow = hidControlPath.value;
	destinationToShow=parseInt(destinationToShow)+parseInt(incrDest);
	offersToShow=parseInt(offersToShow)+parseInt(incrOffer);
	pathsToShow=parseInt(pathsToShow)+parseInt(incrPath);
	hidControl.value = destinationToShow;
	hidControlOffer.value = offersToShow;
	hidControlPath.value = pathsToShow;
	var containerControl = $get("divDestinations");
	var newHtml = '';
	var networksArray = new Array();
    var networksMainControl = $get("hidNetworks");
    var current=0;
    if (networksMainControl != null)
    {
        var parts = networksMainControl.value.split('*|*');
        for(i=0; i<parts.length; i++)
        {
            var parts2 = parts[i].split('|*|');
            if (parts2.length==2)
            {
                networksArray[current] = {'value':parts2[0],'text':parts2[1] };
                current++;
            }
        }
    }

    for (k=0; k<pathsToShow; k++)
    {
        sharePathValue = '0';
        if (k<fieldsArray.length)
            sharePathValue = fieldsArray[k]["sharepath"];
        newHtml += '<div style="float:left"><h3>Path ' + (k+1) + '</h3></div>';
        newHtml += '<div class="leftEnumText3" style="padding-left:100px; padding-top:15px">Share: %</div>';
        newHtml += '<div style="float: left; padding-top:10px"><input type="text" onblur="validateShare(this)" value="' + sharePathValue + '" id="txtSharePath' + k + '" name="txtSharePath' + k + '" class="inputs2"></div>';
        newHtml += '<div class="evenDiv" style="padding-top:12px;"><a href="javascript:;" onclick="evenSharesLandingVs(\'' + k + '_\')">';
	    newHtml += '<img src="images/even_share_button.png" border="0" alt="Even Shares"/></a></div>';
	    newHtml += '<div style="clear:both">&nbsp;</div>';

        for (i=0; i<destinationToShow; i++)
        {
	        var idValue = '0';
            var nameValue = '';
	        var landingIdValue = '0';
	        var urlValue = '';
	        var shareValue = '0';
	        var inactiveValue = false;
            if (k<fieldsArray.length && i<fieldsArray[k]["landing"].length)
	        {
		        idValue = fieldsArray[k]["landing"][i]["id"];
	            nameValue = htmlEntities(fieldsArray[k]["landing"][i]["name"]);
		        landingIdValue = fieldsArray[k]["landing"][i]["landingid"];
		        urlValue = htmlEntities(fieldsArray[k]["landing"][i]["url"]);
		        shareValue = fieldsArray[k]["landing"][i]["share"];
	            inactiveValue = fieldsArray[k]["landing"][i]["inactive"];
            }
	        newHtml += '<div class="leftEnumText2">LP Name:<input type="hidden" name="hidLandingID' + k + '_' + i + '" id="hidLandingID' + k + '_' + i + '"value="' + idValue + '" /></div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLanding' +
		        k + '_' + i + '" id="txtLanding' + k + '_' + i + '" value="' + nameValue + '"/></div>';
	        newHtml += '<div class="leftEnumText3">ID:</div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtLandingPageID' + 
		        k + '_' + i + '" id="txtLandingPageID' + k + '_' + i + '" value="' + idValue + '" onblur="validateLandingID(this)"/></div>';
	        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Url:</div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingUrl' +
		        k + '_' + i + '" id="txtLandingUrl' + k + '_' + i + '" value="' + urlValue + '"/></div>';
	        newHtml += '<div class="leftEnumText3">Share: %</div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtShare' + 
		        k + '_' + i + '" id="txtShare' + k + '_' + i + '" value="' + shareValue + '" onblur="validateShare(this)"/></div>';
		    newHtml += '<div style="clear:both"></div><div class="leftEnumText2">&nbsp;</div><div class="leftEnumTextLong">&nbsp;</div><div class="leftEnumTextShort">';
            newHtml += '<input type="checkbox" name="chkInactive' + k + '_' + i + '" id="chkInactive' + k + '_' + i + '" onclick="clickInactiveLP(this.checked,\'' + k + '_' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
	        newHtml += ' <label for="chkInactive' + k + '_' + i + '">Inactive</label></div>';
	        newHtml += '<div style="clear:both">&nbsp;</div>';
		}
		newHtml += '<div class="evenDiv">';
		newHtml += '<a href="javascript:;" onclick="evenSharesOfferVs(\'' + k + '_\')">';
		newHtml += '<img src="images/even_share_button.png" border="0" alt="Even Shares"/>';
		newHtml += '</a></div><div style="clear:both"></div>';

		for (i=0; i<offersToShow; i++)
        {
            var idValue = '';
            var offerName = '';
            var offerUrl = '';
            var payout = '0';
            var share = '0';
            var networkValue = '';
            var inactiveValue = false;
            if (k<fieldsArray.length && i<fieldsArray[k]["offer"].length)
            {
                idValue = fieldsArray[k]["offer"][i]["id"];
                offerName = htmlEntities(fieldsArray[k]["offer"][i]["offer"]);
                offerUrl = htmlEntities(fieldsArray[k]["offer"][i]["url"]);
                payout = fieldsArray[k]["offer"][i]["payout"];
                share = fieldsArray[k]["offer"][i]["share"];
                networkValue = fieldsArray[k]["offer"][i]["network"];
                inactiveValue = fieldsArray[k]["offer"][i]["inactive"];
            }

            newHtml += '<div class="leftEnumText2">Offer:<input type="hidden" name="hidOfferID' + k + '_' + i + '" id="hidOfferID' + k + '_' + i + '" value="' + idValue + '"/></div>';
            newHtml += '<div style="float:left"><input name="txtCampaignOffer' +
                k + '_' + i + '" id="txtCampaignOffer' + k + '_' + i + '" type="text" class="inputsLong" value="' + offerName + '"  /></div>';
            newHtml += '<div class="leftEnumText3">Payout: $</div>';
            newHtml += '<div style="float:left"><input name="txtPayout' +
                k + '_' + i + '" id="txtPayout' + k + '_' + i + '" type="text" class="inputs2" value="' + payout + '" onblur="validatePayout(this)" /></div>';
            newHtml += '<div style="clear:both"></div><div class="leftEnumText2">URL:</div>';
            newHtml += '<div style="float:left"><input name="txtCampaignOfferURL' +
                k + '_' + i + '" id="txtCampaignOfferURL' + k + '_' + i + '" type="text" class="inputsLong" value="' + offerUrl + '"  /></div>';
            newHtml += '<div class="leftEnumText3">Share: %</div>';
            newHtml += '<div style="float:left"><input name="txtOfferShare' +
                k + '_' + i + '" id="txtOfferShare' + k + '_' + i + '" type="text" class="inputs2" value="' + share + '" onblur="validateShare(this)" /></div>';
            newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Network:</div>';
            newHtml += '<div class="leftEnumTextLong"><select name="ddlNetwork' + k + '_' + i + '" id="ddlNetwork' + k + '_' + i + '">';
            for(j=0; j<networksArray.length; j++)
            {
                var selectedText = '';
                if (networksArray[j]["value"] == networkValue)
                    selectedText = 'selected="selected"';
                newHtml += '<option value="' + networksArray[j]["value"] + '" ' + selectedText + '>' +
                    networksArray[j]["text"] + '</option>';
            }
            newHtml += '</select></div>';
            newHtml += '<div class="leftEnumTextShort"><input type="checkbox" name="chkInactiveOff' + k + '_' + i + '" id="chkInactiveOff' + k + '_' + i + '" onclick="clickInactiveOffer(this.checked,\'' + k + '_' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
            newHtml += ' <label for="chkInactiveOff' + k + '_' + i + '">Inactive</label></div>';
            newHtml += '<div style="clear:both">&nbsp;</div>';
		}
	}
	containerControl.innerHTML = newHtml;
    for (j=0; j<pathsToShow; j++)
    {
	    clickInactiveLPAll(destinationToShow,j + '_');
    	clickInactiveOfferAll(offersToShow,j + '_');
    }
}

function addDestinationCta(incrDest, incrOffer, incrPath)
{
	saveFieldsCta();
	var hidControl = $get("hidDestinationToShow");
	var destinationToShow = hidControl.value;
	var hidControlOffer = $get("hidOffersToShow");
	var offersToShow = hidControlOffer.value;
	var hidControlPath = $get("hidPathsToShow");
	var pathsToShow = hidControlPath.value;
	destinationToShow=parseInt(destinationToShow)+parseInt(incrDest);
	offersToShow=parseInt(offersToShow)+parseInt(incrOffer);
	pathsToShow=parseInt(pathsToShow)+parseInt(incrPath);
	hidControl.value = destinationToShow;
	hidControlOffer.value = offersToShow;
	hidControlPath.value = pathsToShow;
	var containerControl = $get("divDestinations");
	var newHtml = '';
	var networksArray = new Array();
    var networksMainControl = $get("hidNetworks");
    var current=0;
    if (networksMainControl != null)
    {
        var parts = networksMainControl.value.split('*|*');
        for(i=0; i<parts.length; i++)
        {
            var parts2 = parts[i].split('|*|');
            if (parts2.length==2)
            {
                networksArray[current] = {'value':parts2[0],'text':parts2[1] };
                current++;
            }
        }
    }

    for (i=0; i<destinationToShow; i++)
    {
        var idValue = '0';
        var nameValue = '';
        var landingIdValue = '0';
        var urlValue = '';
        var shareValue = '0';
        var inactiveValue = false;
        if (i<fieldsArray["landing"].length)
        {
	        idValue = fieldsArray["landing"][i]["id"];
	        nameValue = htmlEntities(fieldsArray["landing"][i]["name"]);
	        landingIdValue = fieldsArray["landing"][i]["landingid"];
	        urlValue = htmlEntities(fieldsArray["landing"][i]["url"]);
	        shareValue = fieldsArray["landing"][i]["share"];
	        inactiveValue = fieldsArray["landing"][i]["inactive"];
        }
        newHtml += '<div class="leftEnumText2">LP Name:<input type="hidden" name="hidLandingID' + i + '" id="hidLandingID' + i + '"value="' + idValue + '" /></div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLanding' +
	        i + '" id="txtLanding' + i + '" value="' + nameValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">ID:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtLandingPageID' + 
	        i + '" id="txtLandingPageID' + i + '" value="' + landingIdValue + '" onblur="validateLandingID(this)"/></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Url:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingUrl' +
	        i + '" id="txtLandingUrl' + i + '" value="' + urlValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">Share: %</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtShare' + 
	        i + '" id="txtShare' + i + '" value="' + shareValue + '" onblur="validateShare(this)"/></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">&nbsp;</div><div class="leftEnumTextLong">&nbsp;</div><div class="leftEnumTextShort">';
        newHtml += '<input type="checkbox" name="chkInactive' + i + '" id="chkInactive' + i + '" onclick="clickInactiveLP(this.checked,' + i + ')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
	    newHtml += ' <label for="chkInactive' + i + '">Inactive</label></div>';
	    newHtml += '<div style="clear:both">&nbsp;</div>';
	}
	
    for (k=0; k<pathsToShow; k++)
    {
        newHtml += '<div style="float:left"><h3>Option ' + (k+1) + '</h3></div>';
        newHtml += '<div class="evenDiv" style="padding-top:12px;"><a href="javascript:;" onclick="evenSharesOfferVs(\'' + k + '_\')">';
        newHtml += '<img src="images/even_share_button.png" border="0" alt="Even Shares"/></a></div>';
        newHtml += '<div style="clear:both">&nbsp;</div>';
		for (i=0; i<offersToShow; i++)
        {
            var idValue = '';
            var offerName = '';
            var offerUrl = '';
            var payout = '0';
            var share = '0';
            var networkValue = '1';
            var inactiveValue = false;
            if (k<fieldsArray["offer"].length && i<fieldsArray["offer"][k].length)
            {
                idValue = fieldsArray["offer"][k][i]["id"];
                offerName = htmlEntities(fieldsArray["offer"][k][i]["offer"]);
                offerUrl = htmlEntities(fieldsArray["offer"][k][i]["url"]);
                payout = fieldsArray["offer"][k][i]["payout"];
                share = fieldsArray["offer"][k][i]["share"];
                networkValue = fieldsArray["offer"][k][i]["network"];
                inactiveValue = fieldsArray["offer"][k][i]["inactive"];
            }

            newHtml += '<div class="leftEnumText2">Offer:<input type="hidden" name="hidOfferID' + k + '_' + i + '" id="hidOfferID' + k + '_' + i + '" value="' + idValue + '"/></div>';
            newHtml += '<div style="float:left"><input name="txtCampaignOffer' +
                k + '_' + i + '" id="txtCampaignOffer' + k + '_' + i + '" type="text" class="inputsLong" value="' + offerName + '"  /></div>';
            newHtml += '<div class="leftEnumText3">Payout: $</div>';
            newHtml += '<div style="float:left"><input name="txtPayout' +
                k + '_' + i + '" id="txtPayout' + k + '_' + i + '" type="text" class="inputs2" value="' + payout + '" onblur="validatePayout(this)" /></div>';
            newHtml += '<div style="clear:both"></div><div class="leftEnumText2">URL:</div>';
            newHtml += '<div style="float:left"><input name="txtCampaignOfferURL' +
                k + '_' + i + '" id="txtCampaignOfferURL' + k + '_' + i + '" type="text" class="inputsLong" value="' + offerUrl + '"  /></div>';
            newHtml += '<div class="leftEnumText3">Share: %</div>';
            newHtml += '<div style="float:left"><input name="txtOfferShare' +
                k + '_' + i + '" id="txtOfferShare' + k + '_' + i + '" type="text" class="inputs2" value="' + share + '" onblur="validateShare(this)" /></div>';
            newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Network:</div>';
            newHtml += '<div class="leftEnumTextLong"><select name="ddlNetwork' + k + '_' + i + '" id="ddlNetwork' + k + '_' + i + '">';
            for(j=0; j<networksArray.length; j++)
            {
                var selectedText = '';
                if (networksArray[j]["value"] == networkValue)
                    selectedText = 'selected="selected"';
                newHtml += '<option value="' + networksArray[j]["value"] + '" ' + selectedText + '>' +
                    networksArray[j]["text"] + '</option>';
            }
            newHtml += '</select></div>';
            newHtml += '<div class="leftEnumTextShort"><input type="checkbox" name="chkInactiveOff' + k + '_' + i + '" id="chkInactiveOff' + k + '_' + i + '" onclick="clickInactiveOffer(this.checked,\'' + k + '_' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
            newHtml += ' <label for="chkInactiveOff' + k + '_' + i + '">Inactive</label></div>';
            newHtml += '<div style="clear:both">&nbsp;</div>';
		}
	}
	containerControl.innerHTML = newHtml;
	clickInactiveLPAll(destinationToShow,'');
    for (j=0; j<pathsToShow; j++)
    	clickInactiveOfferAll(offersToShow,j + '_');

}

function addDestinationSequence(incrDest, incrOffer, incrPath, incrOption)
{
	saveFieldsSequence();
	var hidControl = $get("hidDestinationToShow");
	var destinationToShow = hidControl.value;
	var hidControlOffer = $get("hidOffersToShow");
	var offersToShow = hidControlOffer.value;
	var hidControlPath = $get("hidPathsToShow");
	var pathsToShow = hidControlPath.value;
	var hidControlOption = $get("hidOptionsToShow");
	var optionsToShow = hidControlOption.value;
	destinationToShow=parseInt(destinationToShow)+parseInt(incrDest);
	offersToShow=parseInt(offersToShow)+parseInt(incrOffer);
	pathsToShow=parseInt(pathsToShow)+parseInt(incrPath);
	optionsToShow=parseInt(optionsToShow)+parseInt(incrOption);
	hidControl.value = destinationToShow;
	hidControlOffer.value = offersToShow;
	hidControlPath.value = pathsToShow;
	hidControlOption.value = optionsToShow;
	hidControl = $get("hidDestinationIndex");
	var destinationIndex = hidControl.value;
	hidControl = $get("hidTrackingIndex");
	var trackingIndex = hidControl.value;
	var containerControl = $get("divDestinations");
	var newHtml = '';
	var networksArray = new Array();
    var networksMainControl = $get("hidNetworks");
    var current=0;
    if (networksMainControl != null)
    {
        var parts = networksMainControl.value.split('*|*');
        for(i=0; i<parts.length; i++)
        {
            var parts2 = parts[i].split('|*|');
            if (parts2.length==2)
            {
                networksArray[current] = {'value':parts2[0],'text':parts2[1] };
                current++;
            }
        }
    }

    for (k=0; k<pathsToShow; k++)
    {
        newHtml += '<div style="float:left"><h3>Level ' + (k+1) + '</h3></div>';
	    newHtml += '<div class="evenDiv" style="padding-top:12px;"><a href="javascript:;" onclick="evenSharesLandingVs(\'' + k + '_\')">';
	    newHtml += '<img src="images/even_share_button.png" border="0" alt="Even Shares"/></a></div>';
	    newHtml += '<div style="clear:both">&nbsp;</div>';
        for (i=0; i<destinationToShow; i++)
        {
	        var idValue = '0';
            var nameValue = '';
	        var landingIdValue = '0';
	        var urlValue = '';
	        var shareValue = '0';
	        var inactiveValue = false;
            if (k<fieldsArray["landing"].length && i<fieldsArray["landing"][k].length)
	        {
		        idValue = fieldsArray["landing"][k][i]["id"];
	            nameValue = htmlEntities(fieldsArray["landing"][k][i]["name"]);
		        landingIdValue = fieldsArray["landing"][k][i]["landingid"];
		        urlValue = htmlEntities(fieldsArray["landing"][k][i]["url"]);
		        shareValue = fieldsArray["landing"][k][i]["share"];
	            inactiveValue = fieldsArray["landing"][k][i]["inactive"];
            }
	        newHtml += '<div class="leftEnumText2">LP Name:<input type="hidden" name="hidLandingID' + k + '_' + i + '" id="hidLandingID' + k + '_' + i + '"value="' + idValue + '" /></div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLanding' +
		        k + '_' + i + '" id="txtLanding' + k + '_' + i + '" value="' + nameValue + '"/></div>';
	        newHtml += '<div class="leftEnumText3">ID:</div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtLandingPageID' + 
		        k + '_' + i + '" id="txtLandingPageID' + k + '_' + i + '" value="' + landingIdValue + '" onblur="validateLandingID(this)"/></div>';
	        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Url:</div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingUrl' +
		        k + '_' + i + '" id="txtLandingUrl' + k + '_' + i + '" value="' + urlValue + '"/></div>';
	        newHtml += '<div class="leftEnumText3">Share: %</div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtShare' + 
		        k + '_' + i + '" id="txtShare' + k + '_' + i + '" value="' + shareValue + '" onblur="validateShare(this)"/></div>';
		    newHtml += '<div style="clear:both"></div><div class="leftEnumText2">&nbsp;</div><div class="leftEnumTextLong">&nbsp;</div><div class="leftEnumTextShort">';
            newHtml += '<input type="checkbox" name="chkInactive' + k + '_' + i + '" id="chkInactive' + k + '_' + i + '" onclick="clickInactiveLP(this.checked,\'' + k + '_' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
	        newHtml += ' <label for="chkInactive' + k + '_' + i + '">Inactive</label></div>';
	        newHtml += '<div style="clear:both">&nbsp;</div>';
		}
	}
	newHtml+='<div style="display:' + (destinationIndex==2 || trackingIndex!=1 ? "none" : "block") + ';" id="divOptIn1">';
	newHtml+='<div style="float:left"><h3>After Opt-In</h3></div>';
    newHtml+='<div class="evenDiv" style="padding-top:12px;"><a href="javascript:;" onclick="evenSharesLandingVs(\'tr\')">';
    newHtml+='<img src="images/even_share_button.png" border="0" alt="Even Shares"/></a></div>';
    newHtml+='<div style="clear:both">&nbsp;</div>';
    for (i=0; i<destinationToShow; i++)
    {
        var idValue = '0';
        var nameValue = '';
        var landingIdValue = '0';
        var urlValue = '';
        var shareValue = '0';
        var inactiveValue = false;
        if (i<fieldsArray["landingtr"].length)
        {
	        idValue = fieldsArray["landingtr"][i]["id"];
	        nameValue = htmlEntities(fieldsArray["landingtr"][i]["name"]);
	        landingIdValue = fieldsArray["landingtr"][i]["landingid"];
	        urlValue = htmlEntities(fieldsArray["landingtr"][i]["url"]);
	        shareValue = fieldsArray["landingtr"][i]["share"];
            inactiveValue = fieldsArray["landingtr"][i]["inactive"];
        }
        newHtml += '<div class="leftEnumText2">LP Name:<input type="hidden" name="hidLandingIDtr' + i + '" id="hidLandingIDtr' + i + '"value="' + idValue + '" /></div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingtr' +
	        i + '" id="txtLandingtr' + i + '" value="' + nameValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">ID:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtLandingPageIDtr' + 
	        i + '" id="txtLandingPageIDtr' + i + '" value="' + landingIdValue + '" onblur="validateLandingID(this)"/></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Url:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingUrltr' +
	        i + '" id="txtLandingUrltr' + i + '" value="' + urlValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">Share: %</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtSharetr' + 
	        i + '" id="txtSharetr' + i + '" value="' + shareValue + '" onblur="validateShare(this)"/></div>';
	    newHtml += '<div style="clear:both"></div><div class="leftEnumText2">&nbsp;</div><div class="leftEnumTextLong">&nbsp;</div><div class="leftEnumTextShort">';
        newHtml += '<input type="checkbox" name="chkInactivetr' + i + '" id="chkInactivetr' + i + '" onclick="clickInactiveLP(this.checked,\'' + 'tr' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
	    newHtml += ' <label for="chkInactivetr' + i + '">Inactive</label></div>';
	    newHtml += '<div style="clear:both">&nbsp;</div>';
	}
	newHtml+='</div><div style="display:' + (destinationIndex==4 && trackingIndex==3 ? "none" : "block") + ';" id="divOptIn2" style="display: block;"><br/><br/>';
	for (k=0; k<optionsToShow; k++)
    {
        newHtml += '<div style="float:left"><h3>Option ' + (k+1) + '</h3></div>';
        newHtml+='<div class="evenDiv" style="padding-top:12px;"><a href="javascript:;" onclick="evenSharesOfferVs(\'' + k + '_\')">';
        newHtml+='<img src="images/even_share_button.png" border="0" alt="Even Shares"/>';
        newHtml+='</a></div><div style="clear:both">&nbsp;</div>';
	    for (i=0; i<offersToShow; i++)
        {
            var idValue = '';
            var offerName = '';
            var offerUrl = '';
            var payout = '0';
            var share = '0';
            var networkValue = '1';
            var inactiveValue = false;
            if (k<fieldsArray["offer"].length && i<fieldsArray["offer"][k].length)
            {
                idValue = fieldsArray["offer"][k][i]["id"];
                offerName = htmlEntities(fieldsArray["offer"][k][i]["offer"]);
                offerUrl = htmlEntities(fieldsArray["offer"][k][i]["url"]);
                payout = fieldsArray["offer"][k][i]["payout"];
                share = fieldsArray["offer"][k][i]["share"];
                networkValue = fieldsArray["offer"][k][i]["network"];
                inactiveValue = fieldsArray["offer"][k][i]["inactive"];
            }

            newHtml += '<div class="leftEnumText2">Offer:<input type="hidden" name="hidOfferID' + k + '_' + i + '" id="hidOfferID' + k + '_' + i + '" value="' + idValue + '"/></div>';
            newHtml += '<div style="float:left"><input name="txtCampaignOffer' +
                k + '_' + i + '" id="txtCampaignOffer' + k + '_' + i + '" type="text" class="inputsLong" value="' + offerName + '"  /></div>';
            newHtml += '<div class="leftEnumText3">Payout: $</div>';
            newHtml += '<div style="float:left"><input name="txtPayout' +
                k + '_' + i + '" id="txtPayout' + k + '_' + i + '" type="text" class="inputs2" value="' + payout + '" onblur="validatePayout(this)" /></div>';
            newHtml += '<div style="clear:both"></div><div class="leftEnumText2">URL:</div>';
            newHtml += '<div style="float:left"><input name="txtCampaignOfferURL' +
                k + '_' + i + '" id="txtCampaignOfferURL' + k + '_' + i + '" type="text" class="inputsLong" value="' + offerUrl + '"  /></div>';
            newHtml += '<div class="leftEnumText3">Share: %</div>';
            newHtml += '<div style="float:left"><input name="txtOfferShare' +
                k + '_' + i + '" id="txtOfferShare' + k + '_' + i + '" type="text" class="inputs2" value="' + share + '" onblur="validateShare(this)" /></div>';
            newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Network:</div>';
            newHtml += '<div class="leftEnumTextLong"><select name="ddlNetwork' + k + '_' + i + '" id="ddlNetwork' + k + '_' + i + '">';
            for(j=0; j<networksArray.length; j++)
            {
                var selectedText = '';
                if (networksArray[j]["value"] == networkValue)
                    selectedText = 'selected="selected"';
                newHtml += '<option value="' + networksArray[j]["value"] + '" ' + selectedText + '>' +
                    networksArray[j]["text"] + '</option>';
            }
            newHtml += '</select></div>';
            newHtml += '<div class="leftEnumTextShort"><input type="checkbox" name="chkInactiveOff' + k + '_' + i + '" id="chkInactiveOff' + k + '_' + i + '" onclick="clickInactiveOffer(this.checked,\'' + k + '_' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
            newHtml += ' <label for="chkInactiveOff' + k + '_' + i + '">Inactive</label></div>';
            newHtml += '<div style="clear:both">&nbsp;</div>';
	    }
	}
	newHtml += '</div><div style="display: ' + (destinationIndex==2 || trackingIndex!=3 ? "none" : "block") + ';" id="divOptIn3">';
	newHtml += '</div>';
	containerControl.innerHTML = newHtml;
    for (j=0; j<pathsToShow; j++)
	    clickInactiveLPAll(destinationToShow,j + '_');
	clickInactiveLPAll(destinationToShow,'tr');
    for (j=0; j<optionsToShow; j++)
        clickInactiveOfferAll(offersToShow,j + '_');
}

function addDestinationEmail(incrDest, incrOffer, incrPath)
{
	saveFieldsEmail();
	var hidControl = $get("hidDestinationToShow");
	var destinationToShow = hidControl.value;
	var hidControlOffer = $get("hidOffersToShow");
	var offersToShow = hidControlOffer.value;
	var hidControlPath = $get("hidPathsToShow");
	var pathsToShow = hidControlPath.value;
	destinationToShow=parseInt(destinationToShow)+parseInt(incrDest);
	offersToShow=parseInt(offersToShow)+parseInt(incrOffer);
	pathsToShow=parseInt(pathsToShow)+parseInt(incrPath);
	hidControl.value = destinationToShow;
	hidControlOffer.value = offersToShow;
	hidControlPath.value = pathsToShow;
	var hidControlType = $get("hidDestinationIndex");
	var destinationIndex = hidControlType.value;
	
	var containerControl = $get("divDestinations");
	var newHtml = '';
	var networksArray = new Array();
    var networksMainControl = $get("hidNetworks");
    var current=0;
    if (networksMainControl != null)
    {
        var parts = networksMainControl.value.split('*|*');
        for(i=0; i<parts.length; i++)
        {
            var parts2 = parts[i].split('|*|');
            if (parts2.length==2)
            {
                networksArray[current] = {'value':parts2[0],'text':parts2[1] };
                current++;
            }
        }
    }

    for (k=0; k<pathsToShow; k++)
    {
        var currentPath = k+1;
        var currentSent = 0;
        if (k < fieldsArray.length)
            currentSent = fieldsArray[k]["offer"][0]["sent"];
		newHtml += '<div class="leftEnumText">No. of Emails Sent:</div><div class="rightEnumText"><input type="text" name="txtSent' + k + '" id="txtSent' + k + '" value="' + currentSent + '" class="inputsShort" style="text-align: center;"/></div>';
    	newHtml += '<div class="leftEnumTextShort">Email #:</div><div style="float:left"><input type="text" value="' + currentPath + '" class="inputsShort" style="text-align: center;" readonly="readonly"/></div>';
    	newHtml += '<div style="clear:both">&nbsp;</div>';
        
        newHtml += '<div id="divLandingPages' + k + '" ' + (destinationIndex!=1 ? ' style="display:none"' : '') + '>'
	    newHtml += '<div class="evenDiv"><a href="javascript:;" onclick="evenSharesLandingVs(\'' + k + '_\')">';
	    newHtml += '<img src="images/even_share_button.png" border="0" alt="Even Shares"/></a></div>';
	    newHtml += '<div style="clear:both"></div>';
        for (i=0; i<destinationToShow; i++)
        {
            var idValue = '0';
	        var nameValue = '';
	        var landingIdValue = '0';
	        var urlValue = '';
	        var shareValue = '0';
	        var inactiveValue = false;
            if (k<fieldsArray.length && i<fieldsArray[k]["landing"].length)
	        {
		        idValue = fieldsArray[k]["landing"][i]["id"];
	            nameValue = htmlEntities(fieldsArray[k]["landing"][i]["name"]);
		        landingIdValue = fieldsArray[k]["landing"][i]["landingid"];
		        urlValue = htmlEntities(fieldsArray[k]["landing"][i]["url"]);
		        shareValue = fieldsArray[k]["landing"][i]["share"];
	            inactiveValue = fieldsArray[k]["landing"][i]["inactive"];
            }
	        newHtml += '<div class="leftEnumText2">LP Name:<input type="hidden" name="hidLandingID' + k + '_' + i + '" id="hidLandingID' + k + '_' + i + '"value="' + idValue + '" /></div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLanding' +
		        k + '_' + i + '" id="txtLanding' + k + '_' + i + '" value="' + nameValue + '"/></div>';
	        newHtml += '<div class="leftEnumText3">ID:</div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtLandingPageID' + 
		        k + '_' + i + '" id="txtLandingPageID' + k + '_' + i + '" value="' + landingIdValue + '" onblur="validateLandingID(this)"/></div>';
	        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Url:</div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingUrl' +
		        k + '_' + i + '" id="txtLandingUrl' + k + '_' + i + '" value="' + urlValue + '"/></div>';
	        newHtml += '<div class="leftEnumText3">Share: %</div>';
	        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtShare' + 
		        k + '_' + i + '" id="txtShare' + k + '_' + i + '" value="' + shareValue + '" onblur="validateShare(this)"/></div>';
		    newHtml += '<div style="clear:both"></div><div class="leftEnumText2">&nbsp;</div><div class="leftEnumTextLong">&nbsp;</div><div class="leftEnumTextShort">';
            newHtml += '<input type="checkbox" name="chkInactive' + k + '_' + i + '" id="chkInactive' + k + '_' + i + '" onclick="clickInactiveLP(this.checked,\'' + k + '_' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
	        newHtml += ' <label for="chkInactive' + k + '_' + i + '">Inactive</label></div>';
		    newHtml += '<div style="clear:both">&nbsp;</div>';
	    }
		newHtml += '</div>';
		
		newHtml += '<div id="divOffers">';
	    newHtml += '<div class="evenDiv">';
	    newHtml += '<a href="javascript:;" onclick="evenSharesOfferVs(\'' + k + '_\')">';
	    newHtml += '<img src="images/even_share_button.png" border="0" alt="Even Shares"/>';
	    newHtml += '</a></div><div style="clear:both"></div>';
    	for (i=0; i<offersToShow; i++)
        {
            var idValue = '';
            var offerName = '';
            var offerUrl = '';
            var payout = '0';
            var share = '0';
            var networkValue = '';
            var inactiveValue = false;
            if (k<fieldsArray.length && i<fieldsArray[k]["offer"].length)
            {
                idValue = fieldsArray[k]["offer"][i]["id"];
                offerName = htmlEntities(fieldsArray[k]["offer"][i]["offer"]);
                offerUrl = htmlEntities(fieldsArray[k]["offer"][i]["url"]);
                payout = fieldsArray[k]["offer"][i]["payout"];
                share = fieldsArray[k]["offer"][i]["share"];
                networkValue = fieldsArray[k]["offer"][i]["network"];
                inactiveValue = fieldsArray[k]["offer"][i]["inactive"];
            }

            newHtml += '<div class="leftEnumText2">Offer:<input type="hidden" name="hidOfferID' + k + '_' + i + '" id="hidOfferID' + k + '_' + i + '" value="' + idValue + '"/></div>';
            newHtml += '<div style="float:left"><input name="txtCampaignOffer' +
                k + '_' + i + '" id="txtCampaignOffer' + k + '_' + i + '" type="text" class="inputsLong" value="' + offerName + '"  /></div>';
            newHtml += '<div class="leftEnumText3">Payout: $</div>';
            newHtml += '<div style="float:left"><input name="txtPayout' +
                k + '_' + i + '" id="txtPayout' + k + '_' + i + '" type="text" class="inputs2" value="' + payout + '" onblur="validatePayout(this)" /></div>';
            newHtml += '<div style="clear:both"></div><div class="leftEnumText2">URL:</div>';
            newHtml += '<div style="float:left"><input name="txtCampaignOfferURL' +
                k + '_' + i + '" id="txtCampaignOfferURL' + k + '_' + i + '" type="text" class="inputsLong" value="' + offerUrl + '"  /></div>';
            newHtml += '<div class="leftEnumText3">Share: %</div>';
            newHtml += '<div style="float:left"><input name="txtOfferShare' +
            k + '_' + i + '" id="txtOfferShare' + k + '_' + i + '" type="text" class="inputs2" value="' + share + '" onblur="validateShare(this)" /></div>';
            newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Network:</div>';
            newHtml += '<div class="leftEnumTextLong"><select name="ddlNetwork' + k + '_' + i + '" id="ddlNetwork' + k + '_' + i + '">';
            for(j=0; j<networksArray.length; j++)
            {
                var selectedText = '';
                if (networksArray[j]["value"] == networkValue)
                    selectedText = 'selected="selected"';
                newHtml += '<option value="' + networksArray[j]["value"] + '" ' + selectedText + '>' +
                    networksArray[j]["text"] + '</option>';
            }
            newHtml += '</select></div>';
            newHtml += '<div class="leftEnumTextShort"><input type="checkbox" name="chkInactiveOff' + k + '_' + i + '" id="chkInactiveOff' + k + '_' + i + '" onclick="clickInactiveOffer(this.checked,\'' + k + '_' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
            newHtml += ' <label for="chkInactiveOff' + k + '_' + i + '">Inactive</label></div>';
            newHtml += '<div style="clear:both">&nbsp;</div>';
		}
		newHtml += '</div>';
        newHtml += '<div style="clear:both">&nbsp;</div>';
	}
	containerControl.innerHTML = newHtml;
    for (j=0; j<pathsToShow; j++)
    {
	    clickInactiveLPAll(destinationToShow,j + '_');
    	clickInactiveOfferAll(offersToShow,j + '_');
    }
}

function addDestinationVs(incrDest, incrOffer)
{
	saveFieldsVs();
	var hidControlDestinations = $get("hidDestinationToShow");
	var destinationToShow = hidControlDestinations.value;
	destinationToShow=parseInt(destinationToShow)+incrDest;
	hidControlDestinations.value = destinationToShow;
	var hidControlOffer = $get("hidOffersToShow");
	var offersToShow = hidControlOffer.value;
	offersToShow=parseInt(offersToShow)+incrOffer;
	hidControlOffer.value = offersToShow;
	var hidControlIndex = $get("hidDestinationIndex");
	var destinationIndex = hidControlIndex.value;
	var containerControl = $get("divDestinations");
	var newHtml = '<div id="divLandingPages"' + (destinationIndex==2 ? 'style="display:none"' : '') + '>';
	newHtml += '<div class="evenDiv"><a href="javascript:;" onclick="evenSharesLandingVs(\'\')">';
	newHtml += '<img src="images/even_share_button.png" border="0" alt="Even Shares"/></a></div><div style="clear:both"></div>';
	var networksArray = new Array();
    var networksMainControl = $get("hidNetworks");
    var current=0;
    if (networksMainControl != null)
    {
        var parts = networksMainControl.value.split('*|*');
        for(i=0; i<parts.length; i++)
        {
            var parts2 = parts[i].split('|*|');
            if (parts2.length==2)
            {
                networksArray[current] = {'value':parts2[0],'text':parts2[1] };
                current++;
            }
        }
    }
    newHtml += '';
    for (i=0; i<destinationToShow; i++)
    {
        var idValue = '0';
        var nameValue = '';
        var landingIdValue = '0';
        var urlValue = '';
        var shareValue = '0';
        var inactiveValue = false;
        if (i<fieldsArray["landing"].length)
        {
	        idValue = fieldsArray["landing"][i]["id"];
	        nameValue = htmlEntities(fieldsArray["landing"][i]["name"]);
	        landingIdValue = fieldsArray["landing"][i]["landingid"];
	        urlValue = htmlEntities(fieldsArray["landing"][i]["url"]);
	        shareValue = fieldsArray["landing"][i]["share"];
            inactiveValue = fieldsArray["landing"][i]["inactive"];
        }
        newHtml += '<div class="leftEnumText2">LP Name:<input type="hidden" name="hidLandingID' + i + '" id="hidLandingID' + i + '"value="' + idValue + '" /></div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLanding' +
	        i + '" id="txtLanding' + i + '" value="' + nameValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">ID:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtLandingPageID' + 
	        i + '" id="txtLandingPageID' + i + '" value="' + landingIdValue + '" onblur="validateLandingID(this)"/></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Url:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingUrl' +
	        i + '" id="txtLandingUrl' + i + '" value="' + urlValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">Share: %</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtShare' + 
	        i + '" id="txtShare' + i + '" value="' + shareValue + '" onblur="validateShare(this)"/></div>';
	    newHtml += '<div style="clear:both"></div><div class="leftEnumText2">&nbsp;</div><div class="leftEnumTextLong">&nbsp;</div><div class="leftEnumTextShort">';
        newHtml += '<input type="checkbox" name="chkInactive' + i + '" id="chkInactive' + i + '" onclick="clickInactiveLP(this.checked,' + i + ')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
	    newHtml += ' <label for="chkInactive' + i + '">Inactive</label></div>';
	    newHtml += '<div style="clear:both">&nbsp;</div>';
	}
	newHtml+='<br/><br/></div><div id="divOffers"><div class="evenDiv"><a href="javascript:;" onclick="evenSharesOfferVs(\'\')">';
	newHtml+='<img src="images/even_share_button.png" border="0" alt="Even Shares"/></a></div><div style="clear:both"></div>';
	for (i=0; i<offersToShow; i++)
    {
        var idValue = '';
        var offerName = '';
        var offerUrl = '';
        var payout = '0';
        var share = '0';
        var networkValue = '1';
        var inactiveValue = false;
        if (i<fieldsArray["offer"].length)
        {
            idValue = fieldsArray["offer"][i]["id"];
            offerName = htmlEntities(fieldsArray["offer"][i]["offer"]);
            offerUrl = htmlEntities(fieldsArray["offer"][i]["url"]);
            payout = fieldsArray["offer"][i]["payout"];
            share = fieldsArray["offer"][i]["share"];
            networkValue = fieldsArray["offer"][i]["network"];
            inactiveValue = fieldsArray["offer"][i]["inactive"];
        }

        newHtml += '<div class="leftEnumText2">Offer:<input type="hidden" name="hidOfferID' + i + '" id="hidOfferID' + i + '" value="' + idValue + '"/></div>';
        newHtml += '<div style="float:left"><input name="txtCampaignOffer' +
            i + '" id="txtCampaignOffer' + i + '" type="text" class="inputsLong" value="' + offerName + '"  /></div>';
        newHtml += '<div class="leftEnumText3">Payout: $</div>';
        newHtml += '<div style="float:left"><input name="txtPayout' +
            i + '" id="txtPayout' + i + '" type="text" class="inputs2" value="' + payout + '" onblur="validatePayout(this)" /></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">URL:</div>';
        newHtml += '<div style="float:left"><input name="txtCampaignOfferURL' +
            i + '" id="txtCampaignOfferURL' + i + '" type="text" class="inputsLong" value="' + offerUrl + '"  /></div>';
        newHtml += '<div class="leftEnumText3">Share: %</div>';
        newHtml += '<div style="float:left"><input name="txtOfferShare' +
             i + '" id="txtOfferShare' + i + '" type="text" class="inputs2" value="' + share + '" onblur="validateShare(this)" /></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Network:</div>';
        newHtml += '<div class="leftEnumTextLong"><select name="ddlNetwork' + i + '" id="ddlNetwork' + i + '">';
        for(j=0; j<networksArray.length; j++)
        {
            var selectedText = '';
            if (networksArray[j]["value"] == networkValue)
                selectedText = 'selected="selected"';
            newHtml += '<option value="' + networksArray[j]["value"] + '" ' + selectedText + '>' +
                networksArray[j]["text"] + '</option>';
        }
        newHtml += '</select></div>';
        newHtml += '<div class="leftEnumTextShort"><input type="checkbox" name="chkInactiveOff' + i + '" id="chkInactiveOff' + i + '" onclick="clickInactiveOffer(this.checked,\'' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
        newHtml += ' <label for="chkInactiveOff' + i + '">Inactive</label></div>';
        newHtml += '<div style="clear:both">&nbsp;</div>';
	}
	newHtml += '</div><br/><br/>';
	containerControl.innerHTML = newHtml;
	clickInactiveLPAll(destinationToShow,'');
    clickInactiveOfferAll(offersToShow,'');

}

function addDestinationLead(incrDest, incrOffer)
{
	saveFieldsLead();
	var hidControlDestinations = $get("hidDestinationToShow");
	var destinationToShow = hidControlDestinations.value;
	var hidControlOffer = $get("hidOffersToShow");
	var offersToShow = hidControlOffer.value;
	destinationToShow=parseInt(destinationToShow)+incrDest;
	hidControlDestinations.value = destinationToShow;
	offersToShow=parseInt(offersToShow)+parseInt(incrOffer);
	hidControlOffer.value = offersToShow;
	hidControl = $get("hidTrackingIndex");
	var trackingIndex = hidControl.value;
	var containerControl = $get("divDestinations");
	var newHtml = '';
	var networksArray = new Array();
    var networksMainControl = $get("hidNetworks");
    var current=0;
    if (networksMainControl != null)
    {
        var parts = networksMainControl.value.split('*|*');
        for(i=0; i<parts.length; i++)
        {
            var parts2 = parts[i].split('|*|');
            if (parts2.length==2)
            {
                networksArray[current] = {'value':parts2[0],'text':parts2[1] };
                current++;
            }
        }
    }
    for (i=0; i<destinationToShow; i++)
    {
        var idValue = '0';
        var nameValue = '';
        var landingIdValue = '0';
        var urlValue = '';
        var shareValue = '0';
        var inactiveValue = false;
        if (i<fieldsArray["landing"].length)
        {
            idValue = fieldsArray["landing"][i]["id"];
	        nameValue = htmlEntities(fieldsArray["landing"][i]["name"]);
	        landingIdValue = fieldsArray["landing"][i]["landingid"];
	        urlValue = htmlEntities(fieldsArray["landing"][i]["url"]);
	        shareValue = fieldsArray["landing"][i]["share"];
	        inactiveValue = fieldsArray["landing"][i]["inactive"];
        }
        newHtml += '<div class="leftEnumText2">LP Name:<input type="hidden" name="hidLandingID' + i + '" id="hidLandingID' + i + '"value="' + idValue + '" /></div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLanding' +
	        i + '" id="txtLanding' + i + '" value="' + nameValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">ID:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtLandingPageID' + 
	        i + '" id="txtLandingPageID' + i + '" value="' + landingIdValue + '" onblur="validateLandingID(this)"/></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Url:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingUrl' +
	        i + '" id="txtLandingUrl' + i + '" value="' + urlValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">Share: %</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtShare' + 
	        i + '" id="txtShare' + i + '" value="' + shareValue + '" onblur="validateLandingID(this)"/></div>';
	    newHtml += '<div style="clear:both"></div><div class="leftEnumText2">&nbsp;</div><div class="leftEnumTextLong">&nbsp;</div><div class="leftEnumTextShort">';
        newHtml += '<input type="checkbox" name="chkInactive' + i + '" id="chkInactive' + i + '" onclick="clickInactiveLP(this.checked,\'' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
        newHtml += ' <label for="chkInactive' + i + '">Inactive</label></div>';
        newHtml += '<div style="clear:both">&nbsp;</div>';
	}
	newHtml+='<div style="display:' + (trackingIndex!=1 ? "none" : "block") + ';" id="divOptIn1">';
	newHtml+='<div style="float:left"><h3>After Opt-In</h3></div>';
    newHtml+='<div class="evenDiv" style="padding-top:12px;"><a href="javascript:;" onclick="evenSharesLandingVs(\'tr\')">';
    newHtml+='<img src="images/even_share_button.png" border="0" alt="Even Shares"/></a></div>';
    newHtml+='<div style="clear:both">&nbsp;</div>';
    for (i=0; i<destinationToShow; i++)
    {
        var idValue = '0';
        var nameValue = '';
        var landingIdValue = '0';
        var urlValue = '';
        var shareValue = '0';
        var inactiveValue = false;
        if (i<fieldsArray["landingtr"].length)
        {
            idValue = fieldsArray["landingtr"][i]["id"];
	        nameValue = htmlEntities(fieldsArray["landingtr"][i]["name"]);
	        landingIdValue = fieldsArray["landingtr"][i]["landingid"];
	        urlValue = htmlEntities(fieldsArray["landingtr"][i]["url"]);
	        shareValue = fieldsArray["landingtr"][i]["share"];
	        inactiveValue = fieldsArray["landingtr"][i]["inactive"];
        }
        newHtml += '<div class="leftEnumText2">LP Name:<input type="hidden" name="hidLandingIDtr' + i + '" id="hidLandingIDtr' + i + '"value="' + idValue + '" /></div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingtr' +
	        + i + '" id="txtLandingtr' + i + '" value="' + nameValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">ID:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtLandingPageIDtr' + 
	        + i + '" id="txtLandingPageIDtr' + i + '" value="' + landingIdValue + '" onblur="validateLandingID(this)"/></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Url:</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputsLong" name="txtLandingUrltr' +
	        + i + '" id="txtLandingUrltr' + i + '" value="' + urlValue + '"/></div>';
        newHtml += '<div class="leftEnumText3">Share: %</div>';
        newHtml += '<div style="float:left"><input type="text" class="inputs2" name="txtSharetr' + 
	        + i + '" id="txtSharetr' + i + '" value="' + shareValue + '" onblur="validateShare(this)"/></div>';
	    newHtml += '<div style="clear:both"></div><div class="leftEnumText2">&nbsp;</div><div class="leftEnumTextLong">&nbsp;</div><div class="leftEnumTextShort">';
        newHtml += '<input type="checkbox" name="chkInactivetr' + i + '" id="chkInactivetr' + i + '" onclick="clickInactiveLP(this.checked,\'' + 'tr' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
	    newHtml += ' <label for="chkInactivetr' + i + '">Inactive</label></div>';
	    newHtml += '<div style="clear:both">&nbsp;</div>';
	}
	newHtml+='</div><div style="display:' + (trackingIndex==4 ? "none" : "block") + ';" id="divOptIn2" style="display: block;"><br/><br/>';
		newHtml+='<div class="evenDiv">';
		newHtml+='<a href="javascript:;" onclick="evenSharesOfferVs(\'\')">';
		newHtml+='<img src="images/even_share_button.png" border="0" alt="Even Shares"/>';
		newHtml+='</a></div><div style="clear:both"></div>';
	for (i=0; i<offersToShow; i++)
    {
        var idValue = '';
        var offerName = '';
        var offerUrl = '';
        var payout = '0';
        var share = '0';
        var networkValue = '1';
        var inactiveValue = false;
        if (i<fieldsArray["offer"].length)
        {
            idValue = fieldsArray["offer"][i]["id"];
            offerName = htmlEntities(fieldsArray["offer"][i]["offer"]);
            offerUrl = htmlEntities(fieldsArray["offer"][i]["url"]);
            payout = fieldsArray["offer"][i]["payout"];
            share = fieldsArray["offer"][i]["share"];
            networkValue = fieldsArray["offer"][i]["network"];
            inactiveValue = fieldsArray["offer"][i]["inactive"];
        }

        newHtml += '<div class="leftEnumText2">Offer:<input type="hidden" name="hidOfferID' + i + '" id="hidOfferID' + i + '" value="' + idValue + '"/></div>';
        newHtml += '<div style="float:left"><input name="txtCampaignOffer' +
            i + '" id="txtCampaignOffer' + i + '" type="text" class="inputsLong" value="' + offerName + '"  /></div>';
        newHtml += '<div class="leftEnumText3">Payout: $</div>';
        newHtml += '<div style="float:left"><input name="txtPayout' +
            i + '" id="txtPayout' + i + '" type="text" class="inputs2" value="' + payout + '" onblur="validatePayout(this)" /></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">URL:</div>';
        newHtml += '<div style="float:left"><input name="txtCampaignOfferURL' +
            i + '" id="txtCampaignOfferURL' + i + '" type="text" class="inputsLong" value="' + offerUrl + '"  /></div>';
        newHtml += '<div class="leftEnumText3">Share: %</div>';
        newHtml += '<div style="float:left"><input name="txtOfferShare' +
             i + '" id="txtOfferShare' + i + '" type="text" class="inputs2" value="' + share + '" onblur="validateShare(this)"  /></div>';
        newHtml += '<div style="clear:both"></div><div class="leftEnumText2">Network:</div>';
        newHtml += '<div class="leftEnumTextLong"><select name="ddlNetwork' + i + '" id="ddlNetwork' + i + '">';
        for(j=0; j<networksArray.length; j++)
        {
            var selectedText = '';
            if (networksArray[j]["value"] == networkValue)
                selectedText = 'selected="selected"';
            newHtml += '<option value="' + networksArray[j]["value"] + '" ' + selectedText + '>' +
                networksArray[j]["text"] + '</option>';
        }
        newHtml += '</select></div>';
        newHtml += '<div class="leftEnumTextShort"><input type="checkbox" name="chkInactiveOff' + i + '" id="chkInactiveOff' + i + '" onclick="clickInactiveOffer(this.checked,\'' + i + '\')"' + (inactiveValue ? ' checked="checked"' : '') + '/>';
        newHtml += ' <label for="chkInactiveOff' + i + '">Inactive</label></div>';
        newHtml += '<div style="clear:both">&nbsp;</div>';
	}
	newHtml += '</div>';
	containerControl.innerHTML = newHtml;
	clickInactiveLPAll(destinationToShow,'');
	clickInactiveLPAll(destinationToShow,'tr');
    clickInactiveOfferAll(offersToShow,'');
}

function addExtraToken()
{
	saveFieldsExtraTokens();
	var hidControl = $get("hidExtraTokensToShow");
	var extraTokensToShow = hidControl.value;
	extraTokensToShow=parseInt(extraTokensToShow)+1;
	if (extraTokensToShow > 10)
	    extraTokensToShow = 10;
	hidControl.value = extraTokensToShow;
	var containerControl = $get("divExtraTokensContainer");
	var newHtml = '';
    for (i=1; i<=extraTokensToShow; i++)
    {
        //var arrayID = i - 1;
        nameValue = '';
        urlValue = '';
        paramValue = '';
        if (i<fieldsArray.length)
        {
            nameValue = fieldsArray[i]["name"];
            urlValue = fieldsArray[i]["url"];
            paramValue = fieldsArray[i]["param"];
        }
        newHtml += '<div class="leftEnumText">Extra Token ' + i + ':';
        if (i==extraTokensToShow && i!=10)
            newHtml += '&nbsp;&nbsp;<a href="javascript:;" onclick="addExtraToken()">(add)</a>'
        newHtml += '</div>';
        newHtml += '<div class="rightEnumText3"><input name="txtExtraTokenName' + i + '" id="txtExtraTokenName' + i + '" type="text" class="inputsShort3" value="' + htmlEntities(nameValue) + '"/></div>';
        newHtml += '<div class="rightEnumText3"><input name="txtExtraTokenUrl' + i + '" id="txtExtraTokenUrl' + i + '" type="text" class="inputsShort3" value="' + htmlEntities(urlValue) + '" onblur="updateDestinationUrl()"/></div>';
        newHtml += '<div class="rightEnumText3"><input name="txtExtraTokenParam' + i + '" id="txtExtraTokenParam' + i + '" type="text" class="inputsShort3" value="' + htmlEntities(paramValue) + '"/></div>';
        newHtml += '<div style="clear:both"></div>';
    }
	containerControl.innerHTML = newHtml;
}

function checkCampaignName()
{
    var nameControl = $get("txtName");
    if (nameControl!=null)
    {
        if (trim(nameControl.value)=='')
        {
            alert('Enter a Campaign Name before Saving the Campaign.');
            return false;
        }
    }
    return true;
}

function checkUrls(pageControlName, urlControlName, count, message)
{
    for(i=0;i<count;i++)
    {
        var pageControl = $get(pageControlName + i);
        var urlControl = $get(urlControlName + i);
        if (pageControl!=null && urlControl!=null && pageControl.value!='' && urlControl.value=='')
        {
            alert('Please enter an URL for ' + message + ' ' + (i + 1));
            return false;
        }
    }
    return true;
}

function checkSharesLead()
{
    if (!checkCampaignName())
        return false;
	var hidControl = $get("hidDestinationToShow");
	var destinationToShow = hidControl.value;
	var hidControlPath = $get("hidOffersToShow");
	var offersToShow = hidControlPath.value;
	hidControl = $get("hidTrackingIndex");
	var trackingIndex = hidControl.value;
	if (!checkUrls('txtLanding','txtLandingUrl',destinationToShow,'landing page'))
	    return false;
	if (trackingIndex==1)
	{
	    if (!checkUrls('txtLandingtr','txtLandingUrltr',destinationToShow,'after opt-in'))
	        return false;
	}
    if (trackingIndex!=4)
    {
        if (!checkUrls('txtCampaignOffer','txtCampaignOfferURL',offersToShow,'offer'))
            return false;
	}
	var totalShare = 0;
	for (i=0; i<destinationToShow; i++)
	{
		var currentShareControl = $get('txtShare' + i);
        var currentUrlControl = $get('txtLandingUrl' + i);
	    var currentInactiveControl = $get('chkInactive' + i);
        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
			totalShare += (+currentShareControl.value);
	}
	if (totalShare != 100)
	{
		alert('Current total share for Landing Pages is ' + totalShare + ' and should be 100. Please fix this!');
		return false;
	}
	if (trackingIndex==1)
	{
        var totalShare = 0;
        for (i=0; i<destinationToShow; i++)
        {
	        var currentShareControl = $get('txtSharetr' + i);
            var currentUrlControl = $get('txtLandingUrltr' + i);
	        var currentInactiveControl = $get('chkInactivetr' + i);
	        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
		        totalShare += (+currentShareControl.value);
        }
        if (totalShare != 100)
        {
	        alert('Current total share for After Opt-In pages is ' + totalShare + ' and should be 100. Please fix this!');
	        return false;
        }
    }
    if (trackingIndex!=4)
    {
	    var totalShare = 0;
	    for (i=0; i<offersToShow; i++)
	    {
	        var currentShareControl = $get('txtOfferShare' + i);
	        var currentUrlControl = $get('txtCampaignOfferURL' + i);
	        var currentInactiveControl = $get('chkInactiveOff' + i);
	        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
	            totalShare += (+currentShareControl.value);
	    }
        if (totalShare != 100)
        {
	        alert('Current total share for Offers is ' + totalShare + ' and should be 100. Please fix this!');
	        return false;
        }
    }
	return true;
}

function checkSharesPath()
{
    if (!checkCampaignName())
        return false;
	var hidControl = $get("hidDestinationToShow");
	var destinationToShow = hidControl.value;
	var hidControlPath = $get("hidPathsToShow");
	var pathsToShow = hidControlPath.value;
	var hidControlPath = $get("hidOffersToShow");
	var offersToShow = hidControlPath.value;
	for (j=0;j<pathsToShow;j++)
	    if (!checkUrls('txtLanding' + j + '_','txtLandingUrl' + j + '_',destinationToShow,'path ' + (j+1) + ' landing page'))
	        return false;
	for (j=0;j<pathsToShow;j++)
	    if (!checkUrls('txtCampaignOffer' + j + '_','txtCampaignOfferURL' + j + '_',offersToShow,'path ' + (j+1) + ' offer'))
	        return false;
	var totalShare = 0;
	for (j=0; j<pathsToShow; j++)
	{
	    var currentShareControl = $get('txtSharePath' + j);
        if (currentShareControl!=null)
		    totalShare += (+currentShareControl.value);
	}
	if (totalShare != 100)
	{
		alert('Current total share for Paths is ' + totalShare + ' and should be 100. Please fix this!');
		return false;
	}
	for (j=0; j<pathsToShow; j++)
	{
	    totalShare = 0;
	    var totalUrls = false;
	    for (i=0; i<destinationToShow; i++)
	    {
		    var currentShareControl = $get('txtShare' + j + '_' + i);
            var currentUrlControl = $get('txtLandingUrl' + j + '_' + i);
	        var currentInactiveControl = $get('chkInactive' + j + '_' + i);
	        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
			    totalShare += (+currentShareControl.value);
	        if (!totalUrls && currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentInactiveControl!=null && !currentInactiveControl.checked)
	            totalUrls = true;
	    }
	    if (totalUrls && totalShare != 100)
	    {
		    alert('Current total share for Path ' + (j+1) + ' Landing Pages is ' + totalShare + ' and should be 100. Please fix this!');
		    return false;
	    }
	}
	for (j=0; j<pathsToShow; j++)
	{
	    totalShare = 0;
	    var totalUrls = false;
	    for (i=0; i<offersToShow; i++)
	    {
	        var currentShareControl = $get('txtOfferShare' + j + '_' + i);
	        var currentUrlControl = $get('txtCampaignOfferURL' + j + '_' + i);
	        var currentInactiveControl = $get('chkInactiveOff' + j + '_' + i);
	        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
	            totalShare += (+currentShareControl.value);
	        if (!totalUrls && currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentInactiveControl!=null && !currentInactiveControl.checked)
	            totalUrls = true;
	    }
        if (totalUrls && totalShare != 100)
        {
	        alert('Current total share for Path ' + (j+1) + ' Offers is ' + totalShare + ' and should be 100. Please fix this!');
	        return false;
        }
	}
	return true;
}

function checkSharesCta()
{
    if (!checkCampaignName())
        return false;
	var hidControl = $get("hidDestinationToShow");
	var destinationToShow = hidControl.value;
	var hidControlPath = $get("hidPathsToShow");
	var pathsToShow = hidControlPath.value;
	var hidControlPath = $get("hidOffersToShow");
	var offersToShow = hidControlPath.value;
	if (!checkUrls('txtLanding','txtLandingUrl',destinationToShow,'landing page'))
	    return false;
	for (j=0;j<pathsToShow;j++)
	    if (!checkUrls('txtCampaignOffer' + j + '_','txtCampaignOfferURL' + j + '_',offersToShow,'option ' + (j+1) + ' offer'))
	        return false;

    var totalShare = 0;
    for (i=0; i<destinationToShow; i++)
    {
	    var currentShareControl = $get('txtShare' + i);
        var currentUrlControl = $get('txtLandingUrl' + i);
        var currentInactiveControl = $get('chkInactive' + i);
	    if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
		    totalShare += (+currentShareControl.value);
    }
    if (totalShare != 100)
    {
	    alert('Current total share for Landing Pages is ' + totalShare + ' and should be 100. Please fix this!');
	    return false;
    }
	for (j=0; j<pathsToShow; j++)
	{
	    var totalShare = 0;
	    var totalUrls = false;
	    for (i=0; i<offersToShow; i++)
	    {
	        var currentShareControl = $get('txtOfferShare' + j + '_' + i);
	        var currentUrlControl = $get('txtCampaignOfferURL' + j + '_' + i);
	        var currentInactiveControl = $get('chkInactiveOff' + j + '_' + i);
	        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
	            totalShare += (+currentShareControl.value);
	        if (!totalUrls && currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentInactiveControl!=null && !currentInactiveControl.checked)
	            totalUrls = true;
	    }
        if (totalUrls && totalShare != 100)
        {
	        alert('Current total share for Option ' + (j+1) + ' Offers is ' + totalShare + ' and should be 100. Please fix this!');
	        return false;
        }
	}
	return true;
}

function checkSharesSequence()
{
    if (!checkCampaignName())
        return false;
	var hidControl = $get("hidDestinationToShow");
	var destinationToShow = hidControl.value;
	var hidControlPath = $get("hidPathsToShow");
	var pathsToShow = hidControlPath.value;
	var hidControlPath = $get("hidOffersToShow");
	var offersToShow = hidControlPath.value;
	var hidControlOption = $get("hidOptionsToShow");
	var optionsToShow = hidControlOption.value;
	hidControl = $get("hidDestinationIndex");
	var destinationIndex = hidControl.value;
	hidControl = $get("hidTrackingIndex");
	var trackingIndex = hidControl.value;
	for (j=0;j<pathsToShow;j++)
	    if (!checkUrls('txtLanding' + j + '_','txtLandingUrl' + j + '_',destinationToShow,'level ' + (j+1) + ' landing page'))
	        return false;
    if (!checkUrls('txtCampaignOffer','txtCampaignOfferURL',offersToShow,'offer'))
        return false;
	for (j=0; j<pathsToShow; j++)
	{
	    var totalShare = 0;
	    var totalUrls = false;
	    for (i=0; i<destinationToShow; i++)
	    {
		    var currentShareControl = $get('txtShare' + j + '_' + i);
            var currentUrlControl = $get('txtLandingUrl' + j + '_' + i);
	        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null)
			    totalShare += (+currentShareControl.value);
	        if (!totalUrls && currentUrlControl!=null && trim(currentUrlControl.value)!='')
	            totalUrls = true;
	    }
	    if (totalUrls && totalShare != 100)
	    {
		    alert('Current total share for Level ' + (j+1) + ' is ' + totalShare + ' and should be 100. Please fix this!');
		    return false;
	    }
	}
	if (destinationIndex==4 && trackingIndex==1)
	{
        if (!checkUrls('txtLandingtr','txtLandingUrltr',destinationToShow,'after opt-in'))
            return false;
        var totalShare = 0;
        for (i=0; i<destinationToShow; i++)
        {
	        var currentShareControl = $get('txtSharetr' + i);
            var currentUrlControl = $get('txtLandingUrltr' + i);
	        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null)
		        totalShare += (+currentShareControl.value);
        }
        if (totalShare != 100)
        {
	        alert('Current total share for After Opt-In pages is ' + totalShare + ' and should be 100. Please fix this!');
	        return false;
        }
    }

    if (destinationIndex!=4 || trackingIndex!=3)
    {
	    for (j=0; j<optionsToShow; j++)
	    {
	        var totalShare = 0;
	        var totalUrls = false;
	        for (i=0; i<offersToShow; i++)
	        {
	            var currentShareControl = $get('txtOfferShare' + j + '_' + i);
	            var currentUrlControl = $get('txtCampaignOfferURL' + j + '_' + i);
	            var currentInactiveControl = $get('chkInactiveOff' + j + '_' + i);
	            if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
	                totalShare += (+currentShareControl.value);
	            if (!totalUrls && currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentInactiveControl!=null && !currentInactiveControl.checked)
	                totalUrls = true;
	        }
            if (totalUrls && totalShare != 100)
            {
	            alert('Current total share for Option ' + (j+1) + ' Offers is ' + totalShare + ' and should be 100. Please fix this!');
	            return false;
            }
        }
    }
	return true;
}

function checkSharesEmail()
{
    if (!checkCampaignName())
        return false;
	var hidControl = $get("hidDestinationToShow");
	var destinationToShow = hidControl.value;
	var hidControlPath = $get("hidPathsToShow");
	var pathsToShow = hidControlPath.value;
	var hidControlPath = $get("hidOffersToShow");
	var offersToShow = hidControlPath.value;
	var hidControlType = $get("hidDestinationIndex");
	var destinationIndex = hidControlType.value;
	if (destinationIndex==1)
	{
	    for (j=0;j<pathsToShow;j++)
	        if (!checkUrls('txtLanding' + j + '_','txtLandingUrl' + j + '_',destinationToShow,'email ' + (j+1) + ' landing page'))
	            return false;
	}
	for (j=0;j<pathsToShow;j++)
	    if (!checkUrls('txtCampaignOffer' + j + '_','txtCampaignOfferURL' + j + '_',offersToShow,'email ' + (j+1) + ' offer'))
	        return false;

	if (destinationIndex==1)
	{
	    for (j=0; j<pathsToShow; j++)
	    {
	        var totalShare = 0;
	        var totalUrls = false;
	        for (i=0; i<destinationToShow; i++)
	        {
		        var currentShareControl = $get('txtShare' + j + '_' + i);
                var currentUrlControl = $get('txtLandingUrl' + j + '_' + i);
                var currentInactiveControl = $get('chkInactive' + j + '_' + i);
                if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
			        totalShare += (+currentShareControl.value);
	            if (!totalUrls && currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentInactiveControl!=null && !currentInactiveControl.checked)
	                totalUrls = true;
	        }
	        if (totalUrls && totalShare != 100)
	        {
		        alert('Current share on Email ' + (j+1) + ' Landing Pages is ' + totalShare + ' and should be 100. Please fix this!');
		        return false;
	        }
	    }
	}
	for (j=0; j<pathsToShow; j++)
	{
	    var totalShare = 0;
        var totalUrls = false;
	    for (i=0; i<offersToShow; i++)
	    {
	        var currentShareControl = $get('txtOfferShare' + j + '_' + i);
	        var currentUrlControl = $get('txtCampaignOfferURL' + j + '_' + i);
	        var currentInactiveControl = $get('chkInactiveOff' + j + '_' + i);
	        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
	            totalShare += (+currentShareControl.value);
            if (!totalUrls && currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentInactiveControl!=null && !currentInactiveControl.checked)
                totalUrls = true;
	    }
        if (totalUrls && totalShare != 100)
        {
	        alert('Current share on Email ' + (j+1) + ' Offers is ' + totalShare + ' and should be 100. Please fix this!');
	        return false;
        }
	}
	return true;
}

function checkSharesVs()
{
    if (!checkCampaignName())
        return false;
	var hidControlPath = $get("hidOffersToShow");
	var offersToShow = hidControlPath.value;
	var hidControlPath = $get("hidDestinationToShow");
	var destinationToShow = hidControlPath.value;
	var hidControlIndex = $get("hidDestinationIndex");
	var destinationIndex = hidControlIndex.value;
    if (destinationIndex!=2)
    {
	    if (!checkUrls('txtLanding','txtLandingUrl',destinationToShow,'landing page'))
	        return false;
	}
	if (!checkUrls('txtCampaignOffer','txtCampaignOfferURL',offersToShow,'offer'))
	    return false;
    var totalShare = 0;
	for (i=0; i<offersToShow; i++)
	{
	    var currentShareControl = $get('txtOfferShare' + i);
        var currentUrlControl = $get('txtCampaignOfferURL' + i);
	    var currentInactiveControl = $get('chkInactiveOff' + i);
	    if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
	        totalShare += (+currentShareControl.value);
	}
    if (totalShare != 100)
    {
	    alert('Current total share for Offers is ' + totalShare + ' and should be 100. Please fix this!');
	    return false;
    }
    if (destinationIndex!=2)
    {
        totalShare = 0;
	    for (i=0; i<destinationToShow; i++)
	    {
	        var currentShareControl = $get('txtShare' + i);
            var currentUrlControl = $get('txtLandingUrl' + i);
	        var currentInactiveControl = $get('chkInactive' + i);
	        if (currentUrlControl!=null && trim(currentUrlControl.value)!='' && currentShareControl!=null && currentInactiveControl!=null && !currentInactiveControl.checked)
	            totalShare += (+currentShareControl.value);
	    }
        if (totalShare != 100)
        {
	        alert('Current total share for Landing Pages is ' + totalShare + ' and should be 100. Please fix this!');
	        return false;
        }
    }
	return true;
}

function validatePayout(control)
{
	var newValue = parseFloat(control.value);
	if (newValue < 0)
		newValue = 0;
	if (isNaN(newValue))
		newValue=0;
	control.value = newValue
}

function validateLandingID(control)
{
	var newValue = parseInt(control.value);
	if (newValue < 0)
		newValue = 0;
	if (isNaN(newValue))
		newValue=0;
	control.value = newValue
}

function validateShare(control)
{
	var newValue = parseInt(control.value);
	if (newValue < 0)
		newValue = 0;
	if (newValue > 100)
		newValue = 100;
	if (isNaN(newValue))
		newValue=0;
	control.value = newValue;
}

function validateTimeout(control)
{
	var newValue = parseFloat(control.value);
	if (isNaN(newValue))
		newValue=30;
	if (newValue < 1)
		newValue = 30;
	control.value = newValue
}

function evenSharesLandingVs(extra)
{
    var hidDestinationToShow = $get('hidDestinationToShow');
    var totalControls=hidDestinationToShow.value;
    var addShare=new Array();
    var removeShare=new Array();
    for (i=0; i<totalControls; i++)
    {
        var control = $get('txtLandingUrl' + extra + i);
        var controlShare = 'txtShare' + extra + i;
        var controlInactive = $get('chkInactive' + extra + i);
        if (control!=null)
        {
            if (control.value!='' && (controlInactive == null || !controlInactive.checked))
            {
                addShare[addShare.length] = controlShare;
            }
            else
            {
                removeShare[removeShare.length] = controlShare;
            }
        }
    }
    var mainShare= Math.round(100 / addShare.length);
    for(i=0; i<addShare.length; i++)
    {
        if (i==addShare.length-1)
            currentShare = 100 - (addShare.length-1) * mainShare;
        else
            currentShare = mainShare;
        $get(addShare[i]).value = currentShare;
    }
    for(i=0;i<removeShare.length; i++)
        $get(removeShare[i]).value=0;
}

function evenSharesOfferVs(extra)
{
    var hidDestinationToShow = $get('hidOffersToShow');
    var totalControls=hidDestinationToShow.value;
    var addShare=new Array();
    var removeShare=new Array();
    for (i=0; i<totalControls; i++)
    {
        var control = $get('txtCampaignOfferURL' + extra + i);
        var controlShare = 'txtOfferShare' + extra + i;
        var controlInactive = $get('chkInactiveOff' + extra + i);
        if (control!=null)
        {
            if (control.value!='' && (controlInactive == null || !controlInactive.checked))
            {
                addShare[addShare.length] = controlShare;
            }
            else
            {
                removeShare[removeShare.length] = controlShare;
            }
        }
    }
    var mainShare= Math.round(100 / addShare.length);
    for (i=0; i<addShare.length; i++)
    {
        if (i==addShare.length-1)
            currentShare = 100 - (addShare.length-1) * mainShare;
        else
            currentShare = mainShare;
        $get(addShare[i]).value = currentShare;
    }
    for(i=0;i<removeShare.length; i++)
        $get(removeShare[i]).value=0;
}

function writeCampaignName(control)
{
    var currentIndex = 1;
    var continueLoop = 1;
    while (continueLoop!=0)
    {
        var currentControl = $get('spCamp' + currentIndex);
        if (currentControl == null)
            continueLoop = 0;
        else
            currentControl.innerHTML = control.value;
        currentIndex++;
    }
}

function clickCaptureExtraTokens(control)
{
    var divExtra = $get('divExtraTokens');
    divExtra.style.display = (control.checked ? "block" : "none");
}

function getAjaxRequest(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	return ajaxRequest;
}

function trim(str) {
    return str.replace(/^\s*|\s*$|\n|\r/g,"");
}

function buildQueryString(params) {
    var query = "";
    for (var i = 0; i < params.length; i++) {
        query += (i > 0 ? "&" : "")
            + escape(params[i].name) + "="
            + escape(params[i].value);
    }
    return query;
}

function updateDestinationUrl()
{
    var txtAppendToken = $get('txtAppendToken');
    var txtCampaignUrl = $get('txtCampaignUrl');
    var hidBaseUrl = $get('hidBaseUrl');
    txtCampaignUrl.value = hidBaseUrl.value + txtAppendToken.value;
    for (i=0; i<=10; i++)
    {
        var txtExtraTokenUrl = $get('txtExtraTokenUrl' + i);
        if (txtExtraTokenUrl != null)
            txtCampaignUrl.value += txtExtraTokenUrl.value;
    }
}

function clickInactiveLP(senderChecked, suffix)
{
    var txtLanding = $get('txtLanding' + suffix);
    var txtLandingPageID = $get('txtLandingPageID' + suffix);
    var txtLandingUrl = $get('txtLandingUrl' + suffix);
    var txtShare = $get('txtShare' + suffix);
    txtShare.readOnly = senderChecked;
    txtLanding.style.color = txtLandingPageID.style.color = txtLandingUrl.style.color = txtShare.style.color = senderChecked ? "#777" : "";
    txtLanding.style.backgroundColor = txtLandingPageID.style.backgroundColor = txtLandingUrl.style.backgroundColor = txtShare.style.backgroundColor = senderChecked ? "#EBEBEB" : "";
    if (senderChecked)
        txtShare.value = 0;
}

function clickInactiveOffer(senderChecked, suffix)
{
    var txtCampaignOffer = $get('txtCampaignOffer' + suffix);
    var txtPayout = $get('txtPayout' + suffix);
    var txtCampaignOfferURL = $get('txtCampaignOfferURL' + suffix);
    var txtOfferShare = $get('txtOfferShare' + suffix);
    var ddlNetwork = $get('ddlNetwork' + suffix);
    txtOfferShare.readOnly = senderChecked;
    txtCampaignOffer.style.color = txtPayout.style.color = txtCampaignOfferURL.style.color = 
        txtOfferShare.style.color = ddlNetwork.style.color = senderChecked ? "#777" : "";
    txtCampaignOffer.style.backgroundColor = txtPayout.style.backgroundColor = txtCampaignOfferURL.style.backgroundColor = 
        txtOfferShare.style.backgroundColor = ddlNetwork.style.backgroundColor = senderChecked ? "#EBEBEB" : "";
    if (senderChecked)
        txtOfferShare.value = 0;
}

function clickInactiveLPAll(count, suffix)
{
    for (i=0; i<count; i++)
    {
        var chkInactive = $get('chkInactive' + suffix + i);
        if (chkInactive != null && chkInactive.checked)
            clickInactiveLP(chkInactive.checked, suffix + i);
    }
}

function clickInactiveOfferAll(count, suffix)
{
    for (i=0; i<count; i++)
    {
        var chkInactiveOff = $get('chkInactiveOff' + suffix + i);
        if (chkInactiveOff != null && chkInactiveOff.checked)
            clickInactiveOffer(chkInactiveOff.checked, suffix + i);
    }
}

function ajaxChangeCampaignDropdownCpv(sender) {
    if ($get('spanCpvName')==null)
        return;
    var ajaxRequest = getAjaxRequest();
    var ddlvalue = sender.options[sender.selectedIndex].value;
    
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
            var divTable = $get('spanCpvName');
            divTable.innerHTML = ajaxRequest.responseText;
      }
    }
    
    var params = [
        { name: "campaignID", value: ddlvalue }
    ];
    ajaxRequest.open("POST", "ajax/getCampaignDetailsCpv.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var body = buildQueryString(params);
    ajaxRequest.send(body); 
}

function ajaxSaveSource() {
    var ajaxRequest = getAjaxRequest();
    var sourceName = $get("txtSourceName");
    var sourceID = $get("txtSourceID");
    var keywordToken = $get("txtKeywordToken");
    var appendToken = $get("txtAppendToken");
    var timezones = $get("ddlCpvTimezone");
    if (sourceName == "")
        return;
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
            var ddl = $get('ddlCurrentSources');
            select_innerHTML(ddl,ajaxRequest.responseText);
            $get("txtSourceName").value = $get("txtSourceID").value = 
                $get("txtKeywordToken").value = $get("txtAppendToken").value = "";
      }
    }
    
    var params = [
        { name: "sourceName", value: sourceName.value },
        { name: "sourceID", value: sourceID.value },
        { name: "keywordToken", value: keywordToken.value },
        { name: "appendToken", value: appendToken.value },
        { name: "timezone", value: timezones.options[timezones.selectedIndex].value }
    ];
    ajaxRequest.open("POST", "ajax/saveSource.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var body = buildQueryString(params);
    ajaxRequest.send(body); 
}

function ajaxDeleteSource() {
    var ajaxRequest = getAjaxRequest();
    var ddlSource = $get("ddlCurrentSources");
    if (ddlSource.selectedIndex<0)
        return;
    var deleteID = ddlSource.options[ddlSource.selectedIndex].value;
    
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
            var ddl = $get('ddlCurrentSources');
            select_innerHTML(ddl,ajaxRequest.responseText);
      }
    }
    
    var params = [
        { name: "deleteID", value: deleteID }
    ];
    ajaxRequest.open("POST", "ajax/deleteSource.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var body = buildQueryString(params);
    ajaxRequest.send(body); 
}

function ajaxGetPredefinedSource(sender) {
    var ajaxRequest = getAjaxRequest();
    var ddlvalue = sender.options[sender.selectedIndex].value
    if (ddlvalue==0)
        return;
    
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
            var txtSourceID = $get('txtSourceID');
            var txtSource = $get('txtSource');
            var txtKeywordToken = $get('txtKeywordToken');
            var txtAppendToken = $get('txtAppendToken');
            var parts = ajaxRequest.responseText.split('*|*');
            if (parts.length==4)
            {
                txtSourceID.value=parts[0];
                txtSource.value=parts[1];
                txtKeywordToken.value=parts[2];
                txtAppendToken.value=parts[3];
            }
            updateDestinationUrl();
        }
    }
    
    var params = [
        { name: "cpvSourceID", value: ddlvalue }
    ];
    ajaxRequest.open("POST", "ajax/getSource.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var body = buildQueryString(params);
    ajaxRequest.send(body); 
}

function ajaxSaveNetwork() {
    var ajaxRequest = getAjaxRequest();
    var sourceName = $get("txtNetworkName");
    if (sourceName == "")
        return;
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
            var ddl = $get('ddlCurrentNetworks');
            select_innerHTML(ddl,ajaxRequest.responseText);
            $get("txtNetworkName").value = "";
      }
    }
    
    var params = [
        { name: "sourceName", value: sourceName.value }
    ];
    ajaxRequest.open("POST", "ajax/saveNetwork.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var body = buildQueryString(params);
    ajaxRequest.send(body); 
}

function ajaxDeleteNetwork() {
    var ajaxRequest = getAjaxRequest();
    var ddlSource = $get("ddlCurrentNetworks");
    if (ddlSource.selectedIndex<0)
        return;
    var deleteID = ddlSource.options[ddlSource.selectedIndex].value;
    
    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
            var ddl = $get('ddlCurrentNetworks');
            select_innerHTML(ddl,ajaxRequest.responseText);
      }
    }
    
    var params = [
        { name: "deleteID", value: deleteID }
    ];
    ajaxRequest.open("POST", "ajax/deleteNetwork.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var body = buildQueryString(params);
    ajaxRequest.send(body); 
}

function ajaxOpenChart(ddlSource,reportTypeID,levelID) {
    var ajaxRequest = getAjaxRequest();
    var column = '';
    var columnS = '';
    if (ddlSource!=null)
    {
        if (ddlSource.id.indexOf('ddlChartColumnsS')>=0)
            columnS = ddlSource.options[ddlSource.selectedIndex].value;
        else
            column = ddlSource.options[ddlSource.selectedIndex].value;
    }
    var ddlCampaigns = $get('ddlCampaigns');
    var campaignID = ddlCampaigns.options[ddlCampaigns.selectedIndex].value;
    var hidWhereClause = $get('hidWhereClause');
    var whereClause = hidWhereClause==null ? '' : hidWhereClause.value;
    var hidIntervalWhere = $get('hidIntervalWhere');
    var intervalWhere = hidIntervalWhere==null ? '' : hidIntervalWhere.value;

    ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
            var divContainer = document.getElementsByName('divContainer' + reportTypeID);
            for (i=0; i<divContainer.length; i++)
                divContainer[i].innerHTML=ajaxRequest.responseText;
      }
    }
    
    var params = [
        { name: "camp", value: campaignID },
        { name: "col", value: column },
        { name: "colS", value: columnS },
        { name: "level", value: levelID },
        { name: "whereClause", value: whereClause },
        { name: "intw", value: intervalWhere }
    ];
    ajaxRequest.open("POST", "ajax/chartDisplay" + reportTypeID + ".php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var body = buildQueryString(params);
    ajaxRequest.send(body); 
}

function select_innerHTML(objeto,innerHTML){
    objeto.innerHTML = ""
    var selTemp = document.createElement("micoxselect")
    var opt;
    selTemp.id="micoxselect1"
    document.body.appendChild(selTemp)
    selTemp = document.getElementById("micoxselect1")
    selTemp.style.display="none"
    if(innerHTML.toLowerCase().indexOf("<option")<0){
        innerHTML = "<option>" + innerHTML + "</option>"
    }
    innerHTML = innerHTML.replace(/<option/g,"<span").replace(/<\/option/g,"</span")
    selTemp.innerHTML = innerHTML
      
    
    for(var i=0;i<selTemp.childNodes.length;i++){
  var spantemp = selTemp.childNodes[i];
  
        if(spantemp.tagName){     
            opt = document.createElement("OPTION")
    
   if(document.all){ //IE
    objeto.add(opt)
   }else{
    objeto.appendChild(opt)
   }       
    
   //getting attributes
   for(var j=0; j<spantemp.attributes.length ; j++){
    var attrName = spantemp.attributes[j].nodeName;
    var attrVal = spantemp.attributes[j].nodeValue;
    if(attrVal){
     try{
      opt.setAttribute(attrName,attrVal);
      opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
     }catch(e){}
    }
   }
   //getting styles
   if(spantemp.style){
    for(var y in spantemp.style){
     try{opt.style[y] = spantemp.style[y];}catch(e){}
    }
   }
   //value and text
   opt.value = spantemp.getAttribute("value")
   opt.text = spantemp.innerHTML
   //IE
   opt.selected = spantemp.getAttribute('selected');
   opt.className = spantemp.className;
  } 
 }    
 document.body.removeChild(selTemp)
 selTemp = null
}

function select_all(control)
{
    control.focus();
    control.select();
}

function htmlEntities(str) {
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}