var globalside = '';
regionarray = new Array();




//////////////////////////////////////////////////
// MultiSelect Box Challenge

// Global Array
dropdownValues = new Array();


function setMultiSelectbox(selectId){
	if(!dropdownValues[selectId]){
		dropdownValues[selectId]=new Array();
		for(var i=0;i< document.getElementById(selectId).options.length;i++){
			dropdownValues[selectId][i] = 0;
			//alert(document.getElementById(selectId).options[i].selected);
			//dropdownValues[selectId][i] = document.getElementById(selectId).options.selected;
		}
	}
	optionId=getRecentlySelected(selectId);
	if(dropdownValues[selectId][optionId]==0){
		dropdownValues[selectId][optionId]=1;
	}
	else{
		dropdownValues[selectId][optionId]=0;
		
		////////////////////////////////////////
		// For Country Dropdown Only
		if(selectId == "ddlCountry") {
			var oValue = $("ddlCountry").options[optionId].value;
			// If the item being unselected is in our Country Array
			// Remove it from the Array AND from the page
			removeArrayElement(countryRegionList, oValue);
		}
		// For Country Dropdown Only
		////////////////////////////////////////
	}
	var output="";
	for(var j=0;j<dropdownValues[selectId].length;j++){
		output=output+" "+ dropdownValues[selectId][j]+", ";
	}
	//alert(output);
	redrawBox(selectId);
}


function redrawBox(selectId){
	var sel=document.getElementById(selectId);
	var selLen=sel.options.length;
	for(var i=0;i<selLen;i++){
		sel.options[i].selected=convertToBool(dropdownValues[selectId][i]);
		//alert('redraw'+convertToBool(dropdownValues[selectId][i]));
	}
}


function convertToBool(i){
	if(i==0){
		return false;
	}	
	else{
		return true;
	}
}


function getRecentlySelected(selectId){
	var sel=document.getElementById(selectId);
	var selLen=sel.options.length;
	for(var i=0;i<selLen;i++){
		if(sel.options[i].selected==true){
			return i;
		}
		//alert('redraw'+convertToBool(dropdownValues[selectId][i]));
	}
}

function multiBoxSelectAll(ddList){
	dropdownValues[ddList] = new Array();
	
	for (var i = 0; i <= $(ddList).length - 1; i++) {
		dropdownValues[ddList].push($(ddList).options[i].value);
		$(ddList).options[i].selected = true;
	}
}

function multiBoxSelectNone(ddList){
	dropdownValues[ddList] = null;
	
	for (var i = 0; i <= $(ddList).length - 1; i++) {
		$(ddList).options[i].selected = false;
	}
}

// MultiSelect Box Challenge
//////////////////////////////////////////////////

function ShowTheQuestionStuff() {
	$("questionable_content").style.display = 'block';
}


function ShowQuestions()
{
	var url = "/inc/ajax/services.php?method=show_q_from_server";
	var pars = "sector_id=" + $("ddlSectors").SelectedValue();
	new Ajax(url, {postBody:pars, onComplete:ShowQuestions2}).request();
}


