////////////// Display the preview page
function openPreviewWindow(url) {
	window.open(url,"previewWindow","width=1000,height=800,scrollbars=1");
}

function validateFormWithPasswords(aform) {
	var errorMsg = '';
	
	password1 = aform.password.value;
	password2 = aform.passwordVerify.value;
	
	if(password1 != password2) {
		errorMsg += 'Your passwords do not match' + '\n';
	}
	
	if(password1.length < 6) {
		errorMsg += 'Your password is too short' + '\n';
	}
	
	if(errorMsg.length > 0) {
		errorMsg = 'There was a problem with your passwords:\n'+errorMsg;
		alert(errorMsg);
		return false;
	} else {
		return validateForm(aform);
	}
}

//Validate emial
function isEmail (s) {
	var isEmail_re       = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
	return String(s).search (isEmail_re) != -1;
}

function validateRadioButton(aform,name,message) {
	myOption = -1;

	var numRadioButtons =  eval("aform."+name+".length");
		
	for (i=0;i<numRadioButtons;++i) {
		if (eval("aform."+name+"["+i+"].checked")) {
			return "";
		}
	}
	
	return message + '\n';
}

//Validate Form
function validateForm(aform) {
	var user_level;
	var el = aform.elements;
	var errorMsg = '';
	var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
	var specialCaseProductErr = 'Select at least 1 product' + '\n';
	var specialCaseProductFlag = false;
	var specialCaseIndustryErr = 'Select at least 1 industry' + '\n';
	var specialCaseIndustryFlag = false;
	var specialCaseCategoryErr = 'Select at least 1 category' + '\n';
	var specialCaseCategoryFlag = false;
	var specialCaseErr = 'Select at least 1 product or industry' + '\n';
	
	var productFlag = true;
	var industryFlag = true;
	var categoryFlag = true;
	
	for(var i = 0 ; i < el.length ; ++i) {
		var googleToolbar = el[i].title;
		
		if(googleToolbar.substr(0,11) == "Your Google") {continue;}
		
		if(el[i].name == 'email') {
			if(isEmail(el[i].value) == false) {
				errorMsg += 'Invalid Email' + '\n';
			}
		} else if(el[i].value && (el[i].name == 'image' || el[i].name == 'thumbnail')) {
			if(el[i].value.lastIndexOf(".jpg")==-1) {
				errorMsg += el[i].name + " - Please upload only .jpg extention file" + '\n';
			}
		} else if(el[i].type == 'checkbox') {
			if((el[i].checked == false && el[i].title.length > 0) && ((el[i].title != 'Product') && (el[i].title != 'Industry')  && (el[i].title != 'Category'))) {
				errorMsg += el[i].title + '\n';
			} else if(el[i].checked == true && (el[i].title == 'Product')) { //Special Cases
				specialCaseProductFlag = false;
				specialCaseProductErr = '';
			} else if(el[i].checked == true && (el[i].title == 'Industry')) { //Special Cases	
				specialCaseIndustryFlag = false;
				specialCaseIndustryErr = '';
			} else if(el[i].checked == true && (el[i].title == 'Category')) { //Special Cases	
				specialCaseCategoryFlag = false;
				specialCaseCategoryErr = '';
			} else if (el[i].title == 'Product') {
				specialCaseProductFlag = true;
			} else if (el[i].title == 'Industry') {
				specialCaseIndustryFlag = true;
			} else if (el[i].title == 'Category') {
				specialCaseCategoryFlag = true;
			}
		} else if (el[i].type == 'select-one') {
			if(el[i].selectedIndex < 1 && el[i].title.length > 0) {
				errorMsg += el[i].title + '\n';
			}
		} else if (el[i].type == 'radio') {
			if(el[i].title == "Product" && productFlag == true) {
				productFlag = false;
				errorMsg += validateRadioButton(aform,'itemProducts','Product');
			} else if (el[i].title == "Industry" && industryFlag == true) {
				industryFlag = false;
				errorMsg += validateRadioButton(aform,'itemIndustries','Industry');
			} else if (el[i].title == "Category" && categoryFlag == true) {
				categoryFlag = false;
				errorMsg += validateRadioButton(aform,'itemCategory','Category');
			}
		} else if (el[i].name == 'content') { //FCK Editor
			var FCKString = FCKeditorAPI.GetInstance('content').GetXHTML();
			if(FCKString.length == 0) { 
				errorMsg += el[i].title + '\n';
			}
		} else if (el[i].title.length > 0) {
			if(el[i].value.length == 0) {
				errorMsg += el[i].title + '\n';
			}
		}
	}
	
	if((specialCaseProductFlag && specialCaseProductErr.length > 0) && (specialCaseIndustryFlag && specialCaseIndustryErr.length > 0)) {
		errorMsg += specialCaseErr;
	}
	
	if((specialCaseProductFlag && specialCaseProductErr.length > 0) && (!specialCaseIndustryFlag)) {
		errorMsg += specialCaseProductErr;
	}
	
	if((specialCaseIndustryFlag && specialCaseIndustryErr.length > 0) && (!specialCaseProductFlag)) {
		errorMsg += specialCaseIndustryErr;
	}
	
	if(specialCaseCategoryFlag && specialCaseCategoryErr.length > 0) {
		errorMsg += specialCaseCategoryErr;
	}
	/*
	if(s) {
		errorMsg += specialCaseIndustryErr;
	}*/
	
	if(errorMsg.length > 0) {
		errorMsg = 'Please complete all required fields below:\n'+errorMsg;
		alert(errorMsg);
		return false;
	} else {
		return true;
	}
	
	return false;
}

