
    function switchWebsiteAssociation( sender )
    {
        var websiteAssociations = document.getElementsByName('data[Website][Website][]');
        
        if(sender.value == 1)
        {
            for(i=0; i<websiteAssociations.length; i++)
            {
                websiteAssociations[i].checked = websiteAssociations[i].disabled = true;
            }
        }
        else
        {
            for(i=0; i<websiteAssociations.length; i++)
            {
                websiteAssociations[i].checked   = websiteAssociations[i].value in myWebsiteAssociations;
                websiteAssociations[i].disabled  = false;
            }
        }
    }
    
    function setWebsiteAssociation( sender )
    {
        if(sender.checked)
        {
            myWebsiteAssociations[sender.value] = "";
        }
        else
        {
            delete myWebsiteAssociations[sender.value];
        }
    }
    
    function switchClient( sender, domId )
    {
        disableInput(null, null, {opacity:75,color:'white'});

        myClientAssociations = {};
        
        new Ajax.Updater(domId, site_root+'/clients/ajax_clients/'+sender.options[sender.selectedIndex].value, {onComplete:function(request){enableInput();}, asynchronous:true, evalScripts:true, requestHeaders:['X-Update', domId]});
    }
    
    function switchWebsite( sender, websiteId )
    {
        var regexTest = /\/([\d]+)/;
        var regexPattern = /([^\d]+)\/([\d]+)(.*)/g;
        
        if(regexTest.test(window.location))
        {
            url = regexPattern.exec(window.location);
            redirect = site_root+url[1];
        }
        
        if(confirm("Switching active Website selection will reload the current page.\r\n\r\nAny un-saved work will be lost!\r\n\r\nAre you sure ?"))
        {
            disableInput(null,null,{opacity:75,color:'white'});
            
            new Ajax.Request(site_root+'/websites/session/insert/dynamiccreativeWebsite/'+sender.options[sender.selectedIndex].value, {onComplete:function(request){window.location.reload();}, asynchronous:false, evalScripts:true});
            window.location = site_root+'/users/recheck_access?return='+window.location;
        }
        else
        {
            for(i=0; i<sender.options.length; i++)
                if(sender.options[i].value == websiteId) { sender.selectedIndex = i; }
            
            return false;
        }
    }
    
    function switchWebsiteInventory( sender, inventoryId )
    {
        if(!sender) { return false; }
        
        if(confirm("Switching active Inventory selection will reload the current page.\r\n\r\nAny un-saved work will be lost!\r\n\r\nAre you sure ?"))
        {
            disableInput(null,null,{opacity:75,color:'white'});
            
            new Ajax.Request(site_root+'/websites/session/insert/dynamiccreativeInventory/'+sender.options[sender.selectedIndex].value, {onComplete:function(request){window.location.reload();}, asynchronous:true, evalScripts:true});
        }
        else
        {
            for(i=0; i<sender.options.length; i++)
                if(sender.options[i].value == inventoryId) { sender.selectedIndex = i; }
                
            return false;
        }
        
        //return true;
        
    }
    
    
    function unselectWebsite()
    {
    	var websiteSelect = $('user-website-select');
    	
    	addSelectOption('user-website-select', '0', ' - ', true);
    }
    
    function disableWebsiteSelect()
    {
    	$('user-website-select').disabled = "disabled";
    }
    
    
    
    /** Budgets **/
    
