function isNumeric()
{
    //only allows user to enter numeric characters

	if(!(event.keyCode >= 48 && event.keyCode <= 57))
		return false;
	
	return true;
}

function changeColor(d)
{
	var f = document.form1;
	
	var newColor = prompt("Please enter color.", d.style.backgroundColor);

	try
	{	
		if(newColor != null && newColor != "")
			d.style.backgroundColor = newColor;f
	}
	catch(e)
	{
		alert("Invalid color.");
	}
}

function checkSignUp()
{
    //verify all fields have been entered before submitting

	var f = document.form1;
	
	if(f.first_name.value.length == 0)
	{
		alert("Please enter your first name.");
		f.first_name.focus();
		
		return false;
	}
	else if(f.last_name.value.length == 0)
	{
		alert("Please enter your last name.");
		f.last_name.focus();
		
		return false;
	}
	else if(f.email_address.value.length == 0)
	{
		alert("Please enter your email address.");
		f.email_address.focus();
		
		return false;
	}
	else if(f.user_name.value.length == 0)
	{
		alert("Please enter your user name.");
		f.user_name.focus();
		
		return false;
	}
	else if(!f.accept_terms_cond.checked)
	{
		alert("You must accept the terms & conditions.");
		f.accept_terms_cond.focus();
		
		return false;
	}
	
	return true;
}

function checkProfile()
{
    //verify all fields have been entered before submitting

	var f = document.form1;
	
	if(f.first_name.value.length == 0)
	{
		alert("Please enter your first name.");
		f.first_name.focus();
		
		return false;
	}
	else if(f.last_name.value.length == 0)
	{
		alert("Please enter your last name.");
		f.last_name.focus();
		
		return false;
	}
	else if(f.email_address.value.length == 0)
	{
		alert("Please enter your email address.");
		f.email_address.focus();
		
		return false;
	}
	
	return true;
}

function checkAddProject()
{
    //verify all fields have been entered before submitting

	var f = document.form1;
	var el;
	
	if(f.proj_name.value.length == 0)
	{
		alert("Please enter a project name.");
		f.proj_name.focus();
		
		return false;
	}
	else if(f.company_name.value.length == 0)
	{
		alert("Please enter a company name.");
		f.company_name.focus();
		
		return false;
	}
	else if(f.company_address.value.length == 0)
	{
		alert("Please enter a company address.");
		f.company_address.focus();
		
		return false;
	}
	else if(f.company_city.value.length == 0)
	{
		alert("Please enter a company city.");
		f.company_city.focus();
		
		return false;
	}
	else if(f.company_state.value.length == 0)
	{
		alert("Please enter a company state.");
		f.company_state.focus();
		
		return false;
	}
	else if(f.company_zip.value.length == 0)
	{
		alert("Please enter a company zip.");
		f.company_zip.focus();
		
		return false;
	}
	else if(f.logo.value != "")
	{
	    //verify they have selected a file with accepted file extension
		var extensions = new Array("gif", "jpg", "jpeg", "bmp", "tif");
		var bGoodExtension;
		var ext = f.logo.value.toLowerCase();
		ext = ext.substring(ext.lastIndexOf(".") + 1);
		
		for(var i = 0; i < extensions.length; i++)
		{
			if(ext == extensions[i])
			{
				bGoodExtension = true;
				break;
			}
		}
		
		if(!bGoodExtension)
		{
			alert("Please select a valid image file: " + extensions);
			f.logo.focus();
		
			return false;
		}
		
	}
	
	f.titlebarbgcolor.value = document.getElementById("div_titlebarbgcolor").style.backgroundColor;
	f.optionslogobgcolor.value = document.getElementById("div_optionslogobgcolor").style.backgroundColor;
	f.optioncontentbgcolor.value = document.getElementById("div_optioncontentbgcolor").style.backgroundColor;
	f.tickerbgcolor.value = document.getElementById("div_tickerbgcolor").style.backgroundColor;
	
	for(var i = 0; i < f.referers.length; i++)
	{
	    //create hidden fields for added referers
	    el = document.createElement("input");
        el.attributes("name").value = "referers" + i;
        el.attributes("value").value = f.referers[i].attributes("value").value;
        el.attributes("type").value = "hidden";
        
        f.appendChild(el);
	}
	
	return true;
}

