function providerSelectChange() {
	newValue = $("#providerSelector :selected").val();
	if (newValue == -1) {
		$("#customProviderName").attr("style", "display:block");
		$("#customProviderValue").attr("style", "display:block");
	}   else {
		$("#customProviderName").attr("style", "display:none");
		$("#customProviderValue").attr("style", "display:none");
	}
}

function changeAddressClick() {
	$("#view-address-area").attr("style", "display:none");
	$("#change-address-area").attr("style", "display:block");
	$("#change-address-link").attr("style", "visibility:hidden");
}

function cancelChangeAddress() {
	$("#view-address-area").attr("style", "display:block");
	$("#change-address-area").attr("style", "display:none");
	$("#change-address-link").attr("style", "visibility:visible");
}

function searchAddressClick(initial) {
	new_value = $("#address-search-value").attr("value");
	$("#change-address-building").attr("value", "true");
	if (new_value == initial) {
		cancelChangeAddress();
		return false;
	}

	if (new_value != undefined || new_value != '') {
		$.post("searchAddress.do", {address : new_value, initial : false, mapTo : true, changeAddressBuilding : true},
			function(data){
				$("#buildnig-info-section").html(data);
		}, "html");
	}
}


function searchAddressClickInStep2(initial) {
	new_value = $("#address-search-value").attr("value");
	$("#change-address-building").attr("value", "true");
	if (new_value == initial) {
		cancelChangeAddress();
		return false;
	}

	if (new_value != undefined || new_value != '') {
		$.post("searchAddressStep2.do", {address : new_value, initial : false, mapTo : true, changeAddressBuilding : true},
			function(data){
				$("#buildnig-info-section").html(data);
		}, "html");
	}
}


function searchAddressClickInWidget(initial, domainname) {

	new_value = $("#address-search-value").attr("value");
	$("#change-address-building").attr("value", "true");
	if (new_value == initial) {
		cancelChangeAddress();
		return false;
	}

	if (new_value != undefined || new_value != '') {
		$.post("widgetSearchAddress.do?quoteSource=" + domainname, {address : new_value, initial : false, mapTo : true, changeAddressBuilding : true},
			function(data){
				$("#buildnig-info-section").html(data);
		}, "html");
	}
}



function submitBuildingInfo() {
	//set up user provided coords
	$("#building-0-latitude").attr("value", flexModel.getLocation().getLat());
	$("#building-0-logitude").attr("value", flexModel.getLocation().getLng());

	//house-value-id
	houseElement = $("#house-value-id");
	//household-income-id
	incomeElement = $("#household-income-id");
	//monthly bill
	billElement= $("#monthly-bill");

	if (houseElement.length != 0) {
			houseElement.val(houseElement.val().replace(/,/g,""));
			houseElement.val(houseElement.val().replace(/\$/g,""));
		}

	if (incomeElement.length != 0) {
			incomeElement.val(incomeElement.val().replace(/,/g,""));
			incomeElement.val(incomeElement.val().replace(/\$/g,""));
		}

		billElement.val(billElement.val().replace(/,/g,""));
		billElement.val(billElement.val().replace(/\$/g,""));
	form = $("#building-info-form");
	if (form.valid()){
		$("#submit-building-button").attr("style", "display:none");
		$("#submit-building-block").attr("style", "display:block");
		$(".bg_request").attr("style", "display:none");		
		$.post("saveQuoteRequest.do", form.serialize(),
			function(data){
				$("#content").html(data);
		}, "html");
	}	
	return false;
}

function submitBuildingInfoInStep2() {

	//set up user provided coords
	
	//$("#building-0-latitude").attr("value", "40.840892604591300");
	//$("#building-0-logitude").attr("value", "-73.001596082590860");
	
	$("#building-0-latitude").attr("value", flexModel.getLocation().getLat());
	$("#building-0-logitude").attr("value", flexModel.getLocation().getLng());

	//house-value-id
	houseElement = $("#house-value-id");
		
	//household-income-id
	incomeElement = $("#household-income-id");
		
	//monthly bill
	billElement= $("#monthly-bill");
	
	if (houseElement.length != 0) {
			houseElement.val(houseElement.val().replace(/,/g,""));
			houseElement.val(houseElement.val().replace(/\$/g,""));
		}

	if (incomeElement.length != 0) {
			incomeElement.val(incomeElement.val().replace(/,/g,""));
			incomeElement.val(incomeElement.val().replace(/\$/g,""));
		}

		billElement.val(billElement.val().replace(/,/g,""));
		billElement.val(billElement.val().replace(/\$/g,""));

		new_value = $("#hdnService").attr("value");
		if (new_value == 66) {
			$("#noOfResidents").attr("value", "0");
		}
		
		//alert(document.getElementById("building-info-forma"));
	form = $("#building-info-form");
	//form = document.getElementById("building-info-form1");
	
	if (form.valid()){
		$("#submit-building-button").attr("style", "display:none");
		$("#submit-building-block").attr("style", "display:block");
		$(".bg_request").attr("style", "display:none");		
		$.post("saveQuoteRequestStep2.do", form.serialize(),
			function(data){
				$("#content").html(data);
		}, "html");
	}	
	return false;
}



