// JavaScript Document

// changes the the name of the user and places it in front of the the short statement for a preview
function statement_preview_fname(t)
{	
	//var f = document.getElementById('fname');
	//f.innerHTML = t;
}
function statement_preview_lname(t)
{
	//var f = document.getElementById('lname');
	//f.innerHTML = t;
}

// limit number of characters
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

// clears or replaces "is/are..." in web forms
function clear_input(name)
{
	// store reference
	var f = document.getElementById('member_statement_short');
	
	if (f.value == 'is/are...')
	{
		f.value = '';
		f.className = 'double dark';
	}
	else if (f.value == '')
	{
		f.value = 'is/are...';
		f.className = 'double light';
	}
}


// search bar
function submitSearch()
{
	// store value
	var str = document.getElementById('q').value;
	// clean string
	str = str.replace("\n", "");
	str = str.replace("\r", "");
	str = str.replace("\t", "");
	// perform a global case-insensitive search for all spaces and replace with "-" for the url
	q = str.replace(/ /gi, "-");
	window.location.href='/search/' + q;	
	return false;
}


function validateFileUpload() 
{
	/* what a pain...
	var current_field = "";
	var str = "";
	var len = 0;
	var i=0;
	
	// loop through each
	for (i=1; i<10; i++)
	{
		current_field = "file_caption" + i;
		str = file_upload_form.elements[current_field].value;
		len = str.length;
		
		if (len >= 255)
		{
			alert("Only 255 characters are allowed in caption #" + i);	
			return false;
		}
		else
		{
			return true;
		}
	}
*/
}



// called from billing to update a date
function updateBillingDate() 
{	
	if (document.getElementById('billing_period_end').value == '')
	{
		var freq	= document.getElementById('billing_frequency'); 
	var end 	= document.getElementById('billing_period_end'); 
		var start 	= document.getElementById('billing_period_start'); 
		var st_arr 	= start.value.split('-');
		
		if (freq.value == 'annual') { numyears = 1; }
		else if (freq.value == 'two-years') { numyears = 2; }
		else if (freq.value == 'five-years') { numyears = 5; }
		
		var dateObj = new Date(Number(st_arr[0])+numyears, Number(st_arr[1]), Number(st_arr[2]));
		
		var y = dateObj.getFullYear();
		if (dateObj.getMonth() < 10) { var m = '0'+ dateObj.getMonth(); } else { var m = dateObj.getMonth(); }
		if (dateObj.getDate() < 10) { var d = '0'+ dateObj.getDate(); } else { var d = dateObj.getDate(); }
		
		end.value = y +'-'+ m +'-'+ d;
	}
}
	
	
function copyBillingRecord(billing_record)
{

	//alert(billing_record);	
	
	var billing_arr = billing_record.split("^");
	document.getElementById('billing_type').value = 'invoice_new';
	document.getElementById('billing_amount').value = billing_arr[1];
	document.getElementById('billing_plan').value = billing_arr[2];
	document.getElementById('billing_frequency').value = billing_arr[3];
	document.getElementById('billing_period_start').value = billing_arr[4];
	document.getElementById('member_id').value = billing_arr[5];	
	document.getElementById('billing_notes').value = 'Hi, the new invoice system is now online. Let me know if you have any questions. Owen';	
	updateBillingDate();
}

/*

echo $list['billing_id'] . "^";
echo $list['billing_amount'] . "^";
echo $list['billing_plan'] . "^";
echo $list['billing_frequency'] . "^";
echo $newdate . "^";
echo $name . "^";

*/
	
