
/** Make sure IFRAME opens within parent, else redirect to parent  **/
function checkForParent(strURLParent) {
	if (strURLParent == "") {
		strURLParent == "/";
	}
	if (parent.location == self.location){
		window.location = strURLParent;
	}
}


function popupFull(URL,name,w,h,m,r,s,t){
	/** KEY **/
	/*
	URL = url of doc
	name = name of window
	w = width
	h = height
	m = menubar (0 or 1)
	r = resizable (0 or 1)
	t = toolbar (0 or 1)
	s = scrollbars (0 or 1)
	
	tag = popupFull('URL','name','w','h','m','r','s','t')
	*/
	window.open(URL,name,"width=" + w + ",height=" + h + ",channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=" + m+ ",resizable=" + r + ",scrollbars=" + s + ",status=0,toolbar=" + t + "");
}

function printWindow() {
	if (navigator.userAgent.indexOf('Mac')>=0) {
		alert("Print function does not appear to be supported by your browser. To print this page press Command-P.");
	} else {
		window.print();
	}
}

function validateDOB(mm,dd,yyyy,minAge,formName){
	if (!checkValidDate(mm,dd,yyyy)){
		return false;
	}/*else{
		return true;
	}*/
	validateSubmitForm(formName);
}

function yearsOld(strDOB){ // format mm/dd/yyyy
	birthday = new Date(strDOB);
    today = new Date();
    years = today.getFullYear() - birthday.getFullYear();
    birthday.setYear( today.getFullYear() );
    // If your birthday hasn't occurred yet this year, subtract 1
    if(today < birthday){
       years-- ;
    }
	return years;
}

function checkValidDate(mm,dd,yyyy) {
	var isNum = new RegExp("[0-9]")
	var daysinmonth = 31;
	var isfeb = false;
	var isleapyear = false;
	if ( mm == "2" || mm == "02" ) {
		isfeb=true;
	}
	if ( yyyy % 4 == 0 ) {
		isleapyear=true;
	}
	if ( (mm == "4") || (mm == "04") || (mm == "6") || (mm == "06") || (mm == "9") || (mm == "09") || (mm == "11") ) {
		daysinmonth=30;
	}
	if (isfeb && isleapyear) {
		daysinmonth=29;
	}
	if (isfeb && !isleapyear) {
		daysinmonth=28;
	}
	if ( (dd > daysinmonth) || (mm > 12) || (yyyy < 1000) || (dd == "") || (mm == "") || (yyyy == "") || (!isNum.test(Number(dd))) || (!isNum.test(Number(mm))) || (!isNum.test(Number(yyyy))) ) {
		alert("The date you have entered is invalid. Please try again.");
		document.getElementById('mm').focus();
		return false;
	}else{
		return true;
	}
}

function validateRegistration(formName){
	if (trim(document.forms[formName].username.value) == ''){
		alert('You must enter a username');
		document.forms[formName].username.focus();
		return false;
	}
	if (!isAlphaNumeric(trim(document.forms[formName].username.value))){
		alert('Your username must be alphanumeric.');
		document.forms[formName].username.focus();
		return false;
	}
	if (trim(document.forms[formName].password.value) == ''){
		alert('You must enter a password.');
		document.forms[formName].password.focus();
		return false;
	}
	if (!isAlphaNumeric(trim(document.forms[formName].password.value))){
		alert('Your password must be alphanumeric.');
		document.forms[formName].password.focus();
		return false;
	}
	if (!compareString(document.forms[formName].password.value,document.getElementById('cnfpassword').value)){
		alert('Your passwords do not match.');
		document.getElementById('cnfpassword').focus();
		return false;
	}
	if (trim(document.getElementById('team_name').value) == ''){
		alert('You must enter a Team or Stand name.');
		document.getElementById('team_name').focus();
		return false;
	}
	if (trim(document.getElementById('fname').value) == ''){
		alert('You must enter your first name.');
		document.getElementById('fname').focus();
		return false;
	}
	if (trim(document.getElementById('lname').value) == ''){
		alert('You must enter your last name.');
		document.getElementById('lname').focus();
		return false;
	}
	if (trim(document.getElementById('address1').value) == ''){
		alert('You must enter an address.');
		document.getElementById('address1').focus();
		return false;
	}
	if (trim(document.getElementById('city').value) == ''){
		alert('You must enter a city.');
		document.getElementById('city').focus();
		return false;
	}
	if (!validateState(document.getElementById('state').value)){
		alert('You must enter a valid state.');
		document.getElementById('state').focus();
		return false;
	}
	if (!isZip(trim(document.getElementById('zip').value))){
		alert('You must enter a valid zip code.');
		document.getElementById('zip').focus();
		return false;
	}
	if (!isEmail(trim(document.getElementById('email').value))){
		alert('You must enter a valid email.');
		document.getElementById('email').focus();
		return false;
	}
	if (!compareString(document.getElementById('email').value,document.getElementById('cnfemail').value)){
		alert('Your emails do not match.');
		document.getElementById('cnfemail').focus();
		return false;
	}
	if (trim(document.getElementById('camera_phone').value) != ''){
		if (!isPhone(trim(document.getElementById('camera_phone').value),10)){
			alert('The camera phone number entered is not a valid phone number.');
			document.getElementById('camera_phone').focus();
			return false;
		}
	}
	if (trim(document.getElementById('phone').value) != ''){
		if (!isPhone(trim(document.getElementById('phone').value),10)){
			alert('The phone number entered is not a valid phone number.');
			document.getElementById('phone').focus();
			return false;
		}
	}
	if (trim(document.getElementById('description').value) == ''){
		alert('You must enter a Stand description.');
		document.getElementById('description').focus();
		return false;
	}
	if (!CheckSize(document.getElementById('description'),500)){
		alert('Your Stand description cannot be longer than 500 characters.');
		document.getElementById('description').focus();
		return false;
	}
	validateSubmitForm(formName);
}