//    function roundNumber(number, decimals)
//    {
//    	
//    	number = Math.round(number*100) / 100;
//    	var numberStr = number.toString();
//    	
//    	if(numberStr.indexOf('.') == (numberStr.length-decimals))
//    	{
//    		return numberStr+'0';
//    	}
//    	else
//    	{
//    		return numberStr;
//    	}
//    }
    
    /*
    var awValues = Array();
    var gValues = Array();
    var yValues = Array();
    
    
    function storeAmount(sender)
    {
    	var index = sender.id.substring(sender.id.length-1, sender.id.length);
    	
    	if(sender.id.indexOf('google'))
    	{
    		gValues[index] = sender.value;
    	}
    	else if(sender.id.indexOf('yahoo'))
    	{
    		yValues[index] = sender.value;
    	}
    	else
    	{
    		console.log('setting '+sender.id+'('+index+') = '+sender.value);
    		awValues[index] = sender.value;
    	}
    	
    }
    
    function amountChanged(sender)
    {
    	var index = sender.id.substring(sender.id.length-1, sender.id.length);
    	alert(sender.id);
    	if(sender.id.indexOf('google') != -1)
    	{
    		return (gValues[index] != sender.value);
    	}
    	else if(sender.id.indexOf('yahoo') != -1)
    	{
    		return (yValues[index] != sender.value);
    	}
    	else
    	{
//    		alert(sender.value+' = '+awValues[index]);
    		console.log('checking '+sender.value+'('+index+') = '+awValues[index]);
    		return (awValues[index] != sender.value);
    	}
    }*/
    
    
    function changeDollarAmount(sender)
    {
//    	var index = sender.id.substring(sender.id.length-1, sender.id.length);
    	var index = sender.id.match('[0-9]+');
    	
    	// update account wide input
    	if(sender.id.indexOf('account_wide') != -1)
    	{
    		// user updated dollar amounts based on percent
//    		if(confirm('Do you want DynamicCreative to automatically update search engine budgets?'))
//    		{
    			gPcInput = $('googlePercent'+index);
    			yPcInput = $('yahooPercent'+index);
    			
    			gDlrInput = $('googleDollar'+index);
    			yDlrInput = $('yahooDollar'+index);
    			
    			if(gPcInput != null)
    			{
//    				alert(gDlrAmt);
	    			gDlrAmt = (gPcInput.value/100) * sender.value;
	    			gDlrAmt = roundNumber(gDlrAmt, 2, false); // Math.round((gPcInput.value) * sender.value)/100;
	    			gDlrInput.value = gDlrAmt;
    			}
    			
    			if(yPcInput != null)
    			{
	    			yDlrAmt = (yPcInput.value/100) * sender.value;
	    			yDlrAmt = roundNumber(yDlrAmt, 2, false); //Math.round((yPcInput.value) * sender.value)/100;
	    			yDlrInput.value = yDlrAmt;
    			}
    			
//    		}
    	}
    	else
    	{
    		// user updated search engine input
	    	var awInput = $('account_wideDollar'+index);
	    	
	    	if(awInput.value == 0 || sender.value == 0)
	    	{
	    		return false;
	    	}
	    	
	    	var pcInputId = sender.id.replace('Dollar', 'Percent');
	    	var pcInput = $(pcInputId);
	    	var realFraction = sender.value / awInput.value;
	    	pcInput.value = roundNumber(realFraction*100, 2, false);
	    	
	    	var opcInputId = null;
	    	var odlrInputId = null;

	    	// also update google/yahoo with remaining budget
	    	if(sender.id.indexOf('google') != -1)
	    	{
	    		opcInputId = sender.id.replace('Dollar', 'Percent');
	    		opcInputId = opcInputId.replace('google', 'yahoo');
	    		odlrInputId = sender.id.replace('google', 'yahoo');
	    		
	    	}
	    	else if (sender.id.indexOf('yahoo') != -1)
	    	{
	    		opcInputId = sender.id.replace('Dollar', 'Percent');
	    		opcInputId = opcInputId.replace('yahoo', 'google');
	    		odlrInputId = sender.id.replace('yahoo', 'google');
//	    		opcInputId = sender.id.replace('yahoo', 'google');
	    	}

	    	// update other search engine with remaing budget
	    	if(opcInputId != null)
		    {
	    		var opcInput = $(opcInputId);
	    		var odlrInput = $(odlrInputId);
	    		
	    		odlrInput.value = roundNumber((awInput.value-sender.value), 2, false);
	    		
	    		var oRealFraction = (awInput.value-sender.value) / awInput.value;
//	    		alert(roundNumber(oRealFraction*100, 2, false));
	    		opcInput.value = roundNumber(oRealFraction*100, 2, false);
	    		
	    	}
	    	
    	}
    }
    
    function changePercentAmount(sender)
    {
//    	var index = sender.id.substring(sender.id.length-1, sender.id.length);
    	var index = sender.id.match('[0-9]+');
    	
    	var awInput = $('account_wideDollar'+index);
    	
    	if(awInput.value == 0 || sender.value == 0)
    	{
    		return false;
    	}
    	
    	var dlrInputId = sender.id.replace('Percent', 'Dollar');
    	var dlrInput = $(dlrInputId);
    	var dollar = (sender.value/100) * awInput.value;
    	
    	dlrInput.value = roundNumber(dollar, 2, false);
    	
    	
    	
    	
    	var opcInputId = null;
    	var odlrInputId = null;
    	
    	// also update google/yahoo with remaining budget
    	if(sender.id.indexOf('google') != -1)
    	{
    		odlrInputId = sender.id.replace('Percent', 'Dollar');
    		odlrInputId = odlrInputId.replace('google', 'yahoo');
    		opcInputId = sender.id.replace('google', 'yahoo');
    		
    	}
    	else if (sender.id.indexOf('yahoo') != -1)
    	{
    		odlrInputId = sender.id.replace('Percent', 'Dollar');
    		odlrInputId = odlrInputId.replace('yahoo', 'google');
    		opcInputId = sender.id.replace('yahoo', 'google');
//    		opcInputId = sender.id.replace('yahoo', 'google');
    	}

    	// update other search engine with remaing budget
    	if(opcInputId != null)
	    {
    		var opcInput = $(opcInputId);
    		var odlrInput = $(odlrInputId);
    		
    		odlrInput.value = roundNumber((awInput.value-dollar), 2, false);
    		
//    		alert('other pc = '+awInput.value+' - '+odlrInput.value+' / '+awInput.value+' = '+(awInput.value-odlrInput.value) / awInput.value);
    		var oRealFraction = (awInput.value-dollar) / awInput.value;
    		opcInput.value = roundNumber(oRealFraction*100, 2, false);
    		
    	}
    	
    	
    	
    }
    
    
    function copyDown(startIndex, fieldType, searchEngine, valueSrcId)
    {
    	
//    	var index = sender.id.substring(sender.id.length-1, sender.id.length);
//    	var idPrefix = sender.id.substring(0, sender.id.length-1);
//    	alert(sender.id);
//    	alert(idPrefix);
//    	var sender.id;
    	
    	valueSrc = $(valueSrcId);
    	
    	index = startIndex + 1;
    	
    	var nextElm = $(searchEngine+fieldType+index);
    	var nextElmIndex = nextElm.id.match('[0-9]+');
    	var nextElmAwInput = $('account_wideDollar'+nextElmIndex);
    	
    	while(nextElm != null)
    	{
    		// Ensure se value does not exceed account wide
    		if(searchEngine != 'account_wide')
    		{
	        	nextElmIndex = nextElm.id.match('[0-9]+');
	        	nextElmAwInput = $('account_wideDollar'+nextElmIndex);
	    		nextElm.value = roundNumber(Math.min(valueSrc.value, nextElmAwInput.value), 2, false);
    		}
    		else
    		{
    			nextElm.value = valueSrc.value;
    		}
    		
    		
    		
    		changeDollarAmount(nextElm);
//    		changePercentAmount(nextElm);
//    		console.log(nextElm.id);
    		nextElm = $(searchEngine+fieldType+index++);
    	}
    	
    }
    
    function checkPercent(sender)
    {
    	if(sender.value > 100)
    	{
    		sender.value = 100;
    	}
    	
    	return true;
    }
    
    
    function checkDollar(sender)
    {
    	
    	if(sender.id.indexOf('account_wide') != -1)
    	{
	    	var index = sender.id.match('[0-9]+');
	    	var awInput = $('account_wideDollar'+index);
	    	
	    	if(sender.value > awInput.value)
	    	{
	    		sender.value = awInput.value;
	    	}
    	}
    	
    	sender.value = roundNumber(sender.value, 2, false);
    	
    	return true;
    }
    
    
    function deleteUser( userId, userFirstName, userLastName )
    {
    	if(isNaN(userId)) { return false; }
    
    	if(confirm('Are you sure you want to permanantly remove the User: '+userFirstName+' '+userLastName+' from DynamicCreative&trade;'))
    	{
    		disableInput(null,null,{opacity:70});
    		
    		var jsonResponse = null;
    		
    		new Ajax.Request(site_root+'/users/remove/', {onComplete:function(transport, json){ jsonResponse = json; }, asynchronous:false, evalJSON:true});
    		
    		if(jsonResponse.failed == 'true')
    		{
    			alert(jsonResponse.error);
    			
    			enableInput();
    			
    			return false;
    		}
    		else
    		{
    			window.location.reload();
    		}
    	}
    	else
    	{
    		return false;
    	}
    }