function submitWidgetBuildingInfo() {
	
	//$("#building-0-latitude").attr("value", "40.840892604591300");
	//$("#building-0-logitude").attr("value", "-73.001596082590860");
	
	$("#building-0-latitude").attr("value", flexModel.getLocation().getLat());
	$("#building-0-logitude").attr("value", flexModel.getLocation().getLng());

	//house-value-id
	houseElement = $("#house-value-id");
	//household-income-id
	incomeElement = $("#household-income-id");
	//monthly bill
	billElement= $("#monthly-bill");

	if (houseElement.length != 0) {
			houseElement.val(houseElement.val().replace(/,/g,""));
			houseElement.val(houseElement.val().replace(/\$/g,""));
		}

	if (incomeElement.length != 0) {
			incomeElement.val(incomeElement.val().replace(/,/g,""));
			incomeElement.val(incomeElement.val().replace(/\$/g,""));
		}

		billElement.val(billElement.val().replace(/,/g,""));
		billElement.val(billElement.val().replace(/\$/g,""));
		
		new_value = $("#hdnService").attr("value");
		if (new_value == 66) {
			$("#noOfResidents").attr("value", "0");
		}
				
	form = $("#building-info-form");
	if (form.valid()){
		$("#submit-building-button").attr("style", "display:none");
		$("#submit-building-block").attr("style", "display:block");
		$(".bg_request").attr("style", "display:none");		
		$.post("saveWidgetQuoteRequest.do", form.serialize(),
			function(data){
				$("#contentpopup").html(data);
		}, "html");
	}	
	return false;
}

function submitAdminBuildingInfo() {
	$("#building-0-latitude").attr("value", flexModel.getLocation().getLat());
	$("#building-0-logitude").attr("value", flexModel.getLocation().getLng());

	form = $("#building-info-form").serialize();
	$.post("../saveQuoteRequest.do", form,
			function(data){
				$("#content").html(data);
		}, "html");
	return false;
}

function submitQuoteOrder() {
	form = $("#quote-option-form").serialize();
	
	$("#submit-order-button1").attr("style", "display:none");
	$("#submit-order-button-back1").attr("style", "display:none");
	$("#submit-order-block1").attr("style", "display:block");
	$("#submit-order-lock-back1").attr("style", "display:block");
	
	$("#submit-order-button2").attr("style", "display:none");
	$("#submit-order-button-back2").attr("style", "display:none");
	$("#submit-order-block2").attr("style", "display:block");
	$("#submit-order-lock-back2").attr("style", "display:block");
	
	
	$.post("placeAnOrder.do", form,
			function(data){
				$("#box2").html(data);
		}, "html");
	return false;
}


function submitConfigureOrder() {

	$.post("configureOrder.do", null,
			function(data){
				$("#content").html(data);
		}, "html");
	return false;
}

function submitPlaceAnOrder() {

	form = $("#place-order-form").serialize();

	$.post("placeAnOrder.do", form,
			function(data){
				$("#content").html(data);
		}, "html");
	return false;
}

function poptastic(url)
{
	var popwindow;
	popwindow=window.open(url,'pop_up_window','location=0,toolbar=0,status=0,resizable=0,directories=0,height=400,width=600,left=200,top=100');
    if (window.focus) {popwindow.focus()}
}

function changeAuxPanelDisplay(domainName){
    if (domainName == "Residential") {
          $("table.quote-tbl tr.aux-panel-class").show();
          $("table.quote-tbl input#house-value-id").attr("disabled", "");
          $("table.quote-tbl input#household-income-id").attr("disabled", "");          
    } else {        
        $("table.quote-tbl tr.aux-panel-class").hide();
        $("table.quote-tbl input#house-value-id").attr("disabled", "true");
        $("table.quote-tbl input#household-income-id").attr("disabled", "true");
    }
            
}

function validatephone(phonenumber) {
	 var maintainplus = '';
 	var numval = phonenumber.value
 	if ( numval.charAt(0)=='+' ){ var maintainplus = '+';}
 	curphonevar = numval.replace(/[\\A-Za-z!"£$%^&*+_={};:'@#~,.¦\/<>?|`¬\]\[]/g,'');
 	phonenumber.value = maintainplus + curphonevar;
 	var maintainplus = '';
 	phonenumber.focus;
}

function changeFields(serviceName, obj) {

	hdnService = $("#hdnService");
	
	if(obj !=null)
	hdnService.attr("value", obj.value);
	
	serviceElement= $("#electricOrHotWater");
	if (serviceName == "Solar Hot Water") {

		serviceElement.val('H');	 
		$("#fullTimeResidentDiv").attr("style", "display:block");
		$("#electricBillLabel").attr("style", "display:none");
		$("#hotWaterBillLabel").attr("style", "display:block");
	}else{

		serviceElement.val('E');
		$("#fullTimeResidentDiv").attr("style", "display:none");
		$("#electricBillLabel").attr("style", "display:block");
		$("#hotWaterBillLabel").attr("style", "display:none");
		$("#providertable").attr("style", "display:block");
	}
}

var serviceName="Solar Electric";
var domainName="Residential";

function displayDomain(domain){
	domainName = domain;
	domainService(domainName,serviceName);	
}

function displayService(service){
	serviceName = service;
	domainService(domainName,serviceName);	
}

function domainService(domain,service) {
	if (domain == "Residential" && service == "Solar Electric") {			
		$("#auxSupportDiv").attr("style", "display:block");	
		$("#house-value-id").attr("value", "");
		$("#household-income-id").attr("value", "");
	}else{
		$("#auxSupportDiv").attr("style", "display:none");
		$("#house-value-id").attr("value", "0.0");
		$("#household-income-id").attr("value", "0.0");
	}	
}
