////////////// Display the preview page
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';
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


//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 interestErr = 'Select at least 1 Area of Interest' + '\n';
	var interestFlag = false;
	
	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].type == 'checkbox') {
			if(el[i].checked == false && el[i].title.length > 0) {
				errorMsg += el[i].title + '\n';
			} else if(el[i].checked == true && (el[i].id == 'interestCheckbox')) { //Erase the error msg
				interestErr = '';
			} else if(el[i].checked == false && (el[i].id == 'interestCheckbox')) { //if interest occurs, mark it
				interestFlag = 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].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) {
			el[i].value = el[i].value.trim();
			if(el[i].value.length == 0) {
				errorMsg += el[i].title + '\n';
			}
		}
	}
	
	if(interestFlag && interestErr) {
		errorMsg += interestErr + '\n';
	}
	
	if(errorMsg.length > 0) {
		errorMsg = 'Please complete all required fields below:\n'+errorMsg;
		alert(errorMsg);
		return false;
	} else {
		return true;
	}
	
	return false;
}


////////////// Registration Box ////////////////////////////////////////////////

var isRegDisplayed = false;

function switchRegDivDisplay() {
	elementLogin = document.getElementById('loginDiv');
	elementReg = document.getElementById('registrationDiv');
	
	if(isRegDisplayed) {
		elementLogin.style.display = '';
		elementReg.style.display = 'none';
		isRegDisplayed = false;
		
	} else {
		elementLogin.style.display = 'none';
		elementReg.style.display = '';
		isRegDisplayed = true;
	
	}
}

var isContactDisplayed = false;

function switchContactDivDisplay() {
	elementContact = document.getElementById('contactDiv');
	elementRegLogin = document.getElementById('registerLoginDiv');
	
	if(isContactDisplayed) {
		elementContact.style.display = '';
		elementRegLogin.style.display = 'none';
		isContactDisplayed = false;
		
	} else {
		elementContact.style.display = 'none';
		elementRegLogin.style.display = '';
		isContactDisplayed = true;
	
	}
}


//////////////////////////////////////// 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,false);
	xmlHttp.send(null);
	
	if(divID.length > 0) {
		document.getElementById(divID).innerHTML=xmlHttp.responseText;
	}
}
   
////////////////// Admin Functions /////////////////////////////

function deletePublication(id,rowID) {
	if(confirm('Are you sure you want to delete this publication?')) {
		ajaxFunction('deletePublication.php?id='+id,'');
		eval("document.getElementById('publication_"+rowID+"').style.display = 'none'");
	}
}

var numOfNewAccomplishmentFormElements = 0;

function addAccomplishmentInput(counter) {
	numOfNewAccomplishmentFormElements++;
	eval("document.getElementById('accomplishment_"+(numOfNewAccomplishmentFormElements+counter)+"').style.display = ''");
	eval("document.forms[0].accomplishment_"+(numOfNewAccomplishmentFormElements+counter)+".focus()");
	
}

var numOfNewEducationFormElements = 0;

function addEducationInput(counter) {
	numOfNewEducationFormElements++;
	eval("document.getElementById('education_"+(numOfNewEducationFormElements+counter)+"').style.display = ''");
	eval("document.forms[0].education_"+(numOfNewEducationFormElements+counter)+".focus()");
}

var numOfNewPubFormElements = 0;

function addPubInput(counter) {
	numOfNewPubFormElements++;
	eval("document.getElementById('publication_"+(numOfNewPubFormElements+counter)+"').style.display = ''");
}

var numOfNewAuthorElements = 0

function addAuthorInput(counter) {
	numOfNewAuthorElements++;
	eval("document.getElementById('whitePaper_"+(numOfNewAuthorElements+counter)+"').style.display = ''");
	eval("document.forms[0].firstName_"+(numOfNewAuthorElements+counter)+".focus()");
}

//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!");
	}
}

var currentMode = 'existing';

function switchContactDisplay() {
	existingElement = document.getElementById('existingContact');
	newElement = document.getElementById('newContact');
	
	if(currentMode == 'existing') {
		existingElement.style.display = 'none';
		newElement.style.display = '';
		
		currentMode = 'new';
	} else {
		existingElement.style.display = '';
		newElement.style.display = 'none';
		
		currentMode = 'existing';
	
	}
}

//////////////////////////////// Ineed Drop downs //////////////////////////////

function relocateToINeed(phpFile) {
	if(phpFile.length > 0) {
		window.open(phpFile,'_top');
	}
}

///////////////////////////////// Small contact form ////////////////////////////
serverPath = '';
function setServerPath(path) {
	serverPath = path;
}

function submitFormFunction() {
	var formInputs = new Array("leadDNAStart","firstName","lastName","email","howHear","comments","contactID","referPage");

	var errorMsg = '';
	var submittedValues = '';
	
	for(var i=0;i<formInputs.length;i++) {
		var inputValue = eval("document.smallContactForm."+formInputs[i]+".value");
		var inputTitle = eval("document.smallContactForm."+formInputs[i]+".title");
		
		////////////////////////////////////////////////////
		if(inputValue == undefined && inputTitle.length > 0) {
			errorMsg += inputTitle+'\n';
		} else if(inputValue.length == 0 && inputTitle.length > 0 && inputTitle != "comments") {
			errorMsg += inputTitle+'\n';
		} else {
			submittedValues += formInputs[i] + "=" + inputValue + "&";
		}
	}
	
	if(errorMsg.length > 0) {
		errorMsg = 'Please enter a value for the following fields:' + '\n' + errorMsg;
		alert(errorMsg);
	} else {
		var ajaxResult = ajaxFunction(serverPath+'contact/submitSmallContactForm.php?'+submittedValues,'displaySmallContactForm');	

		$(function() {
			$('#displaySmallContactForm').hide();
			$('#openContactForm').hide();
			$('#displaySmallContactFormConfirmation').fadeIn(300);
			
		});
}
	return false;

}