function moveOption(sel, dir)
{
    //move options in list box up/down

	var index = sel.selectedIndex;
	var next_index;
	var oldVal, oldText

	if(index != -1)
	{
		var next_index = index + (dir ? -1 : 1);
   		if(next_index < 0 || next_index >= sel.length)
   		    return;
			
		oldVal = sel[index].value;
   		oldText = sel[index].text;
   
		sel[index].value = sel[next_index].value;
		sel[index].text = sel[next_index].text;
		
		sel[next_index].value = oldVal;
		sel[next_index].text = oldText;
		
		sel.selectedIndex = next_index;
	}
}

function checkOptions()
{
    var f = document.form1;
    var el;
    
    for(var i = 0; i < f.options.length; i++)
    {
        //add hidden fields for options that are in list box
        el = document.createElement("input");
        el.attributes("name").value = "options" + i;
        el.attributes("value").value = f.options[i].attributes("value").value;
        el.attributes("type").value = "hidden";
        
        f.appendChild(el);
    }
}

function addOption(bEdit)
{
    var f = document.form1;
    
    if(bEdit)
    {
        if(f.options.selectedIndex != -1)
            window.location = "edit_option.aspx?optID=" + f.options.value;
    }
    else
        window.location = "add_option.aspx";
}

function deleteOption()
{
    var f = document.form1;
    var el;
    var iDel = 0, iKeep = 0;
    
    if(f.options.selectedIndex != -1)
    {
        if(confirm("Are you want to delete the selected option(s) and all associated records?"))
        {
            f.hidAction.value = "";
        
            //loop through all selected options that need to be deleted and created hidden fields
            for(var i = 0; i < f.options.length; i++)
            {
                if(f.options[i].selected)
                {
                    el = document.createElement("input");
                    el.attributes("name").value = "del_options" + iDel;
                    el.attributes("value").value = f.options[i].attributes("value").value;
                    el.attributes("type").value = "hidden";
                 
                    f.appendChild(el);
                    
                    iDel++;
                }
                else
                {
                    el = document.createElement("input");
                    el.attributes("name").value = "options" + iKeep;
                    el.attributes("value").value = f.options[i].attributes("value").value;
                    el.attributes("type").value = "hidden";
                    
                    f.appendChild(el);
                    
                    iKeep++;
                }
            }
            
            f.submit();
        }
    }
}

function copyOption()
{
    var f = document.form1;
    var el;
    var iCopy = 0;
    
    if(f.options.selectedIndex != -1)
    {
        f.hidAction.value = "copy";
    
        el = document.createElement("input");
        el.attributes("name").value = "copy_option";
        el.attributes("value").value = f.options[f.options.selectedIndex].attributes("value").value;
        el.attributes("type").value = "hidden";
     
        f.appendChild(el);
    
        //loop through all selected options that need to be copied and created hidden fields
//        for(var i = 0; i < f.options.length; i++)
//        {
//            if(f.options[i].selected)
//            {
//                el = document.createElement("input");
//                el.attributes("name").value = "copy_options" + iCopy;
//                el.attributes("value").value = f.options[i].attributes("value").value;
//                el.attributes("type").value = "text";
//             
//                f.appendChild(el);
//                
//                iCopy++;
//            }
//        }
//        
        f.submit();
    }
}

