function myclear(field) {
	if (field.value == field.defaultValue)
		field.value = '';
}

function checkfields() {
	s = document.getElementById("school");
	if (s.value == "-- Enter your school's name --")
		s.value = "";

	k = document.getElementById("keywords");
	if (k.value == " -- Enter keywords -- ")
		k.value = "";
}

function changedropdown(region,school,skipblank) {
	document.getElementById('school').options.length = 0;
	if (region == '') {
		return;
	}
	for (i = skipblank; i < schooloptions[region].length; i++) {
		document.getElementById('school').options[i - skipblank] = schooloptions[region][i];
		if (schooloptions[region][i].value == school) {
			document.getElementById('school').selectedIndex = i - skipblank;
		}
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

var req;
var s;
var r;
var action;
var followregion = '';
var followschool = '';

function loadXMLDoc(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() {
    if (req.readyState == 4) {
        if (req.status == 200) {
        	// different action depending on action
        	if (action == 'getregions') {
        		t = r;
        		tagname = 'region';
        		option = 'an LEA';
        	} else {
        		t = s;
        		tagname = 'school';
        		option = 'a school';
        	}
        	selected = 0;
        	options = req.responseXML.documentElement.getElementsByTagName(tagname)
        	t.options[0] = new Option(' -- Select ' + option + ' --', '');
					for (i = 0; i < options.length; i++) {
						t.options[i+1] = new Option(options[i].attributes.getNamedItem("name").value, options[i].attributes.getNamedItem("id").value);
						if (options[i].attributes.getNamedItem("selected").value == '1') {
							selected = i + 1;
						}
					}
					t.disabled = false;
					t.selectedIndex = selected;

					// do school now if follow is set
					if (followregion != '') {
						//alert(followregion + "!" + followschool);
						updateschool(followregion, '', followschool);
						followregion = '';
						followschool = '';
					}
         } else {
	         alert("There was a problem retrieving the XML data:\n" + req.statusText);
         }
    }
}

function updateregion(area, selected) {
	s = document.getElementById('school');
	s.options.length = 0;
	s.disabled = true;
	r = document.getElementById('region');
	r.options.length = 0;
	r.disabled = true;
	if (area != 0) {
		r.options[0] = new Option('Please wait...');
		// get XML data for this area
		action = 'getregions';
		loadXMLDoc('getregions.php?area=' + area + '&selected=' + selected);
	}
}

function updateschool(region, blank, selected) {
	s = document.getElementById('school');
	s.options.length = 0;
	s.disabled = true;
	if (region != 0) {
		s.options[0] = new Option('Please wait...');
		// get XML data for this region
		action = 'getschools';
		loadXMLDoc('getschools.php?region=' + region + "&blank=" + blank + "&selected=" + selected);
	}
}


function updateregionandschool(area, region, school) {
	followregion = region;
	followschool = school;
	updateregion(area, region);
}


// Email Validation. Written by PerlScriptsJavaScripts.com

function check_email(e) {
ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

for(i=0; i < e.length ;i++){
if(ok.indexOf(e.charAt(i))<0){ 
return (false);
}	
} 

if (document.images) {
re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (!e.match(re) && e.match(re_two)) {
return (-1);		
} 

}

}

function check_form(f) { // f is the form (passed using the this keyword)

// check the first email address ( the exclamation means "not" )
if(!check_email(f.visitoremail.value)){
alert("Invalid email detected.");
f.visitoremail.focus(); 
// if the browser is Netscape 6 or IE
if(document.all || document.getElementByID){
// change the color of text field
f.visitoremail.style.background = "yellow";
}
// make sure the form is not submitted
return false;
}

// check the second email address
if(!(f.friendemail_1.value.length==0)){
if(!check_email(f.friendemail_1.value)){
alert("Invalid email detected.");
f.friendemail_1.focus(); 
if(document.all || document.getElementByID){
f.friendemail_1.style.background = "yellow";
}
return false;
}
}


// check the second email address
if(!(f.friendemail_2.value.length==0)){
if(!check_email(f.friendemail_2.value)){
alert("Invalid email detected.");
f.friendemail_2.focus(); 
if(document.all || document.getElementByID){
f.friendemail_2.style.background = "yellow";
}
return false;
}
}


// check the third email address
if(!(f.friendemail_3.value.length==0)){
if(!check_email(f.friendemail_3.value)){
alert("Invalid email detected.");
f.friendemail_3.focus(); 
if(document.all || document.getElementByID){
f.friendemail_3.style.background = "yellow";
}
return false;
}
}


// check the fourth email address
if(!(f.friendemail_4.value.length==0)){
if(!check_email(f.friendemail_4.value)){
alert("Invalid email detected.");
f.friendemail_4.focus(); 
if(document.all || document.getElementByID){
f.friendemail_4.style.background = "yellow";
}
return false;
}
}

// check the fifth email address
if(!(f.friendemail_5.value.length==0)){
if(!check_email(f.friendemail_1.value)){
alert("Invalid email detected.");
f.friendemail_5.focus(); 
if(document.all || document.getElementByID){
f.friendemail_5.style.background = "yellow";
}
return false;
}
}

if(f.friendemail_5.value.length+f.friendemail_4.value.length+f.friendemail_3.value.length+f.friendemail_2.value.length+f.friendemail_1.value.length==0)
{
	alert("You have to enter at least one email address")
	return false;
	}


}


function preview() {
	document.email2friend.task.value='preview';
	if(check_form(document.email2friend)!=false)
		{
			document.email2friend.submit();
	    }
}

function send() {
	document.email2friend.task.value='send';
	if(check_form(document.email2friend)!=false)
		{
			document.email2friend.submit();
	    }
}



function emailgoback() {
	document.preview.submit();
}

function emailsend() {
	document.preview.task.value='send';
	{
			document.preview.submit();
	    }
}


function add_tokens(user,bid) {
var amount=prompt("Enter the number of tokens you would like to add: ","0");
window.location='bulkbuyer.php?task=add&amount='+amount+'&bid='+bid+'&user='+user;
}

function sub_tokens(user,bid) {
var amount=prompt("Enter the number of tokens you want to substract: ","0");
window.location='bulkbuyer.php?task=sub&amount='+amount+'&bid='+bid+'&user='+user;
}


function remove_user(user)
{
	input_box=confirm("Do you really want to delete user: "+user);
	if (input_box==true)
	{ 
	// Output when OK is clicked
		window.location='bulkbuyer.php?task=remove&user='+user;
	}

}


function add_bulk_tokens(user_id) {
var amount=prompt("Enter the number of tokens you would like to add: ","0");
window.location='viewdetails.php?query=bulkbuyer&task=add&amount='+amount+'&user='+user_id;
}

function sub_bulk_tokens(user_id) {
var amount=prompt("Enter the number of tokens you want to substract: ","0");
window.location='viewdetails.php?query=bulkbuyer&task=sub&amount='+amount+'&user='+user_id;
}


function remove_bulk_user(user_id)
{
	input_box=confirm("Do you really want to delete user: "+user);
	if (input_box==true)
	{ 
	// Output when OK is clicked
		window.location='viewdetails.php?query=bulkbuyer&task=remove&user='+user_id;
	}

}