function scalar(obj, str) {return obj.getElementsByTagName(str)[0].firstChild.nodeValue;}

var DataSet = new Class({
	cell: function(iRows, sCol) {
		return this.rows[iRows].get(sCol);
	},
	transform: function() {
		var arrRows = new Array();
		var dt = this.source.getElementsByTagName("Table");
		for (i=0; i<dt.length; i++) // rows
		{
			var hash = new Hash();
			for (j=0; j<dt[i].childNodes.length; j++)
			{
				if (dt[i].childNodes[j].nodeType != 3) // avoid whitespace (MOZ/SAF)
				{
				    try {
					    hash.set(dt[i].childNodes[j].nodeName, dt[i].childNodes[j].childNodes[0].nodeValue);
					} catch(e) {
					    hash.set(dt[i].childNodes[j].nodeName, "");
					}
				}
			}
			arrRows.push(hash);
		}
		
		return arrRows;
	},
	rowcount: function() {
		return this.rows.length;
	},
	initialize: function(oXml) {
		this.source = oXml;
		this.rows = this.transform();
	}
});

Element.extend({
	SelectedValue: function() { 
		if (this.nodeName.toLowerCase() == "select") {
			return this.options[this.selectedIndex].value;
		} else {
			return false;	
		}; 
	},
	SelectedLabel: function() {
		if (this.nodeName.toLowerCase() == "select") {
			return this.options[this.selectedIndex].text;
		} else {
			return false;	
		}; 
	},
	SelectedName: function() {
		if (this.nodeName.toLowerCase() == "select") {
			return this.options[this.selectedIndex].name;
		} else {
			return false;	
		}; 
	},
	SelectedId: function() {
		if (this.nodeName.toLowerCase() == "select") {
			return this.options[this.selectedIndex].id;
		} else {
			return false;	
		}; 
	},
	cleanWhitespace: function() {
        $A(this.childNodes).each(function(node){
            if ($type(node) == 'whitespace') {
                this.removeChild(node);
            }
        }, this);
        return this;
    }
});

function QuickAlert(message)
{
    var pnlAlert = document.createElement("div");
    pnlAlert.setAttribute("id", "pnlAlert");
    document.body.appendChild(pnlAlert);
    $("pnlAlert").appendChild(document.createTextNode(message));
    
    $('pnlAlert').setOpacity(1);
    $("pnlAlert").effect('opacity',{
	    duration: 3500,
	    transition: Fx.Transitions.quartInOut,
	    onComplete: function()
        {
            $("pnlAlert").outerHTML = "";
        }
    }).start(1, 0);
}

function Login()
{
	window.location.href = "/login.php";
}

function Logout()
{
	window.location.href = "/logout.php";
}

// Check the Registration Form
function checkRegForm() {
	// Initial Error Message / Count
	var errorm = new Array();
	
	// ========================= Check Name
	if ($("txtEmail").value == "") {
		errorm.push("Please fill in the Email Address Field");
	}
	
	// ========================= Check Password
	if ($("updateCheck").value == "false") {
		if ($("txtPass").value == "") {
			errorm.push("Please fill in the Password Field");
		}
		
		if ($("txtPass2").value == "") {
			errorm.push("Please fill in the Password (Verify) Field");
		}
		
		if ($("txtPass").value != $("txtPass2").value) {
			errorm.push("Please make sure your Passwords match");
		}
	}
	
	// ========================= Check Country
	if (dropdownValues == null || dropdownValues["ddlCountry"] == null) {
		errorm.push("Please choose a Country");
	}

	// ========================= Check Regions
	var noreg = 0;
	for (var r=0; r<countryRegionList.length; r++) {
		var reg = $('region_dd_'+countryRegionList[r]).value;
		if (reg == "") {
			noreg = 1;
		}
	}

	if (noreg > 0) {
		errorm.push("Please choose region(s) for all countries selected");
	}
	
	// ========================= Check Language
	if (dropdownValues == null || dropdownValues["ddlLanguages"] == null) {
		errorm.push("Please choose a Language");
	}
	
	// ========================= Check Sector
	if ($("ddlSectors").selectedIndex == 0) {
		errorm.push("Please choose a Sector");
	}
		
	// ========================= Check 10 Questions
	var num = 10;
	for (var i = 1; i <= 10; i++) {
		var ans = $('ddlQuestionAnswer_'+i).value;
		if (ans == "") {
			errorm.push("Plese select an answer for question "+i);
		}
	}
	// ========================= Check the Array
	if (errorm.length == 0) {
		$('theform').submit();
		
	} else {
		// Spit out errors
		var alerterror = "";
		
		for (var i = 0; i <= errorm.length - 1; i++) {
			alerterror += (i + 1) + ". " + errorm[i] + ".\n"
		}
		
		// Alert them
		alert(alerterror);
		
	return false;
	}
}