function checkAddOption()
{
    //verify all fields have been entered before submitting

    var f = document.form1;
    var sel = f.type == null ? f.optType : f.type;
    var el;
    var iFieldCount;
    
    if(f.heading.value.length == 0)
    {
        alert("Please enter an option heading.");
        f.heading.focus();
        
        return false;
    }
    
    if(sel.value == "form")
    {
        if(f.form_fields.selectedIndex == -1)
        {
            alert("Please select at least 1 form field.");
            f.form_fields.focus();
        
            return false;
        }
        
        iFieldCount = 0;
        
        for(var i = 0; i < f.form_fields.length; i++)
        {
            if(f.form_fields[i].selected)
            {
                //hidden fields for fields that have been selected
                el = document.createElement("input");
                el.attributes("name").value = "form_fields" + iFieldCount;
                el.attributes("value").value = f.form_fields[i].attributes("value").value;
                el.attributes("type").value = "hidden";
                
                f.appendChild(el);
                
                iFieldCount++;
            }
            
        }
        
    }
    else if(sel.value == "coupon")
    {
        if(f.require_info.checked && f.coupon_fields.selectedIndex == -1)
        {
            alert("Please select at least 1 required field.");
            f.coupon_fields.focus();
        
            return false;
        }
        
        if(f.require_info.checked)
        {
            iFieldCount = 0;
        
            for(var i = 0; i < f.coupon_fields.length; i++)
            {
                if(f.coupon_fields[i].selected)
                {
                    //hidden fields for fields that have been selected
                    el = document.createElement("input");
                    el.attributes("name").value = "coupon_fields" + iFieldCount;
                    el.attributes("value").value = f.coupon_fields[i].attributes("value").value;
                    el.attributes("type").value = "hidden";
                    
                    f.appendChild(el);
                    
                    iFieldCount++;
                }
                
            }
        }
    }
    else if(sel.value == "map")
    {
        if(!f.use_comp_address.checked)
        {
            if(f.map_name.value.length == 0)
            {
                alert("Please enter a name.");
                f.map_name.focus();
            
                return false;
            }
            else if(f.map_address.value.length == 0)
            {
                alert("Please enter an address.");
                f.map_address.focus();
            
                return false;
            }
            else if(f.map_city.value.length == 0)
            {
                alert("Please enter a city.");
                f.map_city.focus();
            
                return false;
            }
            else if(f.map_state.value.length == 0)
            {
                alert("Please enter a state.");
                f.map_state.focus();
            
                return false;
            }
            else if(f.map_zip.value.length == 0)
            {
                alert("Please enter a zip.");
                f.map_zip.focus();
            
                return false;
            }
        }
    }
    else if(sel.value == "link")
    {
        if(f.link.value.length == 0)
        {
            alert("Please enter a link.");
            f.link.focus();
        
            return false;
        }
    }
    else if(sel.value == "clicktocall")
    {
        if(f.campaign_id.value.length == 0)
        {
            alert("Please enter a campaign ID.");
            f.campaign_id.focus();
        
            return false;
        }
    }
    
    if(f.send_email.checked)
    {
        for(var i = 0; i < f.lead_recipients.length; i++)
        {
            el = document.createElement("input");
            el.attributes("name").value = "lead_recipients" + i;
            el.attributes("value").value = f.lead_recipients[i].attributes("value").value;
            el.attributes("type").value = "hidden";
            
            f.appendChild(el);
        }
    }

    return true;
}

function changeRecordView(v)
{
    var f = leads_list.document.form1;

    leads_list.window.location = "leads_list.aspx?field=" + f.sort_field.value + "&order=" + f.sort_order.value + "&search_text=" + f.search_text.value + "&search_field=" + f.search_field.value + "&view=" + (v ? "all" : "");
    
    div_all.style.display = v ? "inline" : "none";
    div_25.style.display = v ? "none" : "inline";
}

