
function init(){
    roundedCorners();

    client = clientSize();
    object = findPos(document.getElementById('rockbottom'));

    if(client[1] > object[1]){
        document.getElementById('packer').style.height = client[1] - object[1];
    }
}


//---- SPM FUNCTIONS ---------------------------------------------------------//

function imageLink(imageID,linkID,theSwitch){
    document.getElementById(imageID).className = (theSwitch) ? 'on' : 'off';
    document.getElementById(linkID).className = (theSwitch) ? 'linkon' : 'link';
}

//show/hide actor quick search layer
function showHideQS(QSID,showHide){
	elmntObj = document.getElementById(QSID+'QSD');
    if(showHide){
        //show layer
        elmntObj.style.visibility = 'visible';
        elmntObj.style.display = 'block';
        //position layer
        coors = findPos(document.getElementById(QSID+'QSA'));
        elmntObj.style.left = coors[0]+'px';
        elmntObj.style.top = (coors[1]+23)+'px';
    }
    else{
        //hide layer
        elmntObj.style.visibility = 'hidden';
        elmntObj.style.display = 'none';
    }

}

//ajax function to update quick search
function QuickSearch(QSID){
    elmntID = QSID+'QSD';
    search = document.getElementById(QSID+'QSO').value;

    if(search != '' && search != ' '){
        document.getElementById(elmntID).innerHTML = 'Loading...';
        showHideQS(QSID,1);

        xmlHttp=GetXmlHttpObject()
        if(xmlHttp == null){
            alert("Browser does not support HTTP Request")
            return
        }

        var url = "./function/ajax.php?ajaxID="+QSID+"QS&string=" + search + "&sid=" + Math.random();
        xmlHttp.onreadystatechange=stateChanged
        xmlHttp.open("GET",url,true)
        xmlHttp.send(null)
    }
    else{
        showHideQS(QSID,0);
    }
}

