// JavaScript Document
var pageLoad = false;
var contact_form_html = "";
function pageLoaded() {
	pageLoad = true;
}
//addOnloadEvent(pageLoaded);
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function showOverlay() {
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}
function hideOverlay() {
	// get objects
	//alert("hiding ... ");
	if (document.getElementById('overlay') && document.getElementById('overlay').style.display != 'none') {
		var objOverlay = document.getElementById('overlay');
		// hide lightbox and overlay
		objOverlay.style.display = 'none';
	}
}

function initOverlay() {
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
		objOverlay.setAttribute('name','overlay_lb');
	objOverlay.onclick = function () {
										//hideOverlay();
										//alert("click");
										//close_adding_layer();
										//ToggleFloatingLayer('FloatingLayer', 0, '');
										//close_div();
										//close_reg();
										reset_account_layer();
										if (objOverlay.style.display != 'none') objOverlay.style.display = 'none';
										return false;
										}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '5000';
 	objOverlay.style.width = '100%';
	objOverlay.style.opacity = '0.9';
	objOverlay.style.backgroundColor = '#514c49';
	objBody.insertBefore(objOverlay, objBody.firstChild);
}
function show_account_layer(type_div) {
	hide_account_layers();
	if (browser == 'Internet Explorer'){
		hideSelects('hidden');
	}
	var obj = document.getElementById("account_layer");
	var parent_obj = document.getElementById("account_links");
	var coor = findPos(parent_obj);
	var width_deduct = parseInt(document.getElementById("account_links").offsetWidth);
	//alert(coor[0]+" - "+coor[1]);
	var left_pos = parseInt(coor[0])-386+width_deduct;
	obj.style.left = left_pos+"px";
	var top = coor[1] + 72;
	obj.style.top = top+"px";
	//if (obj.style.display == "none" || obj.style.display == "") {
		if (document.getElementById(type_div)) document.getElementById(type_div).style.display = "block";
		obj.style.display = "block";
		if (type_div == "login_layer") {if (document.getElementById("account_email")) document.getElementById("account_email").focus();}
		else {if (document.getElementById("fname")) document.getElementById("fname").focus();}
		
	//}
	//else {
	//	if (document.getElementById(type_div)) document.getElementById(type_div).style.display = "none";
	//	obj.style.display = "none";
	//}
}
//addOnloadEvent(initOverlay);
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" ) {
	window.addEventListener( "load", fnc, false );
  }
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}
//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
addOnloadEvent(getBrowserInfo);
function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}
function ToggleFloatingLayer(DivID, iState, object) {
	//table_preview here COULD be my_cart
	//set coordinats if exists:
	if (document.getElementById("mini_cart")) {
		var div_width = document.getElementById("mini_cart").style.width;
		div_width = div_width.substring(0, div_width.length-2);
		var cur_status = document.getElementById(DivID).style.display;
		if (iState == 1 && (cur_status == "none" || cur_status == "")) {
			showOverlay();
			if (document.getElementById("mini_title")) document.getElementById("mini_title").innerHTML='Mini-Cart';
			var y = getElementTop(object);
			var x = getElementLeft(document.getElementById("table_preview"));
			//alert("x="+x);
			y = 3 + y +document.getElementById("table_preview").clientHeight;//object.clientHeight;
			x = x - div_width + document.getElementById("table_preview").clientWidth;
			document.getElementById(DivID).style.top = y+'px';
			document.getElementById(DivID).style.left = x+'px';
			document.getElementById(DivID).style.width = div_width+'px';
			//alert(x+" - "+y+" - "+div_width);
			//alert(object.innerHTML.substring(0,1));
			sendRequest("action=display","mini_cart");
			if (browser == 'Internet Explorer'){
				hideSelects('hidden');
			}
		}
		if (iState == 0) {
			if (document.getElementById('overlay').style.display != 'none') {
				hideOverlay();
				hideSelects('visible');
			}
		}
		//end set coordinats.
		if(document.layers)	   //NN4+
		{
		   document.layers[DivID].display = iState ? "block" : "none";
		}
		else if(document.getElementById)	  //gecko(NN6) + IE 5+
		{
			var obj = document.getElementById(DivID);
			obj.style.display = iState ? "block" : "none";
		}
		else if(document.all)	// IE 4
		{
			document.all[DivID].style.dispaly = iState ? "block" : "none";
		}
	}
	else {
		//alert (document.location);
	}
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
function hideSelects(visibility){
	selects = document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) {
		selects[i].style.visibility = visibility;
	}
}
function login_account(objid) {
	if (pageLoad) {
		if (objid) document.getElementById(objid).style.display = "none";
		if (document.getElementById('overlay') && document.getElementById('overlay').style.display != 'none') {
			close_adding_layer();
			ToggleFloatingLayer('FloatingLayer', 0, '');
			close_div();
			close_reg();
		}
		var obj_link = document.getElementById("a_login_account");
		if (obj_link.className != "account_link") {
			showOverlay();
			obj_link.className = "account_link";
			//document.getElementById("a_create_account").className = "account_link_back";
			var type_div = "login_layer";//login_layer
			show_account_layer(type_div);
			//refresh_user_account();
		}
		else {
			hideOverlay();
			obj_link.className = "account_link_back";
			hide_account_layers();
		}
	}
}
function hide_account_layers() {
	if (!document.getElementById("account_around")) {
		if (document.getElementById("account_layer")) document.getElementById("account_layer").style.display = "none";
		if (document.getElementById("account_layer_msg")) {document.getElementById("account_layer_msg").innerHTML = "";document.getElementById("account_layer_msg").style.display = "none";}
		if (document.getElementById("create_account_layer")) document.getElementById("create_account_layer").style.display = "none";
		if (document.getElementById("login_layer")) document.getElementById("login_layer").style.display = "none";
		if (contact_form_html != "") document.getElementById(divhandler_popUp.divtag).innerHTML = contact_form_html;
			if (browser == 'Internet Explorer'){
				hideSelects('visible');
			}
	}
}
function reset_account_layer() {
	if (document.getElementById("a_create_account")) document.getElementById("a_create_account").className = "account_link_back";
	if (document.getElementById("a_login_account")) document.getElementById("a_login_account").className = "account_link_back";
	hide_account_layers();
}
function findPos(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];
}
function close_account_layer() {
	try {
		objOverlay = document.getElementById("overlay");
		hide_account_layers();
		reset_account_layer();
		if (objOverlay.style.display != 'none') objOverlay.style.display = 'none';
	}
	catch(e) {
		//oops error	
	}
}
// AJAX!
function newPopup(an, pItem) {
	var width='350';
	var height='290';
	var borderStyle='1px solid #8eb4ec';
	//;
	var href = pItem;
	var boxdiv = document.getElementById(href);
  
	refreshCheck = 0;
  if (boxdiv != null) {
    if (boxdiv.style.visibility=='hidden') {
	  refreshCheck = 1;
      move_box(an, boxdiv);
      boxdiv.style.visibility='visible';
    } else
     // boxdiv.style.visibility='hidden';
	  refreshCheck = 1;
    return false;
  }
if(refreshCheck == 0){
  boxdiv = document.createElement('div');
  boxdiv.setAttribute('id', pItem);
  boxdiv.style.visibility = 'visible';
  boxdiv.style.position = 'absolute';
  boxdiv.style.width = width + 'px';
  //boxdiv.style.height = height + 'px';
  boxdiv.style.border = borderStyle;
  boxdiv.style.overflow = 'visible';
  boxdiv.style.backgroundColor = '#fff';


  document.body.appendChild(boxdiv);
  move_box(an, boxdiv);
  
 	event_p(pItem)
  }

  return false;
}
function event_p(pItem) {
	var more = pItem;
	sendRequest_popup(more,pItem);
}
function handleDivTag_calendarm(divtag){
   var divtag;
   return divtag;
}

