var url = "http://portfolio.cgsociety.org/getlist.php"; // The server-side script
var multiurl = "http://portfolio.cgsociety.org/getmultilist.php"; // The server-side script for multi selections

var skill_choice = new Array();
var skill_array = new Array();
var temp_array = new Array();
var other_array = new Array();
var item_count = 25;
var get_other;

function Skill(id, nameString, weight) {
	function getId() {
		return this.id;
	}
	
	function getName() {
		return this.nameString;
	}
	
	function getWeight() {
		return this.weight;
	}
	
	function toString() {
		return "["+this.id+"] "+this.nameString+" ("+this.weight+")";
	}
	
	this.selected = false;
	this.id = id;
	this.nameString = nameString;
	this.weight = weight
}

function toggleCloud() {
	if (item_count == 25) {
		item_count = 100;
		document.getElementById('moreless').innerHTML = '<< less';
	} else {
		item_count = 25;
		document.getElementById('moreless').innerHTML = 'more >>';
	}
	
	getTagCloud('cloud');
}

function addOther(type, number) {
	var result;
	
	var inputName = 'input_'+type+"_"+number;
	
	result  = "<td align='left' valign='top' style='padding:10px 0px 0px 14px'>";
	result += "<input type='text' name='other_"+type+"["+number+"]' value='' size='40' maxlength='40' class='user_input_textfield' style='width:175px'></td>";
	result += "<td><a href=\"javascript:addOther('"+type+"', "+(number+1)+")\">+</a></td>";
	result += "<table  width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td>";
	result += "<div id='div_"+type+"_"+(number+1)+"></div>";
	result += "</td></tr></table>";
	document.getElementById("div_"+type+"_"+number).innerHTML = result;
	
}

function addSingleOther(type) {
	var result;
		
	result  = "<td align='left' valign='top' style='padding:10px 0px 0px 14px'>";
	result += "<input type='text' name='other_"+type+"' value='' size='40' maxlength='40' class='user_input_textfield' style='width:175px'></td>";
	document.getElementById("div_"+type).innerHTML = result;
	
}

//takes a single selection from a dropdown list, and populates another list (specified by type)
function getList(ptype, stype, lid, uid, showother) {
	var sendURL = url+"?ptype="+ptype+"&stype="+stype+"&lid="+lid+"&uid="+uid;
	var curDateTime = new Date();
	var showother = (showother == null) ? false : showother;
	
	var ident = curDateTime+ptype+stype;
	
	http[ident] = getHTTPObject();
	
	oldList = document.getElementById(stype+'_select');
	if (!oldList)
		return;

	temp_array = new Array();

	if (other_array[stype] == null) {
		other_array[stype] = new Array();
		get_other = true;		
	}

	var other = false

	for (var i = 0; i < oldList.length; i++) {
		option = oldList.options[i];
		if(option.selected) temp_array[temp_array.length] = option.value;
		if(other && get_other) other_array[stype][other_array[stype].length] = new Skill(option.value, option.text, 0);
		if(option.value == 'other') other=true;
	}
	
	get_other = false;
		
	document.getElementById(stype+'_select').length=0;
	document.getElementById(stype+'_select').options[0] = new Option('-- Updating --','',true, true);
	document.getElementById(stype+'_select').disabled=true;

	http[ident].open("GET", sendURL, true);
	http[ident].onreadystatechange = function (){ handleHttpResponse(ident, stype, showother)};
	http[ident].send(null);
}

//takes an array of selections from a multiselect list, and populates another list (specified by type)
function getMultiList (ptype, stype, select, uid, showother) {
	var larray = new Array();
	var curDateTime = new Date();
	var showother = (showother == null) ? false : showother;
	
	var ident = curDateTime+ptype+stype;
	
	http[ident] = getHTTPObject();

	for (var i = 0; i < select.options.length; i++) {
  		if (select.options[i].selected) {
  			larray[larray.length] = select.options[i].value;
  		}
  	}

	oldList = document.getElementById(stype+'_select');

	temp_array = new Array();
	
	if (other_array[stype] == null) {
		other_array[stype] = new Array();
		get_other = true;		
	}
	
	var other = false;

	for (var i = 0; i < oldList.length; i++) {
		option = oldList.options[i];
		if(option.selected) temp_array[temp_array.length] = option.value;
		if(other && get_other) other_array[stype][other_array[stype].length] = new Skill(option.value, option.text, 0);
		if(option.value == 'other') other=true;
	}

	get_other = false;

	var sendURL= url+"?ptype="+ptype+"&stype="+stype+"&larray="+larray+"&uid="+uid;
	
	document.getElementById(stype+'_select').length=0;
	document.getElementById(stype+'_select').options[0] = new Option('-- Updating --','',true, true);
	document.getElementById(stype+'_select').disabled=true;
  
	http[ident].open("GET", sendURL, true);
	http[ident].onreadystatechange = function() { handleHttpResponse(ident, stype, showother)};
	http[ident].send(null);
}

function checkForMore (select, divName, single) {
	
	var single = (single == null) ? false : single;
	
	for (var i = 0; i < select.length; i++) {
		if(select.options[i].selected) {
			if(select.options[i].value == 'other') {
				if(single) {
					addSingleOther(divName);
				} else {
					addOther(divName, 1);
				}
			}
		}
	}
}