//global navigation menus//
sfHover = function() 
{ 
	if (document.getElementById("globalNav1"))
	{
		var sfEls = document.getElementById("globalNav1").getElementsByTagName("li"); 
		for (var i=0; i<sfEls.length; i++) 
		{ 
			sfEls[i].onmouseover=function() 
			{ 
				this.className+=" sfhover"; 
			} 
			sfEls[i].onmouseout=function() 
			{ 
				this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); 
			} 
		} 
	}
} 
if (window.attachEvent) window.attachEvent("onload", sfHover);

/////////////////////////////////////// Filter Functions //////////////////////////////////////////////////////

function switchFilterDisplay(value) {
	switchProductDisplay(value);
	switchIndustryDisplay(value);
}

function switchProductDisplay(value) {
	if(value == "itemProducts") {
		setStyle('itemProductDiv','display','');
	} else {
		document.forms[1].filter_itemProduct.selectedIndex = 0;
		setStyle('itemProductDiv','display','none');
	}
}

function switchIndustryDisplay(value) {
	if(value == "itemIndustry") {
		setStyle('itemIndustryDiv','display','');
	} else {
		document.forms[1].filter_itemIndustry.selectedIndex = 0;
		setStyle('itemIndustryDiv','display','none');
	}
}


/////////////////////////////////////// Email a Colleague /////////////////////////////////////////////////////

var visibleInfoRows = 1;

function displayNextRow() {
	visibleInfoRows++;
	
	setStyle('emailColleague_'+visibleInfoRows,'display','');
}

///////////////////////////////////////////////////////////////////// IFrame Admin Function ////////////////////

//Set the current language
var language = '';

function setLanguage(lang) {
	language = lang;
}

//Update the admin iframe Source
function changeAdmIFrameSrc(url) {
	var iframe = document.getElementById('contentManagementFrame');
    
	iframe.src = 'edit-content.php?fileName='+url; 
	
	ajaxFunction('../include/getBreadCrumb.php?fileName='+url,'breadcrumb');
}

function openTreeChildren(elementId) {
	setStyle(elementId,'display','');
}

function closeTreeChildren(elementId) {
	setStyle(elementId,'display','none');
}

function swapDirectoryStatus(element,childElementId) {
	if(element.style.backgroundImage == "url(../graphx/tieminus.gif)") {
		element.style.backgroundImage = "url(../graphx/tieplus.gif)";
		closeTreeChildren(childElementId);
	} else {
		element.style.backgroundImage = "url(../graphx/tieminus.gif)";
		openTreeChildren(childElementId);
	}
	
}

//Update a div with new text
function updateDivWithText(divID,text) {
	document.getElementById(divID).innerHTML = text;
}

//Limit the character count
function limitCharCount(element,count) {
	if(element.value.length > count) {
		var string = element.value;
		element.value = string.substr(0,count);
		alert("Character limit reached!");
	}
}

function limitCharCountTextArea(element,count) {
	if(element.value.length > count) {
		var string = element.value;
		element.value = string.substr(0,count);
		alert("Character limit of "+count+" reached!");
	}
}

