///
/// - Accliptic Support v1.0
///
/// A product of Accliptic Technologies - http://www.accliptic.com/
///
/// Usage terms for this product are covered by the included license
/// document.
///
/// Copyright 2005 Accliptic Technologies, All Rights Reserved
///


// Know the environment...

var isIE=document.all?true:false; /* IE4, IE5, IE6 */
var isDOM=document.getElementById?true:false; /* IE6, NS6 */

// Cookie storage functions...

function GetCookie(cookiename) {
        var cookiestring=''+document.cookie;
        var index1=cookiestring.indexOf(cookiename);

        if (index1==-1 || cookiename=='') {
                return '';
        }

        var index2=cookiestring.indexOf(';',index1);
        if (index2==-1) {
                index2=cookiestring.length;
        }
        return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}
function insertTechSignature(tech,myField) {
  //tech = document.entry_submission_form.tech_signature;
  mySelection = tech.options[tech.selectedIndex].value;
  if(mySelection != "0")
  {
	  myValue = "__TECH_SIGNATURE_INSERTED_HERE__";
	  //myField = document.entry_submission_form.message_content;
	  if (document.selection) {
	       myField.focus();
	       sel =  document.selection.createRange();
	       sel.text = myValue;
	  }
	  //MOZILLA/NETSCAPE support
	  else if (myField.selectionStart || myField.selectionStart == 0) {
	       var startPos = myField.selectionStart;
	       var endPos = myField.selectionEnd;
	       myField.value = myField.value.substring(0, startPos)
				      + myValue
				      + myField.value.substring(endPos, myField.value.length);
	  } else {
	       myField.value += myValue;
	  }
   }	  
}
function RemoveDuplicates(mainlist) {
        var listvalues         = new Array();
        var newlist         = new Array();

        var hash = new Object();

        for (var i=0; i < mainlist.length; i++) {
                if (hash[mainlist[i].toLowerCase()] != 1) {
                        newlist = newlist.concat(mainlist[i]);
                        hash[mainlist[i].toLowerCase()] = 1
                }
        }

        return newlist;
}

function RemoveEntry(oldarray,varname) {
        var listvalues         = new Array();
        var newarray         = new Array();

        var hash = new Object();

        for (var i=0; i < oldarray.length; i++) {
                if (oldarray[i] != varname) {
                        newarray = newarray.concat(oldarray[i]);
                }
        }

        return newarray;
}

function RemoveValueFromCookie(varname, cookiename) {
        var mycookie = GetCookie(cookiename);

        // Delete Item
        if (mycookie != "") {
                var mycookiearray = mycookie.split("^");

                mycookiearray = RemoveEntry(mycookiearray,varname);
        } else {
                var mycookiearray = new Array(varname);
        }

        mycookie = mycookiearray.join("^");

        document.cookie = cookiename + '=' + mycookie + '; path=/; expires=Fri, 16-Sep-2039 00:01:01 GMT';
}

function SaveValueToCookie(varname, cookiename) {
        var mycookie = GetCookie(cookiename);

        // Add Item
        if (mycookie != "") {
                var mycookiearray = mycookie.split("^");

                insertArray        = new Array(varname);
                endArray        = mycookiearray.slice(0);
                mycookiearray.length = 0;

                mycookiearray[0] = varname;

                for(i=0; i < endArray.length; i++){
                        mycookiearray[mycookiearray.length] = endArray[i];
                }
        } else {
                var mycookiearray = new Array(varname);
        }

        mycookiearray = RemoveDuplicates(mycookiearray);

        mycookie = mycookiearray.join("^");

        document.cookie = cookiename + '=' + mycookie + '; path=/; expires=Fri, 16-Sep-2039 00:01:01 GMT';
}

// Visibility management functions...

function HideElement(root_id,commit) {
        if (root_id) {
                var idContentExists                = document.getElementById(root_id + '_con')?true:false;
                var idExpanderExists        = document.getElementById(root_id + '_exp')?true:false;

                if (idContentExists) {
                        document.getElementById(root_id + '_con').style.display = 'none';
                } else {
                        elements = document.getElementsByTagName('tr');
                        for (i=0; i < elements.length; i++) {
                                if (elements.item(i).className == root_id + '_con') {
                                        elements.item(i).style.display = 'none';
                                }
                        }
                }

                if (idExpanderExists) {
                        if (commit) {
                                document.getElementById(root_id + '_exp').src = 'themes/main/images/misc/arrow-col.gif';
                                CreateVisibilityToggleMemory(root_id,0);
                        } else {
                                document.getElementById(root_id + '_exp').src = 'themes/main/images/misc/arrow-nostr-col.gif';
                        }
                }
        }
}

