function isConditionWidthAndHeightFancybox(){
	if(screen.width < 1280 && screen.height < 720){
		return true;
	}
	return false;
}
function getWidthFancybox(){	
	var widthFancybox = 891;		
	if(isConditionWidthAndHeightFancybox()){
		widthFancybox = widthFancybox * 0.8;
	}
	return widthFancybox;					
}

function getHeightFancybox(){
	var heightFancybox = 648;		
	if(isConditionWidthAndHeightFancybox()){
		heightFancybox = heightFancybox * 0.8;
	}
	return heightFancybox;					
}



function viewNewPhoto(photoName){
	var xmlhttp=false;
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}		  	
	xmlhttp.open("POST", "home/newPhoto/photoName/" + photoName, true);
	xmlhttp.onreadystatechange=function correctResponse() {
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				document.getElementById("photo").innerHTML = xmlhttp.responseText;
			}
		}
	};			        		 
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");						 
	var query_string = "photoName=" + encodeURIComponent(photoName);
	xmlhttp.send(query_string);
}

var bName = navigator.appName;

function taCount(taObj, maxValue) { 
	objCnt=createObject('myCounter2');
	objVal=taObj.value;
	if (objVal.length>=maxValue){
		objVal=objVal.substring(0,maxValue);
		objTeaser = createObject(taObj.id);
		objTeaser.value = objVal;
	}
	if (objCnt) {
		if(bName == "Netscape"){	
			objCnt.textContent="Character count: " + objVal.length + " (max. " + maxValue + ")";}
		else{
			objCnt.innerText="Character count: " + objVal.length + " (max. "+ maxValue +")";}
	}
	return true;
}

function createObject(objId) {
	if (document.getElementById) return document.getElementById(objId);
	else if (document.layers) return eval("document." + objId);
	else if (document.all) return eval("document.all." + objId);
	else return eval("document." + objId);
}

function selectThemeOrProgram(selectName, pageViewName, topicId, programId, tagId, themeId){
	if(selectName == 'Topic'){
		changeTopicToCurrent(topicId);
	}
	var xmlhttp=false;
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}		  	
	xmlhttp.open("POST", "/getInfo/select" + selectName + "/topicId/" + topicId + "/programId/" + programId + "/tagId/" + tagId + "/themeId/" + themeId, true);
	xmlhttp.onreadystatechange=function correctResponse() {
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				document.getElementById("pageView"+pageViewName).innerHTML = xmlhttp.responseText;
			}
		}
	};			        		 
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var query_string = "topicId=" + encodeURIComponent(topicId) + "programId=" + encodeURIComponent(programId) + "tagId=" + encodeURIComponent(tagId) + "themeId=" + encodeURIComponent(themeId);
	xmlhttp.send(query_string);
}

function selectATheme(themeId){
	changeThemeToCurrent(themeId);
	var xmlhttp=false;
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}		  	
	xmlhttp.open("POST", "/getInfo/selectATheme/themeId/" + themeId, true);
	xmlhttp.onreadystatechange=function correctResponse() {
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				document.getElementById("pageViewProgram").innerHTML = xmlhttp.responseText;
			}
		}
	};			        		 
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var query_string = "themeId=" + encodeURIComponent(themeId);
	xmlhttp.send(query_string);
}

function getInfoViewAllTopics(){
	var xmlhttp=false;
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}		  	
	xmlhttp.open("POST", "/getInfo/selectAllTopics", true);
	xmlhttp.onreadystatechange=function correctResponse() {
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				document.getElementById("pageViewProgram").innerHTML = xmlhttp.responseText;
			}
		}
	};			        		 
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var query_string = "";
	xmlhttp.send(query_string);
}

function selectElementList(firstFolder, secoundFolder, valueName, valueId, page){	
	var xmlhttp=false;
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}		  	
	xmlhttp.open("POST", firstFolder+"/"+secoundFolder+"/"+valueName+"/"+ valueId+"/page/"+page, true);
	xmlhttp.onreadystatechange=function correctResponse() {
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				document.getElementById("mainWrapper").innerHTML = xmlhttp.responseText;
			}
		}
	};			        		
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	var query_string = valueName+"=" + encodeURIComponent(valueId);
	query_string += "page=" + encodeURIComponent(page);
	xmlhttp.send(query_string);
}


function changeTopicToCurrent(topicId){
	var arrayTopics = document.getElementById('topic_' + topicId).parentNode.childNodes;
	for ( var int = 0; int < arrayTopics.length; int++) {
		var childTopic = arrayTopics[int];
		if(childTopic.className != null){
			childTopic.className = "";
		}
	}
	document.getElementById('topic_' + topicId).className = "current";
}

