var refreshTimer;

function update(a, b, c) {
	getJavaScriptUpdate(a, b, c);
}

function getJavaScriptUpdate(customQuery, post, customUrl) {
    var httpReq = false;
    if (window.XMLHttpRequest) {
        httpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        httpReq = new ActiveXObject('Microsoft.XMLHTTP');
    }
	
    var method = post == null || typeof(post) == undefined ? 'GET' : 'POST';
	var url = customUrl ? customUrl : thisUrl;
	var uri = url + "?ajax=1&pageTime=" + pageTime + "&" + (customQuery ? customQuery : thisQuery);

    httpReq.open(method, uri, true);
    httpReq.setRequestHeader('Content-Type', 'text/plain');
    httpReq.onreadystatechange = function() {
        if (httpReq.readyState == 4) { 
            if (httpReq.status == 200) {
                eval(httpReq.responseText);
            }
        }
    }
    httpReq.send(post);
}

var updatePeriod = 1000;
var updateTimer;
function startPeriodicAjaxUpdate(customUpdatePeriod, customQuery, customPost, customUrl) {
    stopPeriodicAjaxUpdate();
    updatePeriod = customUpdatePeriod ? customUpdatePeriod : updatePeriod;
    getJavaScriptUpdate(customQuery, customPost, customUrl);
    updateFunction = function() { startPeriodicAjaxUpdate(customUpdatePeriod, customQuery, customPost, customUrl) };
    updateTimer = setTimeout(updateFunction, updatePeriod);
}
function stopPeriodicAjaxUpdate() {
    clearTimeout(updateTimer);
}

var coalaseTimer;
function cpov(a, b, c, d) {
	coalescePostObjectValue(a, b, c, d)
}
function coalescePostObjectValue(obj, delay, customQuery, customUrl) {
	stopPeriodicAjaxUpdate();
	stopCoalescePost();
	postFunction = function() { postObjectValue(obj, customQuery, customUrl) };
	coalaseTimer = setTimeout(postFunction, delay);
}
function stopCoalescePost() {
	clearTimeout(coalaseTimer);
}
function pov(a, b, c) {
	postObjectValue(a, b, c);
}
function postObjectValue(obj, customQuery, customUrl) {
    if (customQuery == null) {
        customQuery = "name=" + obj.name;
    } 
    if (obj.type == "checkbox") {
        postValue = obj.checked ? obj.value : "";
    }
	else postValue = obj.value ? obj.value : "";
	getJavaScriptUpdate(customQuery, postValue, customUrl);
}

function setObjectValue(objName, oldValue, newValue) {
        
    var obj = document.getElementsByName(objName);
    
    if (obj.length == 1) {
        if (obj[0].type == "checkbox") {
            obj[0].checked = (obj[0].value == newValue);
        } else if (obj[0].value == oldValue) {
            obj[0].value = newValue;
        }
    } else {  
        for (var i=0; i<obj.length; i++) {
            if (obj[i].type == "radio") {
                obj[i].checked = (obj[i].value == newValue);
            }
        }
    }
}


function togglePlugin(id, data, responderUrl) {
    var ele = document.getElementById(id);
    if (ele != null) {
        if (ele.style.display == "none") {
            ele.style.display = "block";
            if (responderUrl) {
                getJavaScriptUpdate("", data, responderUrl);
            }
        } else {
            $('#' + id).slideUp(1000);
        }
    }
}

 function populateElement(selector, defvalue) {
    if($.trim($(selector).val()) == "") {
        $(selector).val(defvalue);
    }
  
    $(selector).focus(function() {
        if($(selector).val() == defvalue) {
            $(selector).val("");
        }
    });
    
    $(selector).blur(function() {
        if($.trim($(selector).val()) == "") {
            $(selector).val(defvalue);
        }
    });
 }

function getCookie(name) {

	if (document.cookie.length > 0) {
  		c_start = document.cookie.indexOf(name + "=");
  		if (c_start != -1) { 
    		c_start = c_start + name.length+1; 
    		c_end = document.cookie.indexOf(";", c_start);
    		if (c_end==-1) c_end = document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
    	} 
  	}
	return "";
}

function setCookie(name, value) {	
	document.cookie = name + "=" + escape(value) + "; path=/;";
}
