// JavaScript Document

//Function to create an XMLHttp Object.
function getxmlhttp (){
	
	//Create a boolean variable to check for valid IE instance.
	var xmlhttp = false;

	//Check if we are using IE.
	try{
		//If the Javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using Internet Explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 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();
	}
	
	return xmlhttp;
}
// END Function to create an XMLHttp Object.


// Function to process an XMLHttpRequest.
function processajax (serverPage, obj, getOrPost, str){
	//Get an XMLHttpRequest object for use.
	xmlhttp = getxmlhttp ();
	if (getOrPost == "get"){
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	} else {
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(str);
	}
}
// END Function to process an XMLHttpRequest.

// Require all checkboxes to be checked
function checkCheckBoxes(theForm) {
	var formok = true;
	for (var i = 0; i < theForm.elements.length; i++){
		if (theForm.elements[i].type == "checkbox") {
			
			if (theForm.elements[i].checked == false ){
				formok = false;
			}
		} 
	}
	
	return formok;
}
// END checkform function

// Require 1 checkbox to be checked
function need1CheckOrRadio(theForm) {
	var isChecked = false;
	var checkCounter = 0;
	var formType = null;
	for (var i = 0; i < theForm.elements.length; i++){
		if (theForm.elements[i].type == "checkbox") {
			var formType = "checkbox";
			if (theForm.elements[i].checked == true ){
				
				checkCounter = checkCounter + 1;
				
			}
		} else {
			if (theForm.elements[i].type == "radio") {
				
				if (theForm.elements[i].checked == true ){
					isChecked = true;
				}
			}
		}
	}
	if (formType == "checkbox") {
		if (checkCounter >= minCheck && checkCounter <= maxCheck) {
				isChecked = true;
			} else {
				if (minCheck == maxCheck) {
					alert("Please select " + minCheck + " items.");
				} else {
					alert("Please select " + minCheck + " to " + maxCheck + " items.");
				}
			}
		
	}
	return isChecked;
}
// END Require 1 checkbox to be checked

//Function to validate the addtask form.
function validatetask (checked, thevalue, thename) {
	
	var nowcount = true;
	
	if (thename == "delete") {
		if (checked == false) {
			nowcount = false;
		}
	}	
	
	if (thename == "disable") {
		if (checked == false) {
			nowcount = false;
		}
	}
	
	if (thename == "approve") {
		if (checked == false) {
			nowcount = false;
		}
	}
	

	
	if (thename == "thisorthat") {
		if (thevalue == "") {
			nowcount = false;
		}
	}
	
	return nowcount;
}
//End validate function.


//Functions to submit a form.

// getformvalues
var aok;
function getformvalues (fobj, valfunc){
	
	var str = "";
	aok = true;
	var val;
	
	//Run through a list of all the objects contained within the form.
	for (var i = 0; i < fobj.elements.length; i++){
		if(valfunc) {
			if(aok == true){
				val = valfunc (fobj.elements[i].checked,fobj.elements[i].value,fobj.elements[i].name);
				if (val == false){
					aok= false;
				}
			}
		}
		
		if (fobj.elements[i].type == "checkbox" && fobj.elements[i].checked == true) {
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		} else {
			if (fobj.elements[i].type == "radio" && fobj.elements[i].checked == true) {
				str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
			} else {
				if (fobj.elements[i].type != "checkbox" && fobj.elements[i].type != "radio") {
					str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
				}
			}
		}
	}
	//Then return the string values.
	return str;
}
// END getformvalues


// submitform function
function submitform (theform, serverPage, objID, valfunc) {
	var file = serverPage;
	var str = getformvalues(theform, valfunc);
	//If the validation is ok.
	if (aok == true) {
		
		obj = document.getElementById(objID);
		obj.innerHTML = "<img src='images/ajax-loader.gif' align='absmiddle' /> Loading...";
		processajax (serverPage, obj, "post", str);
	}
}
// END submitform function


//Relocate to top of page after adding item to art.  Not currently used.
function relocate(hash) {
    if (location.hash != hash) {
        if (document.images)
            location.replace('index.php#' + hash);
        else
            location.href = 'index.php#' + hash;
    }
}
// END Relocate to top of page after adding item to art.  Not currently used.


<!-- This script is based on the javascript code of Roman Feldblum (web.developer@programmer.net) -->
<!-- Original script : http://javascript.internet.com/forms/format-phone-number.html -->
<!-- Original script is revised by Eralper Yilmaz (http://www.eralper.com) -->
<!-- Revised script : http://www.kodyaz.com -->

var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object){
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}

ParseForNumber1(object)

if(keycode >= 48){
ValidatePhone(object)
}
}

function backspacerDOWN(object,e) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}
ParseForNumber2(object)
} 

function GetCursorPosition(){

var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i=0; i<t1.length; i++)
{
if (t1.substring(i,1) != t2.substring(i,1)) {
if(!bool) {
cursorposition=i
bool=true
}
}
}
}

function ValidatePhone(object){

var p = phonevalue1

p = p.replace(/[^\d]*/gi,"")

if (p.length < 3) {
object.value=p
} else if(p.length==3){
pp=p;
d4=p.indexOf('(')
d5=p.indexOf(')')
if(d4==-1){
pp="("+pp;
}
if(d5==-1){
pp=pp+")";
}
object.value = pp;
} else if(p.length>3 && p.length < 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

object.value = pp; 

} else if(p.length >= 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

l40 = pp.length;
p40 = pp.substring(0,8);
p40 = p40 + "-"

p41 = pp.substring(8,l40);
ppp = p40 + p41;

object.value = ppp.substring(0, maxphonelength);
}

GetCursorPosition()

if(cursorposition >= 0){
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 5) {
cursorposition = cursorposition + 2
} else if (cursorposition == 6) {
cursorposition = cursorposition + 2
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
e1=object.value.indexOf(')')
e2=object.value.indexOf('-')
if (e1>-1 && e2>-1){
if (e2-e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 12) {
cursorposition = cursorposition
}

var txtRange = object.createTextRange();
txtRange.moveStart( "character", cursorposition);
txtRange.moveEnd( "character", cursorposition - object.value.length);
txtRange.select();
}

}

function ParseChar(sStr, sChar)
{
if (sChar.length == null) 
{
zChar = new Array(sChar);
}
else zChar = sChar;

for (i=0; i<zChar.length; i++)
{
sNewStr = "";

var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);

while (iEnd != -1)
{
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

sStr = sNewStr;
}

return sNewStr;
}
// End Phone Number functions