//update databse from Quick Search
function updateQS(QSID,stringID,type){
    showHideQS(QSID,0);
    document.getElementById(QSID+'QSO').value = '';
	document.getElementById(QSID+'QSR').innerHTML = 'Updating Property DP Numbers...';

    xmlHttp=GetXmlHttpObject()
    if(xmlHttp == null){
        alert("Browser does not support HTTP Request")
        return
    }

    var url = "./function/ajax.php?ajaxID="+QSID+"QSupdate&ID="+stringID+"&type="+type+"&sid=" + Math.random();
    xmlHttp.onreadystatechange = function(){
    	if(xmlHttp.readyState == 4){
        	document.getElementById(QSID+'QSR').innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)

    if(QSID == 'dp'){
    	document.getElementById('map').innerHTML = "<p class=\"warning\"><br /><br /><br /><br />You have added or deleted DP Numbers you will need to update your Property Map by clicking on the link below.<br /><br /><br /><br /><br /></p>\r";;
        document.getElementById('mapComplete').value = 0;
        if(document.getElementById('paintLink')){document.getElementById('paintLink').style.visibility = 'hidden';}
    }
}



function updateObj(objectID,stringID,type){
    document.getElementById(objectID).innerHTML = 'Updating...';

    xmlHttp=GetXmlHttpObject()
    if(xmlHttp == null){
        alert("Browser does not support HTTP Request")
        return
    }

    var url = "./function/ajax.php?ajaxID="+objectID+"&ID="+stringID+"&type="+type+"&sid=" + Math.random();
    xmlHttp.onreadystatechange = function(){
    	if(xmlHttp.readyState == 4){
        	document.getElementById(objectID).innerHTML = xmlHttp.responseText;
        }
    }
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function otherEquipment(obj){
    if(obj.selectedIndex == 10){
    	document.getElementById('useOther').value = 1;
        document.getElementById('otherDiv').className='visible';
    }
    else{
        document.getElementById('useOther').value= 0 ;
        document.getElementById('otherDiv').className='abyss';
    }
}
//----------------------------------------------------------------------------//


//---- EDITABLE TABLE --------------------------------------------------------//
/*******
* GLOBALS:
*	activeID	- current edit element
*	busy		- if AJAX is free
*	clickToEdit	- default edit div value
*******/
var activeID = null;
var editBusy = 0;
var clickToEdit = "<span style=\"font-size: 10px\">&nbsp;&nbsp;&nbsp;&nbsp;click to edit</span>";


/*******
* FUNCTION: void editCell(BOOL edit, STRING elementID)
* VARIABLES:
*	edit 		- switch editing on/off
*  	elementID 	- IDName (10Address)
* DESCRIPTION:
*	With the correct elements and id's this function will switch between the div and the edit box.
********/
function editCell(edit,elementID){
    //check if last AJAX call is completed
    if(editBusy){
        alert('Please wait a few moments for your last edit to complete, then try again');
        return;
    }

    //if current element is still in edit mode, set it to div mode first.
    if(activeID != null && activeID != elementID){
        document.getElementById(elementID).className = 'abyss';
    	document.getElementById('div'+elementID).className = 'visible';
    }

    //set curent element to this one
    activeID = elementID;

    //setup function object refferences
    textObj = document.getElementById(elementID);
    divObj = document.getElementById('div'+elementID);

    //set this element to edit mode
    if(edit){
        //exit if this element is still updating
        if(divObj.innerHTML == 'Updating...'){
	        return
	    }

        //if div contains default value, set div to empty
        //DEBUG: alert(divObj.innerHTML.+' == '+clickToEdit.replace(/(<([^>]+)>)/ig,""));
        if(divObj.innerHTML.replace(/(<([^>]+)>)/ig,"") == clickToEdit.replace(/(<([^>]+)>)/ig,"")){
            divObj.innerHTML = '';
	    }

        //copy div text to editbox
        textObj.value = divObj.innerHTML;

        //hide div/show editbox
        divObj.className = 'abyss';
	    textObj.className = 'visible';

        //focus on visiable editbox
        textObj.focus();
    }
    //set this element to div mode
    else{
        //if editbox is empty, set div to default, else set div to editbox
        divObj.innerHTML = (textObj.value == '') ? clickToEdit : textObj.value;

        //show div/hide editbox
        divObj.className = 'visible';
	    textObj.className = 'abyss';
    }
}


/*******
* FUNCTION: void postCell(STRING ajaxID, STRING elementID, STRING ID, STRING fieldName)
* VARIABLES:
*	edit 		- ajax pageID
*  	elementID 	- IDName (10Address)
*	ID 			- MySQL record ID
*  	fieldName 	- MySQL  field name
* DESCRIPTION:
*	Using AJAX, will post editbox changes to the database
********/
function postCell(ajaxID,elementID,ID,fieldName){
    //setup function object refferences
    textObj = document.getElementById(elementID);
    divObj = document.getElementById('div'+elementID);

    //build URL string to pass to AJAX script
	stringID = ID+'|'+fieldName+'|'+escape(textObj.value);

    //inital div value
    initalText = divObj.innerHTML;

    //show div/hide editbox
    editCell(0,elementID);

    //if editbox text = div text, no nedd to update
    if(textObj.value == initalText){
        return
    }

    //set div updating message
    divObj.innerHTML = 'Updating...';
    editBusy = 1;

    //run AJAX function
    xmlHttp=GetXmlHttpObject()
    if(xmlHttp == null){
        alert("Browser does not support HTTP Request")
        return
    }

    var url = "./function/ajax.php?ajaxID="+ajaxID+"&ID="+stringID+"&type=UPDATE&sid=" + Math.random();
    xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readyState == 4){
            //oncomplete set div to AJAX script responce
            divObj.innerHTML = xmlHttp.responseText;
            editBusy = 0;
        }
    }
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}
//----------------------------------------------------------------------------//




