function isValidEmail(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (lstr===0) {
		return false;
	}
	
	if (str.indexOf(at)==-1) {
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)===0 || str.indexOf(at)==lstr) {
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)===0 || str.indexOf(dot)==lstr) {
	    return false;
	}

	if (str.indexOf(at,(lat+1))!=-1) {
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1) {
		return false;
	}
	
	if (str.indexOf(" ")!=-1) {
		return false;
	}

	return true;
	
}

function goChapter(ref, filter){
	location.href = ref + '/Guidance/' + filter;
}

function selectChapter(filter){
	
	if (filter.length > 0) {
		
		var chapSel = document.getElementById('chaptersel');
		
		for(i=0; i<chapSel.options.length; i=i+1) {
			if(chapSel.options[i].value == filter){
				chapSel.options[i].selected = true;
				break;
			}
		}
	}
}

function printAll(ref) {
	pwindow = window.open(ref+"/Guidance?egpType=printall", "printall", "resizable,location=1,status=1,scrollbars=1,width=200,height=200");
	pwindow.moveTo(400,400);
}

function printChapter(ref, filter) {
	pcwindow = window.open(ref+"/Guidance/"+filter+"?egpType=printchapter", "printchapter", "resizable,location=1,status=1,scrollbars=1,width=200,height=200");
	pcwindow.moveTo(400,400);
}

function CreateBookmarkLink(title) {

	url = window.location.href;
 
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);
	} else if (window.opera && window.print) { // Opera Hotlist
		return true;
	}

}
	
function addToFavorites(pageName) {
	if (window.external) {
		window.external.AddFavorite(window.location.href, pageName);
	} else {
		alert("Sorry! Your browser doesn't support this function.");
	}
}

function fontSize(ref, filter, size) {
	location.href = ref + '/Guidance/' + filter + '?fs=' + size;
}

function doExport() {
	
	var exForm = document.getElementById('exportForm');
	
	var found = false;
	
	if (exForm.viewCB.length) {
		for(i=0; i < exForm.viewCB.length; i=i+1) {
			if (exForm.viewCB[i].checked===true){
				found=true;
			}
		}
	} else {
		if (exForm.viewCB.checked===true) {
			found=true;
		}
	}
	
	if (!found) {
		alert('Please select at least one item to export!');
	} else {
		exForm.submit();
	}

}

function doExportToc(m) {
	
	var exForm = document.getElementById('exportForm');
	
	exForm.command.value = "export";
	exForm.target="_blank";
	
	var	found = false;
	
	if (exForm.viewCBToc.length) {
		for (i=0; i < exForm.viewCBToc.length; i=i+1) {
			if (exForm.viewCBToc[i].checked===true) {
				found=true;
			}
		}
	} else {
		if (exForm.viewCBToc.checked===true) {
			found=true;
		}
	}
	
	if (!found) {
		alert('Please select at least one item to export!');
	} else {
		if (m=='email' && !isValidEmail(exForm.emailto.value)) {
			alert('Please provide a valid email address to export!');
		} else {
			exForm.mode.value = m;
			exForm.submit();
		}
	}
	
}

function doSelectionToc(url){
	
	var exForm = document.getElementById('exportForm');

	exForm.action = url;
	exForm.target = "";
	exForm.command.value = "selection";
	
	var found = false;
	
	if (exForm.viewCBToc.length) {
		for (i=0; i < exForm.viewCBToc.length; i=i+1) {
			if (exForm.viewCBToc[i].checked===true) {
				found=true;
			}
		}
	} else {
		if (exForm.viewCBToc.checked===true) {
			found=true;
		}
	}
	
	if (!found) {
		alert('Please select at least one item!');
	} else {
		exForm.submit();
	}
	
}

function doSelection(url, commandx){

	var selFrm = document.getElementById('selectionForm');

	selFrm.action = url;
	selFrm.command.value = commandx;
	if (selFrm.nodata.value == 1) {
		selFrm.submit();
	} else {
		found = false;
		if (selFrm.viewCBToc !== null && selFrm.viewCBToc.length) {
			for (i=0; i < selFrm.viewCBToc.length; i=i+1) {
				if (selFrm.viewCBToc[i].checked===true) {
					found=true;
				}
			}
		} else {
			if (selFrm.viewCBToc !== null && selFrm.viewCBToc.checked===true){ 
				found=true;
			}
		}

		if (!found && (commandx != 'clearall' && commandx != 'myselclearall' && commandx != 'view')) {
			alert('Please select at least one item!');
		} else {
			selFrm.submit();
		}

	}
}

function printme(){
	window.print();
}

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++){
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
			theForm[z].checked = theElement.checked;
		}
    }
}

