//
//  Program         clickcounts.js
//  Project         IMEX
//  Description     Count the clicks from banners
//
//  History
//  27/02/2009      Mike Wood       Written
//

    var xmlhttp = createRequestObject();

    //  Function:   createRequestObject
    //  Desc:       Create an xmlhttp object
    //              ** Not my code - see http://www.phpbuilder.com/columns/kassemi20050606.php3 **
    //  Usage:      var xmlhttp = createRequestObject();
    function createRequestObject(){
    	var request_o; //declare the variable to hold the object.
    	var browser = navigator.appName; //find the browser name
    	//alert(browser);
    	if(browser == "Microsoft Internet Explorer"){
    		/* Create the object using MSIE's method */
    		request_o = new ActiveXObject("Microsoft.XMLHTTP");
    		//request_o = new ActiveXObject("Msxml2.XMLHTTP");
    	}else{
    		/* Create the object using other browser's method */
    		request_o = new XMLHttpRequest();
    	}
    	return request_o; //return the object
    }


    //  Function:   bannerclick
    //  Desc:       Run php script to save form control changes to the db
    //  Usage:      bannerclick (sourceobject); // Called from endedit()
    function bannerclick (bannerid) {

        //alert(bannerid);

        var scriptname = '/bannerclick.php?bannerid=' + bannerid;    

        /*
        // Create shiny object
        var xmlhttp = createRequestObject();
        // Run the script                
        xmlhttp.open('get', scriptname);
        xmlhttp.onreadystatechange = handledesign(xmlhttp);
        xmlhttp.send(null);
        */


        
        // Caching stuff
        if (!xmlhttp.getResponseHeader("Date")) {        

            var cached = xmlhttp;
            xmlhttp = createRequestObject();
            var ifModifiedSince = cached.getResponseHeader("Last-Modified");
            ifModifiedSince = (ifModifiedSince) ? ifModifiedSince : new Date(0); // January 1, 1970
            xmlhttp.open('post', scriptname);
            xmlhttp.setRequestHeader("If-Modified-Since", ifModifiedSince);
            xmlhttp.send(null);
            if(xmlhttp.status == 304) {
                xmlhttp = cached;
            }        
        } else {
            // Run the script                
            xmlhttp.open('post', scriptname);
            xmlhttp.onreadystatechange = handledesign;
            xmlhttp.send(null);            
        }
    }  

    
    //  Function:   handledesign
    //  Desc:       Supporting function for xmlhttp, called in bannerclick
    //  Usage:      xmlhttp.onreadystatechange = handledesign(xmlhttp, sourceobject);
    function handledesign (xmlhttp) {
	    if (xmlhttp.readyState == 4) {
		    var response = xmlhttp.responseText;
	    }
	}