function changeThemeToCurrent(themeId){
	var arrayThemes = document.getElementById('theme_' + themeId).parentNode.childNodes;
	for ( var int = 0; int < arrayThemes.length; int++) {
		var childTheme = arrayThemes[int];
		if(childTheme.className != null){
			childTheme.className = "";
		}
	}
	document.getElementById('theme_' + themeId).className = "current";
}

function addTagInAsset(){
	var tagCodTotal = document.getElementById("tagCod").value;
	var arrTagCod = tagCodTotal.split(",");
	for ( var i = 0; i < arrTagCod.length; i++) {
		var tagCod = arrTagCod[i];
		var tagsArray = document.getElementById("tagsArray").value;
		var arr = tagsArray.split(",");
		var thereIsEqual = false;
		for ( var int = 0; int < arr.length; int++) {
			if(tagCod == arr[int] || tagCod.replace(/^\s*|\s*$/g,"") == ""){
				thereIsEqual = true;
			}
		}
		if(!thereIsEqual){
			strVar = "'" + tagCod + "'"; 
			document.getElementById("arrayDeTags").innerHTML += '<a onclick="deleteTagInAsset('+ strVar +');"><img src="/images/deleteTag.png" width="10" height="10" alt="delete" />' + tagCod + '</a>     ';
			arr[arr.length] = tagCod;
			document.getElementById("tagsArray").value = arr;
		}	
	}
	document.getElementById("tagCod").value = "";
}

function deleteTagInAsset(tagName){
	var tagsArray = document.getElementById("tagsArray").value;
	var arr = tagsArray.split(",");
	var a = new Array();
	var i = 0;
	document.getElementById("arrayDeTags").innerHTML = "";
	for ( var int = 0; int < arr.length; int++) {
		if(tagName != arr[int]){
			a[i] = arr[int];
			strVar = "'" + arr[int] + "'";
			document.getElementById("arrayDeTags").innerHTML += '<a onclick="deleteTagInAsset('+ strVar +');"><img src="/images/deleteTag.png" width="10" height="10" alt="delete" />' + arr[int] +'</a>     ';
			i++;
		}
	}
	document.getElementById("tagsArray").value = a;
}

function changeIdElementValue(elementId, textValue){
	document.getElementById(elementId).value = textValue;
}

function editNameDescriptionAndActive(id, name, description, isActive, type, order){
	document.getElementById("order_" + id).innerHTML = '<input id="newOrder_'+id+'" name="newOrder_'+id+'" style="border:1px solid black; width:24px;" maxlength="3" type="text" value="' + order + '"></input>';
	document.getElementById("name_" + id).innerHTML = '<input id="newName_'+id+'" name="newName_'+id+'" style="border:1px solid black;" type="text" value="' + name + '"></input>';
	document.getElementById("description_" + id).innerHTML = '<textarea id="newDescription_'+id+'" style="border:1px solid black; width:100%; height:80px;" >'+description+'</textarea>';
	//document.getElementById("is_active_" + id).innerHTML = ' <input id="newIsActive_'+id+'" type="checkbox"' + (isActive?'checked="checked"':'') +'></input>';
	str = "'" + type + "'";
	document.getElementById("button_" + id).innerHTML = '<li onclick="updateNameDescriptionAndActive('+id + ',' + str +')"><a class="pointjs">Update</a></li>'+
		'<li onclick="cancelEditNameDescriptionAndActive('+ id + ",'" + name + "','" + description + "'," + isActive + ',' + str + ',' + order + ')"><a class="pointjs">Cancel</a></li>';
}

function cancelEditNameDescriptionAndActive(id, name, description, isActive, type, order){
	document.getElementById("order_" + id).innerHTML = order;
	document.getElementById("name_" + id).innerHTML = name;
	document.getElementById("description_" + id).innerHTML = description;
	//document.getElementById("is_active_" + id).innerHTML = (isActive?'Yes':'No'); 
	str = "'" + type + "'";
	document.getElementById("button_" + id).innerHTML = '<li onclick="editNameDescriptionAndActive('+ id + ",'" + name + "','" + description + "'," + isActive + ',' + str + ',' + order + ')"><a class="pointjs">Edit</a></li>'+
		'<li class="red"><a href="' + type + '/delete?id=' + id + '">Delete</a></li>';	    
}

function updateNameDescriptionAndActive(id, type){
	var name = document.getElementById("newName_" + id).value;
	if(name.replace(/^\s*|\s*$/g,"") == ""){
		alert("Name could not be empty.");
	}else{
		if(name.length > 255){
			alert("Name could not be more than 255 chars.");
		}else{
			var description = document.getElementById("newDescription_" + id).value;
			if(description.replace(/^\s*|\s*$/g,"") == ""){
				alert("Description could not be empty.");
			}else{
				var order = document.getElementById("newOrder_" + id).value;
				if(!isInteger(order)){
					alert("Order Number not valid.")
				}else{
					var active = true;//document.getElementById("newIsActive_" + id).checked;
					window.location = type + "/update" + type + "?id=" + id + "&name=" + name + "&description=" + description + "&active=" + active + "&order=" + order;
				}
			}
		}
	}
}

