// JavaScript Document
function rollOverBlack()
{
	  
	$('place_order').style.background = "transparent url(http://www.tidytrailers.com/images/place_order_black.gif) no-repeat center top";
}

function rollOverOriginal()
{
	  
	$('place_order').style.background = "transparent url(http://www.tidytrailers.com/images/place_order.gif) no-repeat center top";
}




/* This function ensures that only one service type is checked at a time*/
function checkServiceType(id)
{
 
	
		var state =$(id).checked;
		$('id_service_type_single').checked = false;
		$('id_service_type_curbside').checked = false;
		$('u_load_bool').checked = false;
		$('we_load_bool').checked = false;
		$('weight_restricted_bool_id').checked = false;
		 
	 
		$(id).checked = state; 
 
		updateEstimate();
		if (id =='id_service_type_single' && $(id).checked == true)
		{
			scrollTo(0,400);
		}
		 
			//If a service type gets unselected, so do all the children,
			//and the child div disappears.
			 
			if ($('id_service_type_curbside').checked == false)
			{
				 
				$('curbside_customize').style.display = 'none'; 
				$('curbside_id_customize_household_bool').checked =  false;
				$('curbside_id_customize_yard_bool').checked =  false;
				$('curbside_id_customize_drywall_bool').checked =  false;
			}
				
			if ($('u_load_bool').checked == false)
			{
				$('u_load_customize').style.display = 'none'; 
				$('u_load_id_customize_household_bool').checked =  false;
				$('u_load_id_customize_yard_bool').checked =  false;
				$('u_load_id_customize_drywall_bool').checked =  false;
			}
			
			if ($('we_load_bool').checked == false)
			{
				$('we_load_customize').style.display = 'none'; 
				$('we_load_id_customize_household_bool').checked =  false;
				$('we_load_id_customize_yard_bool').checked =  false;
				$('we_load_id_customize_drywall_bool').checked =  false;
			}
			
			if ($('weight_restricted_bool_id').checked == false)
			{
				$('id_restricted_load_roof').checked = false;
				$('id_restricted_load_brick').checked = false;
				$('id_restricted_load_soil').checked = false;
		    }
		 
		    /*If a service type to which ewaste is not applicable gets selected, must unselect ewaste */
		 	if ($('weight_restricted_bool_id').checked == true || $('id_service_type_single').checked == true)
				$('id_ewaste_surcharge_bool').checked = false;
		
	 
	
	updateEstimate();
}


function checkSelect(id)
{
	//called when a selection is changed.
	//id_customize_yard_value
	id = id.replace( "value", "bool" );
	$(id).checked = true;
	updateEstimate();
	 
}

function checkInput(form)
{
	var checkboxes = $$('input.how_hear');
	var atLeastOne = false;
	var result = true;
	 
	for(var i=0; i<checkboxes.length; i++)
	{
		 if ( checkboxes[i].checked == true)
		 {
			 atLeastOne = true;
		 }
	}
	
	if (atLeastOne == false)
	{
		alert("How did you hear about us? Please check at least one option. ");
		return false;
	}
	
	result = form.name.value  != "" && form.name.value  != null &&
	form.address.value != "" && form.address.value  != null &&
	form.email.value != "" && form.email.value  != null &&
	form.delivery_address.value != "" && form.delivery_address.value  != null &&
    form.region.value  != "" && form.region.value  != null &&
	form.postal_code.value != "" && form.postal_code.value  != null &&
	form.tel.value  != "" && form.tel.value != null ;
	
	if ( result == false)
		alert("Please input all required fields. ");
		

	return result;
}



function scrollTo()
{
	window.scrollBy(0,900); // horizontal and vertical scroll increments
}

function showDiv(id)
{
	//alert(id);
	//alert($(id));
	/*$('curbside_customize').show(); This doesn't work - why?*/
	$(id).style.display = 'block'; 
	 
}

function checkMe(id)
{
	//can only be checked if restricted is unchecked.

 //If a child gets checkbox checked, so does the parent
 if ($(id).checked == true)
 {
		if (id == 'curbside_id_customize_household_bool' || id == 'curbside_id_customize_yard_bool' || id == 'curbside_id_customize_drywall_bool')
		{
			$('id_service_type_curbside').checked = true;
			checkServiceType('id_service_type_curbside');
			$(id).checked = true;
		}
		if (id == 'u_load_id_customize_household_bool' || id == 'u_load_id_customize_yard_bool' || id == 'u_load_id_customize_drywall_bool')
		{
			$('u_load_bool').checked = true;
			checkServiceType('u_load_bool');
			$(id).checked = true
		}
		if (id == 'we_load_id_customize_household_bool' || id == 'we_load_id_customize_yard_bool' || id == 'we_load_id_customize_drywall_bool')
		{
			$('we_load_bool').checked = true;
			checkServiceType('we_load_bool');
			$(id).checked = true;
		}	
		
		if (id == 'id_ewaste_surcharge_bool')
		{
				if ( $('weight_restricted_bool_id').checked == true || $('id_service_type_single').checked == true)
					$('id_ewaste_surcharge_bool').checked = false;
		}
 }
		updateEstimate();
	 
	
	
}

function checkRestricted(id)
{
	var state =$(id).checked;
	
	if  ($('id_restricted_load_roof').checked == true || $('id_restricted_load_brick').checked == true || $('id_restricted_load_soil').checked == true)
	{
			$('weight_restricted_bool_id').checked = true;
			checkServiceType('weight_restricted_bool_id');
	}
	 
    
	 
		//wipe out all other ones, but the one that was clicked.
		//Preserve state of thing as it was when it was clicked, make sure all other ones are wiped out. 
	
		$('id_restricted_load_roof').checked = false;
		$('id_restricted_load_brick').checked = false;
		$('id_restricted_load_soil').checked = false;
		$(id).checked = state; 
 

	 
	 	
 
		updateEstimate();
	
}

