
// operate when starting here
function clearBudget() {
if (document.getElementById('amount_available').value=='Start Here') {
document.getElementById('amount_available').value='';
document.getElementById('amount_available').style.backgroundColor='ffffff';
document.getElementById('amount_available').style.color='000000';
document.getElementById('amount_available').style.textAlign='right';
}
}

// operate when starting here
function enableAll() {
var budget=document.getElementById('amount_available').value;
var num = budget.toString().replace(/\$|\,/g,'');
num=fixQuote(num);


if(isNaN(num)) {

document.getElementById('amount_available').value='Start Here';
document.getElementById('amount_available').style.backgroundColor='cae9c7';
document.getElementById('amount_available').style.color='ff0000';
document.getElementById('amount_available').style.textAlign='center';

for(i=0; i<document.calculate.elements.length; i++)
{
document.calculate.elements[i].disabled=true;
}

document.calculate.amount_available.disabled=false;

alert('The value you entered is not a number! \nPlease enter a valid dollar amount.');
}


else if (budget==0) {

document.getElementById('amount_available').value='Start Here';
document.getElementById('amount_available').style.backgroundColor='cae9c7';
document.getElementById('amount_available').style.color='ff0000';
document.getElementById('amount_available').style.textAlign='center';

for(i=0; i<document.calculate.elements.length; i++)
{
document.calculate.elements[i].disabled=true;
}

document.calculate.amount_available.disabled=false;

alert('Please enter a starting budget greater than $0.00');
}

//if blank return to Default
else  {

for(i=0; i<document.calculate.elements.length; i++)
{
document.calculate.elements[i].disabled=false;
}

var newtotal=num*1;
document.getElementById("amount_available").value=formatCurrency(newtotal);


calcBudget();

with (document.calculate) {
fetchData('scripts/holiday_spending_actions.cfm?sid=' + session_id.value + '&type=f&fname=amount_available&fvalue=' + newtotal,5,'response_field');
                          }

}}



var win= null;
function OpenCentered(mypage,myname){
var winl = (screen.width-780)/2;
var wint = (screen.height-590)/2;
var settings ='height='+590+',';
settings +='width='+780+',';
settings +='top='+wint+',';
settings +='left='+winl+',';
settings +='scrollbars=1,';
settings +='resizable=yes,status=0';

win=window.open(mypage,myname,settings);
if(parseInt(navigator.appVersion) >= 4){}
} 

function MM_openBrWindow(theURL,winName,features) { 
window.open(theURL,winName,features);
}




function cnfDel(r) {
if (confirm('Delete this recipient and gift?')) {

var trow='row_' + r;
var estimatetd='estimate_' + r;
var pricetd='price_' + r;

document.getElementById(trow).style.display='none';
document.getElementById(estimatetd).value=0;
document.getElementById(pricetd).value=0;

fetchData('scripts/holiday_spending_actions.cfm?sid=' + document.calculate.session_id.value + '&type=d&row=' + r,5,'response_field');



}
}




// If number is 0 when clicking into it
function clearBox(box) {
 if(box.value==0) {
	 box.value = "";
	 }
}



