function redirect(href) {
	window.location = href;
}
function popup_window(href) {
	window.open(href)
}
function password() {
	var form = $('form');
	if(form.new_password.value != form.confirm_password.value) {
		alert('Your new password does not match your confirm password!');
		return false;
	}
	return true;
}
function check_records() {
	var theForm = document.form;

	//return false if no records are present to prevent js error
	if(!theForm.elements['main']) return false;

	if(theForm.elements['main'].checked == true) {
		for(var i=0; i < theForm.elements['records[]'].length; i++){
			theForm.elements['records[]'][i].checked = true;
		}
	} else {
		for(var i=0; i < theForm.elements['records[]'].length; i++){
			theForm.elements['records[]'][i].checked = false;
		}
	}
}
function showHide(div_id) {
	var div = document.getElementById(div_id);

	if (div) {
		if (div.style.display == 'none') {
			expand(div_id);
		} else {
			collapse(div_id);
		}
	}
}
function expand(div_id) {
    var div = document.getElementById(div_id);
    var icon = document.getElementById('icon_'+div_id);
	
	if (div.style.display == 'none') {
		div.style.display = 'block';
		icon.src = "images/minus.gif";
    }
}
function collapse(div_id) {
    var div = document.getElementById(div_id);
    var icon = document.getElementById('icon_'+div_id);

    if (div.style.display != 'none') {
      div.style.display = 'none';
      icon.src = "images/plus.gif";
	}
}
function check_all(type) {
	var text = document.form.select_boxes;
	var area_div = document.getElementById('other_territory').getElementsByTagName('input');

	if(text.value == 'Select All') {
		for (i = 0; i < area_div.length; i++) {
			area_div[i].checked = true;
		}
		text.value = 'Un-Select All';
	} else {
		for (i = 0; i < area_div.length; i++) {
			area_div[i].checked = false;
		}
		text.value = 'Select All';
	}		
}


function products()
{
	var form = $('form');
	if(!checkBoxArray("products", "Product")) {
		return false;
	}
	return true;
}

function checkBoxArray(fldname, fldlabel) {
	var check = false;
	for(var i=0; i < document.form.elements[fldname+'[]'].length; i++){
		if(document.form.elements[fldname+'[]'][i].checked) {
			check = true;
		}
	}
	if(check==false) {
		alert("Please select atleast one "+fldlabel);
		return false;
	}
	return true;
}
function first_login(action) {
	if(action == 'account') {
		alert("This is your first login!\n\nPlease Update your Profile and verify the following:\n\n* Company Name and Address\n* Contact Name\n* License Number\n* Insurance Carrier\n* Years in Business\n* Phone, fax, and email info\n* etc...");
	} else if(action == 'territories') {
		alert("Please review and update your territory to product selection.  You may select a different territory for each product that you sell and install.");
	} else if(action == 'products') {
		alert("Please take a moment to update your products that you sell and install, and add any additional products that you may do.\n\nFor each product you select you can choose a different budget.\nFor each product you select you must update your territory for that specific product in order for you to recieve leads.");
	} else if(action == 'selection') {
		alert("Please take a moment to select your territory type.");
	}
}
function checkRadio() {
	for (i=0, n=document.form.budget.length; i<n; i++) {
	   if (document.form.budget[i].checked) {
	      return true;
	   }
	}
	alert("Please choose a Budget.");
	return false;
}

function update_start(form_name)
{
	var theForm = document.forms[form_name];
	var years = theForm.years.value;
	var months = theForm.months.value;
	var days = theForm.days.value;

	theForm.date_sold.value = years+'-'+months+'-'+days;
}
function format_currency(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) {
		i = 0.00;
		alert('You have entered an invalid Number, please only use numbers and decimals');
		return false;
	}
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}