
function id(id) 
{
	return document.getElementById(id);
}

//Mostly for iframe calendar
function setInputValue(inputId, newValue)
{
    id(inputId).value = newValue;
}


String.prototype.trim = String.prototype.trim || function() {
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}


Array.prototype.indexOf = Array.prototype.indexOf || function(item, start) {
    for (var i = (start || 0); i < this.length; i++) {
        if (this[i] == item) {
            return i;
        }
    }
    return -1;
}


Array.prototype.push = Array.prototype.push || function() {
    for (var i = 0; i < arguments.length; i++) {
        this[this.length] = arguments[i];
    }
    return this.length;
}


Array.prototype.splice = Array.prototype.splice || function(start, deleteCount) {
    var beg = this.slice(0, start);
    var del = this.slice(start, start + deleteCount);
    var end = this.slice(start + deleteCount);

    var ins = [];
    for (var i = 2; i < arguments.length; i++) {
    	ins[i-2] = arguments[i];
    }
    
    this.length = 0;
    var tmp = this.concat(beg, ins, end);
    for (var i = 0; i < tmp.length; i++) {
    	this[i] = tmp[i];
    }

    return del;
}


if (!document.getElementsByTagName) {
	if (document.all) { // Create getElementsByTagName for IE 5.x and below
		document.getElementsByTagName = function(nem) {
			return document.all.tags(nem.toUpperCase());
		}
	}
}


function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


function getClasses(element) {
    return element.className ? element.className.trim().split(/\s+/) : [];
}


function addClass(element, c) {
    var classes = getClasses(element);
    if (classes.indexOf(c) == -1) {
        classes.push(c);
        element.className = classes.join(' ');
    }
}


function removeClass(element, c) {
    var classes = getClasses(element);
    var idx = classes.indexOf(c);
    if (idx != -1) {
        classes.splice(idx, 1);
        element.className = classes.join(' ');
    }
}


function hasClass(element, c) {
    return getClasses(element).indexOf(c) != -1;
}


function toggleDisplay(objectId)
{
    if(id(objectId).style.display == 'block')
    {
        id(objectId).style.display = 'none';
    }
    else
    {
        id(objectId).style.display = 'block';
    }
}


function setClass(objectId, newClass)
{
    var object = document.getElementById(objectId);

    if(object)
    {
    	object.className = newClass;
    }	
}
      

function setStyleDef(objectId, styleName, newVal)
{
	var object = document.getElementById(objectId);

	if(object)
    {
    	object.style[styleName] = newVal;
    }		     	
}	


function disableBody()
{
	condor.disableBody();
}

function enableBody()
{
	condor.enableBody();
}

function showWait()
{
	return condor.showWait();
}

function hideWait()
{
	return condor.hideWait();
}


function getPageX()
{

	var x;
	
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
	}	
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
	}
	
	return x;
}


function getPageY()
{
	var y;
	if (self.pageYOffset) // all except Explorer
	{
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		y = document.body.scrollTop;
	}
	
	return y;
}



function prepSocketCall(socketModule, socketName, vars)
{
	
	if (document.URL.indexOf('?') > -1)
	{ 
		var socketCall = '&';
	}
	else
	{
		var socketCall = '?';
	}
	
	if(vars != false)
	{
		for(name in vars)
	    {
	    	
	    	socketCall += name + '=' + escape(vars[name]) + '&';
	    }
	}

	socketCall += 'socketModule=' + socketModule + '&socketName=' + socketName;

    if(document.URL.indexOf('#') > -1)
    {
        var urlBase =  document.URL.substr(0,  document.URL.indexOf('#'));
    }
    else
    {
        var urlBase = document.URL;
    }
    
    return urlBase + socketCall;
    
}


function prepTaskCall(taskModule, taskName, vars)
{
	
	if (document.URL.indexOf('?') > -1)
	{ 
		var taskCall = '&';
	}
	else
	{
		var taskCall = '?';
	}
	
	if(vars != false)
	{
		for(name in vars)
	    {
	    	
	    	taskCall += name + '=' + escape(vars[name]) + '&';
	    }
	}

	taskCall += 'taskModule=' + taskModule + '&taskName=' + taskName;

    if(document.URL.indexOf('#') > -1)
    {
        var urlBase =  document.URL.substr(0,  document.URL.indexOf('#'));
    }
    else
    {
        var urlBase = document.URL;
    }
    
    return urlBase + taskCall;
    
}