function validateEvent(formName){
	if (trim(document.getElementById('event').value) == ''){
		alert('You must enter an event.');
		document.getElementById('event').focus();
		return false;
	}
	var month = document.getElementById('mm').value;
	var day = document.getElementById('dd').value;
	var year = document.getElementById('yyyy').value;
	if (!checkValidDate(month,day,year)){
		return false;
	}
	if (document.getElementById('start_hour').value=='' || document.getElementById('start_min').value=='' || document.getElementById('start_meridian').value==''){
		alert('You must enter a valid start time.');
		document.getElementById('start_hour').focus();
		return false;
	}
	if (document.getElementById('end_hour').value=='' || document.getElementById('end_min').value=='' || document.getElementById('end_meridian').value==''){
		alert('You must enter a valid end time.');
		document.getElementById('end_hour').focus();
		return false;
	}
	if (trim(document.getElementById('city').value) == ''){
		alert('You must enter a city.');
		document.getElementById('city').focus();
		return false;
	}
	if (trim(document.getElementById('state').value) == ''){
		alert('You must select a state.');
		document.getElementById('state').focus();
		return false;
	}
	if (trim(document.getElementById('location').value) == ''){
		alert('You must enter a location.');
		document.getElementById('location').focus();
		return false;
	}
	if (trim(document.getElementById('description').value) == ''){
		alert('You must enter a description.');
		document.getElementById('description').focus();
		return false;
	}
	if (!CheckSize(document.getElementById('description'),125)){
		alert('Your description cannot be longer than 125 characters.');
		document.getElementById('description').focus();
		return false;
	}
	validateSubmitForm(formName);
}

function validateReport(formName){
	var month = document.getElementById('mm').value;
	var day = document.getElementById('dd').value;
	var year = document.getElementById('yyyy').value;
	if (!checkValidDate(month,day,year)){
		return false;
	}
	if (trim(document.getElementById('description').value) == ''){
		alert('You must enter a description.');
		document.getElementById('description').focus();
		return false;
	}
	if (!CheckSize(document.getElementById('description'),500)){
		alert('Your report cannot be longer than 500 characters.');
		document.getElementById('description').focus();
		return false;
	}
	validateSubmitForm(formName);

}

function validateUpload(formName){
	if (trim(document.getElementById('photo').value) == ''){
		alert('You didn\'t choose a file for upload.');
		document.getElementById('photo').focus();
		return false;
	}
	if (!document.getElementById('disclaimerPhotoAgree').checked){
		alert('You must accept the terms of the Photo Disclaimer to proceed.');
		return false;
	}
	validateSubmitForm(formName);
}

