var homepage_title = "Automated Robotic Solutions for Delivery of Hospital Goods and Healthcare Supplies, Asset Management, and Asset Tracking";

var case_study_array = [];

case_study_array[0] = {"parent":"applications", "src":"images/", "prefix":"caseStudy_", "suffix":".jpg", "root":"spotlight/case_studies.php"};
case_study_array[1] = {"src":"pharmacy", "alt":"Featured Case Study - University of Maryland Medical Center", "copy":"University of Maryland Medical Center"};
case_study_array[2] = {"src":"lab", "alt":"Featured Case Study - Alle-Kiski Medical Center", "copy":"Alle-Kiski Medical Center"};
case_study_array[3] = {"src":"distribution", "alt":"Featured Case Study - Providence Hospital", "copy":"Providence Hospital"};

function init(loc, site_root) {
	
	setNum();
	handleCaseStudy(loc, site_root);
	setTitle(loc, site_root);
	setNav(loc, site_root);
	if(document.getElementById("flashArea")) {
		swfobject.embedSWF("flash/AETHON_homepage_v3_750x211.swf", "flashArea", "750", "211", "9.0.0", "#FFFFFF");
	}
	
	if(document.getElementById("videoArea")) {
		loadVideo("tug_video");
	}
	
	
}

function setNav(loc, site_root){
	var section = loc.split(site_root);
	var nav_id = section[1].substring(0,section[1].lastIndexOf("/"))
	if(section[1] != "default.php" && document.getElementById(nav_id)) {
		document.getElementById(nav_id).className = "selected";
	}
}

function setTitle(loc, site_root) {
	var page = loc.split(site_root);
	var str = "Aethon - ";
	
	if(page[1] != "default.php") {
		var tag = document.getElementsByTagName("h1");
		str += tag[0].innerHTML;
	} else {
		str += homepage_title;
	}

	str = str.replace("&amp;", "and");
	document.title = str;
}

function handleCaseStudy(loc, site_root) {
	var n;
	var local = loc.substring(loc.lastIndexOf("/")+1, loc.length-4);
	var _parent = loc.split("/");
	_parent = _parent[_parent.length-2];
	
	/*if(_parent == case_study_array[0]['parent'] && local != "default") {
		for(i=1; i < case_study_array.length; i++) {
			if(case_study_array[i]['src'] == local) {
				n = i;
				break;
			}
		}
	} else {*/
		n = Math.floor(Math.random()*(case_study_array.length-1))+1;
	//}
	
	var cs_image = document.getElementById("caseStudy");
	var cs_link = document.getElementById("caseStudy_link");
	var cs_alt = "'" + case_study_array[n]['alt'] + "'";
	
	if (document.location == 'http://www.aethon.com/default.php' || document.location == 'http://www.aethon.com/') {
			cs_image.innerHTML = '<h3>Recently Featured on Good Morning America</h3></br><embed src="../flash/GMAHealthcare.swf" width="175" height="141" bgcolor="#FFFFFF" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"flashvars="file=../flash/GMAHealthcare.swf" />'; 
			cs_image.innerHTML += '<p><a href=http://abcnews.go.com/GMA/video/robots-operating-room-11486241>View video at ABCNews.com</a>';
	}
	else {
		cs_image = document.getElementById("caseStudy_img");
		cs_image.innerHTML = "<a href=\"../spotlight/case_studies.php\"><img src=" + site_root + case_study_array[0]['src'] + case_study_array[0]['prefix'] + case_study_array[n]['src'] + case_study_array[0]['suffix'] + " alt=" + cs_alt + " width=\"143\" height=\"97\" /></a>";
		cs_link.innerHTML = "<a href=\"../spotlight/case_studies.php\">" + case_study_array[n]['copy'] + "</a><br /><br /><a href=" + site_root + case_study_array[0]['root'] + ">View All Case Studies</a>";
	}
	
}

function setNum(){
	old_num = 0;
}
function faq(num,this_link){
	//if a section is already open, close it
	document.getElementById("faq_1").className = "faq_hide";
	if (old_num != 0){
		document.getElementById("faq_"+old_num).className = "faq_hide";
		if (num == old_num) {
			old_num = 0;
		// if not, open new one
		} else {
			document.getElementById("faq_"+num).className = "faq_show";
			old_num = num;
		}
		// if no section is opne, open it and store it's number
	} else {
		document.getElementById("faq_"+num).className = "faq_show";
		old_num = num;
	}
}

function hide_faq(){
	for (n=1;n<=10;n++){
		document.getElementById("faq_"+n).className = "faq_hide";
	}
}

function benefits(num,this_link){
	//if a section is already open, close it
	document.getElementById("benefits_1").className = "benefits_hide";
	if (old_num != 0){
		document.getElementById("benefits_"+old_num).className = "benefits_hide";
		if (num == old_num) {
			old_num = 0;
		// if not, open new one
		} else {
			document.getElementById("benefits_"+num).className = "benefits_show";
			old_num = num;
		}
		// if no section is opne, open it and store it's number
	} else {
		document.getElementById("benefits_"+num).className = "benefits_show";
		old_num = num;
	}
}

function hide_benefits(){
	for (n=1;n<=7;n++){
		document.getElementById("benefits_"+n).className = "faq_hide";
	}
}

