var crHandler = function(responseText, responseStatus, responseXML) {
	if (responseStatus==200) {
		if (responseText == 'true') {
			var cur_step = getEleVal('cr_step');
			cr_goToStep((parseInt(cur_step) + 1),cur_step);
		} else {
			// error
			modal({
					content	: 'straight',
					straight_content : responseText,
					title_text : 'An error has occurred',
					width	: 400,
					height	: 200,
					title	: true
				});
		}
	}	
}

function cr_print_report() {

	modal({
			content	: 'iframe',
			iframe_url : '/xml/catch_report.xml.php?printreport',
			title_text : 'Print Catch Report',
			width	: 600,
			height	: 400,
			title	: true
		});
}

function cr_goToStep(to_step, from_step) {
	showLoading();
	var ajax_a = new ajaxObject('/xml/catch_report.xml.php');
	ajax_a.callback = function(responseText, responseStatus, responseXML) {
		if (responseStatus==200) {
			hideLoading();
			if (responseText == 'false') {
				alert('Failed Getting Next Step:' + responseText);
			} else {					
				document.getElementById('cr_steps').innerHTML = responseText
				
				// eval js
				var scripts = document.getElementById('cr_steps').getElementsByTagName("script");
				for (var i=0; i<scripts.length; i++) {eval(scripts[i].text);}
			}
		}
	}
	ajax_a.update('getStep&step='+to_step);
}

function cr_start_over() {
	if (confirm('Are you sure you want to start this catch report over?')) {
		document.crFrmReset.submit();
	}
}

function cr_getBoatInfo(boat_data) {
	// boat type
	var data = boat_data.getAttribute('data').split(':');
	var data_type = data[0];
	var data_length = data[1];
	
	
	//set boat type
	boat_type_obj = document.getElementById('cr_boat_type');
	for (i=0; i < boat_type_obj.length; i++) {
		if (boat_type_obj[i].value == data_type) {
			boat_type_obj.selectedIndex = i;
			break;
		}
	}

	//set boat size
	boat_size_obj = document.getElementById('cr_boat_size');
	for (i=0; i < boat_size_obj.length; i++) {
		var opt_data = boat_size_obj[i].getAttribute('data').split(':');
		var opt_data_min = opt_data[0];
		var opt_data_max = opt_data[1];
		
		if (opt_data_min <= data_length && opt_data_max >= data_length) {
			boat_size_obj.selectedIndex = i;
			break;
		}
	}

}

function loadAnglers(type) {
	var ajax_a = new ajaxObject('/xml/catch_report.xml.php');
	ajax_a.callback = function(responseText, responseStatus, responseXML) {
		if (responseStatus==200) {		
			document.getElementById('cr_anglers').innerHTML = responseText
		}
	}
	ajax_a.update(type);
}

function loadMeatfish() {
	var ajax_a = new ajaxObject('/xml/catch_report.xml.php');
	ajax_a.callback = function(responseText, responseStatus, responseXML) {
		if (responseStatus==200) {		
			document.getElementById('cr_meatfish').innerHTML = responseText
		}
	}
	ajax_a.update('getMeatfish');
}

function cr_delAngler(idx) {
	if (confirm('Are you sure you want to delete this angler?')) {
		var ajax_a = new ajaxObject('/xml/catch_report.xml.php');
		ajax_a.callback = function(responseText, responseStatus, responseXML) {
			if (responseStatus==200) {		
				loadAnglers('getAnglers');
			}
		}
		ajax_a.update('delAngler&idx='+idx);
	}
}

function cr_delBillfish(ang_idx, bf_idx) {
	if (confirm('Are you sure you want to delete this billfish?')) {
		var ajax_a = new ajaxObject('/xml/catch_report.xml.php');
		ajax_a.callback = function(responseText, responseStatus, responseXML) {
			if (responseStatus==200) {		
				loadAnglers('getAnglersBillfish');
			}
		}
		ajax_a.update('delBillfish&ang_idx='+ang_idx+'&bf_idx='+bf_idx);
	}
}

function cr_getForm(formname, idx, idx2, title_override) {
	var url = '/xml/catch_report.xml.php?action=getForm&frm='+formname;
	var pre_title = 'Add ';
	if (idx) {
		url+='&idx='+idx;
		pre_title = 'Edit ';
	}
	if (idx2) url+='&idx2='+idx2;
	
	var title = pre_title+formname.substr(0, 1).toUpperCase() + formname.substr(1);
	
	if (title_override) title = title_override;
	
	modal({
		content	: 'iframe',
		iframe_url : url,
		title_text : title,
		width	: 450,
		height	: 350,
		title	: true,
		modal	: true
	});
}

function getAttr(ele, attrName) {
	for( var x = 0; x < ele.attributes.length; x++ ) {
		if( ele.attributes[x].nodeName.toLowerCase() == 'attrName' ) {
			return ele.attributes[x].nodeValue;
		}
	}	
}

function autoSuggestFill_angler(thisValue, angler_id, inputFieldId, suggestionsDivId) {
	//alert(thisValue+':'+angler_id+':'+inputFieldId+':'+suggestionsDivId);
	$('#'+inputFieldId).val(thisValue);
	cr_get_angler_info(angler_id);
	setTimeout("$('#"+suggestionsDivId+"').fadeOut();", 300);
}

function cr_get_angler_info(angler_id) {
	var url = '/xml/catch_report.xml.php';
	
	var ajax_a = new ajaxObject(url);
	ajax_a.callback = function(responseText, responseStatus, responseXML) {
		if (responseStatus==200) {
			if (responseText != '') {
				//0id,1member_id,2name,3first_name,4last_name,5address,6city,7state,8zip,9gender,10:16_and_under AS sixteen,11member
				angler_info = responseText.split(':');
				document.frmAngler.angler_id.value	= angler_info[0];
				document.frmAngler.address.value	= angler_info[5];
				document.frmAngler.city.value		= angler_info[6];
				document.frmAngler.state.value		= angler_info[7];
				document.frmAngler.zip.value		= angler_info[8];
				document.frmAngler.gender[((angler_info[9] == 'male') ? 0 : 1)].checked = true;
				document.getElementById('sixteen').checked = (angler_info[10] == '1') ? true : false;
				document.frmAngler.member.checked	= (angler_info[11] == '1') ? true : false;
				document.getElementById('existing_user').style.display = '';
			}
		}
	}
	ajax_a.update('action=getAnglerInfo&angler_id='+angler_id);
}

function cr_val_final_step(theForm, member_type) {
	var val = true;
	if (member_type == 'member') {
		if (theForm.time_last_caught_hr.selectedIndex == 0	|| theForm.time_last_caught_min.selectedIndex == 0) val = false;
	} else {
		if (theForm.time_last_caught_hr.selectedIndex == 0	|| theForm.time_last_caught_min.selectedIndex == 0 || theForm.angler.value == '' || theForm.num_caught.value == '') {
			val = false;
		}
	}

	if (val == false) {
		alert('Please fill in all the required fields');
		return false;
	}
	submitAjaxForm('frmCatchReport',crHandler);
	return false;
}

function cr_reset_user() {
	document.frmAngler.new_user.checked = false;
	document.getElementById('existing_user').style.display = 'none'
	document.frmAngler.angler_id.value = '';
}
