// for targed links in the popup window

if (window.name != "docs_popup") {
	window.name = "docs_main";
	this.focus();
	}

// pop up window

var docs_popup;
function popup(url, name, w, h) {
	var temp;
	if (!name) {name = 'docs_popup';}
	if (!w) {w = 460;}
	if (!h) {h = 500;}
	eval('temp = ' + name);
	temp = window.open(url, name, 'width=' + w + ',height=' + h + ',scrollbars,resizable,left=12,top=12');
	if (parseInt(navigator.appVersion) >= 4) { temp.window.focus(); }
	eval(name + ' = temp');
}



// drop-downs

var DOCSMenu = null;
var ClosedMenu = null;
var Timeout_ID;

function menuOver(MenuID) {
	
	ClosedMenu = null;
	// old netscape browsers
	if (document.layers) {
		if (document.layers[MenuID]!=null) {
			// the window is already open
			if (DOCSMenu==document.layers[MenuID]){
				clearTimeout(Timeout_ID);
				} else{
				// opening a new menu, so close the old one
				if(DOCSMenu!=null){
					clearTimeout(Timeout_ID);
					hideNow();
					}
				}
			// show the menu
			DOCSMenu = document.layers[MenuID];
			if (DOCSMenu.visibility!=null) {
				Timeout_ID = window.setTimeout('showNow();',600);
				}
			}
	// newer browsers
		} else {
		if ((document.getElementById)&&(document.getElementById(MenuID)!=null)) {
		// the window is already open
			if (DOCSMenu==document.getElementById(MenuID)){
				clearTimeout(Timeout_ID);
				} else{
				// opening a new menu, so close the old one
				if(DOCSMenu!=null){
					clearTimeout(Timeout_ID);
					hideNow();
					}
				}
			// show the menu
			DOCSMenu = document.getElementById(MenuID);
			if ((DOCSMenu.style) && (DOCSMenu.style.visibility!=null)) {
				Timeout_ID = window.setTimeout('showNow()',600);
				}
			}
		}
}

function showNow() {
	
	if (ClosedMenu != DOCSMenu) {
		// old netscape browsers
		if (document.layers) {
			DOCSMenu.visibility = 'visible';
		// newer browsers
			} else {
			if ((DOCSMenu.style)&&(DOCSMenu.style.visibility)) {
				DOCSMenu.style.visibility = 'visible';
				}
			}
		}
}

function menuOut(MenuID){

	
	// old netscape browsers
	if (document.layers) {
		if (document.layers[MenuID]!=null) {
			DOCSMenu = document.layers[MenuID];
			ClosedMenu = DOCSMenu;
			Timeout_ID = window.setTimeout('hideNow();',60);
			}
	// newer browsers
		} else {
		if ((document.getElementById)&&(document.getElementById(MenuID)!=null)) {
			DOCSMenu = document.getElementById(MenuID);
			ClosedMenu = DOCSMenu;
			Timeout_ID = window.setTimeout('hideNow();',60);
			}
		}
}

function hideNow() {
	
	// old netscape browsers
	if (document.layers) {
		DOCSMenu.visibility = 'hidden';
	// newer browsers
		} else {
		if ((DOCSMenu.style)&&(DOCSMenu.style.visibility)) {
			DOCSMenu.style.visibility = 'hidden';
			}
		}
}
	


		
function getInsideWindowHeight() {
	
    if (window.innerHeight) {
        return window.innerHeight;
        } else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
		} else {
		return 600;
		}
}

		
function getInsideWindowWidth() {
	
    if (window.innerWidth) {
        return window.innerWidth;
        } else if (document.documentElement && document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
		 } else if (document.body) {
		return document.body.clientWidth;
		} else {
		return 800;
		}
}

// Standard DOCStudio AJAX script
function DOCStudioSharedAjaxFunction(geturl,posturl,poststr,on_success,on_fail) {
	var xmlHttp;
	try {	xmlHttp=new XMLHttpRequest(); }// Firefox, Opera 8.0+, Safari
	catch (e) {
		try	{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } // Internet Explorer
		catch (e) {
			try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
			catch (e) {
				on_fail("Your browser does not support AJAX.");
				}
			}
		}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			if (xmlHttp.responseText) { on_success(xmlHttp.responseText); }
			//else { on_fail("XMLHttpRequest Error with status: " + xmlHttp.status) }
			}
		}

if (posturl != '' && poststr != '') {
	xmlHttp.open('POST', posturl, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", poststr.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(poststr);
	} else {
	xmlHttp.open("GET",geturl,true);
	xmlHttp.send(null);
	}

}