function isInteger(s) {
	return (s.toString().search(/^-?[0-9]+$/) == 0);
}

// only work in firefox
function refreshTheNewTheme(id){	
	window.location = "topic?themeId=" + id;
}
// to safari and ie
function refreshTheNewThemeBySelect(it){
	window.location = "topic?themeId=" + it.value;
}

function refreshTheNewThemeTicker(id){	
	window.location = "ticker?displayId=" + id;
}
function refreshTheNewThemeByTicker(it){
	window.location = "ticker?displayId=" + it.value;
}

function editNameDescriptionActiveAndPopular(id, name, description, isActive, isPopular){
	document.getElementById("name_" + id).innerHTML = '<input id="newName_'+id+'" name="newName_'+id+'" style="border:1px solid black;" type="text" value="' + name + '"></input>';
	document.getElementById("description_" + id).innerHTML = '<textarea id="newDescription_'+id+'" style="border:1px solid black; width:100%; height:80px;" >'+description+'</textarea>';
	//document.getElementById("is_active_" + id).innerHTML = ' <input id="newIsActive_'+id+'" type="checkbox"' + (isActive?'checked="checked"':'') +'></input>';
	document.getElementById("is_popular_" + id).innerHTML = ' <input id="newIsPopular_'+id+'" type="checkbox"' + (isPopular?'checked="checked"':'') +'></input>';

	
	description = description.replace(/'/g,"\\'");
	description = description.replace(/"/g,"\\'\\'");		
	name = name.replace(/'/g,"\\'");
	name = name.replace(/"/g,"\\'\\'");
	
	document.getElementById("button_" + id).innerHTML = '<li onclick="updateNameDescriptionActiveAndPopular('+id+')"><a class="pointjs">Update</a></li>'+
		'<li onclick="cancelEditNameDescriptionActiveAndPopular('+ id + ",'" + name + "','" + description + "'," + isActive +','+ isPopular +')"><a class="pointjs">Cancel</a></li>';
}

function cancelEditNameDescriptionActiveAndPopular(id, name, description, isActive, isPopular){
	document.getElementById("name_" + id).innerHTML = name;
	document.getElementById("description_" + id).innerHTML = description;
	//document.getElementById("is_active_" + id).innerHTML = (isActive?'Yes':'No');
	document.getElementById("is_popular_" + id).innerHTML = (isPopular?'<input type="checkbox" disabled="disabled" checked="checked"/>':'<input type="checkbox" disabled="disabled"/>');
	
	description = description.replace(/'/g,"\\'");
	description = description.replace(/"/g,"\\'\\'");		
	name = name.replace(/'/g,"\\'");
	name = name.replace(/"/g,"\\'\\'");
	
	document.getElementById("button_" + id).innerHTML = '<li onclick="editNameDescriptionActiveAndPopular('+ id + ",'" + name + "','" + description + "'," + isActive +','+ isPopular +')"><a class="pointjs">Edit</a></li>'+
		'<li class="red"><a href="topic/delete?id=' + id + '">Delete</a></li>';	    
}

function updateNameDescriptionActiveAndPopular(id){
	var name = document.getElementById("newName_" + id).value;
	if(name.replace(/^\s*|\s*$/g,"") == ""){
		alert("Name could not be empty.");
	}else{
		if(name.length > 255){
			alert("Name could not be more than 255 chars.");
		}else{
			var description = document.getElementById("newDescription_" + id).value;
			if(description.replace(/^\s*|\s*$/g,"") == ""){
				alert("Description could not be empty.");
			}else{
				var active = true;//document.getElementById("newIsActive_" + id).checked;
				var popular = document.getElementById("newIsPopular_" + id).checked;
				window.location = "topic/updateTopic?id=" + id + "&name=" + name + "&description=" + description + "&active=" + active + "&popular=" + popular;
			}
		}
	}
}

function changeActionInForm(url){
	document.forms["myform"].action = url;
	document.forms["myform"].submit();
}

function changeActionInFormByName(formName, url){
	document.forms[formName].action = url;
	document.forms[formName].submit();
}

function deleteValueDescription(obj){
	if(obj.value == 'Description'){
		obj.value = "";
	}
	return true;
}

function deleteValueName(obj){
	if(obj.value == 'Name'){
		obj.value = "";
	}
	return true;
}

function deleteValueTag(obj){
	if(obj.value == 'Add new tag'){
		obj.value = "";
		obj.style.color = "black";
	}
	return true;
}

function deleteValueObject(obj, text){
	if(obj.value == text){
		obj.value = "";
		obj.style.color = "black";
	}
	return true;
}

function deleteValueObjectSearch(obj, text){
	if(obj.value == text){
		obj.value = "";
	}
	return true;
}

function putTextWhenValueObjectSearch(obj, text){
	if(obj.value == "" || obj.value == null){
		obj.value = text;
	}
	return true;
}

function validateSingleFile(){
	var fileValue = document.getElementById("file1").value;
	if(fileValue == null || fileValue.trim() == ""){
		document.getElementById("errorInFile").innerHTML = '<tr id="errorInFile"><td colspan="3" style="color: red;">Empty File Name</td></tr>';
	}else{
		var arrStrFile = fileValue.split(".");
		var typeFile = arrStrFile[arrStrFile.length-1].toUpperCase(); 
		if(typeFile == 'PDF' || typeFile == 'DOC' || typeFile == 'DOCX' || typeFile == 'XLS' || typeFile == 'XLSX' || typeFile == 'PPT'
			|| typeFile == 'PPTX' || typeFile == 'JPG' || typeFile == 'GIF' || typeFile == 'PNG' || typeFile == 'TXT'){			
			document.forms["myform"].submit();
		}else{
			document.getElementById("errorInFile").innerHTML = '<tr id="errorInFile"><td colspan="3" style="color: red;">'+typeFile+' files are not allowed </td></tr>';
		}
	}
}

function changeConnect(id,count){   
	var arrayConnects = document.getElementById('allbtsconnect').childNodes;
	//no current to all buttons
	for ( var int = 0; int < arrayConnects.length; int++) {
		var childBt = arrayConnects[int];		
		if(childBt.className != null){
			childBt.className = "";
		}
	}
	//not display anything
	for ( var int = 0; int < count; int++) {
		var childInfo = document.getElementById('infoConnect_' + int); 
		childInfo.style.display = 'none';
	}	
	document.getElementById('resource_1').className = "";	
	document.getElementById('resource_2').className = "";	
	document.getElementById('infoResource_1').style.display = 'none';
	document.getElementById('infoResource_2').style.display = 'none';
	
	//set current and set the display
	if (id >= 0){
	  //dynamic divs from the left column
	  document.getElementById('connect_' + id).className = "current";	
	  document.getElementById('infoConnect_' + id).style.display = '';
	  document.getElementById('barLeft').style.width = "205px";
	  document.getElementById('barCenter').style.width = "386px";
	  document.getElementById('centerParagraph').style.display = "block";
	  $('#barLeft').css("border-top-color", "#93252D");
	  $('#barRight').css("border-top-color", "#AA9F97");
	} else {
	 //static divs for right column "Resources"
	  document.getElementById('resource_' + (-1)*id).className = "currentRightCol";	
	  document.getElementById('infoResource_' + (-1)*id).style.display = '';
	  document.getElementById('barLeft').style.width = "184px";
	  document.getElementById('barCenter').style.width = "404px";
	  document.getElementById('centerParagraph').style.display = "none";
	  $('#barLeft').css("border-top-color", "#AA9F97");
	  $('#barRight').css("border-top-color", "#93252D");
	}			
}

function displayDiv(obj){
	var elemImage = document.getElementById("divImage");
	var elemText = document.getElementById("divText");
	var elemPreview = document.getElementById("divPreview");
	
	if(obj.value == 0){
		elemImage.style.display = 'none';
		elemText.style.display = '';
		elemPreview.style.display = '';
	}else{
		elemText.style.display = 'none';
		elemPreview.style.display = 'none';
		elemImage.style.display = '';
	}
}

function changeMonthsInYear(year){
	var aMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var strDateMonth = '<select id="donate_exp_date_month" name="donate[exp_date_month]">';
	if(year == new Date().getFullYear()){
		for ( var int = 0; int < aMonth.length; int++) {
			iVal = int+1;
			if(new Date().getMonth() <= int){
				if(iVal.length > 1){
					iVal = '0'+iVal;
				}
				iVal = iVal+'';
				strDateMonth += '<option value="' + iVal + '">' + aMonth[int] + '</option>';
			}
		}
	}else{
		for ( var int = 0; int < aMonth.length; int++) {
			iVal = int+1;
			if(iVal.length > 1){
				iVal = '0'+iVal;
			}
			iVal = iVal+'';
			strDateMonth += '<option value="' + iVal + '">' + aMonth[int] + '</option>';
		}
	}
	strDateMonth += '</select>'; 
	document.getElementById("dateMonth").innerHTML = strDateMonth;
}

function printDiv() {
  frames["iframeTest"].focus();
  frames["iframeTest"].print();
}
