

function showExpand(obj){
	objId = obj.id;
	$(objId+'-plusMinus').innerHTML = '<div class="expand"></div>';
	//alert('showExpand');
}
function showContract(obj){
	objId = obj.id;
	$(objId+'-plusMinus').innerHTML = '<div class="contract"></div>';
	//alert('showContract');
}
function scrollTaculous(){
	new Effect.ScrollTo(obj.id+'-scroll');  
}

/**
 * Function ajax
 *  Adds simple Ajax functionality
 */

function ajax(page, objID, loadObj, getOrPost, str, dropDown){
	
	
	// Process Ajax:
	obj=document.getElementById(objID); // Object where retrieved content will display.
	//obj.style.display="block";
	
	// get XMLHttp Object:
	
	var xmlhttp=false; //Clear our fetching variable
	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object?
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
		} catch (E) {
			xmlhttp = false;
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
	}
	if (loadObj!=='0') {
		if (dropDown){
			$(objID+'-plusMinus').innerHTML='<div id="'+loadObj+'">&nbsp;</div>'; // Start Loading...
		} else {
			$(objID).innerHTML='<div style="margin:0 0 20px 150px" id="'+loadObj+'">&nbsp;</div>';
		}
	}
	
	var file = '/assets/php/'+page; // Server Page
	
	if (getOrPost == "get"){
		xmlhttp.open('GET', file+str , true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
		
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4 && xmlhttp.status == 200) { //Check if it is ready to recieve data
				var content = xmlhttp.responseText; //The content data which has been retrieved ***
				if( content ){ //Make sure there is something in the content variable
					obj.style.display="none";
					obj.innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
					Effect.BlindDown(obj, {duration: 1, afterFinish: showContract});
				}
			}
		}
		xmlhttp.send(null); //Nullify the XMLHttpRequest
		
	} else {

		xmlhttp.open('POST', file, true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4 && xmlhttp.status == 200) { //Check if it is ready to recieve data
				var content = xmlhttp.responseText; //The content data which has been retrieved ***
				if( content ){ //Make sure there is something in the content variable
					if (dropDown){
						window.content=content;
						obj.style.display="none";
						showContract(obj);
					}
					obj.innerHTML = content; //Change the inner content of your div to the newly retrieved content ****
					if (dropDown){
						 new Effect.Parallel([
							new Effect.BlindDown(obj, {duration: 1 }),
							new Effect.Opacity(obj, { sync: true, from: 0, to: 1 })
						  ], { 
							duration: 1
						  });

					}

				}
			}
		}
		xmlhttp.send(str);
	}
}

// Used in submitform() to get form objects
function getformvalues (fobj){
	fobj=$(fobj);
	var str='';
	
	// run through list of objects in form and create var string.
	for (var i=0; i<fobj.elements.length; i++){
		str += fobj.elements[i].name + '=' + escape(fobj.elements[i].value) + '&';
	}
	return str;
}

// Submits form with ajax() //
function submitform (page, obj, loadObj, theform){
	var str=getformvalues(theform);
	ajax(page, obj, loadObj, "post", str, 0);
}

// form validation function //
function validate(form) {
	return true;
  var name = form.name.value;
  var phone = form.phone.value;
  var email = form.email.value;
  //var gender = form.gender.value;
  var message = form.message.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  if(name == "") {
    inlineMsg('name','Please enter your name.',2);
    return false;
  }
  if(!name.match(nameRegex)) {
    inlineMsg('name','You have entered an invalid name.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','Please enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(phone == "") {
    inlineMsg('phone','Please enter your phone number.',2);
    return false;
  }
  if(!checkInternationalPhone(phone)) {
    inlineMsg('phone','<strong>Error</strong><br />You have entered an invalid phone number.',2);
    return false;
  }
  /*
  if(gender == "") {
    inlineMsg('gender','<strong>Error</strong><br />You must select your gender.',2);
    return false;
  }
  */
  
  if(message == "") {
    inlineMsg('message','You must enter a message.');
    return false;
  }
  if(message.match(messageRegex)) {
    inlineMsg('message','You have entered an invalid message.');
    return false;
  }
  return true;
}


// form validation for Mini Contact function //
function validateMini(form) {
  var name = form.mininame.value;
  //var phone = form.phone.value;
  var email = form.miniemail.value;
  //var gender = form.gender.value;
  var message = form.minimessage.value;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  if(name == "") {
	//alert('Please enter your name.');
    inlineMsg('mininame','Please enter your name.',2);
    return false;
  }
  if(!name.match(nameRegex)) {
	//alert('You have entered an invalid name.');
    inlineMsg('mininame','You have entered an invalid name.',2);
    return false;
  }
  if(email == "") {
	//alert('Please enter your email.');
    inlineMsg('miniemail','Please enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
	//alert('Error: You have entered an invalid email.');
    inlineMsg('miniemail','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  /*
  if(phone == "") {
	alert('Error: You have entered an invalid email.');
    inlineMsg('phone','Please enter your phone number.',2);
    return false;
  }
  if(!checkInternationalPhone(phone)) {
    inlineMsg('phone','<strong>Error</strong><br />You have entered an invalid phone number.',2);
    return false;
  }
  */
  /*
  if(gender == "") {
    inlineMsg('gender','<strong>Error</strong><br />You must select your gender.',2);
    return false;
  }
  */
  
  if(message == "") {
	//alert('You must enter a message.');
    inlineMsg('minimessage','You must enter a message.');
    return false;
  }
  if(message.match(messageRegex)) {
	//alert('You have entered an invalid message.');
    inlineMsg('minimessage','You have entered an invalid message.');    
    return false;
  }
  return true;
}



// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// fade the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}



/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



// Scriptaculous Toggle
function s_toggle(id,style){
	if (style==1)
	Effect.toggle(id, 'appear', { duration: 0.5 });
	else
	Effect.toggle(id, 'blind', { duration: 0.5 });
}

// Scriptaculous Show
function show(id){
	$(id).style.display="block";
	//Effect.Appear(id, { duration: 0.5 });
}

// Scriptaculous Hide
function hide(id){
	$(id).style.display="none";
	//Effect.Fade(id, { duration: 0.5 });
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

 

function saveHomePagePositions(){

	//cookie string:
	var cookie = '';

	// iterate cols (editable_row class)
	var cols = document.getElementsByClassName(
      'editable_row', '_portal_'
    );
    
    cols.each(	
    	function (col) {
	    	
	    	var col_id=col.id.split('-');
			if (col_id[1]==1){
				cookie += 'c'+col_id[1]++;
			} else {
				cookie += '-c'+col_id[1]++;
			}
			var children = $A($(col.id).childNodes);
			children.each(function(child) {
	 			if (child.className=='box'){
	 				var child_id = child.id;
	 				var box_id = child_id.split('-');
	 				var cat_id = $('box-'+box_id[1]+'-value').value;
	 				cookie += '_'+cat_id;
	 				//alert(child.id+'-'+child_id+'-'+box_id);
	 			}
			});

			/*
			var boxes = $(col.id).getElementsByClassName('box', '_portal_');
			boxes.each(
    			function (box) {
    				var box_id=box.id.split('-');
    				cookie += '_'+box_id[1];
    			}
    		);
    		*/
    	}
    );
	
	
	//alert(cookie);
	// save cookie
	Set_Cookie( 'beacon_interface_settings', cookie, 30, '/', '', '' );
	
}



// Copyright 2006-2007 javascript-array.com

var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 