function validateMMS(formObj){
	atLeastOneChecked = false;
	for (x=0;x<formObj.elements.length;x++){
		if (formObj.elements[x].type == 'checkbox'){
			if (formObj.elements[x].checked){
				atLeastOneChecked = true;
			}
		}
	}
	if (!atLeastOneChecked) {
		alert('You didn\'t check any photos.');
		return false;
	}
	if (!document.getElementById('disclaimerMMSAgree').checked){
		alert('You must accept the terms of the MMS Photo Disclaimer to proceed.');
		return false;
	}
	formObj.submit();
}

function validateLogin(username,password,formName){
	if (trim(document.forms[formName].username.value) == ''){
		alert('You must enter a username.');
		document.forms[formName].username.focus();
		return false;
	}
	if (trim(document.forms[formName].password.value) == ''){
		alert('You must enter a password.');
		document.getElementById(password).focus();
		return false;
	}
	validateSubmitForm(formName);
}

function validateTellAFriend(formName){
	if (trim(document.getElementById('strToAddress').value) == ''){
		alert('You forgot your friend\'s email address.');
		document.getElementById('strToAddress').focus();
		return false;
	}
	if (trim(document.getElementById('strFromAddress').value) == ''){
		alert('You forgot your email address.');
		document.getElementById('strFromAddress').focus();
		return false;
	}
	validateSubmitForm(formName);
}

function validatePhotoMeta(formName){
	if (!CheckSize(document.getElementById('photoDescription'),120)){
		alert('Your description cannot be longer than 120 characters.');
		document.getElementById('photoDescription').focus();
		return false;
	}
	validateSubmitForm(formName);
}

function validatePhotoMetaMulti(formName){
	var intPhotos = document.getElementById('photoCount').value;
	for (x=1;x<=intPhotos;x++){
		if (!CheckSize(document.getElementById('photoDescription'+'_' + x),120)){
			alert('Your description cannot be longer than 120 characters.');
			document.getElementById('photoDescription'+'_' + x).focus();
			return false;
		}
	}
	validateSubmitForm(formName);
}


function validateSubmitForm(formName){
	document.forms[formName].submit();
}

// checks email format
function isEmail(str) {
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
	if (!r1.test(str) && r2.test(str)) {
		return true;
	}else{
		return false;
	}
}

// remove leading and trailing spaces
function trim(str){
	var objRegExpLeadSpaces = /^\s*/;
	var objRegExpTrailSpaces = /\s*$/;
	str = str.replace(objRegExpLeadSpaces,"");
	str = str.replace(objRegExpTrailSpaces,"");
	return str;
}

function isAlphaNumeric(str){
	var regex=/(^[a-zA-Z_0-9]+$)/;
	if(regex.test(str)){
		return true;
	} else {
		return false;
	}
}

function compareString(str1,str2){
	if (str1 === str2){
		return true;
	}else{
		return false;
	}
}

function isZip(zip){
	var regZip = new RegExp("(^\\d{5}$)|(^\\d{5}-\\d{4}$)");
	var regAllZeros = new RegExp("(^[0]{5}$)|(^[0]{5}-[0]{4}$)");
	if (regZip.test(zip) && !regAllZeros.test(zip)){
		return true;
	}else{
		return false;
	}
}

function validateState (strValue ) {
	var objRegExp = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NC|ND|NE|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i; 

  return objRegExp.test(strValue);
}

function isPhone(phNo,howManyDigits){
	var regPhoneStr = "^[0-9]{" + howManyDigits + "}$";
	var regPhoneStr2 = "^[0-9]{3}[-|\.][0-9]{3}[-|\.][0-9]{4}$";
	var regPhone = new RegExp(regPhoneStr);
	var regPhone2 = new RegExp(regPhoneStr2);
	phNo = stripCharsInBag(phNo,' ');
	if (regPhone.test(phNo) || regPhone2.test(phNo)){
		return true;
	}else{
		return false;
	}
}

function CheckSize(objElem, intMaxChars) {
	if  (isNaN(intMaxChars)) {
		intMaxChars = 0;      
	}
	if  (objElem.value.length > intMaxChars) {
		//alert("This field can only hold " + intMaxChars + " characters");
		objElem.value = objElem.value.substring(0, intMaxChars);
		return false;
	} else {
		return true;
	}
}

// remove characters supplied in bag
function stripCharsInBag (s, bag){
	var i;
	var returnString = "";
    for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function changeStyle(id,bgcolor) {
	document.getElementById(id).style.backgroundColor = bgcolor;
}

function changeBackground(id, img) {
	document.getElementById(id).style.backgroundImage = img;
}