//****************************** Browser Detection. ******************************	
var detect = navigator.userAgent.toLowerCase();
	var OS,browser,version,total,thestring;
	insubcategory = false;
	if (checkIt('konqueror'))
	{
		browser = "Konqueror";
		OS = "Linux";
	}
    else if (checkIt('netscape')) browser = "Netscape Navigator";
	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('firefox')) browser = "Mozilla Firefox";
	else if (!checkIt('compatible'))
	{
		browser = "Mozilla"
		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 checkIt(string)
	{
		place = detect.indexOf(string) + 1;
		thestring = string;
		return place;
	}
	
	


//****************************** Nav Controls ******************************	
function showNav(itemName,itemNum,width) {	
	showItem = document.getElementById(itemName);
	//define physical nav width for all dropdowns
	//how many pages in the root level nav
	//var totalMasterPages = 10;
	//alert("browser: "+browser+" OS: "+OS);
	
	///###### COMMENT OUT to force sub nav to utilize parent's width ######
	width = 125;

	if(browser != "Internet Explorer" && OS == "Windows") {
    //Non-IE, PC
	//add a little to the margins
	showItem.style.width = width;
	if(itemNum == 1) {
	showItem.style.marginTop = "3px";
	showItem.style.marginLeft = "-1px";
	} else {
	showItem.style.marginTop = "-9px";	
	}
	//show nav
    showItem.style.display = "block";
	//alert("firefox-ish");
    }
	//only 
	else if(OS =="Mac") {
    //Safari & Firefox - Mac (both render the same)
    //add a little to the margins
	showItem.style.width = width;
	if(itemNum == 1) {
	showItem.style.marginTop = "3px";
	showItem.style.marginLeft = "-1px";
	} else {
	showItem.style.marginTop = "-9px";	
	}
			/*
			//move the last nav item more to the left
			if(itemNum < totalMasterPages) {
				showItem.style.marginLeft = "-1px";
			} else {
				showItem.style.marginLeft = "-90px";	
			}
			*/
	//show nav
    showItem.style.display = "block";
	//alert("safari");		
    } 	
	else if(browser == "Internet Explorer" && OS =="Windows") {
    //IE 6+ in 'standards compliant mode'
	//else do a buncha other stuff 'cuz IE sucks
	//add a little to the margins
	showItem.style.width = width;	
	if(itemNum == 1) {
	showItem.style.marginTop = "3px";
	showItem.style.marginLeft = "-40px";
	} else {
	showItem.style.marginTop = "-10px";	
	}
	/*
		//move the last nav item more to the left
	if(itemNum > totalMasterPages) {
		showItem.style.marginLeft = "-100px";	
	}
	*/
	//show nav
    showItem.style.display = "block";	
    } else {
	showItem.style.width = width;	
	if(itemNum == 1) {
	showItem.style.marginTop = "3px";
	} else {
	showItem.style.marginTop = "-9px";	
	}
	//show nav
    showItem.style.display = "block";	
	}

}

//****************************** General Functions ******************************	
//show layer by id
function show(itemName) {	
	clearInterval(timer);
	showItem = document.getElementById(itemName);
	showItem.style.display="block";
	}

//hide layer by id
function hide(itemName)
{
	hideItem = document.getElementById(itemName);
	
	if(hideItem)
	{
		hideItem.style.display="none";
	}
}

//hides an item on a delay

lastfield = "";
function hideTimer(itemName) {
	
	if(lastfield != itemName)
	{	
		timer2 = setTimeout("hide('"+lastfield+"')",2000); 
		lastfield = itemName;
		if (!insubcategory)
		{
		timer = setTimeout("hide('"+itemName+"')",2000);
	
		}
		else
		{
	 	clearTimer();
		}
	}
}

function subcatTrue()
{
	insubcategory = true;	
}

function subcatFalse()
{
	insubcategory = false;	
}


function clearTimer() {
	
	
	clearTimeout(timer);
	//clearInterval(timer);
}



function toggle(itemName) {	
	toggleItem = document.getElementById(itemName);
	
	if(toggleItem.style.display == "none") {
		toggleItem.style.display = "block";
	} else if(toggleItem.style.display == "block") {
		toggleItem.style.display = "none";
	}	
	
	
}

//generic popup window
function openWindow(url, name, width, height,scrollbars) {
window.open(url,name,'width='+width+',height='+height+',top=200,left=200,scrollbars='+scrollbars+',resize=yes');	
}

//dreamweaver preloader stuff
function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)

    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function swapImgRestore() { //v3.0
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function swapImage() { //v3.0
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//****************************** SIMPLE AJAX FOR BLOG PAGES, ETC ******************************	

//Detection Scripting 
var please_wait = "<span class='colorHeader1'><b>Loading...</b></span>";
//GENERICA AJAX CALL
function open_url(url,page_id,level,data,target) {
	
	//alert(page_id+","+level+","+target);
	
 	if ( ! document.getElementById) {
  		return false;
 	}

 	if (please_wait != null) {
  		document.getElementById(target).innerHTML = please_wait;
 	}
	
 	if (window.ActiveXObject) {
  		link = new ActiveXObject("Microsoft.XMLHTTP");
 	} else if (window.XMLHttpRequest) {
  		link = new XMLHttpRequest();
 	}

 	if (link == undefined) {
  		return false;
 	}
	
 	link.onreadystatechange = function() { response(url, target); }
 	link.open("POST", url+"?page_id="+page_id+"&level="+level+"&data="+data, true);
	link.send(null);
}

function response(url, target) {
 	if (link.readyState == 4) {
	 	document.getElementById(target).innerHTML = (link.status == 200) ? link.responseText : "<span class='defaultText'>Error! Data Unavailable. Errorcode: " + link.status + "</span>";
	}
}

//****************************** Generic Form Validation ******************************	

function validateForm(formname)
{
	//root vars for the validate, dont move them!
	var thisForm = eval('document.'+formname);
	var total_items = thisForm.elements.length;
	var errors = "";
	var prevRadio = "";

	//RegEx determines how explicit validation will be based on type
	var noRegEx = /^.*$/;
	var textRegEx = /^.+$/;
	var phoneRegEx = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);

	var emailRegEx = /^([0-9a-zA-Z.])+(@)[0-9a-zA-Z.]+$/;
	var webRegEx = /^(http:\/\/)*(www.)[0-9a-zA-Z.]+$/;

	for(i=0;i<total_items;i++)
	{
		//id
		thisId = thisForm.elements[i].id;	
		//id for output
		thisLabel = thisForm.elements[i].id.substr(2);
		label_name = thisForm.elements[i].id;	

		//name for radios
		thisName = thisForm.elements[i].name;
		
		//value to determine REGEX values where applicable - t for text or select, p for phone, w for web, r for radio
		thisVal = thisForm.elements[i].id.substr(0,1);	
		
		var regEx = "";
		
		//pull diff RegEx values for diff field types
		switch(thisVal)
		{
			case "p":
				regEx = phoneRegEx;
				break;
			case "w":
				regEx = webRegEx;
				break;
			case "e":
				regEx = emailRegEx;
				break;
			case "x":
				regEx = noRegEx;
				break;
			default:
				regEx = textRegEx;
		}
		
		//text & select fields
		if(thisForm.elements[i].type == "text" || thisForm.elements[i].type == "select")
		{	
			if(thisVal == "t")
			{
				if(thisForm.elements[i].value == "")
				{	
					errors += "*Please Enter "+label_name+"\n";
				}				
			}
			else
			{
				if(!regEx.test(thisForm.elements[i].value))
				{	
					if(thisVal == "p")
					{
						errors += "*Please Enter a Phone Number (000)000-0000\n";	
					}
					else if(thisVal == "w")
					{
						errors += "*Please Enter a Valid Website [www.site.com]\n";	
					}
					else if(thisVal == "e")
					{
						errors += "*Please Enter a Valid Email [yourname@yoursite.com]\n";	
					}
					else
					{
						errors += "*Please Enter "+label_name+"\n";	
					}				
				} 	
			}
		//radio buttons
		}
		else if(thisForm.elements[i].type == "radio")
		{
			//get total radio btns of this name
		    thisLength = thisForm.elements[thisName].length;	
	
			var numRadios = thisLength;		
			var radioErrors = 0;
									
			for(x=0;x<numRadios;x++)
			{		
				//get checked properties for each radio btn	in this group
				if(thisForm.elements[thisName][x].checked == false)
				{
					radioErrors++;
				}
				
				if(radioErrors == numRadios)
				{
					//only log errors for each radio group
					if(prevRadio != thisName)
					{
						prevRadio = thisName;
						errors += "*Please Select a Choice for "+label_name+"\n";
					}
				} 				
			}
		}
		else if(thisForm.elements[i].type == "checkbox")
		{
			if(thisVal == "c")
			{
				if(thisForm.elements[i].checked == false)
				{
					errors += "*Please Check an Option for "+label_name+"\n";
				}			
			}
			//for any other element
		}
		else
		{
			if(thisForm.elements[i].value == "")
			{	
				errors += "*Please Enter (or Select) "+label_name+"\n";
			} 	
		}
	}
	
	if(errors != "")
	{
		alert(errors);
		return false;
	}
	
	return true;
}

function ValidateEmailFriend()
{
	if(document.email_friend.sender_name.value == "")
	{
		alert("Please Enter Your Name");
		document.email_friend.sender_name.focus();
		return false;
	}
	var fnameCheck =/^[0-9a-zA-Z \,#.]+$/;
	if(!fnameCheck.test(document.email_friend.sender_name.value)) {
		alert("Please Check Your Name for Valid Characters (only Numbers,Letters, ,#.)");
		document.email_friend.sender_name.focus();
		return false;
	}

	var emailCheck =/^([0-9a-zA-Z.])+(@)[0-9a-zA-Z.]+$/;
	if(!emailCheck.test(document.email_friend.sender_email.value))
	{
		alert("Please Enter Your Valid Email Address");
		document.email_friend.sender_email.focus();
		return false;
	}
	
	if(document.email_friend.friend_name.value == "")
	{
		alert("Please Enter Your Friend's Name");
		document.email_friend.friend_name.focus();
		return false;
	}
	var fnameCheck =/^[0-9a-zA-Z \,#.]+$/;
	if(!fnameCheck.test(document.email_friend.friend_name.value)) {
		alert("Please Check Your Friend's Name for Valid Characters (only Numbers,Letters, ,#.)");
		document.email_friend.friend_name.focus();
		return false;
	}

	var emailCheck =/^([0-9a-zA-Z.])+(@)[0-9a-zA-Z.]+$/;
	if(!emailCheck.test(document.email_friend.friend_email.value))
	{
		alert("Please Enter Your Friend's Valid Email Address");
		document.email_friend.friend_email.focus();
		return false;
	}

	return true;
	
}

function update_view_details(media_id, media_details)
{
	open_url('/manage/mng_assets/asset_details.php',media_id,'',media_details,'media')
}
function update_details(page_id, search_type, search_text)
{
	open_url('/manage/mng_assets/asset_list.php',page_id,search_type,search_text,'media_layer');
}