function isText(txtbox) {
		var fname=txtbox.name;
		var fvalue=txtbox.value; 
		
with (document.calculate) {
fetchData('scripts/holiday_spending_actions.cfm?sid=' + session_id.value + '&type=v&fname=' + fname + '&fvalue=' + urlencodedformat(fvalue),5,'response_field');
                          }
}




 function isBlank(ipbox) {
 var numtocheck=fixQuote(ipbox.value);

		 if(numtocheck=="") {
	 	 	 ipbox.value = ipbox.defaultValue;
	 	 }
		 else if (isNaN(numtocheck)) {
 alert('The Value You Entered is NOT A VALID NUMBER!\nDo not use dollar signs ($) or commas (,) in your amounts .');
ipbox.style.backgroundColor='pink';
	 	 	 ipbox.value = ipbox.defaultValue;
 return false;
         }
		 else {
		 ipbox.style.backgroundColor='white';
		var fname=ipbox.name;
		var fvalue=numtocheck;
		 
with (document.calculate) {
fetchData('scripts/holiday_spending_actions.cfm?sid=' + session_id.value + '&type=v&fname=' + fname + '&fvalue=' + fvalue,5,'response_field');
                          }
		 
		 }
}

 function isBlankFixed(ipbox) {
 
  var numtocheck=fixQuote(ipbox.value);

		 if(numtocheck=="") {
	 	 	 ipbox.value = ipbox.defaultValue;
	 	 }
		 else if (isNaN(numtocheck)) {
 alert('The Value You Entered is NOT A VALID NUMBER!\nDo not use dollar signs ($) or commas (,) in your amounts .');
ipbox.style.backgroundColor='pink';
	 	 	 ipbox.value = ipbox.defaultValue;
 return false;
         }
		 else {
		 ipbox.style.backgroundColor='white';
		var fname=ipbox.name;
		var fvalue=numtocheck;
		 
with (document.calculate) {
fetchData('scripts/holiday_spending_actions.cfm?sid=' + session_id.value + '&type=f&fname=' + fname + '&fvalue=' + fvalue,5,'response_field');
                          }
		 
		 }
}


function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
//  
}




function listappend(list,value,delimiters) {
if(!delimiters){var delimiters=','}
_TempListFirstSplitArray = list.split(delimiters)
added = _TempListFirstSplitArray.splice(_TempListFirstSplitArray.length,0,value)
_TempStringToReturn = _TempListFirstSplitArray.join(delimiters)
return _TempStringToReturn
}





// Beging Calculating budget
function calcBudget(action) {


var est=0;
var budg=0;


var rcntname='iteration_tbody';
var giftcnt=document.getElementById(rcntname).value;


var i=1;
for (i=1;i<=giftcnt;i++)
{
var est_fname='estimate_' + i;
var price_fname='price_' + i;
var estv=fixQuote(document.getElementById(est_fname).value);
var pfnv=fixQuote(document.getElementById(price_fname).value);


est=est + (estv*1);
budg=budg + (pfnv*1);

}


document.getElementById('gift_estimate').value=formatCurrency(est);
document.getElementById('gift_price').value=formatCurrency(budg);
document.getElementById('gift_estimate2').value=formatCurrency(est);
document.getElementById('gift_price2').value=formatCurrency(budg);

var miscest = (document.getElementById('b_decorations').value*1)+(document.getElementById('b_tree').value*1)+(document.getElementById('b_menorah').value*1)+(document.getElementById('b_Kinara').value*1)+(document.getElementById('b_candles').value*1)+(document.getElementById('b_cards').value*1)+(document.getElementById('b_Stamps').value*1)+(document.getElementById('b_GiftWrap').value*1)+(document.getElementById('b_entertainment').value*1)+(document.getElementById('b_donations').value*1)+(document.getElementById('b_travel').value*1);
  
var mispaid = (document.getElementById('p_decorations').value*1)+(document.getElementById('p_tree').value*1)+(document.getElementById('p_menorah').value*1)+(document.getElementById('p_Kinara').value*1)+(document.getElementById('p_candles').value*1)+(document.getElementById('p_cards').value*1)+(document.getElementById('p_Stamps').value*1)+(document.getElementById('p_GiftWrap').value*1)+(document.getElementById('p_entertainment').value*1)+(document.getElementById('p_donations').value*1)+(document.getElementById('p_travel').value*1);


document.getElementById('misc_estimate').value=formatCurrency(miscest);
document.getElementById('misc_paid_amnt').value=formatCurrency(mispaid);

var estTotal=(est*1)+(miscest*1);
var paidTotal=(budg*1)+(mispaid*1);

document.getElementById('estimate').value=formatCurrency(estTotal);
document.getElementById('paid').value=formatCurrency(paidTotal);


var pot=fixQuote(document.getElementById('amount_available').value)
var amntused=paidTotal;
var remaing=pot-amntused;

document.getElementById('amnt_used').value=formatCurrency(amntused);
document.getElementById('amnt_remaining').value=formatCurrency(remaing);


if (remaing > 0) {
document.getElementById('amnt_remaining').style.color='green';
document.getElementById('paid').style.color='green';
}
else if (remaing < 0) {
document.getElementById('amnt_remaining').style.color='red';
document.getElementById('paid').style.color='red';
}
else {
document.getElementById('amnt_remaining').style.color='black';
document.getElementById('paid').style.color='black';
}

with (document.calculate) {
fetchData('scripts/holiday_spending_actions.cfm?sid=' + session_id.value + '&gift_estimate=' + fixQuote(gift_estimate.value) + '&gift_price=' + fixQuote(gift_price.value) + '&misc_estimate=' + fixQuote(misc_estimate.value) + '&misc_paid_amnt=' + fixQuote(misc_paid_amnt.value) + '&estimate=' + fixQuote(estimate.value) + '&paid=' + fixQuote(paid.value) + '&type=m' + '&amntremang=' + fixQuote(amnt_remaining.value) + '&amntused=' + fixQuote(amnt_used.value),5,'response_field');
}



}
// end Calculating budget