function createRequestObject_popup() {
	var req;
	if(window.XMLHttpRequest){
		req = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("There was a problem creating the XMLHttpRequest object");
	}
		//alert("Test");
	return req;
}
	
var http_popUp = createRequestObject_popup();
var divhandler_popUp = new handleDivTag_calendarm(null);


function sendRequest_popup(more,divtag) {
	//alert(divtag);
	//if (document.getElementById(divtag)) document.getElementById(divtag).innerHTML = "<img src='/filebin/images/loading.gif'> Loading...";
	document.getElementById("quickSubmit").innerHTML = '<img src="/filebin/images/sending.gif" border="0" />';
	http_popUp.open("get", "http://www.orbitmedia.com/pages/popup/177.php?"+more);
	http_popUp.onreadystatechange = handleResponse_popup;
	divhandler_popUp.divtag = divtag;
	//if (document.getElementById(divhandler_popUp.divtag).innerHTML == "logout") window.location.reload();
	http_popUp.send(null);
	//alert("Test or something");
}
function handleResponse_popup() {    
	if(http_popUp.readyState == 4 && http_popUp.status == 200){
		var response = http_popUp.responseText;
		if(response) {
			contact_form_html = document.getElementById(divhandler_popUp.divtag).innerHTML;
			document.getElementById(divhandler_popUp.divtag).innerHTML = response;
			google_track_thankyou();
		}
		//else document.getElementById(divhandler_popUp.divtag).innerHTML = "Loading...";
	}
}
function google_track_thankyou() {
	try {
		//var pageTracker = _gat._getTracker("UA-2263284-3");
		pageTracker._trackPageview('/pages/thank_you/177.php');
	} 
	catch(err) {}	
}

// Form Validation

var requiredArr = Array("name");
var requiredArrName = Array("Name");

function isEmailAddress (string) {
  var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  return addressPattern.test(string);
}
function notEmptyStr(strValue) {
	if (strValue != "") return true;
	else return false;
}
function NumericOnly (string) {
	var objRegExp  = /[^0-9]/;
	return(objRegExp.test(string));
}

function validateForm(f) {
	var err ="";
	
	if (!isEmailAddress(f.email.value)) {
		err += "Your Email is invalid"+"\r\n";
	}

	for (var i=0; i<requiredArr.length; i++) {
		var curInput = eval("f."+requiredArr[i]);
		if (curInput) {
			if(notEmptyStr(curInput.value)==false) {
				err+=(requiredArrName[i]+" is blank or invalid\r\n");
			}
		}
	}	
	if (err!="") { 
		alert(err);
		return false;
	} else {
	sendRequest_popup(variables(),'contactForm'); 
	return false;
	}
}

var contact = "";
function variables (){
var more = "";
more += "name="+document.getElementById("name").value.replace(/\//g,"*@*").replace(/&/g,"@@@")+"&";
more += "email="+document.getElementById("email").value.replace(/\//g,"*@*").replace(/&/g,"@@@")+"&";
more += "phone="+document.getElementById("phone").value.replace(/\//g,"*@*").replace(/&/g,"@@@")+"&";
more += "contact="+contact+"&";
more += "page_id=www.orbitmedia.com*@*pages*@*page*@*"+page_id+".php&";
more += "comments="+document.getElementById("comments").value.replace(/\//g,"*@*").replace(/&/g,"@@@")+"&";
more += "form_id=6";
//alert (more);
return more;
}