// JavaScript Document

function select_date(evt,form_field){
	var xpos = evt.screenX;
	var ypos = evt.screenY;
	var new_window = window.open('select_date.php?form_field='+form_field,'','width=180,height=180,screenX='+xpos+',screenY='+ypos+',left='+xpos+',top='+ypos+',toolbar=no,scrollbars=no,resizeable=no,status=no,menubar=no,status=no,copyhistory=no,directories=no,location=no');
}

function copyValue(fromField,toField){
	document.getElementById(toField).value = document.getElementById(fromField).value;
}

function popup(url,width,height){
	var xpos = (screen.width-width)/2;
	var ypos = (screen.height-height)/2;
	var new_window = window.open(url,'','width='+width+',height='+height+',screenX='+xpos+',screenY='+ypos+',left='+xpos+',top='+ypos+',toolbar=no,scrollbars=yes,resizeable=no,status=no,menubar=no,copyhistory=no,directories=no,location=no');
}

function showConditionalDiv(divBaseId,idNumberToShow,totalDivs){
	for(i=1;i<=totalDivs;i++){
		document.getElementById(divBaseId+i).style.display = "none";
	}
	document.getElementById(divBaseId+idNumberToShow).style.display = "";
}

function dispCoorFileFields(){
	var selected = document.upload_document.doc_type.value;
	if(selected == 0){
		showConditionalDiv('fieldsdiv',0,1);
	}else{
		showConditionalDiv('fieldsdiv',1,1);
		if(((selected >= 1) && (selected <= 4)) || (selected == 8)){
			showConditionalDiv('doctypefields',1,2);
		}else if((selected == 5) || (selected == 14) || (selected == 13) || (selected == 11)){
			showConditionalDiv('doctypefields',2,2);
		}else if((selected == 6) || (selected == 7) || (selected == 10) || (selected == 12)){
			showConditionalDiv('doctypefileds',0,2);
		}
	}
}

function populateFields(typeDiv,fileInfoArray){
	var form;
	if(typeDiv == 'file_info'){
		form = document.edit_file;
		form.file_id.value = fileInfoArray[0];
		form.description.value = fileInfoArray[1];
		form.gov_body.value = fileInfoArray[2];
		if(fileInfoArray[2] == 3){
			form.comm.value = fileInfoArray[3];
		}else if(fileInfoArray[2] == 4){
			form.club.value = fileInfoArray[3];
		}
		form.meeting.value = fileInfoArray[4];
		form.effect_date.value = fileInfoArray[5];
		if(fileInfoArray[6] == 1){
			form.approved.checked = true;
		}
	}else if(typeDiv == 'club_member'){
		form = document.edit_member;
		form.member_id.value = fileInfoArray[0];
		form.fname.value = fileInfoArray[1];
		form.lname.value = fileInfoArray[2];
		form.phone1.value = fileInfoArray[3].substring(0,3);
		form.phone2.value = fileInfoArray[3].substring(3,6);
		form.phone3.value = fileInfoArray[3].substring(6);
		form.email.value = fileInfoArray[4];
	}
}

function toggleHiddenDiv(divToToggle,typeDiv,fileInfoArray){
	var popupdiv = document.getElementById(divToToggle);
	var bg_fade = document.getElementById('bg_fade');
	if(popupdiv.style.visibility=='hidden'){
		populateFields(typeDiv,fileInfoArray);
		var scroll_x = (document.all)?document.body.scrollLeft:window.pageXOffset;
		var pos_x = ((document.documentElement.clientWidth-400)/2)+scroll_x;
		var scroll_y = (document.all)?document.body.scrollTop:window.pageYOffset;
		var pos_y = ((document.documentElement.clientHeight-300)/2)+f_scrollTop();
		if (window.innerHeight && window.scrollMaxY) {// Firefox
			yWithScroll = window.innerHeight + window.scrollMaxY;
			xWithScroll = window.innerWidth + window.scrollMaxX - 16;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
			xWithScroll = document.body.scrollWidth;
		} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			xWithScroll = document.body.clientWidth;
			yWithScroll = document.body.clientHeight + 30;
		}
		
		popupdiv.style.left = pos_x+'px';
		popupdiv.style.top = pos_y+'px';
		popupdiv.style.visibility = 'visible';
		bg_fade.style.height = yWithScroll+'px';
		bg_fade.style.visibility = 'visible';
	}
	else{
		popupdiv.style.visibility = 'hidden';
		bg_fade.style.visibility = 'hidden';
	}
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function validateField(formName,fieldName,fieldType,cond){
	// Validation rules for text fields
	if(fieldType == 'text_field'){
		var field = 'document.'+formName+'.'+fieldName+'.value';
		eval('var fieldValue = '+field+';');
		// Make sure the field is NULL
		if(cond == 'is null'){
			if(fieldValue.length > 0){
				return false;
			}
		}
		// Make sure the field is NOT NULL
		else if(cond == 'is not null'){
			if(fieldValue.length == 0){
				return false;
			}
		}
		// Make sure the field does not match a given string
		else if(cond.substring(0,9) == 'not match'){
			if(fieldValue == cond.substring(10)){
				return false;
			}
		}
		// Make sure the field value is greater than a given value
		else if(cond.substring(0,1) == '>'){
			if(parseInt(fieldValue) <= parseInt(cond.substring(1))){
				return false;
			}
		}
		// Make sure the field value is less than a given value
		else if(cond.substring(0,1) == '<'){
			if(parseInt(fieldValue) >= parseInt(cond.substring(1))){
				return false;
			}
		}
		// Make sure the length of the field value is greater than a given value
		else if(cond.substring(0,7) == 'length>'){
			if(fieldValue.length <= parseInt(cond.substring(7))){
				return false;
			}
		}
		// Make sure the length of the field value is less than a given value
		else if(cond.substring(0,7) == 'length<'){
			if(fieldValue.length >= parseInt(cond.substring(7))){
				return false;
			}
		}
		// Make sure the field value is a number
		else if(cond == 'is number'){
			if(fieldValue.length > 0){
				if(!parseFloat(fieldValue)){
					return false;
				}
			}
		}
		// Make sure the field value is an integer
		else if(cond == 'is integer'){
			if(fieldValue.length > 0){
				if(!parseInt(fieldValue)){
					return false;
				}
			}
		}
	}
	// Validation rules for check box arrays
	else if(fieldType == 'checkbox_array'){
		var boxArrayList = document.forms[formName].elements[fieldName];
		// Make sure at least a given number of objects have been checked (selected)
		if(cond.substring(0,8) == 'at least'){
			var numChecked = 0;
			for(n=0;n<boxArrayList.length;n++){
				if(boxArrayList[n].checked == true){
					numChecked++;
				}
			}
			if(numChecked < parseInt(cond.substring(8))){
				return false;
			}
		}			
	}else{
		return false;
	}
	return true;
}