// custom wwt funcs

function youtube(movieid) {
	document.write("<object width='425' height='344'><param name='movie' value='http://www.youtube.com/v/" + movieid + "&hl=en'></param><embed src='http://www.youtube.com/v/" + movieid + "&hl=en' type='application/x-shockwave-flash' width='425' height='344'></embed></object>");
}

// validation routines.
// copyright (c) 2000 alvin ward. all rights reserved.

function validText (name, txtbox, mandatory) {
	if ((mandatory) && (txtbox.value == '')) {
		try { txtbox.focus(); } catch (ex) {}
		alert (name + ' is a mandatory field');
		return false;
	}
	return true;
}

function validNumeric (name, txtbox, mandatory) {
	if ((mandatory) && (txtbox.value == '')) {
		try { txtbox.focus(); } catch (ex) {}
		alert (name + ' is a mandatory field');
		return false;
	}
	if ((txtbox.value != '') && (isNaN(parseFloat(txtbox.value)))) {
	//if ((txtbox.value != '') && (isNaN(txtbox.value))) {
		try { txtbox.focus(); } catch (ex) {}
		alert (name + ' must be a valid numeric field');
		return false;
	}
	return true;
}

function validDate (name, txtbox, mandatory) {
	if ((mandatory) && (txtbox.value.length != 10)) {
		try { txtbox.focus(); } catch (ex) {}
		alert (name + ' is a mandatory field');
		return false;
	}
	if (txtbox.value != '') {
		var parts = txtbox.value.split("-");
		if ((txtbox.value.length != 10) || (parts.length != 3)) {
			try { txtbox.focus(); } catch (ex) {}
			alert (name + ' must be a valid date (yyyy-mm-dd)');
			return false;
		}
		if (isNaN(parseInt(parts[0])) || isNaN(parseInt(parts[1])) || isNaN(parseInt(parts[2]))) {
			try { txtbox.focus(); } catch (ex) {}
			alert (name + ' must be a valid date (yyyy-mm-dd)');
			return false;
		}
	}
	return true;
}

function validSelect (name, lstbox, mandatory) {
	if ((mandatory) && (lstbox.value == "")) {
		try { lstbox.focus(); } catch (ex) {}
		alert (name + ' is a mandatory field');
		return false;
	}
	return true;
}

function validRadio (name, optbutton, mandatory) {
	if (mandatory) {
		for (var count = 0; count < optbutton.length; count++) {
			if (optbutton[count].checked) { return true; }
		}
		try { optbutton[0].focus(); } catch (ex) {}
		alert (name + ' is a mandatory field');
		return false;
	}
	return true;
}

// utility functions
function val(value) {
	return (isNaN(parseFloat(value)) ? 0 : parseFloat(value));
}

function confirmRemove () {
	return confirm('Are you sure you want to remove this item?');
}
// <a href="" target="_blank" onclick="return showPopup(this.href);"></a>
function showPopup(url) {
	window.open(url, "_blank", "height=500,width=700,resizable=yes,scrollbars=yes,status=yes", true);
	return false;
}

var previousSwapColor;

function swapColor(parent, color) {
	if (parent.children.length > 0) previousSwapColor = parent.children.item(0).className;
	for (var count = 0; count < parent.children.length; count++) {
		parent.children.item(count).className = color;
	}
}

function restoreColor(parent) {
	for (var count = 0; count < parent.children.length; count++) {
		parent.children.item(count).className = previousSwapColor;
	}
}

function getCookie(name) {
	var cooki = document.cookie.split("; ");
	for (var count = 0; count < cooki.length; count++) {
		var crumb = cooki[count].split("=");
		if (name == crumb[0]) return unescape(crumb[1]);
	}
	return "";
}

function createOption(code, dscr) {
	var opt = document.createElement("OPTION");
	opt.value = code;
	opt.text = dscr;
	return opt;
}

// <img src="images/excel.gif" align="right" id="icoHtml" onclick="showExcel('icoHtml', 'tableHtml', 'icoExcel', 'objExcel')"/>
// <span id="tableHtml"><table /></span>