// Url encode strings
function urlencodedformat(myString) {
return escape(myString);
}

function fixQuote(number) {
 // replace all the single, double quotes:
var fvalue = number;
var ReplacedNumber = fvalue.replace(/\,/g,'');
var ReplacedNumber2= ReplacedNumber.replace(/\$/g,'');
var ReplacedNumber3= ReplacedNumber2.replace(/\s/g,'');
return ReplacedNumber3;

}

//function to add rows to tbody
function addRowToTable(tb)
{

var rowname='iteration_tbody';
  
var crntRn=document.getElementById(rowname).value
var nextrow=(crntRn*1+1);
document.getElementById(rowname).value=nextrow;

 var tbFname='tb_' + tb;
 
 
  var tbl = document.getElementById(tbFname);

  var row = tbl.insertRow(crntRn);
      row.id = 'row_' + nextrow;
  
  // 1 Recipient
  var cellRight0 = row.insertCell(0);
    cellRight0.className='data_td';
  var el0 = document.createElement('input');
  el0.type = 'text';
  el0.name = 'recipient_'  + nextrow;
  el0.id = 'recipient_' + nextrow;
  el0.className='input_ltext';
  el0.size = 8;
  el0.value='';
  el0.onchange = function() {isText(this);};
  cellRight0.appendChild(el0);  

 
  // 2 Gift number no form 
  var cell1 = row.insertCell(1);
  cell1.innerHTML=nextrow;
  cell1.className='gft_num';

  
  // Enter Gift Name
  var cellRight2 = row.insertCell(2);
    cellRight2.className='data_td';
  var el2 = document.createElement('input');
  el2.type = 'text';
  el2.name = 'gift_' + nextrow;
  el2.id = 'gift_'  + nextrow;
  el2.className='input_ltext';
  el2.size = 8;
  el2.value='';
  el2.onchange = function() {isText(this);};
  cellRight2.appendChild(el2);  
  
  
  // Enter Gift size
  var cellRight3 = row.insertCell(3);
    cellRight3.className='data_td';
  var el3= document.createElement('input');
  el3.type = 'text';
  el3.name = 'size_' + nextrow;
  el3.id = 'size_' + nextrow;
  el3.className='input_ctext';
  el3.size = 8;
  el3.value='';
  el3.onchange = function() {isText(this);};
  cellRight3.appendChild(el3);
  
  // Enter Gift Color
  var cellRight4 = row.insertCell(4);
    cellRight4.className='data_td';
  var el4= document.createElement('input');
  el4.type = 'text';
  el4.name = 'color_'  + nextrow;
  el4.id = 'color_'  + nextrow;
  el4.className='input_ctext';
  el4.size = 8;
  el4.value='';
  el4.onchange = function() {isText(this);};
  cellRight4.appendChild(el4);
  
  // Enter Gift Store
  var cellRight5 = row.insertCell(5);
    cellRight5.className='data_td';
  var el5= document.createElement('input');
  el5.type = 'text';
  el5.name = 'store_' + nextrow;
  el5.id = 'store_' + nextrow;
  el5.className='input_ltext';
  el5.size = 8;
  el5.value='';
  el5.onchange = function() {isText(this);};
  cellRight5.appendChild(el5);
  
  
  // Enter Gift Estimate
  var cellRight6 = row.insertCell(6);
    cellRight6.className='data_td';
  var el6= document.createElement('input');
  el6.type = 'text';
  el6.name = 'estimate_' + nextrow;
  el6.id = 'estimate_' + nextrow;
  el6.className='input_number';
  el6.size = 8;
  el6.value='0.00';
  el6.onfocus = function() {clearBox(this); };
  el6.onblur = function() {isBlank(this); calcBudget();};
  cellRight6.appendChild(el6);
  
  
  // Enter Gift cost
  var cellRight7 = row.insertCell(7);
    cellRight7.className='data_td';
  var el7= document.createElement('input');
  el7.type = 'text';
  el7.name = 'price_' + nextrow;
  el7.id = 'price_' + nextrow;
  el7.className='input_number';
  el7.size = 8;
  el7.value='0.00';
  el7.onfocus = function() {clearBox(this); };
  el7.onblur = function() {isBlank(this); calcBudget();};
  cellRight7.appendChild(el7);
   

  // Enter Gift cost
  var cellRight8 = row.insertCell(8);
    cellRight8.className='data_td';
  var el8= document.createElement('input');
  el8.type = 'button';
  el8.name = 'del_' + nextrow;
  el8.id = 'del_' + nextrow;
  el8.className='del_btn';
    el8.value='X';
  el8.onclick = function() {cnfDel(nextrow); };
  cellRight8.appendChild(el8);
    
  var addBtn='add_r';

  //make exception for first update on save budgets
  
  var addBtnTxt='Add Recipient ' + ((1*nextrow)+1);
  document.getElementById(addBtn).value=addBtnTxt;

}





