// Client Payment Form Javascript
// by Raina Mason from MouseUp Multimedia
// http://www.mouseup.com.au

  var cost_soiltest = 0;
  var cost_survey = 0;
  var cost_designpackage = 0;
  var cost_NEsurcharge = 0;
  
  var m_curBasePrice = 0;
  var m_iNumUnits = 0;
  var m_curExtraUnitPrice = 450;
  
  var m_curAdjustment = 0;
  
  
  
  var m_curStandardDesignPrice = 200;
  var m_curCustomDesignSlabOnGroundPrice = 650;
  var m_curCustomDesignTwoStoryPrice = 1200;
  
  
  var m_curSoilTestPrice = 550;
  var m_curContourSurveyPrice = 550;
  var m_curContourSurveyAHDPrice = 400;
  var m_curNESurchargePrice = 350;
  
  
  // functions to show/hide the divs  
  function changeDiv(the_div,the_change)
  {
    var the_style = getStyleObject(the_div);
    if (the_style != false)
    {
      the_style.display = the_change;
    }
  }
  
  function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
    if(document.getElementById && document.getElementById(objectId)) {
	  return document.getElementById(objectId).style;
    }
    else if (document.all && document.all(objectId)) {  
	  return document.all(objectId).style;
    } 
    else if (document.layers && document.layers[objectId]) { 
	  return document.layers[objectId];
    } else {
	  return false;
    }
  }
  
  function calculateCost() {
    
    //if(document.forms[0].elements['chkIncludeServices'].checked)
    //{
    //    //include Services is checked so add all services to total
    //    var cost_total = cost_soiltest + cost_survey + cost_designpackage + cost_NEsurcharge +(m_iNumUnits * m_curExtraUnitPrice) + m_curAdjustment;
	//}
	//else
	    var cost_total = m_curAdjustment;
	
	
	//document.forms[0].elements['amount'].value=cost_total;
	document.getElementById('displaycost').innerHTML = '' + cost_total;
  }
  
  //------------------------
  //-- Call this function whenever user changes ANY checkbox/Radio button.
  //------------------------
  function updateForm(){
  
    //if(document.forms[0].elements['chkIncludeServices'].checked)
    //    changeDiv('ServicesDiv','block')
    //else
        changeDiv('ServicesDiv','none')
        
    if(isNaN(parseInt(document.forms[0].elements['txtAdjustment1'].value)))
        document.forms[0].elements['txtAdjustment1'].value = 0;
        
    m_curAdjustment = parseInt(document.forms[0].elements['txtAdjustment1'].value);
  
    
        
    
          
     
    
     calculateCost();   
  
  }
  
  
  // handle soiltest
  function updateSoilTest() {
    if (cost_soiltest==0) {
	  cost_soiltest=550;
	} else {
	  cost_soiltest=0;
	}
	calculateCost();
  }
  
  // handle types of survey
  function update_survey(cost) {
    
    cost_survey = 0;
  
    if(document.forms[0].elements['Survey'].checked)
        cost_survey = parseInt(m_curContourSurveyPrice);
        
    if(document.forms[0].elements['Survey2'].checked)
        cost_survey += parseInt(m_curContourSurveyAHDPrice);
        
  
    //cost_survey=cost;
	calculateCost();
  }
  
  
  // handle design package 
  function updatedesignpackage(cost) {
    
    m_curBasePrice = cost;
    cost_designpackage = cost;
		calculateCost();  
  }
  
  function handlemulti() {
    var multiple = parseInt(document.forms[0].elements['unitnumber'].value);
    
    m_iNumUnits = multiple;
    
    
		//cost_designpackage = (450 * multiple);
		calculateCost();
  }  
  
  
  // postcode / new England surcharge handling.
  function checkPostcode() {
    
    //No longer use this function to add EO surcharge.
    //Client is now required to tick the EO surchard option as directed by their sales rep.
    
    /*
    
	var theStrPostcode = document.forms[0].elements['b_postcode'].value;
	var thePostcode = parseInt(theStrPostcode);
	// checks first three digits. If they are in range 234 to 237 then surcharge applies
    var range = thePostcode - 2340;
	// then if (range>0)&(range<33) it will be in New England.  
	if ((range>=0)&&(range<33)) {
	  // in New England postcodes
	  cost_NEsurcharge = 350;
	  changeDiv('nes','block');
	  fillinfield('item_name_6','New England Surcharge');
	  fillinfield('amount_6','350');
	  calculateCost();
	} else {
      // not in NE postcodes
      cost_NEsurcharge = 0;
      changeDiv('nes','none');
      fillinfield('item_name_6','New England Surcharge - N/A');
      fillinfield('amount_6','0');
      calculateCost();
    }
    
    */
  } 
  
  // form checking
  
  function checknsend() {
	  if (noMajorBlanks()){
		  // start setting the variables to pass to paypal.
		 var landdetails = '';
		 landdetails = landdetails + 'Client: ' + getValue('ClientName') + '. Contact: ' + getValue('Contact')+ '. Lot: ' + getValue('b_Lot') + ', ';
		 if (!(getValue('b_DP')=='')){
            landdetails = landdetails + 'DP: ' + getValue('b_DP') + ', ';
         }
		 landdetails = landdetails + 'Sales Consultant: ' + getValue('b_Sales');
		 setValue('os0_1',landdetails);
		 var landaddress = '';
		 landaddress = landaddress + getValue('b_Str_No') + ' ' + getValue('b_address') + ' ' + getValue('b_city') + ' ' + getValue('b_postcode');
		 setValue('os1_1',landaddress);
		 
		 
		 //Item number that will be incremented for each item.
		 var iItemNumber = 2;
		 
		 
		 
		// var blnIncServices = document.forms[0].elements['chkIncludeServices'].checked;


        

		//Price Adjustment.
		//setValue('item_name_' + iItemNumber,'Price Adjustment');
		setValue('amount_' + iItemNumber, m_curAdjustment);
		setValue('quantity_' + iItemNumber, '1');
		setValue('on0_' + iItemNumber, 'Description');
		setValue('os0_' + iItemNumber, document.forms[0].elements['txtDescription1'].value);
		 
		iItemNumber++;
		 
		 
		// for(var i = iItemNumber; i <=8;i++)
		//    RemoveZeroQuantitys(i);
		       
	    
	
		 
		 
		 // send the form		  
		 document.forms[0].elements['hiddensubmitbutton'].click();
		 
	  }
  }
  
  function RemoveZeroQuantitys(ItemNum){
  
      //var blnDisabled = false;
      //if(getValue('quantity_7') * 1 <= 0 || getRadioValue(0,'Drafting')=='Standard' ){
      //  blnDisabled = true;
      //}
      //else{
      //  blnDisabled = false;
      //}
      
      var blnDisabled = true;
      
      var elementName = 'item_name_' + ItemNum;
      document.forms[0].elements[elementName].disabled=blnDisabled;
      
      elementName = 'amount_' + ItemNum;
      document.forms[0].elements[elementName].disabled=blnDisabled;
      
      elementName = 'quantity_' + ItemNum;   
      document.forms[0].elements[elementName].disabled=blnDisabled;
      
      elementName = 'on0_' + ItemNum;  
      document.forms[0].elements[elementName].disabled=blnDisabled;
        
      elementName = 'os0_' + ItemNum;
      document.forms[0].elements[elementName].disabled=blnDisabled;
        
        

  }
  
  function CalcAdditionalUnits()
  {
	
	return value;
  }
  
	
 
  function getValue(theField) {
	  theValue = document.forms[0].elements[theField].value;
	  return theValue;
  }
  
  function getRadioValue(frmName,radioGroup) {
	  var group=document.forms[frmName].elements[radioGroup];
	  for (var i=0;i<=group.length;i++) {
		  try {
			  if (group[i].checked) {
				  return group[i].value;
			  }
		  } catch(err) {
		  }
	  }
  }
  
  function setValue(theField, theValue) {
	  document.forms[0].elements[theField].value=theValue;
  }
  
  function changeFocus(theField) {
	  document.forms[0].elements[theField].focus();
	  document.forms[0].elements[theField].style.backgroundColor='#FFCC66';
  }
  
  function radioChecked(frmName,radioGroup) {
	var group=document.forms[frmName].elements[radioGroup];
	for (var i=0;i<=group.length;i++) {
		try {

			if (group[i].checked) {
				return true;
			}
		} catch(err) {
		}
	}
	return false;
  }
  
  function noMajorBlanks() {
	  if (getValue('ClientName')=='') {
		  changeFocus('ClientName');
		  alert('Please fill in the Client Name and resubmit the form.');
		  return 0;
	  } 
	  
	  if (getValue('address1')=='') {
		  changeFocus('address1');
		  alert('Please fill in the Postal Address (street address) and resubmit.');
		  return 0;  
	  }
	  
	  if (getValue('city')=='') {
		  changeFocus('city');
		  alert('Please fill in the Postal town/city and resubmit.');
		  return 0;
	  } 
	  
	  if (getValue('zip')==''){
		  changeFocus('zip');
		  alert('Please enter your postcode and resubmit');
		  return 0;
	  }
	  	  
	  if (getValue('email')==''){
		  changeFocus('email');
		  alert('Please enter your email address and send the form again.');
		  return 0;
	  }
	  
	  
	  //if amount is blank change to 0.
	  if(isNaN(parseInt(document.forms[0].elements['txtAdjustment1'].value)))
        document.forms[0].elements['txtAdjustment1'].value = 0;
	  
	  if (parseInt(document.forms[0].elements['txtAdjustment1'].value)<=0){
		  changeFocus('txtAdjustment1');
		  alert('Please enter a payment greater than $0.');
		  return 0;
	  }
	  
	  if (getValue('txtDescription1')==''){
		  changeFocus('txtDescription1');
		  alert('Please enter a description.');
		  return 0;
	  }
	  
	  
	  
	  /*
	  if (getValue('b_Lot')==''){
		  changeFocus('b_Lot');
		  alert('Please enter the Lot number of the land.');
		  return 0;
	  } 
	  
	  if (getValue('b_address')==''){
		  changeFocus('b_address');
		  alert('Please enter the street address of the land.');
		  return 0;
	  }  
	  
	  if (getValue('b_city')==''){
		  changeFocus('b_city');
		  alert('Please enter the town/city where the land is located.');
		  return 0;
	  }
	  
	  if (getValue('b_postcode')==''){
		  changeFocus('b_postcode');
		  alert('Please enter the postcode where the land is located.');
		  return 0;
	  }
	  */
	  
	  //if (!radioChecked(0,'Drafting')) {
		//  //document.forms[0].elements['Drafting'].focus();
		//  alert('Please choose either custom or standard drafting package.');
		//  return 0;
	  //}
	  
  	  if (!document.forms[0].elements['signature'].checked){
		  // document.forms[0].elements['signature'].focus();
		  changeFocus('signature');
		  alert('You must check the box to signify you have agreed to the Preliminary Agreement');
		  return 0;
	  }

	  //if (!document.forms[0].elements['financing'].checked){
		//  // document.forms[0].elements['financing'].focus();
		//  alert('You must check the box if you disagree to being contacted by Integrity Financing');
		//  return 0;
	  //}
	  // only gets to this if there are no major blanks 
	  return 1;						  
  }
  
	  
  function fillinfield(the_name, the_value) {
    document.forms[0].elements[the_name].value = the_value;	  
  }
  