/**
 * DHTML textbox character counter script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
maxL=500;
var bName = navigator.appName;
function taLimit(taObj) {
	if (taObj.value.length==maxL) return false;
	return true;
}

function taCount(taObj,Cnt) { 
	objCnt=createObject(Cnt);
	objVal=taObj.value;
	if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
	if (objCnt) {
		if(bName == "Netscape"){	
			objCnt.textContent=maxL-objVal.length;}
		else{objCnt.innerText=maxL-objVal.length;}
	}
	return true;
}
function createObject(objId) {
	if (document.getElementById) return document.getElementById(objId);
	else if (document.layers) return eval("document." + objId);
	else if (document.all) return eval("document.all." + objId);
	else return eval("document." + objId);
}

//----------------------------JUMP MENU-----------------------------
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
/*--------------------------SEARCH FORM----------------------------*/
// specify the name of your form
var thisForm = "form_search";

// load field names and default values into list
var defaultVals = new Array();
defaultVals[0] = new Array("sw", "Enter keyword to search");
//defaultVals[1] = new Array("email", "(your email address here)");
//defaultVals[2] = new Array("phone", "(your phone number here)");

// populate fields with default values on page load
function MPLoadDefaults() {
with (document.forms[thisForm]) {
for (var n=0; n<defaultVals.length; n++) {
var thisField = defaultVals[n][0];
var thisDefault = defaultVals[n][1];
if (elements[thisField].value == '')
elements[thisField].value = thisDefault;
}}}

// clear default value from field when selected
function MPClearField(field) {
var fieldName = field.name;
for (var n=0; n<defaultVals.length; n++) {
var thisField = defaultVals[n][0];
var thisDefault = defaultVals[n][1];
if (thisField == fieldName) {
if (field.value == thisDefault) field.value = '';
break;
}}}

// clear all defaults when form is submitted
function MPClearAll() {
with (document.forms[thisForm]) {
for (var n=0; n<defaultVals.length; n++) {
var thisField = defaultVals[n][0];
var thisDefault = defaultVals[n][1];
if (elements[thisField].value == thisDefault)
elements[thisField].value = '';
}}}

//----------------------------FORMS VALIDATION-----------------------------
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_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n\n'+errors);
  document.MM_returnValue = (errors == '');
}

function limitText (oTextElement, minLength, type) {
if(oTextElement.type == "text" || oTextElement.type == "password") {
if(oTextElement.value.length < minLength) {
//alert('Username already existed. Please try again!'); history.go(-1);
	
alert("You must enter at least " + minLength + " characters. Please try again!");
oTextElement.focus();
// or simply push him back to complete the number of characters

}
}
}

function AcceptDigits(objtextbox)
{
var exp = /[^\d]/g;
objtextbox.value = objtextbox.value.replace(exp,'');
}

function limitlength(obj, length){
var maxlength=length
if (obj.value.length>maxlength)
obj.value=obj.value.substring(0, maxlength)
}

//-->
function validatePwd() {
//var invalid = " "; // Invalid character is a space
//var minLength = 6; // Minimum length
var pw1 = document.registration.password.value;
var pw2 = document.registration.confirmpassword.value;
if (pw1 != pw2) {
		alert('Passwords did not match. Please try again.');
		document.registration.confirmpassword.focus();
		return false ;
	  } 
}

function reallyCenterWindow() {
leftPos = 0
topPos = 0
if (screen) {
leftPos = (screen.width / 2) - 220
topPos = (screen.height / 2) - 290
}
ElementWindow = window.open('videos/index.php','ElementWin',
'width=440,height=580,menubar=no,resizable=no,status=no,left='+leftPos+',top='+topPos)

} 
function orderProducts() {
leftPos = 0
topPos = 0
if (screen) {
leftPos = (screen.width / 2) - 180
topPos = (screen.height / 2) - 110
}
ElementWindow = window.open('../../order/index.php','ElementWin',
'width=360,height=220,menubar=no,resizable=no,status=no,left='+leftPos+',top='+topPos)

} 

function viewOrders() {
leftPos = 0
topPos = 0
if (screen) {
leftPos = (screen.width / 2) - 250
topPos = (screen.height / 2) - 150
}
ElementWindow = window.open('administrator/view-order.php?&code=$code','ElementWin',
'width=500,height=300,menubar=no,toolbar=no,resizable=no,status=no,left='+leftPos+',top='+topPos)

} 