// <img src="images/html.gif" align="right" id="icoExcel" onclick="showHtml('icoHtml', 'tableHtml', 'icoExcel', 'objExcel')" style="display: none;"/>
// <object classid="clsid:0002E510-0000-0000-C000-000000000046" id="objExcel" height="400" width="85%" style="display: none;"></object>

function showExcel(imgHtml, spanTable, imgExcel, objectExcel) {
	imgHtml = document.all(imgHtml);
	spanTable = document.all(spanTable);
	imgExcel = document.all(imgExcel);
	objectExcel = document.all(objectExcel);

	objectExcel.Refresh();
	objectExcel.DataType = "HTMLData";
	objectExcel.HTMLData = spanTable.innerHTML;
	objectExcel.Refresh();
	objectExcel.ActiveSheet.UsedRange.AutoFitColumns();
	
	imgHtml.style.display = "none";
	spanTable.style.display = "none";
	imgExcel.style.display = "inline";
	objectExcel.style.display = "inline";
}

function showHtml(imgHtml, spanTable, imgExcel, objectExcel) {
	imgHtml = document.all(imgHtml);
	spanTable = document.all(spanTable);
	imgExcel = document.all(imgExcel);
	objectExcel = document.all(objectExcel);

	imgHtml.style.display = "inline";
	spanTable.style.display = "inline";
	imgExcel.style.display = "none";
	objectExcel.style.display = "none";
}

// extended validation routines.

function validMod10 (number) {
	var check;
	var sum = 0;
	var weight = 2;

	if (isNaN(number)) { return false; }
	for (var count = number.length - 2; count >= 0; count--) {
		check = number.charAt(count) * weight;
		if (check > 9) {
			check = check - 9;
		}
		sum = sum + check;
		weight = Math.abs(weight - 3);
	}
	return ((10 - sum % 10) % 10) == number.charAt(number.length - 1);
}
				
function validMod11 (number) {
	var check;
	var sum = 0;
	var weight = 1;

	if (isNaN(number)) { return false; }
	for (var count = number.length - 1; count >= 0; count--) {
		check = number.charAt(count) * weight;
		sum = sum + check;
		weight = (weight % 10) + 1;
	}
	return (sum % 11) == 0;
}

// id validation

function getRsaIdDOB(idno) {
    idno = new String(idno);
    if (idno.length > 5) {
        var yy = val(idno.substr(0, 2)) + 2000;
        if (yy > new Date().getFullYear()) yy -= 100;
        return yy + "-" + idno.substr(2, 2) + "-" + idno.substr(4, 2);
    }
    return "";
}
function getRsaIdDateOB(idno) {
    idno = new String(idno);
    if (idno.length > 5) {
        var yy = val(idno.substr(0, 2)) + 2000;
        var mm = val(idno.substr(2, 2)) - 1;
        var dd = val(idno.substr(4, 2));
        if (yy > new Date().getFullYear()) yy -= 100;
        return new Date(yy, mm, dd);
    }
    return "";
}
function getRsaIdGender(idno) {
    idno = new String(idno);
    if (idno.length > 6) {
        var digit = val(idno.charAt(6));
        if ((digit >= 0) && (digit <= 4)) return "f";   // female
        if ((digit >= 5) && (digit <= 9)) return "m";   // male
    }
    return "";
}
function getRsaIdCitizen(idno) {
    idno = new String(idno);
    if (idno.length > 10) {
        var digit = val(idno.charAt(10));
        if (digit == 0) return "l"; // local
        if (digit == 1) return "f"; // foreigner
    }
    return "";
}
function checkRsaId(idno) {
    idno = new String(idno);
    if (idno.length < 13) return false;

    var tot = 0;   // sum odd numbers
    for (var count = 0; count < 12; count += 2)
        tot += val(idno.charAt(count));

    var buffer = "";   // concat even numbers
    for (var count = 1; count < 12; count += 2)
        buffer += idno.charAt(count);
    
    // sum the result of b * 2
    buffer = (val(buffer) * 2).toString();
    for (var count = 0; count < buffer.length; count++)
        tot += val(buffer.charAt(count));
 
    buffer = tot.toString();
    var digit = 10 - val(buffer.charAt(buffer.length - 1));
    if (digit >= 10) digit = 0;
    return (idno.charAt(12) == digit.toString() ? true : false);
}