function ShowQuestions2(sText, oXml) {
	var ds = new DataSet(oXml);
	
	$("hdnVersionID").value = ds.cell(0, "version_id");
	for (var i = 0; i < ds.rowcount(); i++) {
		var divNum = (i + 1);
		
		$("answer_left_"+(i+1)).innerHTML = ds.cell(i, "question_content");
		$("hdnQuestionID_"+(i+1)).value = ds.cell(i, "question_id");
		if (ds.cell(i, "multiple_select") == 1)
		{
			var multiNum = 1;
			// For use when an array is being given -- for now not using
			$("ddlQuestionAnswer_"+(i+1)).name = "ddlQuestionAnswer_" + (i+1) + "[]";
			//$("ddlQuestionAnswer_"+(i+1)).name = "ddlQuestionAnswer_" + (i+1);
			$("ddlQuestionAnswer_"+(i+1)).multiple = true;
			$("ddlQuestionAnswer_"+(i+1)).size = 5;
				var passToFunction= function (){
					setMultiSelectbox(this.id);
				};
			$("ddlQuestionAnswer_"+(i+1)).onchange = passToFunction;
		} else {
			var multiNum = 0;
			$("ddlQuestionAnswer_"+(i+1)).name = "ddlQuestionAnswer_" + (i+1);
			$("ddlQuestionAnswer_"+(i+1)).multiple = false;
			$("ddlQuestionAnswer_"+(i+1)).size = 1;
		}
		
		var url = "/inc/ajax/services.php?method=answers_from_question_reg";
		var pars = "question_id=" + ds.cell(i, "question_id");
		pars += "&question_div=" + divNum;
		pars += "&multiNum=" + multiNum;
		//alert(pars);
		new Ajax(url, {postBody:pars, onComplete:ShowAnswers}).request();
	}
}


function ShowAnswers(sText, oXml) {
	var ds = new DataSet(oXml);
	var divNum = ds.cell(0, "question_div");
	var divMulti = ds.cell(0, "multiNum");
	
	$("ddlQuestionAnswer_" + divNum).options.length = 0
	
	for (var i = 0; i < $("ddlQuestionAnswer_" + divNum).options.length; i++){
		//$("ddlQuestionAnswer_" + divNum).options[i] = null;
	}
	
	// Add initial option
	var optionObject = new Option("-- Choose an Answer --", "");
	
	if (divMulti == 0) {
		var multiNum = 1;
		$("ddlQuestionAnswer_"+divNum).options[0]=optionObject;
	} else {
		var multiNum = 0;	
	}
	
	for (var i = 0; i < ds.rowcount(); i++) {
		
		//$("txtAnswer" + question_order + "_" + (i+1)).value = ds.cell(i, "answer_content");
		
		var optionObject = new Option(ds.cell(i, "answer_content"),ds.cell(i, "answer_id"));
    		//var optionRank = $("ddlQuestionAnswer_"+question_order).options.length;
    	
		$("ddlQuestionAnswer_"+divNum).options[(i + multiNum)]=optionObject;
		if(i==0){
			//$("ddlQuestionAnswer_"+divNum).options[0].selected=true;
		}


	}
	if(divNum==10){
		moveWindow(); 
	}
}


// =======================================================
// Submit the Registration Form
function submit_user_reg(id) {	
	// Get the Users ID
		currentUser = id;
	
	// Get Countries
		userCountry = "";
		countryLength = $("ddlCountry").length;
		
		// Loop through them all
		for (i = 0; i <= countryLength - 1; i++) {
			if($("ddlCountry").options[i].selected == true) {
				
			}
		}
	
}


function submit_countries(cValue) {
	var url = "/inc/ajax/services.php?method=submit_country";
	//pars = "country_id=" + $("ddlCountry").SelectedValue();
	pars = "country_id=" + sValue;
	//alert(pars);
	
	new Ajax(url, {postBody:pars, onComplete:ShowRegions}).request();
}


//////////////////////////////////////////////////
// Region Redraw

// Search an Array for a value
function searchTheArray(theArray, searchFor) {
	//alert(theArray.length);
	var isFound = false;
	
	for (var i = 0; i < theArray.length; i++) {
		// If the searched value is found
		if (theArray[i] == searchFor) {
			isFound = true;
		}
	}
	
	return isFound;
}