function searchItems()
{
    var f = leads_list.document.form1;
	var f2 = document.form1;
	
	//reload the lead gen records with specified search criteria
	//loadXMLXSL("data/" + document.form1.key.value + "/info.xml", "records.xsl", document.getElementById("records"), ["sort_field", f.sort_field.value, "sort_order", f.sort_order.value, "search_text", f.search_text.value, "search_field", f.search_field.value, "view", f.view.value]);
	leads_list.window.location = "leads_list.aspx?field=" + f.sort_field.value + "&order=" + f.sort_order.value + "&search_text=" + f2.search_text.value + "&search_field=" + f2.search_field.value + "&view=" + f.view.value;
}

function exportRec(exportType, i)
{
	var f = i.document.form1
	
	f.export_type.value = exportType;
	f.submit();
	
}

function selType()
{
    //when adding an option, user can select different types. appropriate rows are displayed upon type selection

    var tr = new Array("tr_form", "tr_form2", "tr_form3", "tr_coupon1", "tr_coupon2", "tr_coupon3", "tr_coupon4", "tr_coupon5", "tr_coupon6", "tr_map1", "tr_map2", "tr_map3", "tr_map4", "tr_text", "tr_link1", "tr_link2", "tr_clicktocall");
    var sel = document.form1.type;
    var f = document.form1;

    //hide all
    for(var i = 0; i < tr.length; i++)
    {
        document.getElementById(tr[i]).style.display = "none";
    }
    
    //show appropriate rows
    if(sel.value == "form")
    {
        document.getElementById("tr_form").style.display = "inline";
        document.getElementById("tr_form2").style.display = "inline";
        document.getElementById("tr_form3").style.display = "inline";
    }
    else if(sel.value == "coupon")
    {
        document.getElementById("tr_coupon1").style.display = "inline";
        document.getElementById("tr_coupon2").style.display = "inline";
        document.getElementById("tr_coupon3").style.display = "inline";
        document.getElementById("tr_coupon4").style.display = "inline";
        document.getElementById("tr_coupon5").style.display = "inline";
        document.getElementById("tr_coupon6").style.display = f.require_info.checked ? "inline" : "none";
    }
    else if(sel.value == "map")
    {
        document.getElementById("tr_map1").style.display = "inline";
        document.getElementById("tr_map2").style.display = f.use_comp_address.checked ? "none" : "inline";
        document.getElementById("tr_map3").style.display = f.use_comp_address.checked ? "none" : "inline";
        document.getElementById("tr_map4").style.display = f.use_comp_address.checked ? "none" : "inline";
    }
    else if(sel.value == "text")
    {
        document.getElementById("tr_text").style.display = "inline";
    }
    else if(sel.value == "link")
    {
        document.getElementById("tr_link1").style.display = "inline";
        document.getElementById("tr_link2").style.display = "inline";
    }
    else if(sel.value == "clicktocall")
    {
        document.getElementById("tr_clicktocall").style.display = "inline";
    }
}

function selAfterSubmitMessage()
{
    var f = document.form1;

    //show/hide div for message
    document.getElementById("div_message").style.display = f.after_submit_message.checked ? "inline" : "none";
}

function selAfterSubmitLink()
{
    var f = document.form1;

    //show/hide div for link
    document.getElementById("div_link").style.display = f.after_submit_link.checked ? "inline" : "none";
}

function selCouponRequireInfo()
{
    var f = document.form1;

    //show/hide row for coupon required info
    document.getElementById("tr_coupon6").style.display = f.require_info.checked ? "inline" : "none";
}

function checkMap()
{
    var f = document.form1;
    
    //show/hide rows to enter map info if not using company address
    document.getElementById("tr_map2").style.display = f.use_comp_address.checked ? "none" : "inline";
    document.getElementById("tr_map3").style.display = f.use_comp_address.checked ? "none" : "inline";
    document.getElementById("tr_map4").style.display = f.use_comp_address.checked ? "none" : "inline";
}

function removeLogo()
{
    var f = document.form1;
    
    if(confirm("Are you sure you want to remove the logo?"))
    {
        f.remove_logo.value = "yes";
        
        //hide logo row if removing
        document.getElementById("tr_logo").style.display = "none";
    }
}