condor_outlineEditable = function()
{
	var elems = getElementsByClass("outlineMe");
	var hid = 0;
	for (var i = 0; i < elems.length; i++)
	{		
		var elem = elems[i];

		if (!document.defaultView.getComputedStyle(elem, ""))
			hid++;
			
		if (elem.offsetParent) // Hidden elements have no offsetParent (only in IE they do...)
		{								
			elem.style.zoom = "1";
			if (elem.offsetWidth) // But in IE they don't have an offsetWidth (maybe I should find a better way of detecting visibility?) Getting style display doesn't work, since the element isn't rendered, thus no styling information is available - it might be hidden by a parent element
			{
				if (condor_getStyle(elem.offsetParent, "position") == "static")
					elem.offsetParent.style.position = "relative";
				
				var oe = elem.cloneNode(false);	
				//var oe = document.createElement("div");
							
				oe = elem.offsetParent.appendChild(oe);
					
				oe.onmouseover = null;
				oe.targetElem = elem;
				oe.id = "outline_" + elem.id;
				oe.onclick = function()
				{
					condor_simulateClick(this.targetElem);
				}
				
				removeClass(oe, "outlineMe");
				addClass(oe, "outline");
				
				oe.style.top = elem.offsetTop + "px";
				oe.style.left =elem.offsetLeft + "px";
				oe.style.width = parseInt(elem.offsetWidth - 2) + "px";
				oe.style.height = parseInt(elem.offsetHeight - 2 ) + "px";
		
				elem.style.zoom = "normal";
			}
		}
	}
	
	alert("Hidden elements: " + hid);
}

condor_removeOutline = function()
{
	var elems = getElementsByClass("outline");
	for (var i = 0; i < elems.length; i++)
	{
		var elem = elems[i];
		elem.parentNode.removeChild(elem);
	}
}

condor_getStyle = function (oElm, strCssRule)
{
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/-(w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

condor_get_absolute_offset = function (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];
}

condor_simulateClick = function(elem)
{
	if (typeof(elem) == "string")
		elem = document.getElementById(elem);
	if ((elem) && (elem.onclick))
		elem.onclick();
}


if (!condorjs)
	var condorjs = new Object();
	
condorjs.utmLink = function(elem)
// Usage: <a href="http://reservations.ihotelier.com/istay.cfm?xxx[...]" onfocus="condorjs.utmLink(this)">
// OR: <form action="http://reservations.ihotelier.com/istay.cfm" onfocus="condorjs.utmLink(this)">
// Putting it in the onfocus event makes it easier to implement without disturbing any other functionality + plus it works for no-mouse users
// For hyperlinks it's better than Google's recommended solution, because it allows the user to open link a new window
{
	if (!elem.utmLinked)
	{
		if (elem.submit) // Element is a form
		{
			if (__utmLinkPost)
			{
				// Google's function for passing cookies from one site to another via a form post. 
				__utmLinkPost(elem); // The function adds the _utm parameters to the end of the action attribute
				elem.utmLinked = true;
			}
		}
		else if (elem.href) // Element is a hyperlink
		{
			if (__utmLinker)
			{
				_udl = new Array(); // Reassiging this Google variable
				// Google's function for passing cookies from one site to another via normal hyperlink. 
				__utmLinker(elem.href); // The function will try to jump to the new site, but since we've reassigned it's reference to docment.location, it will remain passive
				elem.href = _udl.href; // Getting the hyperlink back
				elem.utmLinked = true;
				_udl = _ubd.location; // Assigning back to what it was before
			}
		}
	}
}


condorjs.getUrlFromForm = function(form)
// Returns a URL string representing the form, it's target and it's values
// Example of returned value: "http://domain.com/myform.php?value1=2&value2=jorge"
// Example of usage:
// <form [...] onsubmit="window.open(condorjs.getUrlFromForm(this)); return false">
{
	var parameters = new Array();
	for (var i = 0; i < form.elements.length; i++)
	{
		parameters.push(form.elements[i].name + "=" + encodeURIComponent(form.elements[i].value));
	}
	var qs = parameters.join("&"); // qs = query string
	var url = form.getAttribute("action");
	var parts = url.split(/\?/);
	url = parts[0]; // The script name part
	if (parts[1])	// ?appended parameters
	{
		qs = qs + "&" + parts[1]; // We want the utm-parameters at the end!
	}
		
	url = url + "?" + qs;

	return url;
}

// Dummy console object for IE not to throw an error
if (!window.console)
{
	console = new Object();
	console.error = function(){}
}