function filterData(pageRequest,objectID){
	var object = document.getElementById(objectID);
	if (pageRequest.readyState == 0 || pageRequest.readyState == 1 || pageRequest.readyState == 2 || pageRequest.readyState == 3)
		object.innerHTML = '<img src="../images/ajax_loader.gif" alt="Loading" border="0">';
	if (pageRequest.readyState == 4)	{
	    if (pageRequest.status==200) {object.innerHTML = pageRequest.responseText ;
}
		else if (pageRequest.status == 404) object.innerHTML = 'Sorry, that information is not currently available.';
		else object.innerHTML = 'Sorry, there seems to be some kind of problem.';
	}
}




function fetchData(url,dataToSend,objectID){
	var pageRequest = false
	if (window.XMLHttpRequest) {
		pageRequest = new XMLHttpRequest()
	}
	else if (window.ActiveXObject){ 
		try {
			pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e) {
			try{
				pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	else return false
	pageRequest.onreadystatechange=function() {	
		filterData(pageRequest,objectID)
}
	if (dataToSend) {		
		var sendData = 'sendData=' + dataToSend;
		pageRequest.open('POST',url,true);
    pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   	pageRequest.send(sendData);
	}
	else {
		pageRequest.open('GET',url,true)
		pageRequest.send(null)	
	}
}





function confirmDelete() {
if (confirm('Press OK to DELETE your Holiday Spending Budget.')) {
window.location='holidayspending.cfm?delid=' + document.calculate.session_id.value + '&bdgid=' + document.calculate.budget_id.value;
}
}



