// ======================================================================
function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

// ======================================================================
function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name){
			return unescape(y);
		}
	}
}

// ======================================================================
function  jsonToCheckBox(data, nameBase){
	optionList = "";
	$.each(data, function(key, value){
		$.each(value, function(key2, value2){
			optionList += '<input type="checkbox" name="'+nameBase+'[]" id="'+nameBase+'_'+value2.value+'" value="'+value2.value+'">'+value2.name+'<br>';
		});
	});
	return optionList;
}

// ======================================================================
function  jsonToRadio(data, nameBase){
	optionList = "";
	$.each(data, function(key, value){
		$.each(value, function(key2, value2){
			optionList += '<input type="radio" name="'+nameBase+'" value="'+value2.value+'" />'+value2.name+'<br />';		
		});
	});
	return optionList;
}


// ======================================================================
function  jsonToOptionList(data){
	optionList = "";
	$.each(data, function(key, value){
		$.each(value, function(key2, value2){
			optionList += '<option value="'+value2.value+'">'+value2.name+'</option>';
		});
	});
	return optionList;
}

// ======================================================================
function htmlEntities2(str) {
	return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&acute;');
}

function htmlEntities(str) {
	return str;
}

// ======================================================================
function echo(string){
	document.write(string);
}

// ======================================================================
// throws picklist array into $("div#"+baseName+"Container")
// if there are any $("td."+baseName), it will convert them to readables
function displayFilterFromPicklist(arySelected,baseName,LogicalName,EntityLogicalName,reloadCookie,justFixUp){	
	var cookie = getCookie(baseName+"ListJson");
	if (cookie != null && cookie != "" && reloadCookie == false){
		if(justFixUp != true){
			$("div#"+baseName+"Container").html(jsonToCheckBoxTable(JSON.parse(cookie), "sel"+baseName));
			$('input[name^="sel'+baseName+'"]').each(function(){for(element in arySelected)if($(this).attr('value') == arySelected[element]) $(this).attr('checked','checked');});
		}
	} else {
		$.ajax({
			url:"http://www.oga.org/index.php/EOG/getPicklistMetaData",
			type:"POST",
			data: "LogicalName="+LogicalName+"&EntityLogicalName="+EntityLogicalName,
			dataType:"json",
			error: function(data){console.log("Error: "+data);},
			success: function(data){
				setCookie(baseName+"ListJson",JSON.stringify(data),365);
				cookie = getCookie(baseName+"ListJson");
				if(justFixUp != true){
					$("div#"+baseName+"Container").html(jsonToCheckBoxTable(data, 'sel'+baseName));
					$('input[name^="sel'+baseName+'"]').each(function(){for (element in arySelected)	if($(this).attr('value') == arySelected[element]) $(this).attr('checked','checked');});
				}	
			}	
		});
	}
	
	// fix up all displayed values
		var aryTemp = new Array();
		$.each(JSON.parse(cookie).picklist, function(){aryTemp[this.value] = this.name;});	//build display name array
		$("."+baseName).each(function(){$(this).html(aryTemp[$(this).html()]);}); // convert the displayed values to their names
}

// ======================================================================

// ======================================================================
function  jsonToCheckBoxTable(data, nameBase){
	ITEMSPERROW = 2;
	counter = 0;
	table = '<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"><tr>';
	$.each(data, function(key, value){
		$.each(value, function(key2, value2){
			
//			if(value2.value == 7 || value2.value == 6 || value2.value == 8){ // hide municipal and military course types governement
			
//			} else {
				if(counter % ITEMSPERROW == 0 && counter != 0) table += '</tr><tr>';
				table += '<td width="10" height="18" align="center" valign="middle"><input type="checkbox" name="'+nameBase+'[]" id="'+nameBase+'_'+value2.value+'" value="'+value2.value+'" /></td>';
				table += '<td width="64" height="18" align="center" valign="middle">'+value2.name+'</td>';
				counter++;
//			}	
				
		});
	});
	return table += '</tr></table>';
}

// ======================================================================
function  jsonToRadioTable(data, nameBase){
	ITEMSPERROW = 2;
	counter = 0;
	table = '<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"><tr>';
	$.each(data, function(key, value){
		$.each(value, function(key2, value2){
				if(counter % ITEMSPERROW == 0 && counter != 0) table += '</tr><tr>';
				table += '<td width="10" height="18" align="center" valign="middle"><input type="radio" name="'+nameBase+'" value="'+value2.value+'" /></td>';
				table += '<td width="64" height="18" align="center" valign="middle">'+value2.name.replace(/\s/g, "");+'</td>';
				counter++;
		});
	});
	return table += '</tr></table>';
}

// ======================================================================