function selTicker()
{
    var f = document.form1;
    
    //show/hide ticker row
    tr_ticker_text.style.display = f.show_ticker.checked ? "inline" : "none";
}

function addReferer(bAdd)
{
    var f = document.form1;
    var sel = f.referers;
    var strReferer;

    if(bAdd)
    {
        if(f.referer.value.length != 0)
        {
            //add referer to list box
            strReferer = f.referer.value;

            if(strReferer.substring(strReferer.length - 1) == "/")
                strReferer = strReferer.substring(0, strReferer.length - 1);
                
            var opt = new Option();
            opt.text = strReferer;
            opt.value = strReferer;
            
            sel[sel.length] = opt;
            
            f.referer.value = "http://";
            f.referer.focus();
        }
    }
    else
    {
        //remove referer
        if(sel.selectedIndex != - 1 && confirm("Are you want to delete the selected referer?"))
            sel.options[sel.selectedIndex] = null;
    }
}

function addLeadRecipient(bAdd)
{
    var f = document.form1;
    var sel = f.lead_recipients;
    var strLeadRecipent;

    if(bAdd)
    {
        if(f.lead_recipient.value.length != 0)
        {
            //add lead recipient to list box
            strLeadRecipent = f.lead_recipient.value;
    
            var opt = new Option();
            opt.text = strLeadRecipent;
            opt.value = strLeadRecipent;
            
            sel[sel.length] = opt;
            
            f.lead_recipient.value = "";
            f.lead_recipient.focus();
        }
    }
    else
    {
        //remove lead recipient from list box
        if(sel.selectedIndex != - 1 && confirm("Are you want to delete the selected email address?"))
            sel.options[sel.selectedIndex] = null;
    }
}

function selSendEmail()
{
    var f = document.form1;
    
    //show/hide email row
    tr_email.style.display = f.send_email.checked ? "inline" : "none";
}

function checkAll(f, chkSel, chk)
{
    //check all check boxes for records listing
    for(var i = 1; ; i++)
    {
        if(f[chkSel + i])
            f[chkSel + i].checked = chk.checked;
        else
            break;
    }
}

function deleteRecords(bDetails)
{
    
    var bOne = false;
    var el;
    var iFieldCount = 1;

    if(bDetails != null)
    {
        var f = document.form3;
        var f2 = document.form1;
    
        if(confirm("Are you sure you want to delete record?"))
        {
            //hidden field for delete record
            el = document.createElement("input");
            el.attributes("name").value = "delete1";
            el.attributes("value").value = f2.item_id.value;
            el.attributes("type").value = "hidden";
            
            f2.appendChild(el);
            
            el = document.createElement("input");
            el.attributes("name").value = "delete_opt1";
            el.attributes("value").value = f2.opt_id.value;
            el.attributes("type").value = "hidden";
            
            f2.appendChild(el);
        
            f2.submit();
        }
    }
    else
    {
        var f = document.form3;
        var f2 = leads_list.document.form1;
    
        if(confirm("Are you sure you want to delete the selected record(s)?"))
        {
            for(i = 1; ; i++)
            {
                if(f2["chkSel" + i])
                {
                    if(f2["chkSel" + i].checked)
                    {
                        //hidden field for all checked records to delete
                        el = document.createElement("input");
                        el.attributes("name").value = "delete" + iFieldCount;
                        el.attributes("value").value = f2["chkSel" + i].value;
                        el.attributes("type").value = "text";
                        
                        f.appendChild(el);
                        
                        el = document.createElement("input");
                        el.attributes("name").value = "delete_opt" + iFieldCount;
                        el.attributes("value").value = f2["chkSel" + i].opt_id;
                        el.attributes("type").value = "text";
                        
                        f.appendChild(el);
                    
                        bOne = true;
                        iFieldCount++;
                    }
                }
                else
                    break;
            }
            
            if(bOne)
                f.submit();
        }
    }
    
}