//---- ROUNDED CORNERS -------------------------------------------------------//
function roundedCorners() {

    var divs = document.getElementsByTagName('div');
    var rounded_divs = [];
    // First locate all divs with 'rounded' in their class attribute
    for (var i = 0; i < divs.length; i++) {
        if (/\brounded\b/.exec(divs[i].className)) {
            rounded_divs[rounded_divs.length] = divs[i];
        }
    }
    // Now add additional divs to each of the divs we have found
    for (var i = 0; i < rounded_divs.length; i++) {
        var original = rounded_divs[i];
        if(navigator.appName.indexOf('Internet Explorer') > -1){
        	// Change div class to inner div class
	        //original.className = original.className.replace('rounded','');
	        // Create new elements
	        var t = document.createElement('div');  t.className = 't'; t.style.width=original.style.width;
	        var b = document.createElement('div');  b.className = 'b';
	        var l = document.createElement('div');  l.className = 'l';
	        var r = document.createElement('div');  r.className = 'r';
	        var bl = document.createElement('div'); bl.className = 'bl';
	        var br = document.createElement('div'); br.className = 'br';
	        var tl = document.createElement('div'); tl.className = 'tl';
	        var tr = document.createElement('div'); tr.className = 'tr';

	        // Replace current div with first new div
	        original.parentNode.replaceChild(t, original);
	        // Append all new div's
	        t.appendChild(b);
	        b.appendChild(l);
	        l.appendChild(r);
	        r.appendChild(bl);
	        bl.appendChild(br);
	        br.appendChild(tl);
	        tl.appendChild(tr);
	        // Append original inner div last
	        tr.appendChild(original);
	    }
	    else{
        	original.style.border='1px solid #C0C0C0';
        }
    }
}
//----------------------------------------------------------------------------//


//---- AJAX FUNCTIONS --------------------------------------------------------//
var xmlHttp
var elmntID

function stateChanged(){
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
        document.getElementById(elmntID).innerHTML=xmlHttp.responseText
    }
}

function GetXmlHttpObject(){
    var objXMLHttp=null
    if (window.XMLHttpRequest){
        objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject){
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
}
//----------------------------------------------------------------------------//


//---- OTHER FUNCTIOS --------------------------------------------------------//
//find size of inner browser
function clientSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
}

//find position of object on screen
function findPos(obj){
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

//bool is value numeric
function IsNumeric(sText){
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++){
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1){
            IsNumber = false;
        }
    }
    return IsNumber;
}

// Returns true if the passed value is found in the
// array. Returns false if it is not.
Array.prototype.inArray = function (value){
    var i;
    var ctr = 0;
    for (i=0; i < this.length; i++) {
        if (this[i] == value) {
            return true;
        }
    }
    return false;
}