/***************** AJAX ********************/
	<!--
	
	//Create a boolean variable to check for a valid Internet Explorer instance.
	var xmlhttp = false;
	
	
	//Check if we are using IE.
	try {
		//If the Javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		//alert ("You are using Microsoft Internet Explorer.");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using Internet Explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			//alert ("You are using Microsoft Internet Explorder");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}
	
	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
		//alert ("You are not using Microsoft Internet Explorer");
	}
	
	function makerequest(serverPage, objID) {
		
		var obj = document.getElementById(objID);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	}
	
	//-->
/***************** FORM VALIDATIONS ********************/

function checkemail(str){
	var testresults
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		testresults=true
	else{
		testresults=false
	}
	return (testresults)
}

function validateSignUp(form){
	
	if (form.username.value == "") {
		alert('Please enter your Username');
		form.username.focus();
		return false ;
	  } 
	if (form.password.value == "") {
		alert('Please enter a Password');
		form.password.focus();
		return false ;
	  } 	  
	if (form.confirmpass.value == "") {
		alert('Please Re-confirm your password.');
		form.confirmpass.focus();
		return false ;
	  } 	  
	if (form.confirmpass.value != form.password.value) {
		alert('Passwords do not match. Please re-enter your password.');
		form.confirmpass.focus();
		return false ;
	  } 
	  if(form.email.value == '' || !checkemail(form.email.value)){
		alert('Please enter a valid email address!');
		form.email.focus();
		return false ;
	  } 
}

function validateContact(form){
	
	 if (form.visitor.value == "") {
		alert('Please enter your Name');
		form.visitor.focus();
		return false ;
	  } 
	 if(form.visitormail.value == '' || !checkemail(form.visitormail.value)){
		alert('Please enter a valid email address!');
		form.visitormail.focus();
		return false ;
	  } 
	 if (form.attn.value == "") {
		alert('Please enter your Subject');
		form.attn.focus();
		return false ;
	  } 
	   if (form.contact.value == "") {
		alert('Please enter your Contact Number');
		form.contact.focus();
		return false ;
	  }
	    if (form.notes.value == "") {
		alert('Please enter your Message');
		form.notes.focus();
		return false ;
	  }
	     if (form.code.value == "") {
		alert('Please enter Code');
		form.code.focus();
		return false ;
	  } 
}

function validateGB(form){
	
	 if (form.from.value == "") {
		alert('Please enter your Location.');
		form.from.focus();
		return false ;
	  }
	   if (form.email.value == "") {
		alert('Please enter your Email.');
		form.email.focus();
		return false ;
	  } 
	   if (form.mysecnum.value == "") {
		alert('Please enter your Verification Code.');
		form.mysecnum.focus();
		return false ;
	  } 
}


function validateSearch(form){
	
	 if (form.sw.value == "") {
		alert('Please enter text to search.');
		form.sw.focus();
		return false ;
	  }
}

function validateSubject(form){
	
	if (form.subjcode.value == "") {
		alert('Please enter a Subject Code');
		form.subjcode.focus();
		return false ;
	}
	
	if (form.schedcode.value == "") {
		alert('Please enter a Schedule Code');
		form.schedcode.focus();
		return false ;
	} 
	
	if (form.description.value == "") {
		alert('Please enter a Description');
		form.description.focus();
		return false ;
	} 
	
	var x = 0;
	for(var i = 0;i < form.semester.length; i++){
		if(form.semester[i].checked)
			x = 1;
	}
	
	if(x==0){
		alert("Please select a semester");
		form.semester[0].focus();
		return false;
	}
	
	if (form.year.value == 0) {
		alert('Please select a School Year');
		form.year.focus();
		return false ;
	} 
	
	if (form.syllabus.value == 0) {
		alert('Please select a Syllabus');
		form.syllabus.focus();
		return false ;
	}	
}

function validateStud(form){
	if(form.subject.value == 0){
		alert("Please choose a subject");
		form.subject.focus();
		return false ;
	}
	
	if(form.student.value == ""){
		alert("Please select your student/s");
		form.subject.focus();
		return false;
	}
	var myArr = new Array();
	var x = 0;
	for(var i=0;i<form.student.options.length;i++){
		if(form.student.options[i].selected == true){
			myArr[i] = form.student.options[i].value;
			x++;
		}
	}
	
	if(x == 0){
		alert("Please select your student/s");
		form.subject.focus();
		return false;
	}
	
	form.studz.value = myArr.concat(",");
	//alert(form.studz.value);
	return true;
}