// Search an Array for a value
function removeArrayElement(theArray, removeThis) {
	for (var i = 0; i < theArray.length; i++) {
		// If the searched value is found
		if (theArray[i] == removeThis) {
			theArray.splice(i, 1);
			destroyRegionDropdown(removeThis);
		}
	}
}
function selectAll(selectBox,selectAll) {
	// have we been passed an ID
	if (typeof selectBox == "string") {
		selectBox = document.getElementById(selectBox);
	}
	// is the select box a multiple select box?
	if (selectBox.type == "select-multiple") {
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = selectAll;
		}
	}
}
// Create the Region Dropdown Box
function createRegionDropdown(ds, selectID) {
	//alert("create" + "; " + selectID);
	var hideAllReg=false;

	if (globalside=="public" && ds.rowcount()>1) {
		hideAllReg = true;
	}
	else{
		hideAllReg = false;
	}
	
	
	var Container 		= new Element('div', {'id': 'c_r_' + selectID, 'class': 'field_text bottom_space clearfix'});
	var Container_dd = new Element('div', {'class': 'field'});
	
	var sel = document.createElement('select');
		sel.name = "region_dd_" + selectID + "[]";
		sel.id = "region_dd_" + selectID;
		sel.size = "5";
		sel.multiple = "multiple";
			var passToFunction= function (){
				setMultiSelectbox(this.id);
			};
		sel.onchange = passToFunction;
	
	var allButton= document.createElement('input');
	var pre_selectAll= function (){
	
				selectAll("region_dd_" + selectID,true);
			};
	allButton.type="button";		
	allButton.onclick=pre_selectAll;
	allButton.value="Select All";
	allButton.width="10";
	
	var Container_txt = new Element('div', {'class':'text'});
	var Container_txt_2 = new Element('p');
		Container_txt_2.appendChild(document.createTextNode($("c_v_" + selectID).text));
		
	Container.appendChild(Container_dd);
		Container_dd.appendChild(sel);
		//Container_dd.appendChild(allButton);
	Container.appendChild(Container_txt);
		Container_txt.appendChild(Container_txt_2);
	
	$("region_container").appendChild(Container);
	
	var counter=0;

	for (var i=0; i<ds.rowcount(); i++){
		if ((hideAllReg && (ds.cell(i, "region_name_long") == "All Regions"))) {
		}
		else{
			$("region_dd_" + selectID).options[counter] = new Option(ds.cell(i, "region_name_long"), ds.cell(i, "region_id"));
			if (ds.rowcount() == 1) {
				var reg = ds.cell(i,"region_id");
				$("region_dd_"+selectID).value = reg;
			}
			if(counter==0){
				//$("region_dd_" + selectID).options[counter].selected=true;
				
			}
			counter++;
		}
	}
}

// Destroy the Region Dropdown Box
function destroyRegionDropdown(removeThis) {
	//alert("destroy" + "; " + removeThis);
	
	$("region_container").removeChild($("c_r_" + removeThis));
}

// Initial Array
countryRegionList = new Array();

function DrawRegions(side) {
	globalside=side;
	for (i = 0; i <= $("ddlCountry").length - 1; i++) {
		SelObj = $("ddlCountry").options[i];
			
		
			
		if ($("ddlCountry").options[i].selected == true) {
			regionCode = $("ddlCountry").options[i].value;
				
			// ===========================================================================
			// Populate the regions
			// ===========================================================================
			if (regionCode != 99999) {//all countries selected so no regions
				FindRegions(regionCode);
			}
		}
		
	}
}
function FindRegions(sValue) {
	var url = "/inc/ajax/services.php?method=find_regions";
	//pars = "country_id=" + $("ddlCountry").SelectedValue();
	pars = "country_id=" + sValue;
	//alert(pars);
	
	new Ajax(url, {postBody:pars, onComplete:redrawRegionBoxes}).request();
}

function redrawRegionBoxes(sText, oXml) {
	var ds = new DataSet(oXml);
	
	

		var all = ds.cell(0, 'region_name_long');
		var selectID = ds.cell(0, 'country_id');

		if (searchTheArray(countryRegionList, selectID) == false) {
			countryRegionList.push(selectID);
			createRegionDropdown(ds, selectID);
		}
	}	
	
	
	//$("Region_Test").innerHTML = countryRegionList;


// Region Redraw
//////////////////////////////////////////////////