function updateEstimate()
{
	var val = 149.0;
	
	if(document.getElementById('extra_days').checked == true)
	{
		val +=  parseFloat(extractValue (document.getElementById('extra_day').value)) ;
	}
	 
	if(document.getElementById('we_load').checked == true)
	{
		val +=  25.0 ;
	}
	
	 
	
	document.getElementById('estimate').value = money(val);
	var inner_html = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Your tidy estimate: $" + money(val);
	$('div_estimate_dollar').innerHTML = inner_html;
	
	 
}
 
 
function extractValue(str)
{
	//Something else might be stored in the value.
	//Get everything after special character "gh--"
	if ( str.indexOf(":") >=0 )
	{
		var result = str.substring(str.indexOf(":") + 1);
		//alert(result);
		return  result;
	}
	else
	{
		return str;
	}
}
 
 
 
 
function money(value)

{

       var thousandsSeperator = ",";

       var decimalSeperator = ".";

       var precision = 2;

      

       var intVal = parseInt( value ) + "";

      

       var finalVal = "";

       var j = 0;

      

       for ( var i = intVal.length; i >= 0; i-- )

       {

              finalVal = ( j % 3 == 0 && j > 0 && i > 0 ? thousandsSeperator : "" ) + intVal.charAt(i) + finalVal;

              j++

       }

      

       var decVal = "";

 

       if ( precision > 0 )

       {

              if ( intVal != value )

                     decVal = ( value + "" ).substr( ( value + "" ).lastIndexOf(".") + 1 );

 

              while( decVal.length < precision )

                     decVal += "0";

                    

              if ( decVal.length > precision )

                     decVal = Math.round( Number(decVal) / Math.pow( 10, decVal.length - precision ) );

                    

              decVal = decimalSeperator + decVal;

       }

      

       return finalVal + decVal;

} 
 
 
function copyAddress()
{
	$add = document.getElementById('address');
	$del_add = document.getElementById('delivery_address');
	$del_add.value = $add.value; 
}

function flipOver(image) 
{
	document.getElementById('vancity').src = "http://www.tidytrailers.com/images/" + image; return true;
}
function original() 
{
	document.getElementById('vancity').src = "http://www.tidytrailers.com/images/map_main.jpg"; return true;
}

function toggleElement()
{
	var myCheckbox = document.getElementById("curb_box");
	 
	var optionalDiv1 = document.getElementById("optional1");
	var optionalDiv2 = document.getElementById("optional2");
	
	if (myCheckbox.checked == true)
	{
		optionalDiv1.style.display = 'block'; //does work. 
		optionalDiv2.style.display = 'inline-block'; //does work. 
		//optionalDiv1.setAttribute("style", "display = 'block'"); //does not work.
	}
	else
	{
		optionalDiv1.style.display = 'none';
		optionalDiv2.style.display = 'none';
	} 
}

function removeElement()
{
	//remove
	//
	var childDiv1 = document.getElementById("new_box_div");
	var childDiv2 = document.getElementById("new_label_div");
	childDiv1.parentNode.removeChild(childDiv1);
	childDiv2.parentNode.removeChild(childDiv2);
	
	//replace
	var parentDiv = document.getElementById('curb');
    var newInputDiv = document.createElement('div');
	newInputDiv.setAttribute('class', "form_box_left");
	newInputDiv.setAttribute('onclick', "addElement();");
	newInputDiv.innerHTML = "<INPUT TYPE=CHECKBOX onclick='removeElement();'>"   
	var curb_first_child = document.getElementById('curb_first_child');
	newInputDiv.setAttribute('id', "curb_first_child");
	parentDiv.replaceChild(newInputDiv, curb_first_child);	
}

function addElement() 
{
	var parentDiv = document.getElementById('curb');
	
	var newInputDiv = document.createElement('div');
	newInputDiv.setAttribute('class', "form_box_left");
	newInputDiv.setAttribute('onclick', "removeElement();");
	newInputDiv.innerHTML = "<INPUT TYPE=CHECKBOX checked='checked' onclick='removeElement();'>"   
	var curb_first_child = document.getElementById('curb_first_child');
	newInputDiv.setAttribute('id', "curb_first_child");
	parentDiv.replaceChild(newInputDiv, curb_first_child);
	
	//newInputDiv.style.display=None
	 
	//Add the new div
	var parentCurb = document.getElementById("parentCurb");
    var newBoxDiv = document.createElement('div');
	newBoxDiv.setAttribute('id', "new_box_div");
	newBoxDiv.setAttribute('class', "form_box_left");
	newBoxDiv.innerHTML = "<INPUT TYPE=CHECKBOX >";
	//parentDiv.appendChild(newBoxDiv);
	parentCurb.appendChild(newBoxDiv);
	
	var newLabelDiv  = document.createElement('div');
	newLabelDiv.setAttribute('id', "new_label_div");
	newLabelDiv.setAttribute('class', "form_box_right");
	newLabelDiv.innerHTML = "<strong>Customer Convenience Trailer Drop     $15.00</strong><br/> * per 24 hour period <br/><br/>";
	//parentDiv.appendChild(newLabelDiv);
	parentCurb.appendChild(newLabelDiv);
	 
}
