var csshoverParsed=false;
var ValidationPhoneFormat=null;

//Used for delete butttons, confirmation popup is added to delete action
function ConfirmPostback(ConfirmText,ControlName,Action){
    if (confirm(ConfirmText)){
        __doPostBack(ControlName,Action);
    }
}
function addLoadEvent(func) {
  //pass this function the name of a function to add to the onload.
  //example addLoadEvent(MyOnloadFunction);
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function Mid(str, start, n) {
    if (start < 0 || n<0) {
        return str;
    }
    var iEnd,iLen=String(str).length;
    if (start + n > iLen) {
        iEnd=iLen;
    }else{
        iEnd=start+n;
    }
    return String(str).substring(start,iEnd);
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images ) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

//var preloadFlag = false;
//function preloadImages() {

	//if (document.images) {
		//Banner_01_over = newImage("images/MainBanner_01-over.gif");
		//MainBanner_03_over = newImage("images/MainBanner_03-over.gif");
		//MainBanner_05_over = newImage("images/MainBanner_05-over.gif");
		//MainBanner_06_over = newImage("images/MainBanner_06-over.gif");
		//MainBanner_07_over = newImage("images/MainBanner_07-over.gif");
		//preloadFlag = true;
	//}
//}

function popup(mylink, windowname)
{
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;
	window.open(href, "ddsf", 'width=790,height=590,scrollbars=yes');
	return false;
}

function putFocus(formInst, elementInst) { 
 		if (document.forms.length > 0) {
			if (document.forms[formInst].elements[elementInst].type.toLowerCase() != "hidden") {
				document.forms[formInst].elements[elementInst].focus();
			}else{
				if (document.forms[formInst].elements[elementInst+1].type.toLowerCase() != "hidden") {
				document.forms[formInst].elements[elementInst+1].focus();
				}
			}
	  }
}
	
function goNow(str,val) {
   if (confirm(str)) {
		document.location=val;
       }
}

//2009-10-14 MPK comment this function out because it is always overridden on the pages that use it.
//function goNowReturn(str,val) {
//   if (confirm(str)) {
//		document.location=val;
//		return true;
//       }else{return false;}
//}
// 
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];}}
}

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
function ToggleShowDebugSQLDivs(){
	var allDIVS = document.getElementsByTagName('div');
	//alert(allDIVS.length);
	var i
	for(i=0;i<allDIVS.length;i++){
		if (allDIVS.item(i).id == 'DivShowDebugSQL'){
			if (allDIVS.item(i).style.display != "none") {
				allDIVS.item(i).style.display="none";
			}else{
				allDIVS.item(i).style.display="block";
			}
		}
	}
	//alert(allDIVS.item(0).id);
	//var el, i = 0;
	//while (el = allDIVS.item(i++)) 
	//if (typeof el.style != 'undefined') {
		//alert(el.id);
	//	}
	//}
	//alert(document.DivShowDebugSQL.length);
}
function ToggleHideDebugStuffDivs(){
	var allDIVS = document.getElementsByTagName('div');
	//alert(allDIVS.length);
	var i
	for(i=0;i<allDIVS.length;i++){
		if (allDIVS.item(i).id == 'HideDebugStuff'){
			if (allDIVS.item(i).style.display != "none") {
				allDIVS.item(i).style.display="none";
			}else{
				allDIVS.item(i).style.display="block";
			}
		}
	}
}
function autoTab(input,len, e) {
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}

function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}

function phoneTab(input, len, e) {
	if (input.value.length >= len && ((e.keyCode > 47 && e.keyCode < 58) || ( e.keyCode > 95 && e.keyCode < 106 ))  ) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input) + 1) % input.form.length].focus();
	}

	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input) index = i;
		else i++;
		return index;
	}
	return true;
}

function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}

var xmlhttp