function ShowElement(root_id,commit) {
        if (root_id) {
                var idContentExists                = document.getElementById(root_id + '_con')?true:false;
                var idExpanderExists        = document.getElementById(root_id + '_exp')?true:false;

                if (idContentExists) {
                        if (isIE) {
                                document.getElementById(root_id + '_con').style.display = 'block';
                        } else {
                                document.getElementById(root_id + '_con').style.display = 'table-row';
                        }
                } else {
                        elements = document.getElementsByTagName('tr');
                        for (i=0; i < elements.length; i++) {
                                if (elements.item(i).className == root_id + '_con') {
                                        if (isIE) {
                                                elements.item(i).style.display = 'block';
                                        } else {
                                                elements.item(i).style.display = 'table-row';
                                        }
                                }
                        }
                }

                if (idExpanderExists) {
                        if (commit) {
                                document.getElementById(root_id + '_exp').src = 'themes/main/images/misc/arrow-exp.gif';
                                CreateVisibilityToggleMemory(root_id,1);
                        } else {
                                document.getElementById(root_id + '_exp').src = 'themes/main/images/misc/arrow-nostr-exp.gif';
                        }
                }
        }
}

function ShowAndHide(root_id,commit) {
        if (isDOM) {
                if (document.getElementById(root_id + '_con').style.display == 'none') {
                        ShowElement(root_id,commit);
                } else {
                        HideElement(root_id,commit);
                }
        }
        return false;
}

function RetrieveToggleMemory(root_id) {
        var visibilitycookie = GetCookie('visibility');
        var visibility = visibilitycookie.split("^");

        for (var i = 0; i < visibility.length; i++) {
                if (visibility[i] == root_id) {
                        ShowAndHide(root_id,1);
                }
        }
}

function CreateVisibilityToggleMemory(value,currentstatus) {
        if (currentstatus == 1) {
                // Is currently visible...
                RemoveValueFromCookie(value,'visibility');
        } else {
                // Is currently invisible...
                SaveValueToCookie(value,'visibility');
        }
}

// Pop-up window functions...

function OpenPopUp(url, popwindow) {
        window.open(url,popwindow,"toolbar=no,location=0,directories=no,status=no,menubar=0,resizable=1,copyhistory=0,height=450,width=450,scrollbars=yes");
}

function OpVEn(entry_id,queue_id) {
	if(queue_id==15) {
	window.open("?section=phonetool&action=none&entryid=" + entry_id, 'ac_popup', "toolbar=no,location=0,directories=no,status=no,menubar=0,resizable=1,copyhistory=0,height=800,width=850,scrollbars=yes");
	} else {
	window.open("?section=entrydetails&action=popup&entry_id=" + entry_id, 'ac_popup', "toolbar=no,location=0,directories=no,status=no,menubar=0,resizable=1,copyhistory=0,height=450,width=750,scrollbars=yes");
	}
}

function showAttach(entry_id) {
	window.open("?section=entries&popuptype=showattach&action=popup&entry_id=" + entry_id, 'ac_popup', "toolbar=no,location=0,directories=no,status=no,menubar=0,resizable=1,copyhistory=0,height=225,width=375,scrollbars=yes");
}
function OpUHn(user_id) {
	window.open("?section=userhistory&action=popup&user_id=" + user_id, 'ac_popup', "toolbar=no,location=0,directories=no,status=no,menubar=0,resizable=1,copyhistory=0,height=450,width=1000,scrollbars=yes");
}
function OpUEd(user_id,user_access) {
		
        // Open user editor...
        if(user_access=='Admin'||user_access=='CSR') 
        OpenPopUp("?section=other&action=popup&popuptype=edituseraccount&user_id=" + user_id, 'ac_popup');
        else
        alert("You do not have enough privileges to edit this user account.");
}

function OpUEn(user_id, entry_id, view_queue, path_code) {
        // Open user entries...
        window.location = "?section=entries&action=view&user_id=" + user_id + "&entry_id=" + entry_id + "&view_queue=" + view_queue + "&path_code=" + path_code;
}

function OpUIn(user_id, entry_id, view_queue, path_code) {
        // Open user entries...
        window.location = "?section=inquiries&action=view&entry_id=" + entry_id;
}

function RmvLck(user_id) {
        // Lock editor...
        OpenPopUp("?section=entries&action=popup&popuptype=viewlocklisting&user_id=" + user_id);
}
function fileuploadpopup() {
	var w = 480, h = 340;
	if (document.all) { 
		 w = document.body.clientWidth;
		 h = document.body.clientHeight; 
	}
	else if(document.layers) {
		 w = window.innerWidth;
		 h = window.innerHeight;
	}
	var popW = 800, popH = 300;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
    window.open('uploadimg.php','popup','width=' + popW + ',height='+popH+',top='+topPos+',left='+leftPos+',scrollbars=yes,resizable=yes,status=yes'); 
}

function TrapEnter(theevent, thefield) {
        var thekey = theevent.keyCode ? theevent.keyCode : theevent.which ? theevent.which : theevent.charCode;
        if (thekey != 13) {
                return true;
        } else {
                var tempvar;

                for (tempvar = 0; tempvar < thefield.form.elements.length; tempvar++) {
                        if (thefield == thefield.form.elements[tempvar]) {
                                break;
                        }
                }
                tempvar = (tempvar + 1) % thefield.form.elements.length;

                thefield.form.elements[tempvar].focus();

                return false;
        }
}
