SignUp = function()
{
	var t = this;
	t.broker = {};
	t.broker_location = {};
	t.global_type_default = {};
	t.creoba = false;
	t.su_amount_update = function()
	{
		var sel = $('id__package_id').value;
		var price = 0;
		for(var i=0;i<qp_list.length;i++)
		{
			if (sel == qp_list[i]['id'])
			{
				price = qp_list[i]['price'] * qp_list[i]['quantity'];
				$('id__qp_list_id').value = qp_list[i]['id'];
			}
		}
		if (price == 0)
		{
			$('id__qp_list_id').value = 0;
		}
		$('id__total_amount').value = price + price_pack;
	}

	t.change_broker = function(id)
	{
		if (id == '')
		{
			$('company_information').style.display = 'none';
			return false;
		}
		else if ('new' == id)
		{
			$('company_information').style.display = 'block';
			$('new__company_name').style.display = 'block';
			$('new__broker_location').style.display = 'none';
			$('new_location_info').style.display = 'block';
			return true;
		}
		else
		{
			$('company_information').style.display = 'block';
			$('new__company_name').style.display = 'none';
			$('new__broker_location').style.display = 'block';
			$('new_location_info').style.display = 'none';
		}
		var req = new JsHttpRequest();
		var params = {id:id};
	    req.onreadystatechange = function()
	    {
			if (req.readyState == 4)
	      	{
	      		if (req.responseJS.result != undefined)
	      		{
	      			if (req.responseJS.result)
	      			{
		      			var data = req.responseJS.data;
		      			sign_up.broker_location = data;
		      			data_location = data;
		      			var select = $('id__broker_location_id'); 
		      			select.options.length = 0;
		      			
		      			if (data.length>1)
		      			{
			      			select.options[select.options.length] = new Option('Please Select','', false, false);
		      			}
		      			for(var rec = 0; rec < data.length; rec++) 
		      			{
		      				var bb = false;
		      				select.options[select.options.length] = new Option(data[rec]['address'], data[rec]['id'], false, false);
		      			}
		      			select.options[select.options.length] = new Option('New Location', 'new', false, false);
	      			}
	      			else
	      			{
	      				
	      			}
	      		}
	      	}
	    }
	    req.caching = false;
	    req.open('POST', ajax_prefix + '?rm=ajax_get_broker_location', true);
	    req.send(params);		
	}

	t.change_broker_location = function(id)
	{
		if (id == '')
		{
			$('new_location_info').style.display = 'none';
		}
		else if ('new' == id)
		{
			$('new_location_info').style.display = 'block';
		}
		else
		{
			$('new_location_info').style.display = 'none';			
		}
		sign_up.change_global_type();
	}

	t.payment = function(bb)
	{
		
		if (bb)
		{
			$('payment_info').style.display = 'block';
		}
		else
		{
			$('payment_info').style.display = 'none';
		}
	}

	t.submit_step1 = function()
	{
		var valid_main = validator.check('signup_form_1');
		var valid = sign_up.validation1();
		if (valid_main && valid)
		{
			document.signup_form_1.submit();
		}
	}

	t.validation1 = function()
	{
		var check = true;
		
		if ($('id__broker_id').value == 'new' || $('id__broker_location_id').value == 'new') 
		{
			if ($('id__broker_id').value == 'new')
			{
				if ($('id__com_name').value == '')
				{
					if ( $('company_name').style.diplay == 'block' )
					{
						validator.add_error_style($('id__com_name'), "<div/>");
						$('id__com_name').focus();
					}
					check = false;
				}
			}
			var field = {id__com_address:'Incorrect Address', id__com_city:'Incorrect City', id__com_state:'Incorrect State', id__com_zip:'Incorrect ZIP', /*id__com_phone:'Incorrect Office Phone', id__com_fax:'Incorrect Office Fax',*/ id__com_website:'Incorrect Website Address'}
			for(var p in field)
			{
				if ($(p).value == '')
				{
					validator.add_error_style($(p), field[p]);
					$(p).focus();
					check = false;
				}
			}
		}
		else
		{
			if ($('id__broker_location_id').value == '')
			{
				if ( $('company_information').style.display == 'block' )
				{
					validator.add_error_style($('id__broker_location_id'), "<div/>");
					$('id__broker_location_id').focus();
				}
				check = false;
			}
		}
		if ($('id__email').value != $('id__confirm_email').value)
		{
			validator.add_error_style($('id__confirm_email'), "Email addresses are not equal");
			$('id__confirm_email').focus();
			check = false;
		}
		return check;
	}

	t.submit_step3 = function()
	{
		var valid_main = validator.check('signup_form_3');
		var valid = sign_up.validation3();

		if (valid_main && valid)
		{
			document.signup_form_3.submit();
		}
	}

	t.validation3 = function()
	{
		var check = true;
		return check;
	}

	t.change_global_type = function()
	{
		var type_user = $('id__type_user').checked;
		if (type_user)
		{
			t.created_select_global_type();
			return 1;
		}
		
		var broker = $('id__broker_id').value;
		var broker_location = $('id__broker_location_id').value;
		if (broker != 'new' && broker_location !='new')
		{
			var local = sign_up.broker_location;
			for(var rec = 0; rec < local.length; rec++) 
			{
				if (local[rec]['id'] == broker_location)
				{
					if (local[rec]['global_type'] == 'Commercial' || local[rec]['global_type'] == 'Residential')
					{
						t.created_select_global_type([local[rec]['global_type']]);
						return 1;
					}
				}
			}
		}
		t.created_select_global_type(['Commercial','Residential']);
	}

	t.created_select_global_type = function(data)
	{
		if (data == undefined || data == [])
		{
			data = t.global_type_default;
		}

		var select = $('id__global_type');
		var field_selected = {'Both':false,'Commercial':false,'Residential':false};
		for(var rec = 0; rec < select.length; rec++)
		{
			field_selected[select.options[rec].value] = select.options[rec].selected;
		}

		select.options.length = 0;
		for(var rec = 0; rec < data.length; rec++)
		{
			var bb = false;
			select.options[select.options.length] = new Option(data[rec], data[rec], field_selected[data[rec]], field_selected[data[rec]]);
		}
	}
	
	t.help_field = function(helps_text)
	{
		var groupAIds = [];
    	var pref = 'box_help_';
    	var i = 0;
    	for (var h in helps_text)
    	{
    		var id_help = pref + h;
    		if ($(id_help) != undefined)
    		{
	   			$(id_help).title = helps_text[h];
	   			groupAIds[i] = id_help;
	   			i++;
    		}
    	}
	    var ttA = new YAHOO.widget.Tooltip("box_help", {
        	context:groupAIds,
        	zindex:1000,
        	effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.20}
    	});		
	}
	
	t.change_filter_state = function(el_value)
	{
		var state_id = el_value;
		var req = new JsHttpRequest();
		var params = {state_id:state_id};
	    req.onreadystatechange = function()
	    {
			if (req.readyState == 4)
	      	{
	      		if (req.responseJS.result != undefined)
	      		{
	      			if (req.responseJS.result)
	      			{
		      			var data = req.responseJS.data;
		      			sign_up.broker = data;
		      			var select = $('id__broker_id'); 
		      			select.options.length = 0;
		      			select.options[select.options.length] = new Option('Please Select','', false, false);
		      			for(var rec = 0; rec < data.length; rec++) 
		      			{
		      				var bb = false;
		      				select.options[select.options.length] = new Option(data[rec]['company_name'], data[rec]['id'], false, false);
		      			}
		      			select.options[select.options.length] = new Option('New Company', 'new', false, false);
	      			}
	      			else
	      			{
	      				
	      			}
	      			$('company_information').style.display = 'none';
	      		}
	      	}
	    }
	    if ( el_value != undefined && el_value != 0 && el_value != ''  )
	    {
	    	$('company_name').style.display = 'block';
	    } else {
	    	$('company_name').style.display = 'none'
	    }
	    req.caching = false;
	    req.open('POST', ajax_prefix + '?rm=ajax_su_get_broker_filter', true);
	    req.send(params);	
	}
	
	t.change_memberships = function(el)
	{
		var select = el;
		for(var rec = 0; rec < select.length; rec++) 
		{
			if ('CREOBA' == select.options[rec].text)
			{
				sign_up.creoba = select.options[rec].selected;
			}
		}
		t.change_price_creoba();
	}
	
	t.change_price_creoba = function ()
	{
		if (t.creoba)
		{
			show_hide_element('id__price_box', 'none');
			show_hide_element('id__price_box_creoba', 'block');
			show_hide_element('id__days_free_trial__box', 'none');
			show_hide_element('id__days_free_trial__box_creoba', 'block');

		}
		else
		{
			show_hide_element('id__price_box', 'block');
			show_hide_element('id__price_box_creoba', 'none');
			show_hide_element('id__days_free_trial__box', 'block');
			show_hide_element('id__days_free_trial__box_creoba', 'none');

		}
	}
}

var sign_up = new SignUp();