function loadXMLDoc(url,procfunction)
{
// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  eval('xmlhttp.onreadystatechange=' + procfunction + ';');
  xmlhttp.open("GET",url,true)
  xmlhttp.send(null)
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
    eval('xmlhttp.onreadystatechange=' + procfunction + ';');
    xmlhttp.open("GET",url,true)
    xmlhttp.send()
    }
  }
}
function xmlhttpCheckDBMode()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
  if (trim(xmlhttp.responseText) != 'true'){
  	alert('You are not in the correct data mode to run this page.  You will need to start from the main menu.');
	window.location='menu.asp';
  }
  }
}
function CheckPhone(PhoneID1,PhoneID2,PhoneID3,FormatType) {
//This function will take the three inputs from the phone and meld them together, checking that it is a valid 10 digit phone
    var Phone1 = document.getElementById(PhoneID1).value;
    var Phone2 = document.getElementById(PhoneID2).value;
    var Phone3 = document.getElementById(PhoneID3).value;
    var FullPhone = Phone1.toString() + Phone2.toString() + Phone3.toString();
    if (FormatType != null) {
        return FormatType.test(FullPhone);
    }
    else {
        if (ValidationPhoneFormat == null) ValidationPhoneFormat = /^[2-9]\d{2}\d{3}\d{4}$/;
        return ValidationPhoneFormat.test(FullPhone);
    }
}
function CheckEmail(EmailID) {
//This function will make sure the email is in the valid format.
//User request allow spaces infront or after email and trim them in codebehind
    var Email = document.getElementById(EmailID).value;
    var Expression = /^(\s*)[\w-\.\+]+@([\w-]+\.)+[\w-]{2,4}(\s*)$/;
    if (Expression.test(Email)) {
        return true;
    }else{
        return false;
    }
}
function DecryptEmail(EncryptedEmail){
    //this function used to hide real email addresses.
    EncryptedEmail=EncryptedEmail.replace('^','@')
    EncryptedEmail=EncryptedEmail.replace('~~','mailto')
    EncryptedEmail=EncryptedEmail.replace('|','.')
    document.write(EncryptedEmail)
}
function StopAutoDial(){
    if(parent){
        if (confirm('Are you sure you want to stop taking calls?')){
            parent.window.location='/';
        }
    }
}
function FindInList(List, SearchFor, Delimiter) {
    SearchFor = SearchFor + '';
    List = List + '';
    SearchFor = SearchFor.toLowerCase();
    var listArr = List.toLowerCase().split(Delimiter);
    for (var i = 0; i < listArr.length; i++) {
        if (listArr[i] == SearchFor) {
            return i;
        }
    }
    return -1;
}
function RemoveInListAt(List, Location, Delimiter) {
    if (Location < 0) {
        return List;
    }
    List = List + '';
    var listArr = List.split(Delimiter);
    eval('listArr = List.split(/' + Delimiter + '/i)'); //this is case insensitive.
    var NewList = '';
    for (var i = 0; i < listArr.length; i++) {
        if (i != Location) {
            if (NewList != '') {
                NewList = NewList + Delimiter;
            }
            NewList = NewList + listArr[i];
        }
    }
    return NewList;
}
function RemoveInList(List, SearchFor, Delimiter) {
    return RemoveInListAt(List, FindInList(List, SearchFor, Delimiter), Delimiter);
}