//Swith the view of a div inside the website-tools section of the admin
function displayNative() {
	setStyle('englishDocuments','display','none');
	setStyle('nativeDocuments','display','');
	
	document.forms[1].fileName.title='';
	document.forms[1].title.title='';
	document.forms[1].title_native.title='Title';
	//document.forms[1].fileName_native.title='Upload Document';
}

function displayEnglish() {
	setStyle('englishDocuments','display','');
	setStyle('nativeDocuments','display','none');
	
	document.forms[1].fileName.title='Select Document';
	document.forms[1].title.title='Title';
	document.forms[1].title_native.title='';
	document.forms[1].fileName_native.title='';
	
}

function setStyle(divId,style,value) {
	eval("document.getElementById('"+divId+"').style."+style+"='"+value+"'");
}

/////////////////////////////////////// Rollover for admin tree ////////////////

function setDivRollover(element,status) {
	if(status == 'over') {
		element.style.backgroundColor = '#569A11';
		//element.style.backgroundImage = '#569A11';
	} else {
		element.style.backgroundColor = '';
	}
}

//////////////////////////////////////// AJAX ///////////////////////////////////

function setSliderActive(tableName,id) {
	ajaxFunction('../include/setActive.php?tableName='+tableName+'&id='+id,'saveMsg');
}

//The base ajax function 
function ajaxFunction(url,divID) {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	//Add a random id to the end of the url
	url = url+"&sid="+Math.random();
	//alert(url);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	if(divID.length > 0) {
		displayAjaxResult(xmlHttp,0,divID);
	}
}

//Will try 100 times over every 10 miliseconds to look for a response, up to 1 sec
function displayAjaxResult(xmlHttp,count,divID) {
		var tempCount = count + 1;
		if(tempCount > 100) {
			//Do nothing
		} else if(xmlHttp.readyState!=4) {
			window.setTimeout(function () { displayAjaxResult(xmlHttp,tempCount,divID); }, 10);
		} else {
			document.getElementById(divID).innerHTML=xmlHttp.responseText;
		}
}

/////////////////////////////////////// Admin Style Functions ///////////////////////////////////

function changeLangTable(element) {
	var roleID = element.options[element.selectedIndex].value;
	
	//adminTable = document.getElementById('moderator_language');
	otherTable = document.getElementById('other_language');
	
	if(roleID == 1) {
		//adminTable.style.display = '';
		otherTable.style.display = 'none';
	} else {
		//adminTable.style.display = 'none';
		otherTable.style.display = '';
	
	}
}

//////////////////////////////////////// popup window fuction ///////////////////////////////////

var popUpWin=0;
function showPopup(URLStr, left, top, width, height) {
	if(popUpWin) {
		if(!popUpWin.closed) popUpWin.close();
	}
	
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	
}

function open_win(url_add) {
   window.open(url_add,'newWin','width=500,height=500,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes');
}

function open_privacy_win(url_add) {
   window.open(url_add,'newWin','width=1080,height=800,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes');
}
   
/////////////////////////////////////////// Modal Handling //////////////////////////////////////////

//Open A modal
function openModal(url) {
	//alert(url);
	//scroll(0,0);
	TB_show('',url,'false');
}

//Close a modal 
function closeModalLeaveOverlay() {
	TB_remove_Only_Modal();
}

function closeModal() {
	$("element").setStyle('height','900');
	TB_remove();
}

//Close the login and open the register box
function openRegisterBox(includePath) {
	closeModalLeaveOverlay();
	//setTimeout(function(){myFunction(parameter)},myTimeout);
	window.setTimeout(function(){openModal(includePath+'registration.php?height=570&width=770')},200);
		
}

///////////////////////////////////// Old Victrex Admin Functions //////////////////////////////////////

function expandcontent(cid){
	document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
}

function displayFiles(divId) {
	var element = document.getElementById(divId);
	
	if(element.style.display == 'none') {
		setStyle(divId,'display','');
	} else {
		setStyle(divId,'display','none');
	}
}

function checkAlphaNumeric(alphanum) {
	var val = alphanum.value
	if (/^\w+$/.test(val)) {
		return true;
	} else {	
		alert("The text entered for " + alphanum.name + ", '" + val + "' is not alphanumeric! Please re-enter.");
		//alphanum.select();
		return false;
	}
}
function checkNumeric(num) {
	var val = num.value
	if (/^\d+$/.test(val)) {
		return true;
	} else {	
		alert("The text entered '" + val + "' is not numeric! Please re-enter.");
		num.select();
		return false;
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////