//Load up the preview Page
function loadPreviewPage(elementList) {
	
	var previewWindow = window.open('../preview.php','previewWindow','location=1,status=1,scrollbars=1,width=640,height=400')

	document.forms[0].target="previewWindow";
	document.forms[0].elementList = elementList;
	document.forms[0].action = "../preview.php";
	
}

//Make sure the action and target of form is set to self
function submitForm() {
	document.forms[0].target="_self";
	document.forms[0].action = "";
}

//Make sure the action and target of form is set to self
function submitFormNoValid() {
	document.forms[0].target="_self";
	document.forms[0].action = "";
	document.forms[0].onsubmit = "";
}

//Limit the number of characters that can be added to an element
function limitChars(total,element) {
	var elementValue = element.value;
	if(elementValue.length > total) {
		element.value = elementValue.substr(0,total);
		alert('The character limit of '+total+' has been reached!');
	}
}

function requireState(element,formNum) {
	var country = element.value;
	var stateElement = document.forms[formNum].state;
	if((country == 'USA') || (country == 'Canada')) {
		stateElement.title = 'State';
	} else {
		stateElement.title = '';
	}
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   // alert("Invalid E-mail ID")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    //alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    //alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    //alert("Invalid E-mail ID")
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    //alert("Invalid E-mail ID")
	    return false
	 }

		 return true					
}

//Validate Form
function validateForm(aform) {
	var user_level;
	var el = aform.elements;
	var errorMsg = '';
	
	
	for(var i = 0 ; i < el.length ; ++i) {
		//alert(el[i].type);
		var googleToolbar = el[i].title;		
		if(googleToolbar.substr(0,11) == "Your Google") {continue;}
		
		if(el[i].type == 'checkbox') {
			if(el[i].checked == false && el[i].title.length > 0) {
				errorMsg += el[i].title + '\n';
			} 
		} else if (el[i].type == 'select-one') {
			if(el[i].selectedIndex == 0 && el[i].title.length > 0) {
				errorMsg += el[i].title + '\n';
			}
		} else if (el[i].name == 'calloutContent') { //FCK Editor
			var FCKString = FCKeditorAPI.GetInstance('calloutContent').GetXHTML();
			if(FCKString.length == 0) { 
				errorMsg += el[i].title + '\n';
			}
		} else {
			if(el[i].value.length == 0 && el[i].title.length > 0) {
				errorMsg += el[i].title + '\n';
			}
			if(el[i].name == 'email') {
				if(!echeck(el[i].value)) {
					errorMsg += 'Properly Formatted Email Address'+'\n';
				}
			}
		}
	}
	
	if(errorMsg.length > 0) {
		errorMsg = 'Please complete all required fields below:\n'+errorMsg;
		alert(errorMsg);
		return false;
	} else {
		return true;
	}
	
	return false;
}

//Reset from elemnets, used onClick on filters
function resetValues(list) {
	
	//split the list apart
	var splitValue = list.split(',');
	
	//loop through the array and set all values to null
	for(var i = 0 ; i < splitValue.length ; ++i) {
		eval("document.forms[0]."+splitValue[i]+".value=''");
	}
}

//Display the Modal
function displayModal(divId) {

	closeOtherModals(divId);
	
	window.scrollTo(0,0);
	document.getElementById('body').style.overflow = 'hidden';
	document.getElementById('greyBox').style.display = '';
	eval("document.getElementById('"+divId+"').style.display = ''");
}

//Close the Modal
function closeModal(divId) {
	document.getElementById('body').style.overflow = 'auto';
	document.getElementById('greyBox').style.display = "none";
	eval("document.getElementById('"+divId+"').style.display = 'none'");
}

//Close any other open modals
function closeOtherModals(divId) {
	if(divId == 'register') {
		document.getElementById('login').style.display = "none";
	}
}

//Update the style of a given modal
function updateStyle(divID,styleElement,styleValue) {
	eval("document.getElementById('"+divID+"').style."+styleElement+" = '"+styleValue+"'");
}

//Validate the email colleague form
function checkInvite(TheForm) {
    var errors = '';
	if(TheForm.first_name.value.length == 0) {
		errors = "- First Name\n";
    }
	if(TheForm.last_name.value.length == 0) {
		errors += "- Last Name\n";
    }
	if(TheForm.email.value.length == 0) {
		errors += "- Email\n";
    }
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(TheForm.email.value)) {
		
	} else {
		errors += "- A properly formatted email address\n";
	}
	if(TheForm.comments.value.length == 0) {
		errors += "- Comments\n";
    }
	if(TheForm.first_name1.value.length == 0) {
		errors += "- Colleague's First Name\n";
    }
	if(TheForm.last_name1.value.length == 0) {
		errors += "- Colleague's Last Name\n";
    }
	if(TheForm.email1.value.length == 0) {
		errors += "- Colleague's Email\n";
    }
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(TheForm.email1.value)) {
		
	} else {
		errors += "- A properly formatted colleague's email address\n";
	}
	if(errors != '') {
		alert('The form needs some corrections before it can be submitted.\n' + errors);
		return false;
	} 
return true;
}

function convertLoginToModal() {
	updateStyle('loginContainer','border','0px');
	updateStyle('loginContainer','margin','0px');
	updateStyle('loginContainer','backgroundColor','#ffffff');
}

//Changes teh status of a click on row item, by update two hidden input fields in form and submit form
function changeStatus(id,status) {
	var newStatus = '';
	
	//Change the status
	if(status == 'Active') {
		newStatus = 'Inactive';
	} else {
		newStatus = 'Active';
	}
	
	//Update the form elements
	document.forms[0].id.value = id;
	document.forms[0].status.value = newStatus;
	
	//Submit the form
	document.forms[0].submit();

}

function popupVideo(which) {
	window.open('../flash/video_player.php?vid='+which,'_blank','toolbar=no,location=no,menubar=no,width=435,height=300');
}

function displayOther(element) {
	var selObj = document.getElementById('lead_source');

	if (selObj.options[element.selectedIndex].text == 'Other') {
		updateStyle('lead_source_other_div','display','');
		document.getElementById('lead_source_other').title = 'Other';
	} else {
		updateStyle('lead_source_other_div','display','none');
		document.getElementById('lead_source_other').title = '';
	}
}