// JavaScript Document
<!--
// jump to passed URL
function jumpMenu ( targ,selObj,restore ) {
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}
// for AJAX
var xmlHttp;
var tag;
var tag2;

function grabData ( mode, where, tg ) {
	tag = tg;
	var url = ( '../engine/responseTxt.php?q=' + mode + '&w=' + where );
	xmlHttp = GetXmlHttpObject ();
	if ( xmlHttp == null ) {
		alert ( "Browser does not support HTTP Request" );
		return;
	} 
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open ( "GET", url, true );
	xmlHttp.send ( null );
}

function grabUL ( query, href, tg, txt, tg2 ) {
	tag = tg;
	tag2 = tg2;
	var url;
	
	if ( href == "" ) url = ( '../engine/responseButtons.php?q=' + queryBuilder( query ));
	else url = ( '../engine/responseButtons.php?q=' + queryBuilder( query ) + '&href=' + href );
	
	xmlHttp = GetXmlHttpObject ();
	if ( xmlHttp == null ) {
		alert ( "Browser does not support HTTP Request" );
		return;
	} 
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open ( "GET", url, true );
	xmlHttp.send ( null );
	
	if ( txt != "" ) document.getElementById( tag2 ).innerHTML = txt;
}

// edit this for specific site
function queryBuilder ( id ) {
	var query = "SELECT county FROM sp_county WHERE provinceId = "+id;
	return query;
}

function stateChanged () { 
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		var r = document.getElementById(tag);
		var txt = xmlHttp.responseText;
		if ( txt == "" ) r.innerHTML = "no data";
		else r.innerHTML = txt;
	}
}

function clearContentInDb ( tbl, row, col, tg ) {
	// grab the tag
	tag = tg;
	// build the url
	var url;
	// oooh clear the table of data!
	if ( row == 'clear' ) {
		url = ( '../engine/clearContentInDb.php?clear='+tbl );
	} else {
		// first check that both row and col are set
		if ( row != '' && col != '' ) url = ( '../engine/clearContentInDb.php?t='+tbl+'&r='+row+'&c='+col );
		// oh ok, do we have row content?
		else if ( row != '' ) url = ( '../engine/clearContentInDb.php?t='+tbl+'&r='+row );
		// shit! delete the table! o_O
		else url = ( '../engine/clearContentInDb.php?t='+tbl );
	}
	// AJAXIAN magic!
	xmlHttp = GetXmlHttpObject ();
	if ( xmlHttp == null ) {
		alert ( "Browser does not support HTTP Request" );
		return;
	}
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			// check to see if the tag is set
			if ( tag.indexOf("?tid=") == -1 ) document.getElementById( tag ).innerHTML = xmlHttp.responseText;
			// if not use tag as the appended variable in the url for reloading!
			else eval("parent.location='"+tag+"'");
		}
	}
	xmlHttp.open ( "GET", url, true );
	xmlHttp.send ( null );
}

function GetXmlHttpObject () { 
	var xmlHttp=null;
	try {
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	} catch (e) {
 		//Internet Explorer
 		try {
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		} catch (e) {
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
	return xmlHttp;
}

// new school phonetic password generator using ajax
// o_O
function GeneratePhonetic ( tg ) {
	tag = tg;
	var url = ( '../engine/responsePhonetic.php' );
	xmlHttp = GetXmlHttpObject ();
	if ( xmlHttp == null ) {
		alert ( "Browser does not support HTTP Request" );
		return;
	} 
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			document.getElementById( tag ).value = xmlHttp.responseText;
		}
	}
	xmlHttp.open ( "GET", url, true );
	xmlHttp.send ( null );
}
// random password generator - oldschool
//
function GeneratePassword ( len, noPunc, rLen, tag ) {

    if (parseInt(navigator.appVersion) <= 3) {
        alert("Sorry this only works in 4.0+ browsers");
        return true;
    }

    var length=8;
    var sPassword = "";
    length = len;

    var noPunction = noPunc;
    var randomLength = rLen;

    if (randomLength) {
        length = Math.random();

        length = parseInt(length * 100);
        length = (length % 7) + 6
    }


    for (i=0; i < length; i++) {

        numI = getRandomNum ();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

        sPassword = sPassword + String.fromCharCode(numI);
    }

    document.getElementById( tag ).value = sPassword

    return true;
}

function getRandomNum () {

    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;

    return rndNum;
}

function checkPunc (num) {

    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }

    return false;
}

function clearMe ( formfield ){
	if (formfield.defaultValue==formfield.value)
	formfield.value = ""
}
//-->