function promptMe(username,task,id2){
	/*var nxt = '';
	if(id2!=''){
		nxt = "&username="+id2;
	}*/
	if(confirm("Are you sure you want to delete this record?")){
		location.href = "administrator/save.php?task=delete&username="+username;
	}
}

function promptMe1(username,task,id2){
	/*var nxt = '';
	if(id2!=''){
		nxt = "&username="+id2;
	}*/
	if(confirm("Are you sure you want to delete your account?")){
		location.href = "member/save.php?task=delete&username="+username;
	}
}
function promptMe2(id,task,id2){
	/*var nxt = '';
	if(id2!=''){
		nxt = "&username="+id2;
	}*/
	if(confirm("Are you sure you want to delete this record?")){
		location.href = "administrator/delete-dealer.php?task=delete&id="+id;
	}
}
function promptMe3(username,task,id2){
	/*var nxt = '';
	if(id2!=''){
		nxt = "&username="+id2;
	}*/
	if(confirm("Are you sure you want to delete this record?")){
		location.href = "administrator/delete-order.php?task=delete&username="+username;
	}
}
function promptMe4(id,task,id2){
	/*var nxt = '';
	if(id2!=''){
		nxt = "&username="+id2;
	}*/
	if(confirm("Are you sure you want to delete this record?")){
		location.href = "administrator/delete-article.php?task=delete&id="+id;
	}
}
function promptMe5(id,task,id2){
	/*var nxt = '';
	if(id2!=''){
		nxt = "&username="+id2;
	}*/
	if(confirm("Are you sure you want to delete this record?")){
		location.href = "administrator/delete-farmer.php?task=delete&id="+id;
	}
}

function promptMe6(id,task,id2){
	/*var nxt = '';
	if(id2!=''){
		nxt = "&username="+id2;
	}*/
	if(confirm("Are you sure you want to delete this record?")){
		location.href = "administrator/delete-order-unit.php?task=delete&id="+id;
	}
}

function promptMe7(id,task,id2){
	/*var nxt = '';
	if(id2!=''){
		nxt = "&username="+id2;
	}*/
	if(confirm("Are you sure you want to delete this record?")){
		location.href = "administrator/delete-product-code.php?task=delete&id="+id;
	}
}

function promptMe8(id,task,id2){
	/*var nxt = '';
	if(id2!=''){
		nxt = "&username="+id2;
	}*/
	if(confirm("Are you sure you want to delete this record?")){
		location.href = "administrator.php?opt=recipes&task=delete&id="+id;
	}
}
function chkLogin(form){
	if (form.username.value == "") {
		alert('Please your Username!');
		form.username.focus();
		return false ;
	} 
	if (form.password.value == "") {
		alert('Please enter your Password!');
		form.password.focus();
		return false ;
	}

}

function chkAnnouncement(form){
	if(form.announce.value.length < 1){
		form.announce.focus();
		alert("Please enter an announcement");
		return false;
	}	
}

function chkFilter(form){
	if(form.subject.value == 0){
		alert("Please choose a subject");
		form.subject.focus();
		return false;
	}
}

function chkUpload(form){
	if(form.file.value == 0){
		alert("Please select a file to upload");
		form.file.focus();
		return false;
	}
	
	if(form.caption.value == 0){
		alert("Please put a caption");
		form.caption.focus();
		return false;
	}
}

function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}

function mailto(domain,user) 
    { 
        document.location.href = "mailto:" + user + "@" + domain; 
    }
	

function killerrors(){return true;}window.onerror = killerrors;
function saveselection() {if (document.form_search.sw) document.cookie='sw='+escape(document.form_search.sw.value);};function readcookie(sname){var c=document.cookie;if (c.length<0) return false;var b=c.indexOf(sname+'=');if (b==-1) return false;var d=c.indexOf(';',b);if (d==-1) d=c.length;return unescape(c.substring(b+sname.length+1,d));};function readselection() {if (document.form_search.sw) {c=readcookie('sw');if (c) document.form_search.sw.value=c;};};
function OnSearchSubmit(){var b_return=true;if (document.form_search.sw){for(var i=0;i<document.form_search.sw.value.length;i++){if (document.form_search.sw.value.charAt(i)!=" ")	{lh="searchresult.php?sw="+encodeURIComponent(document.form_search.sw.value);b_return=false;break;}}}if (b_return) return;saveselection();window.open(lh,"_self");}