// email validator
function DOCStudioSharedEmailCheck(email) {
	var emailadd = email;
	var msg = "";
	if (emailadd.indexOf('"') != -1 || emailadd.indexOf("'") != -1) {
		msg = "Your email address cannot contain quotes. ";
		}
	if (emailadd.indexOf(",") != -1) {
		msg += "Your email address cannot contain commas. ";
		}
	if (msg=="") {
		if (emailadd.search(/^\S+\@\S+\.\S+$/) == -1) {
			msg = "Please enter a full email address - for example, byron@yahoo.com";
			}
		}
	
	return(msg);
}

// show-hide div
function DOCStudioShowHide(divid,disp) {
	if (disp == '') { disp = 'block'; }
	var ShowExpandDiv=document.getElementById(divid);
	if (ShowExpandDiv.style.visibility == 'visible') {
		ShowExpandDiv.style.visibility='hidden';
		ShowExpandDiv.style.display='none';
		} else {
		ShowExpandDiv.style.visibility='visible';
		ShowExpandDiv.style.display=disp;
		}
}


function autoGenerateFilename(sourceField,targetField,already_published) {

	if (already_published == '') {
		var page_name = sourceField.value.toLowerCase();
		var page_filename = "", temp = "";
		var filter ="abcdefghijklmnopqrstuvwxyz1234567890_-";
		var i = 0, t = 32, hyphencount = 0, maxhyphens = 4;
		// replace accents
		page_name = page_name.replace(/[àáâãäå]/g, "a");
		page_name = page_name.replace(/ç/g, "c");
		page_name = page_name.replace(/[èéêë]/g, "e");
		page_name = page_name.replace(/[ìíîï]/g, "i");
		page_name = page_name.replace(/ñ/g, "n");
		page_name = page_name.replace(/[òóôõöø]/g, "o");
		page_name = page_name.replace(/[ùúûü]/g, "u");
		// Skip to the first non-space character
		while( page_name.charAt(i) == ' ' ) { i++; }
		// Skip any spaces at the end
		if ( page_name.charAt(t) == ' ' ) {
		while( page_name.charAt(t) == ' ' && t != 0) { t--; }
		}
		for(; i <= t; i++) {
			if (page_name.charAt(i) == ' ') {
				hyphencount++;
				if (hyphencount <= maxhyphens) { temp += '-'; }
				} else if (page_name.charAt(i) == '-' && i == t) {
				temp = temp;
				} else if (filter.indexOf(page_name.charAt(i).toLowerCase(), 0) != -1) {
				temp += page_name.charAt(i);
				}	
			if (hyphencount > maxhyphens) { i = t; }
		}
		
		var flag = false;
		// Remove repeating hyphens
		for( i = 0; i < temp.length; i++) {
		
		if (temp.charAt(i) != '-' || !flag) {
		page_filename += temp.charAt(i);
		}
		if (temp.charAt(i) == '-') {
		flag = true;
			} else {
		flag = false;
			}
		}
		
		// Check for a trailing space.
		if (page_filename.charAt(temp.length) == '-') {
		page_filename = page_filename.substring(0, temp.length);
		}
		targetField.value = page_filename.toLowerCase();
	}
}

function DSformatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}


	function toggleChecked(status) {
		$(".listcheckbox").each( function() {
		$(this).attr("checked",status);
		})
	}

	
	function isEmpty(obj) {
		for(var prop in obj) {
			if(obj.hasOwnProperty(prop))
				return false;
		}
	
		return true;
	}
	
	function timeConverter(UNIX_timestamp){
		 var a = new Date(UNIX_timestamp*1000);
		 var t = new Date();
		 var td = t.getDate();
		 var tm = t.getMonth();
		 var ty = t.getFullYear();

		 var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
			 var year = a.getFullYear();
			 var month = a.getMonth();
			 var month_word = months[a.getMonth()];
			 var date = a.getDate();
			 var hour = a.getHours();
			 var min = a.getMinutes();
			 var sec = a.getSeconds();
			 if (year < 2010 || isNaN(month) || isNaN(date) || isNaN(year)) {
			 	// invalid date or 00/00/00
			 	var time = "--";
			 	} else if (td == date && tm == month && ty == year) {
			 	var time = "today";
			 	} else {
			 	month++;
			 	var time = month+'/'+date+'/'+year;
			 	}
			 return time;
	 }



 	 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);
	}


