String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

// from http://www.dynamicdrive.com/forums/archive/index.php/t-11111.html
function convertName(form) {
value = form.value;
newValue = '';
value = value.split(' ');
for(var i = 0; i < value.length; i++) {
newValue += value[i].substring(0,1).toUpperCase() +
value[i].substring(1,value[i].length) + ' ';
}
form.value = newValue;
}

function stopRKey(evt) {
	var evt  = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text")) { return false; }
}

function showMemberPicWindow(url)
{
	newwindow=window.open(url,'change_pic','height=360,width=360,resizable=false,scrollbars=no,toolbar=no,menubar=no,location=no,status=no,directories=no');
	if (window.focus) {newwindow.focus()}
}

function deleteAnnouncements(din, ad)
{
	var _delete= confirm("Are you sure you want to delete the announcements for " + ad + "?");
	if (_delete)
	{
		window.location="/content/members/delete_announcements.php?din=" + din + "&ad=" + ad;
	}
}

function deleteMember(din, mn)
{
	var _delete= confirm("CAUTION!!\n\nAre you sure you want to delete " + mn + "'s information?\n\nClick OK to delete, otherwise, click Cancel.\n");
	if (_delete)
	{
		window.location="/content/members/delete_member.php?din=" + din;
	}
}

function getSelected(opt) {
	var selected = new Array();
	var index = 0;
	for (var intLoop = 0; intLoop < opt.length; intLoop++) {
		if ((opt[intLoop].selected) || (opt[intLoop].checked)) {
		  index = selected.length;
		  selected[index] = new Object;
		  selected[index].value = opt[intLoop].value;
		  selected[index].index = intLoop;
		}
	}
	return selected;
}

function outputSelected(opt) {
	var sel = getSelected(opt);
	var strSel = "";
	for (var intLoop = 0; intLoop < sel.length; intLoop++) {
		strSel = strSel + "'" + sel[intLoop].value + "'";
		if ((intLoop + 1) < sel.length) {
			strSel += ', ';
		}
	}
	return strSel;
}

function doSearch(theForm) {
	var selectedLocalities=outputSelected(theForm.locality.options);
	theForm.h_locality.value = selectedLocalities;
	theForm.submit();
}

function disableControlsForSubmit(theform) {
	//if IE 4+ or NS 6+
	if (document.all || document.getElementById) {
		//iterate thru every element in the form
		for (i=0; i<theform.length; i++) {
			var tempobj = theform.elements[i];
			if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
			tempobj.disabled=true
		}
	}
}


function submitInfoForm(theForm) {
	disableControlsForSubmit(theForm);
	theForm.submit();
}

function sha1Hash(msg)
{
    // constants [§4.2.1]
    var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];


    // PREPROCESSING
 
    // add trailing '1' bit to string [§5.1.1]
    msg += String.fromCharCode(0x80);

    // convert string msg into 512-bit/16-integer
    // blocks arrays of ints [§5.2.1]

    // long enough to contain msg plus 2-word length
    var l = Math.ceil(msg.length/4) + 2; 
    // in N 16-int blocks
    var N = Math.ceil(l/16);             
    var M = new Array(N);
    for (var i=0; i<N; i++) {
        M[i] = new Array(16);
        // encode 4 chars per integer, big-endian encoding
        for (var j=0; j<16; j++) { 
            M[i][j] = (msg.charCodeAt(i*64+j*4)<<24) |
                          (msg.charCodeAt(i*64+j*4+1)<<16) |
                          (msg.charCodeAt(i*64+j*4+2)<<8) |
                          (msg.charCodeAt(i*64+j*4+3));
        }
    }
    // add length (in bits) into final pair of 32-bit integers
    // (big-endian) [5.1.1]
    // note: most significant word would be
    // ((len-1)*8>>> 32, but since JS converts
    // bitwise-op args to 32 bits, we need to simulate
    // this by arithmetic operators
    M[N-1][14] = ((msg.length-1)*8) / Math.pow(2, 32);
    M[N-1][14] = Math.floor(M[N-1][14]);
    M[N-1][15] = ((msg.length-1)*8) & 0xffffffff;

    // set initial hash value [§5.3.1]
    var H0 = 0x67452301;
    var H1 = 0xefcdab89;
    var H2 = 0x98badcfe;
    var H3 = 0x10325476;
    var H4 = 0xc3d2e1f0;

    // HASH COMPUTATION [§6.1.2]

    var W = new Array(80); var a, b, c, d, e;
    for (var i=0; i<N; i++) {

        // 1 - prepare message schedule 'W'
        for (var t=0;  t<16; t++)
            W[t] = M[i][t];
        for (var t=16; t<80; t++)
            W[t] = ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);

        // 2 - initialise five working variables
        // a, b, c, d, e with previous hash value
        a = H0; b = H1; c = H2; d = H3; e = H4;

        // 3 - main loop
        for (var t=0; t<80; t++) {
            // seq for blocks of 'f' functions and 'K' constants
            var s = Math.floor(t/20);
            var T = (ROTL(a,5) + f(s,b,c,d) + e + K[s] + W[t])
                          & 0xffffffff;
            e = d;
            d = c;
            c = ROTL(b, 30);
            b = a;
            a = T;
        }

        // 4 - compute the new intermediate hash value
       
        // note 'addition modulo 2^32'
        H0 = (H0+a) & 0xffffffff; 
        H1 = (H1+b) & 0xffffffff;
        H2 = (H2+c) & 0xffffffff;
        H3 = (H3+d) & 0xffffffff;
        H4 = (H4+e) & 0xffffffff;
    }

    return H0.toHexStr() + H1.toHexStr() + H2.toHexStr() +
                H3.toHexStr() + H4.toHexStr();
}

//
// function 'f' [§4.1.1]
//
function f(s, x, y, z)
{
    switch (s) {
    case 0: return (x & y) ^ (~x & z);           // Ch()
    case 1: return x ^ y ^ z;                    // Parity()
    case 2: return (x & y) ^ (x & z) ^ (y & z);  // Maj()
    case 3: return x ^ y ^ z;                    // Parity()
    }
}

//
// rotate left (circular left shift) value x
// by n positions [§3.2.5]
//
function ROTL(x, n)
{
    return (x<<n) | (x>>>(32-n));
}

//
// extend Number class with a tailored hex-string method
//   (note toString(16) is implementation-dependant, and
//   in IE returns signed numbers when used on full words)
//
Number.prototype.toHexStr = function()
{
    var s="", v;
    for (var i=7; i>=0; i--) {
        v = (this>>>(i*4)) & 0xf; s += v.toString(16); }
    return s;
}

function login(theForm) {
	theForm.user_password.value=sha1Hash(theForm.user_password.value);
	theForm.action="process_login.php";
	theForm.submit();
}