function ietruebody()
{return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

var win = null;
function customWindow(Wlink, Wname, Wwidth, Wheight){
    Swidth = screen.width;
    Sheight =  screen.height-50; //50 for start bar

    Wtop = ((Sheight - Wheight)/2);
    Wleft = ((Swidth - Wwidth)/2);

    //if(win){
    //    win.focus();
    //}
    //else{
    	win = window.open(Wlink,Wname,'width=' + Wwidth + ',height=' + Wheight + ',resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no,top=' + Wtop + ',left=' + Wleft);
    //}
}

function confirmLink(labelText, link){
    question = confirm(labelText);
	if(question){window.location.href = link;}
}


function confirmDelete(labelText, code){
    message = (labelText.indexOf('click to edit') >= 0) ? 'WARNING!\r\n'+'Are you sure you want to delete this record?\r\n' : 'WARNING!\r\n'+'Are you sure you want to delete: \r\n\r\n'+labelText+'?\r\n';

	question = confirm(message);
	if(question){eval(code);}
}

//----------------------------------------------------------------------------//

//---- QUICK SEARCH POPUP ----------------------------------------------------//
/*******
* FUNCTION: void QuickSearch2(STRING anchorID, STRING divID, STRING objID, STRING ajaxID)
* VARIABLES:
*	anchorID 	- <a><a> ID
*  	divID 		- div popup ID
*	objID 		- edit object <input /> <textarea></textarea>
*  	ajaxID 		- ajax script pageID
* DESCRIPTION:
*	Revised QuickSearch function, allows you to specify each of the invloved elements ID's.
*	Will pop uip a div below the input object for suggestions of what the user is entering
********/
function QuickSearch2(anchorID,divID,objID,ajaxID){
    //setup function object refferences and variables
    elmntID = divID;
	elmntObj = document.getElementById(elmntID);
    search = document.getElementById(objID).value;

    //if search is empty hide layer
    if(search != '' && search != ' '){
        //inital layer value
        elmntObj.innerHTML = 'Loading...';

        //show layer
        showHideQS2(anchorID,divID,1);

        //run AJAX script
        xmlHttp=GetXmlHttpObject()
        if(xmlHttp == null){
            alert("Browser does not support HTTP Request")
            return
        }

        var url = "./function/ajax.php?anchorID="+anchorID+"&divID=" + divID + "&objID=" + objID + "&ajaxID=" + ajaxID + "&string=" + search + "&sid=" + Math.random();
        xmlHttp.onreadystatechange=stateChanged
        xmlHttp.open("GET",url,true)
        xmlHttp.send(null)
    }
    else{
        //hide layer
        showHideQS2(anchorID,divID,0);
    }
}

/*******
* FUNCTION: void showHideQS2(STRING anchorID, STRING divID, BOOL showHide)
* VARIABLES:
*	anchorID 	- <a><a> ID
*  	divID 		- div popup ID
*	showHide 	- 1/0 true/false
* DESCRIPTION:
*	Revised showHide function, allows you to specify each of the invloved elements ID's.
*	Will show or hide the popup suggestion div
********/
function showHideQS2(anchorID,divID,showHide){
    //setup function object refferences and variables
    elmntObj = document.getElementById(divID);
    if(showHide){
        //show layer
        elmntObj.style.visibility = 'visible';
        elmntObj.style.display = 'block';
        //position layer
        coors = findPos(document.getElementById(anchorID));
        elmntObj.style.left = coors[0]+'px';
        elmntObj.style.top = (coors[1]+23)+'px';
    }
    else{
        //hide layer
        elmntObj.style.visibility = 'hidden';
        elmntObj.style.display = 'none';
    }
}



/*******
* FUNCTION: void updatePropduct(STRING searchDivID, STRING elementID, STRING acvm)
* VARIABLES:
*	searchDivID - div popup ID
*  	elementID 	- elementID
*	acvm 		- ACVM (Licence_No)
* DESCRIPTION:
*   Will update product name on page and Licence_No in DB
********/
function updatePropduct(searchDivID,elementID,acvm){
    //setup function object refferences and variables
    elmntID = 'div'+elementID;
    textObj = document.getElementById(elementID);
	divObj = document.getElementById('div'+elementID);

    //hide search div
	showHideQS2('',searchDivID,0);

    //acvm is empty when cancel button is pushed
    if(acvm == ''){
        //show text div/hide editbox
        divObj.className = 'visible';
	    textObj.className = 'abyss';

        //reset editbox value
        textObj.value = divObj.innerHTML;
    }
    else{
        //div inital value
        divObj.innerHTML = 'Updating...';

        //show text div/hide editbox
	    divObj.className = 'visible';
	    textObj.className = 'abyss';

	    //AJAX update div with product name, var licenceNo
	    xmlHttp=GetXmlHttpObject()
	    if(xmlHttp == null){
	        alert("Browser does not support HTTP Request")
	        return
	    }

        //Extract numbers from string (1234mrbrown = 1234)
        ID = elementID.replace(/\D/g,'');
        var url = "./function/ajax.php?ajaxID=updateProduct&ID="+ID+"&acvm="+acvm+"&sid=" + Math.random();
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
                product = xmlHttp.responseText;
                //update text div and editbox
	            divObj.innerHTML = product;
                textObj.value = product;
            }
	    }
	    xmlHttp.open("GET",url,true)
	    xmlHttp.send(null)
    }

}
//----------------------------------------------------------------------------//