function validateEmail(id) {
	
	var obj = document.getElementById(id);
	var e = obj.value.replace(/^\s+|\s+$/g, '');
	var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(!email_filter.test(e) || e == "") {
		obj.value = "Invalid Email Address";
	    return false;
    } else {
        return true;
    }
    
}	

function validateForm(id) {
    
	if(document.all || document.getElementById) {
		
	    var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	    
	    var errMsg = "Please complete these required fields:<br />";
	    var err = new Boolean();
		err = false;
		
	    var f = document.getElementById(id);
	    var lbl = toArray(f.getElementsByTagName("label"));
	    var fi = toArray(f.getElementsByTagName("input"));
	    fi.pop();
	    var fs = toArray(f.getElementsByTagName("select"));
	    var ft = toArray(f.getElementsByTagName("textarea"));
	    var fieldsArray = fi.concat(fs,ft);
	    cleanArray(fieldsArray);
	    
	    for(var i = 0; i < fieldsArray.length; i++) {
		    var x = fieldsArray[i];
		    var s = new String();
		    if(x.id.indexOf("_req")+1 && x.value == "") {
			    err = true;
			    s = x.id.substr(0,x.id.indexOf("_")) + "_lbl";
			    errMsg += "&nbsp;-&nbsp;" + document.getElementById(s).innerHTML + "<br />";
		    }
	    }
	    
	    errMsg = errMsg.replace(/:\*/g, "");
	    
	    if(!err) {
		    var e = document.getElementById("errorMsg");
		    var ie = validateEmail("email_req");
		    var ce = validateEmail("emailConfirm_req");
		    if(ie && ce) {
		   		 if(document.getElementById("email_req").value != document.getElementById("emailConfirm_req").value) {
	          		  e.innerHTML = "Email addresses do not match.<br />";
	          		  err = true;
	       		 }
       		 }
       		 else {
	       		 e.innerHTML = "Invalid Email Entries.<br />";
	       		 err = true;
	       	}
	    }
	    else {
		    var e = document.getElementById("errorMsg");
		   
		    e.innerHTML = errMsg + "<br />";
	    }

		if(err) { window.location.hash = "errorAnchor"; return(false); }
		else return(true);
		
	}
}

function validateForwardForm() {
    
	if(document.all || document.getElementById) {
	    
		var to_err = validateEmail("to_email");
		var from_err = validateEmail("from_email");
	    
		if(to_err && from_err) return true;
	    else return false;
		
	}
}

function validateOptInForm() {
    
	if(document.all || document.getElementById) {
	    
		var to_err = validateEmail("to_email");
		var from_err = validateEmail("from_email");
	    
		if(to_err && from_err) return true;
	    else return false;
		
	}
}


function toArray(obj) {
	var a = new Array();
	for(var i = 0; i < obj.length; i++) {
		a[a.length] = obj[i];
	}
	return a;
}

function cleanArray(obj) {
	for(var i = 0; i < obj.length; i++) {
		obj[i].value = obj[i].value.replace(/^\s+|\s+$/g, '');
	}
	return obj;
}

function loadVideo(s) {
	
	if (s == 'tug_video') {
		var videoPlacement = document.getElementById("videoArea");
	
		videoPlacement.innerHTML = "<param name=\"quality\" value=\"high\" /><embed src=\"flash/aethon_tug_video.swf\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"360\" height=\"270\"></embed>";
	}
	
	else if (s == 'youTube_04') {
		var videoPlacement = document.getElementById("videoArea");
		videoPlacement.innerHTML = "<param name=\"movie\" value=\"http://www.youtube.com/v/eU9C26uNjnk&amp;hl=en&amp;fs=1&amp;rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/eU9C26uNjnk&amp;hl=en&amp;fs=1&amp;rel=0\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"360\" height=\"270\"></embed>";
		
		}
		
	else if (s == 'youTube_01') {
		var videoPlacement = document.getElementById("videoArea");
	
		videoPlacement.innerHTML = "<param name=\"movie\" value=\"http://www.youtube.com/v/55Cv6yU8TPE&hl=en&fs=1&rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/55Cv6yU8TPE&hl=en&fs=1&rel=0\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"360\" height=\"270\"></embed>";
		}
		
	else if (s == 'youTube_02') {
		var videoPlacement = document.getElementById("videoArea");
	
		videoPlacement.innerHTML = "<param name=\"movie\" value=\"http://www.youtube.com/v/_wTHlMPkqpU&hl=en&fs=1&rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/_wTHlMPkqpU&hl=en&fs=1&rel=0\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"360\" height=\"270\"></embed>";
		}
		
	else if (s == 'youTube_03') {
		var videoPlacement = document.getElementById("videoArea");
	
		videoPlacement.innerHTML = "<param name=\"movie\" value=\"http://www.youtube.com/v/9P9UxnVb7EU&hl=en&fs=1&rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/9P9UxnVb7EU&hl=en&fs=1&rel=0\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"360\" height=\"270\"></embed>";
		}
}
	
function whitePaperForm(n, sr) {
	window.open("../wp.php?id=" + n,null,"width=400,height=425,scrollbars=no,resizable=no");
}	

function external() {
    if (document.getElementsByTagName) {
	var a;
	a = document.getElementsByTagName('a');
	for (i = 0; i<a.length; i++) {
        if (a[i].getAttribute('href') && a[i].getAttribute('rel') == 'external'){
    		a[i].target = '_blank';
    		
			}
		}
	 }
}