function deleteProjects()
{
    var f = document.form1;
    var f2 = project_list.document.form1;
    var bOne = false;
    var el;
    var iFieldCount = 1;

    if(confirm("Are you sure you want to delete the selected project(s)?"))
    {
        for(i = 1; ; i++)
        {
            if(f2["chkSel" + i])
            {
                if(f2["chkSel" + i].checked)
                {
                    //hidden field for all checked records to delete
                    el = document.createElement("input");
                    el.attributes("name").value = "delete" + iFieldCount;
                    el.attributes("value").value = f2["chkSel" + i].value;
                    el.attributes("type").value = "hidden";
                    
                    f.appendChild(el);
                
                    bOne = true;
                    iFieldCount++;
                }
            }
            else
                break;
        }
        
        if(bOne)
            f.submit();
    }
    
}

function selResizeLogo()
{
    var f = document.form1;

    //show/hide logo rows
    document.getElementById("tr_logo3").style.display = f.resize_logo.checked ? "inline" : "none";
    document.getElementById("tr_logo4").style.display = f.resize_logo.checked ? "inline" : "none";
}

function showCode(type)
{
    var f = document.form1;
    //var strHTML = "&lt;html&gt;<br/>&lt;head&gt;<br/>&lt;scr" + "ipt id='milg_script' language='javascript' src='http://" + server_name + "/lead_gen/scripts/lead_gen.aspx?key=" + key + "'&gt;&lt;/scr" + "ipt&gt;<br/>&lt;/head&gt;<br/>&lt;body" + (type == "onload" ? " onload='openMILG(MILG_ACTION_RESET);'" : "") + "&gt;<br/><br/>&lt;!-- Example --&gt;<br/>&lt;!-- To display an option other than the first one when shown you can add a second parameter to specify the option index to be displayed i.e. openMILG(MILG_ACTION_RESET, 3) --&gt;<br/><br/>";
    var strHTML = "&lt;scr" + "ipt id='milg_script' language='javascript' src='http://" + server_name + "/lead_gen/scripts/lead_gen.aspx?key=" + key + "'&gt;&lt;/scr" + "ipt&gt;<br/><br/>&lt;!-- Example --&gt;<br/>";
    
    //show different types of example code depending on radio choice
    if(type != "delay")
    {
        strHTML += "&lt;!-- To display an option other than the first one when shown you can add a parameter to specify the option index to be displayed i.e. openMILG(3) --&gt;<br/><br/>";
        
        if(type == "link")
        {
            strHTML += "&lt;a href='javascript: openMILG();'&gt;Click Me&lt;/a&gt;";
        }
        else if(type == "image")
        {
            strHTML += "&lt;img src='some_image.gif' onclick='openMILG();' /&gt;";
        }
        else if(type == "onload")
        {
            strHTML = "&lt;body onload='openMILG();'&gt;<br/><br/>" + strHTML + "&lt;/body&gt;";
            //strHTML += "Show on load.";
        }
    }
    else
    {
        strHTML += "&lt;!-- First parameter is how long the delay should be in seconds, to display an option other than the first one when shown you can add a second parameter to specify the option index to be displayed i.e. delayMILG(5, 3) --&gt;<br/><br/>";
    
        strHTML += "&lt;scr" + "ipt&gt;<br/>delayMILG(5);<br/>&lt;/scr" + "ipt&gt;";
    }
    
    
    
    document.getElementById("code").innerHTML = strHTML;    
    
}

function changeGraph(type)
{
    //chang to/from this year pie chart & last 12 months line graph
    document.getElementById("tbl_chart_year").style.display = "none";
    document.getElementById("tbl_chart_month_by_month").style.display = "none";

    if(type == "monthbymonth")
        document.getElementById("tbl_chart_month_by_month").style.display = "inline";
    else if(type == "year")
        document.getElementById("tbl_chart_year").style.display = "inline";
}