function VANUploaderOpen(BaseLocation, LocationID, AdditionalParameters) {
	var additionalQueryString = '';
	if (AdditionalParameters !== undefined) {
		additionalQueryString = AdditionalParameters;
	}
	var UploadedWindow = window.open('/FileUploader.aspx?Base=' + BaseLocation + '&FID=' + LocationID + additionalQueryString, 'VANFileUploader', 'width=355,height=250,toolbars=no,menubar=no,location=no,resizable=yes', '');
}
function VANUploaderSaveFile(BaseLocation, LocationID, FileID, FileName) {
    var MultipleFileMode = false;
    var tmpobj = document.getElementById(BaseLocation + LocationID + '_Multiple');
    if (tmpobj != null) {
        if (tmpobj.value = '1') {
            MultipleFileMode = true;
        }
    }
    var MultipleFiles = false;
    var tmpobj = document.getElementById(BaseLocation + LocationID + '_FileID');
    if (tmpobj != null) {
        if (tmpobj.value != '' && MultipleFileMode) {
            MultipleFiles = true;
            tmpobj.value = tmpobj.value + ',' + FileID;
        } else {
            tmpobj.value = FileID;
        }
    }
    var tmpobj = document.getElementById(LocationID + '_FileDisplay');
    if (tmpobj != null) {
        var rowNum=tmpobj.rows.length;
        if (tmpobj.rows.length>0) {
            rowNum=tmpobj.rows.length-1;
        }
        var newRow = tmpobj.insertRow(rowNum);
        newRow.id='Row_' + FileID;
        var newCell1 = newRow.insertCell(0);
        newCell1.style.borderColor='white';
        newCell1.style.borderBottomStyle = 'solid';
        newCell1.valign = 'bottom';
        newCell1.innerHTML= FileName;
        var newCell2 = newRow.insertCell(1);
        newCell2.width='20%';
        newCell2.align='center';
        newCell2.style.borderColor='white';
       
        newCell2.style.borderStyle='solid';
        newCell2.innerHTML = '<a href="javascript:VANUploaderRemoveFile(\'' + BaseLocation + '\',\'' + LocationID + '\',\'' + FileID + '\');">Delete</a>'
        if (MultipleFileMode) {
            newCell2.style.borderWidth = '0px 0px 1px 1px';
            newCell1.style.borderBottomWidth = '1px';
        }
        else {
            newCell2.style.borderWidth = '0px 0px 0px 1px'; 
            newCell1.style.borderBottomWidth = '0px';
        }
    }
    var tmpobj = document.getElementById(BaseLocation + LocationID + '_FileName');
    if (tmpobj != null) {
        if (MultipleFiles) {
            tmpobj.value = tmpobj.value + ',' + FileName;
        } else {
            tmpobj.value = FileName;
        }
    }
    if (!MultipleFileMode) {
        var tmpobj = document.getElementById(BaseLocation + LocationID + '_FileLink');
        if (tmpobj != null) {
            tmpobj.parentNode.parentNode.style.display = 'none';            
        }
    }
}
function VANUploaderRemoveFile(BaseLocation, LocationID, FileID) {
    var MultipleFileMode = false;
    var tmpobj = document.getElementById(BaseLocation + LocationID + '_Multiple');
    if (tmpobj != null) {
        if (tmpobj.value = '1') {
            MultipleFileMode = true;
        }
    }
    if (MultipleFileMode) {
        var FileToRemoveLocation = -1;
        var tmpobj = document.getElementById(BaseLocation + LocationID + '_FileID');
        if (tmpobj != null) {
            if (tmpobj.value != '') {
                FileToRemoveLocation = FindInList(tmpobj.value, FileID, ',');
            }
        }
    }
    var tmpobj = document.getElementById(BaseLocation + LocationID + '_FileID');
    if (tmpobj != null) {
        if (MultipleFileMode) {
            tmpobj.value = RemoveInListAt(tmpobj.value, FileToRemoveLocation, ',');
        } else {
            tmpobj.value = '';
        }
    }
    var tmpobj = document.getElementById('Row_' + FileID);
    if (tmpobj != null) {
        tmpobj.style.display='none';
    }
    var tmpobj = document.getElementById(BaseLocation + LocationID + '_FileName');
    if (tmpobj != null) {
        if (MultipleFileMode) {
            tmpobj.value = RemoveInListAt(tmpobj.value, FileToRemoveLocation, ',');
        } else {
            tmpobj.value = '';
        }
    }
    if (!MultipleFileMode) {
        var tmpobj = document.getElementById(BaseLocation + LocationID + '_FileLink');
        if (tmpobj != null) {
            //hide the entire row of the link
            tmpobj.parentNode.parentNode.style.display = '';
        }
    }
}
//Classes
function VANAjaxRequestClass(){
    //This Class is used to make ajax requests on the van.

    //Example Call
    //Function to accept return data
    //function AjaxTestReturnData(returndata){
    //    alert(returndata);
    //}
    //var AjaxTest=new VANAjaxRequestClass;
    //AjaxTest.setURL('/AjaxData.aspx?AjaxDataID=50&VanID=');
    //AjaxTest.sendAjaxRequest("AjaxTestReturnData");

    this.PostOptions=null;  //allows you to set post options.
    this.url="";
    this.useHttpGET=false;
    
    //Function defs
    this.setURL=setURL; //Function that makes sure we append rnd and such.
    this.init_ajax_object=init_ajax_object; //Creates the XMLHTTP object.
    this.sendAjaxRequest=sendAjaxRequest; //Sends the request and links it to the callback function
    
    function setURL(url){
        var appendvar='?';
        if (url.indexOf('?') > 0){
            appendvar='&';
        }
        url=url + appendvar + "rnd="+ Math.random();
        this.url=url;
    }
    function init_ajax_object() {
        var x;
        try {
                x=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                        x=new ActiveXObject("Microsoft.XMLHTTP");
                } catch (oc) {
                        x=null;
                }
        }
        if(!x && typeof XMLHttpRequest != "undefined")
                x = new XMLHttpRequest();
        if (x)
                return x;
    }
    function sendAjaxRequest(return_func){
        with(this)
        {
            if (url==""){
                alert('Ajax Failure. URL empty.');
                return;
            }
            var x=init_ajax_object();
            
            if(useHttpGET) {
                x.open("GET", url,true);            
            } else {
                var contentLength = 0;
                if(url.indexOf('?') > 0 && PostOptions == null) {
                    PostOptions = url.substring(url.indexOf('?')+1);
                    url = url.substring(0,url.indexOf('?'));
                    contentLength = PostOptions.length;
                }
//                alert('content-length: ' + contentLength + '\r\nurl: ' + url + '\r\nPostOptions: ' + PostOptions);
                x.open("POST", url,true);
                x.setRequestHeader('Content-length',contentLength);
                x.setRequestHeader('Connection', 'close');
            }
            x.onreadystatechange = function() {
                    if (x.readyState != 4){
                        return; //wait until ajax call completes
                    }else{
                        eval(return_func + '(x.responseText)');
                        delete x;   //Remove the xmlhttp object.
                        //delete this; //Deallocate the class it's self too.
                        return;
                    }
            }
            x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            x.send(PostOptions);
        }
    }
}