//uses two hardcoded arrays from multiselect lists - and fills a textbox with javascript keywords (also hardcoded)
function getTagCloud (ptype, stype) {
	var ind_array = new Array();
	var job_array = new Array();
	var i = 0;
	
	var curDateTime = new Date();
	
	var ident = curDateTime+ptype+stype;
	
	http[ident] = getHTTPObject();
	
	select = document.getElementById('industry_select');

	for ( i = 0; i < select.options.length; i++) {
		if (select.options[i].selected) {
			ind_array[ind_array.length] = select.options[i].value;
		}
	}

	select = document.getElementById('job_select');

	for ( i = 0; i < select.options.length; i++) {
		if(select.options[i].selected) {
			job_array[job_array.length] = select.options[i].value;
		}
	}

	var sendURL = url+"?ptype="+ptype+"&indarray="+ind_array+"&jobarray="+job_array+"&item_count="+item_count;

	http[ident].open("GET", sendURL, true);
	http[ident].onreadystatechange = function() {handleCloudResponse(ident, ptype)};
	http[ident].send(null);
}

function swap(type, skill) {
	exists = false;
	
	for (var i = 0; i < skill_choice.length; i++) {
		if (skill_choice[i].toLowerCase() == "") {
			skill_choice.splice(i, 1);
		} else if (skill_choice[i].toLowerCase() == skill.toLowerCase()) {
			skill_choice.splice(i, 1);
			exists = true;
		}
	}
	
	if (!exists) {
		skill_choice[skill_choice.length] = skill;
	}

	document.getElementById(type+'_field').value = skill_choice.toString().replace(',', ', ', 'gi');
		
	updateCloud(type);
}

function updateCloud(type, stype) {
	field = document.getElementById(type+'_field').value;
	
	skill_choice = field.split(", ");
	
	for (var i =0; i < skill_array.length; i++) {
		skill_array[i].selected = false;
		for (var j=0; j < skill_choice.length; j++) {
			if (skill_array[i].nameString.toLowerCase() == skill_choice[j].toLowerCase()) {
				skill_array[i].selected = true;
			}	
		}
		
		if (skill_array[i].selected) {
			document.getElementById('skill_'+skill_array[i].id+'_span').style.backgroundColor = '#CCCCCC';
		} else {
			document.getElementById('skill_'+skill_array[i].id+'_span').style.backgroundColor = '#E2E2E2';
		}
	}
}

function handleHttpResponse(ident, type, showother) {

	var showother = (showother == null) ? false : showother;

	if (http[ident].readyState == 4) {

		if (http[ident].responseText == null)
			results = new Array ();
		else
			results = http[ident].responseText.split("~");
		
		target = document.getElementById(type+'_select');
		
		target.length = 0;
		
		typename = type;
		singular = 'a';
		
		switch(typename) {//saves the hassle of refactoring a bunch of stuff.
			case 'job':
			typename='occupation';
			singular = 'an';
			break;
		}
		
		if(results.length > 1) {
			target.options[0] = new Option ('Please select '+singular+' '+typename, '', true);
			target.disabled=false;
		} else {
			target.options[0] = new Option ('No '+typename+' available', '', true);
			target.disabled=false;
		}
		
		for(i=0;  i<results.length-1; i++) {
			result = results[i].split('^');
			target.options[i+1] = new Option(result[1], result[2], false);
		}
		
		
		if(showother) {
			target.options[target.length] = new Option ('Other ...', 'other', false);
		
			for(i=0; i<other_array[type].length; i++) {
				target.options[target.length] = new Option(other_array[type][i].nameString, other_array[type][i].id, false);
			}
		}
		
		for(i = 0; i < target.length; i++) {
			for (j = 0; j < temp_array.length; j++) {
				if(target.options[i].value  == temp_array[j]) {
					target.options[i].selected = true;	
				}	
			}
		}
	}
}

function handleCloudResponse(ident) {
	if (http[ident].readyState == 4) {
		results =http[ident].responseText.split("~");
		
		skill_array = new Array();
		
		if(results.length > 1) {
			
			for(i=0; i<results.length-1; i++) {
				result = results[i].split('^');
				
				skill_array[skill_array.length] = new Skill(result[2], result[1], result[0]);
				
			}
		}
		
	//alert(skill_array);	
	getHtmlForCloud();
	
	updateCloud('skill');	
	}
}

function getHtmlForCloud() {
	htmlcode="";
	
	for(var i = 0; i < skill_array.length; i++) {
	
		htmlcode += '<span id="skill_'+skill_array[i].id+'_span" ';
		
		htmlcode += 'STYLE="background-color: #E2E2E2"';
		
		htmlcode += ' class="cloud_skill_'+skill_array[i].weight+'"';
		
		htmlcode += '>';
		
		htmlcode += '<a href="javascript:swap(\'skill\', \''+skill_array[i].nameString+'\')"';
				
		if(1) {//trickery to change how bold something is
			htmlcode +='class="skill">';
		} else {
			//don't be stupid	
		}
				
		htmlcode += skill_array[i].nameString;
		htmlcode += '</a></span>';
				
		if(i<(results.length-1)) {
			htmlcode += ', ';
		}	
	}		
	document.getElementById('tag_cloud').innerHTML = htmlcode;
}

function getHTTPObject() {
	var xmlhttp;

	/*@cc_on
		@if (@_jscript_version >= 5)
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) {
					xmlhttp = false;
				}
			}
		@else
			xmlhttp = false;
	@end @*/

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

 // We create the HTTP Object
var cloudhttp = getHTTPObject();
var http = new Array();
