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

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

function DOCStudioSharedEmailCheckES(email) {
	var emailadd = email;
	var msg = "";
	if (emailadd.indexOf('"') != -1 || emailadd.indexOf("'") != -1) {
		msg = "Su correo electrónico no puede contener comillas. ";
		}
	if (emailadd.indexOf(",") != -1) {
		msg += "Su correo electrónico no puede contener comas. ";
		}
	if (msg=="") {
		if (emailadd.search(/^\S+\@\S+\.\S+$/) == -1) {
			msg = "Tiene que escribir su correo electrónico completo - por ejemplo 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);
}