/*
    //  Function:   startedit
    //  Desc:       Put the form control into edit mode
    //  Usage:      <input class="viewtext" ... onclick="startedit(this);" onblur="endedit(this);">
    function startedit (sourceobject) {

        // Remove 'focussed' style of pagebreak divs
        pagebreak_blur ();

        // Change the class of the input field to edit mode
        sourceobject.className = 'edittext';
        sourceobject.focus();

        // Update details of the current object in the form
        var obj = new objectdetail (sourceobject);
        
        // Update properties on the design form
        with (document.designform) {
            currentobject.value = obj.name;
            type.value          = obj.type;
            fieldname.value     = obj.fieldname;
            questionid.value    = obj.questionid;
            choiceid.value      = obj.choiceid;
            qtext.value         = obj.qtext;
            pagetextid.value    = obj.pagetextid;
            pagetexttype.value  = obj.pagetexttype;
            pagebreakid.value   = obj.pagebreakid;
            // Store the url for advanced edit
            advancedurl.value   = obj.advancedurl;
            // Store the pre-edit value
            preeditvalue.value  = sourceobject.value;            
        
            // If the advanced controls form is visible
            if (window.parent.document.getElementById('advanced')) {
                
                // If an advanced url is set
                if (advancedurl.value) {

                    // Strip off the domain (need a proper solution for this)
                    var fullurl     = window.parent.document.getElementById('advanced').src;
                    var strippedurl = fullurl.replace('http://refreg.reftech.co.uk/', '');
                    
                    if (strippedurl != advancedurl.value) {
                        window.parent.document.getElementById('advanced').src = advancedurl.value;
                    }
                } else {
                    // Use a default url
                    window.parent.document.getElementById('advanced').src = "tmp_advanced.php";
                }                
            }
        }
    }


    //  Function:   endedit
    //  Desc:       After editing, put the control to view mode, save changes etc
    //  Usage:      <input class="viewtext" ... onclick="startedit(this);" onblur="endedit(this);">
    function endedit (sourceobject) {
        
        // Return the class to view mode
        sourceobject.className = 'viewtext';        
        
        // If the value has changed
        if (sourceobject.value != document.designform.preeditvalue.value) {

            // Write to the db (with dynamic shiny)
            dbwrite (sourceobject);
            
            // If the advanced controls form is visible
            if (window.parent.document.getElementById('advanced')) {
                
                // If an advanced url is set
                if (document.designform.advancedurl.value) {
                    
                    // Strip off the domain (need a proper solution for this)
                    var fullurl     = window.parent.document.getElementById('advanced').src;
                    var strippedurl = fullurl.replace('http://refreg.reftech.co.uk/', '');
                    
                    // Only load if it's not already the correct form
                    if (strippedurl != document.designform.advancedurl.value) {
                        window.parent.document.getElementById('advanced').src = document.designform.advancedurl.value;
                    }
                } else {
                    // Use a default url
                    window.parent.document.getElementById('advanced').src = "tmp_advanced.php?type=what";
                }                
            }             
        }
    }  


    //  Function:   objectdetail
    //  Desc:       Return details of a source objects name, type, questionid, choiceid
    //              based on the name formats Design[dlg][firstname], or Design[demog][qid][cid]
    //  Usage:      var obj = new objectdetail (sourceobject); alert(obj.questionid);
    function objectdetail (sourceobject) {
        
        if (sourceobject.id.indexOf('pagebreak') >= 0) {
            this.name   = sourceobject.id;
            this.value  = 0;
        } else {        
            // Get the name and value
            this.name   = sourceobject.name; 
            this.value  = sourceobject.value; 
        }
        
        // Set defaults
        this.type           = "";
        this.fieldname      = "";
        this.questionid     = 0;
        this.choiceid       = 0;
        this.qtext          = "";
        this.pagetextid     = 0;
        this.pagetexttype   = "";
        this.pagebreakid    = 0;
        this.advancedurl    = "";                

        // Get the name, replacing any ']' brackets
        var tmpname = this.name.replace(/]/g, ''); 
        // Break the name into component parts by '[' brackets
        var parts = tmpname.split('[');
        
        // Get a count of the parts
        var numparts = parts.length;
        
        // Most basic component should have 3 elements minimum
        if (numparts >= 3) {
        
            // Check it's a 'design' object
            if (parts[0] == 'Design') {

                // Save the object type
                this.type = parts[1];
    
                // Is it a contact field
                if (parts[1] == 'dlg') {

                    this.fieldname = parts[2];                                

                    // Set advanced controls form
                    this.advancedurl = 'tmp_advanced.php?type=dlg';
                    
                // or demog question                
                } else if (parts[1] == 'demog') {
                    
                    this.questionid = parts[2];   
                    if (numparts > 3) {
                        
                        // Is last element the question text type
                        if (parts[3] == 'qtext' || parts[3] == 'rtext') {
                            this.qtext = parts[3];
                            // Set advanced controls form
                            this.advancedurl = 'demog.php?questionid=' + this.questionid;
                        // or a choice                            
                        } else {                            
                            this.choiceid = parts[3];   
                            // Set advanced controls form
                            this.advancedurl = 'tmp_advanced.php?type=demogchoice';
                        }
                    }
                    
                // Or page text
                } else if (parts[1] == 'pagetext') {

                    this.pagetextid = parts[2];   

                    // Containes sectionheader or sectioncontent
                    this.pagetexttype = parts[3];

                    // Set advanced controls form
                    this.advancedurl = 'pagetext.php?pagetextid=' + this.pagetextid;

                // Or page text
                } else if (parts[1] == 'pagebreak') {
                    
                    //alert('found a page break');
                    
                    this.pagebreakid = parts[2];
                    // Set advanced controls form
                    this.advancedurl = 'pagebreak.php?pagebreakid=' + this.pagebreakid;                    

                // or something else?                                    
                } else {
                }
            
            } else {
                // No processing for non-design objects
            }    
        }
    }     
    
      
    // Load the relevant advanced control form
    function loadadvanced (sourceobject) {
    }
    
    
    // Re-load the form display pane
    function preview_refresh () {
        window.parent.document.getElementById('preview').src = 'formdisplay.php';    
    }            
        

    // Pagebreak version of startedit
    function pagebreak_click (sourceobject) {
        
        // De-focus any other pagebreaks first, passing this id as an exception
        pagebreak_blur (sourceobject.id);
        // Emulate focus
        sourceobject.className = 'pagebreak_clicked';
        
        // Update details of the current object in the form
        var obj = new objectdetail (sourceobject);
        
        // Update properties on the design form
        with (document.designform) {
            currentobject.value = obj.name;
            type.value          = obj.type;
            fieldname.value     = obj.fieldname;
            questionid.value    = obj.questionid;
            choiceid.value      = obj.choiceid;
            qtext.value         = obj.qtext;
            pagetextid.value    = obj.pagetextid;
            pagetexttype.value  = obj.pagetexttype;
            pagebreakid.value   = obj.pagebreakid;
            // Store the url for advanced edit
            advancedurl.value   = obj.advancedurl;
            // Store the pre-edit value
            preeditvalue.value  = "";            
        
            // If the advanced controls form is visible
            if (window.parent.document.getElementById('advanced')) {
                
                // If an advanced url is set
                if (advancedurl.value) {

                    // Strip off the domain (need a proper solution for this)
                    var fullurl     = window.parent.document.getElementById('advanced').src;
                    var strippedurl = fullurl.replace('http://refreg.reftech.co.uk/', '');
                    
                    if (strippedurl != advancedurl.value) {
                        window.parent.document.getElementById('advanced').src = advancedurl.value;
                    }
                } else {
                    // Use a default url
                    window.parent.document.getElementById('advanced').src = "tmp_advanced.php";
                }                
            }
        }       
        
    }        
                
    
    // Pagebreak version of endedit
    function pagebreak_blur (exceptionid) {
        
        // Get all the divs
        var alldivs = document.getElementsByTagName('div');
        var divid   = "";
        for (var i = 0; i < alldivs.length; i++){
            divid = alldivs[i].id;
            // Check for an id like 'pagebreak'
            if (divid.indexOf('pagebreak') >= 0) {
                
                // Skip the passed exception, ie. another div that needs focus
                if (exceptionid != divid) {
                    // De-focus
                    alldivs[i].className = 'pagebreak';
                }                    
            }
        }
    } 
              
              
    // Proceed with deletion of a pagebreak?
    function pagebreak_delete () {
    
        if (confirm('Do you really wish to delete this Page Break?')) {
            return true;
        } else {
            return false;
        }
    }
*/                    