function getElementsByClassName(searchClass,node,tag)
{
    if(document.getElementsByClassName)
    {
        if(node)
        {
            return node.getElementsByClassName(searchClass);
        }
        else
        {
            return document.getElementsByClassName(searchClass);
        }
    }
    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 getElementsByClassName(class_name)
{
  var all_elements = document.getElementsByTagName('*');
  var els = new Array();

  for(var i=0; i<all_elements.length; i++)
  {
    el = all_elements[i];
    classes = el.className.split(' ');
    for(var j=0;j<classes.length && classes[j]!='';j++)
    {
        if(classes[j]==class_name)
        {
            els.push(el);
            break;
        }
    }
  }
  return els;
}*/

function showMessage(div,response)
{
    alert(response.response);
    if(response.cleanUpFunction)
    {
        eval(response.cleanUpFunction+'()');
    }
}

function getRealChildren(element)
{
  var children = new Array();
  var count = 0;

//commented section doesn't work in Safari
/*  for(var child in element.childNodes)
  {
    if(typeof(element.childNodes[child].tagName)!='undefined')
    {
      children[count] = element.childNodes[child];
      count++;
    }
  }*/

  var clength = element.childNodes.length;

  for(var i=0;i<clength;i++)
  {
    if(typeof(element.childNodes[i].tagName)!='undefined')
    {
      children[count] = element.childNodes[i];
      count++;
    }
  }      

  return children;
}

function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 
	for(var i = 0; i < hashes.length; i++)
	{
	hash = hashes[i].split('=');
	vars[hash[0]] = hash[1];
	}
 
	return vars;
}

function splitUrl(url)
{
    var hash = [];
    var bits = [];

    hash = url.split('?');
    bits[0] = hash[0];
    bits[1] = getParams(url);

    return bits;
}

function getParams(strng)
{
	var vars = [], hash;
	var hashes = strng.slice(strng.indexOf('?') + 1).split('&');
 
	for(var i = 0; i < hashes.length; i++)
	{
	hash = hashes[i].split('=');
	vars[hash[0]] = hash[1];
	}
 
	return vars;
}

function getPosition(e)
{
    if(e==null) e=window.event;

    var posx=0,posy=0;

	if(e.pageX || e.pageY)
	{
	  posx=e.pageX; posy=e.pageY;
	}
	else if(e.clientX || e.clientY)
	{
	  if(document.documentElement.scrollTop)
	  {
	    posx=e.clientX+document.documentElement.scrollLeft;
	    posy=e.clientY+document.documentElement.scrollTop;
	  }
	  else
	  {
	    posx=e.clientX+document.body.scrollLeft;
	    posy=e.clientY+document.body.scrollTop;
	  }
	}
	return [posx,posy];
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

Array.prototype.in_array = function ( obj ) {
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if ( this[x] == obj ) return true;
	}
	return false;
}

function toggleDiv(el)
{
    var idToToggle = el.id.substring(el.id.indexOf('_')+1);
    var div = document.getElementById(idToToggle);
    if(div.style.display=='none')
    {
        div.style.display='block';
    }
    else
    {
        div.style.display='none';
    }
}

//for IE's inability to change the name attribute once the element has been created

function createNamedElement(type, name) {
   var element = null;
   // Try the IE way; this fails on standards-compliant browsers
   try {
      element = document.createElement('<'+type+' name="'+name+'">');
   } catch (e) {
   }
   if (!element || element.nodeName != type.toUpperCase()) {
      // Non-IE browser; use canonical method to create named element
      element = document.createElement(type);
      element.name = name;
   }
   return element;
}

function emailForm(email,details)
{
    var subject,body,sig;

    if(details.subject && details.body)
    {
        var mailto_link = 'mailto:'+email+'?subject='+escape(details.subject)+'&body='+escape(details.body);
    }
    else
    {
        switch(details.type)
        {
            case 'inspection':
                subject = details.projectname+' - confirmation of your free tour';
                body = 'Dear '+details.contactname+'\n\nI am pleased to confirm that your free tour of '+details.projectname+' has been booked for:\n\nDate: DATE\nTime: TIME\n\nI will call you at that time to take you through the site over the phone.\n\nIn the meantime, if you would like to read more about '+details.projectname+', you can do so here: '+ details.projectaddress + '\n\nRegards,\n\n';
                break;
        }
    }

    win = window.open(mailto_link,'emailWindow');
    if (win && win.open &&!win.closed) win.close();
} 

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function reloadPage(dummy,url)
{
    var currUrl = window.location.href;
    if(url.firstChild)
    {
	    url = url.firstChild.data || currUrl;
    }
    else
    {
        url = currUrl;
    }
	window.location = url;
}

function printPage()
{
	window.print();
}
