function textChange(bbb,ccc) {
	$(bbb).style.color = ccc;
}

/* trims leading and tailing whitespace from a string */
function trim(str) {
  var newstr = str;
  var newstr = newstr.replace(/^\s+|\s+$/g, '');  // remove leading and trailing spaces
  return newstr.replace(/^(\&nbsp\;)+|(\&nbsp\;)+$/g, ''); // remove leading and trailing nbsp;
}

/* creates a popup window for things like privacy policy, etc and defaults to 600 x 500 */
function popup(mylink, windowname) {
    return popupWithSize(mylink, windowname, 600, 500);
}

/* creates a popup window for things like privacy policy, etc */
function popupWithSize(mylink, windowname, width, height) {
    if (! window.focus) {
        return true;
    }
    var href;
    if (typeof(mylink) == 'string') {
       href=mylink;
    }
    else {
       href=mylink.href;
    }
    window.open(href, windowname, 'width=' + width + ',height=' + height + ',scrollbars=yes');
    return false;
}



function getfileextension() { 
 var fileinput = $("new_file"); 
 if(!fileinput ) return ""; 
 var filename = fileinput.value; 
 if( filename.length == 0 ) return ""; 
 var dot = filename.lastIndexOf("."); 
 if( dot == -1 ) return ""; 
 var extension = filename.substr(dot,filename.length); 
 return extension; 
}

function checkfileType(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) { 
 var ext = getfileextension(); 
 if (ext == a || ext == b || ext == c || ext == d || ext == e || ext == f || ext == g || ext == h || ext == i || ext == j || ext == k || ext == l || ext == m || ext == n || ext == o || ext == p || ext == q || ext == r || ext == s || ext == t) {
 	$("submit").style.display = "";
 	if($("file_ext_message").style.display != "none") {
		Effect.BlindUp("file_ext_message");
	}
	if($("image_ext_message").style.display != "none") {
		Effect.BlindUp("image_ext_message");
	}
 } else {
 	$("submit").style.display = "none";
 	Effect.BlindDown("file_ext_message");
 }
}

function checkimageType(a,b,c,d,e,f,g,h) { 
 var ext = getfileextension();
 if (ext == a || ext == b || ext == c || ext == d || ext == e || ext == f || ext == g || ext == h) {
	$("submit").style.display = "";
 	if($("file_ext_message").style.display != "none") {
		Effect.BlindUp("file_ext_message");
	}
	if($("image_ext_message").style.display != "none") {
		Effect.BlindUp("image_ext_message");
	}
 } else {
 	$("submit").style.display = "none";
 	Effect.BlindDown("image_ext_message");
 }
} 


function HideContent(d) {
	if(d.length < 1) {
		return;
	}
	document.getElementById(d).style.display = "none";
	document.getElementById('bgpop').style.display = "none";
    
    return false;
}

function HideFlagContent(d) {
	if(d.length < 1) {
		return;
	}
	document.getElementById(d).style.display = "none";
	document.getElementById('flagbgpop').style.display = "none";
}

function ShowContent(d) {
	if(d.length < 1) { 
		return;
	}
	document.getElementById(d).style.display = "";
	//document.getElementById('bgpop').style.display = "";
}

function ReverseContentDisplay(d) {
	if(d.length < 1) { 
		return;
	}
	if(document.getElementById(d).style.display == "none") { 
		document.getElementById(d).style.display = "";
		document.getElementById('bgpop').style.display = "";
	} else {
		document.getElementById(d).style.display = "none";
		document.getElementById('bgpop').style.display = "none";
	}
    
    return false;
}

function ReverseContentFlagDisplay(d) {
	if(d.length < 1) { 
		return;
	}
	if(document.getElementById(d).style.display == "none") { 
		document.getElementById(d).style.display = "";
		document.getElementById('flagbgpop').style.display = "";
	} else {
		document.getElementById(d).style.display = "none";
		document.getElementById('flagbgpop').style.display = "none";
	}
    
    return false;
}

/**
 * This function determines if a person is of age, meaning they are a person who can create
 * an account for themselves.  Currently this is defined as the age of 18 as per Cochlear.
 * @param {Object} month
 * @param {Object} day
 * @param {Object} year
 */
function isOfAge(month, day, year) {
    var result = false;
    
    var currDate = new Date();
    
    var yearsAgo = currDate.getFullYear() - 18;
    var otherDate = new Date();
    otherDate.setMonth(currDate.getMonth());
    otherDate.setDate(currDate.getDate());
    otherDate.setFullYear(yearsAgo);
    
    var bday = new Date(month + " " +
                        day + ", " +
                        year);
        
    if(otherDate.getTime() < bday.getTime())  { // this is a child
        result = false;
    } 
    else { // adult
        result = true;
    }
    
    return result;
}

/**
 * This function determines if a child is under thirteen, since children under thirteen
 * will have to have their parents download and fax in a permission form.
 * @param {Object} month
 * @param {Object} day
 * @param {Object} year
 */
function isTeenOrOlder(month, day, year) {
    var result = false;
    
    var currDate = new Date();
    
    var yearsAgo = currDate.getFullYear() - 13;
    var otherDate = new Date();
    otherDate.setMonth(currDate.getMonth());
    otherDate.setDate(currDate.getDate());
    otherDate.setFullYear(yearsAgo);
    
    var bday = new Date(month + " " +
                        day + ", " +
                        year);
        
    if(otherDate.getTime() < bday.getTime())  { // this is a child under thirteen
        result = false;
    } 
    else { // adult
        result = true;
    }
    
    return result;
}

/**
 * Checks the recipient's date of birth. If the recipient is less than 18 years old,
 * return false.
 * 
 * @return false if the recipient is less than 18 years old; true otherwise -- even 
 * if the date is not fully entered yet.
 */
function checkRecipientDOB(){
    return true;
}

function checkRecipientDOBold() {
    hideStuff = false; // hides the error messages
    
    // membertype 3 means "A child"
    if(($('membertype').value != -3) && 
        $('membertype').value != 3 &&
        $('other_birthdate_month').value != "" && 
        $('other_birthdate_day').value != "" && 
        $('other_birthdate_year').value != "") {
        
        var adult = isOfAge($('other_birthdate_month').value, $('other_birthdate_day').value, $('other_birthdate_year').value);
        
        if(!adult) {
            $('whohas').style.color = "red";
            $('whohas').style.fontWeight = "bold";
            if($('recipDiv').style.display == "none") {
                $('recipDiv').style.display = "";
                Effect.BlindDown('recipDiv');
            }
        }
        else {
            hideStuff = true;
        }
    }
    
    if(hideStuff || $('membertype').value == -3/*"A child"*/ || $('membertype').value == 3/*"A child"*/ || $('membertype').value == 0/*"I do"*/) {
        $('whohas').style.color = "black";
        $('whohas').style.fontWeight = "normal";
        if($('recipDiv').style.display != "none") {
            $('recipDiv').style.display = "none";
            Effect.BlindUp('recipDiv');
        }
        
        if($('membertype').value == 0/*"I do"*/) { // blank out the recip birthday
            $('other_birthdate_month').value = "";
            $('other_birthdate_day').value = "";
            $('other_birthdate_year').value = "";
        }
    }
    
    return true;
}

/**
 * This is called during step 2 of registration and during editing of a profile. 
 * When a user selects "A child" as an option for member type, 
 * the Device Information and Gender are not supposed to be shown; as a result,
 * this function checks and hides or displays these fields based on what is selected
 * for membertype. Additionally, if a person selects "I do", the candidate DOB will
 * hide, since candidate dob and member dob should be the same (DOB = date of birth).
 * Profile does not have a gender div, so that is why the boolean is passed -- to 
 * avoid javascript errors.
 */
function memberType(profile) {
    type = $('membertype').value;
    if(type == "A child" || type == -3 || type == 3) {
        $('deviceInfo').style.display = "none";
        Effect.BlindUp('deviceInfo');
        if(!profile) {
            $('gender').style.display = "none";
            Effect.BlindUp('gender');
        }
        
        /*if($('dobRecipient').style.display == "none") {
            $('dobRecipient').style.display = "";
            Effect.BlindDown('dobRecipient');
        }*/
        
        if($('parentQuestion').style.display == "none") {
            $('parentQuestion').style.display = "";
            Effect.BlindDown('parentQuestion');    
        }

        // about and cause of loss show for every case except 'A Child'        
        if($('aboutLoss').style.display != "none") {
            Effect.BlindUp('aboutLoss');
            $('aboutLoss').style.display = "none";
        }
        if($('causeLoss').style.display != "none") {
            Effect.BlindUp('causeLoss');
            $('causeLoss').style.display = "none";
        }
        
        Effect.BlindDown('aChild');
        $('aChild').style.display = "";
    }
    else if(type == "I do" || type == 0) {
        if($('deviceInfo').style.display == "none") {
            Effect.BlindDown('deviceInfo');
            $('deviceInfo').style.display = "";
        }
        if(!profile) {
            if($('gender').style.display == "none") {
                Effect.BlindDown('gender');
                $('gender').style.display = "";
            }
        }

        // date of birth does not show for individual
        /*if($('dobRecipient').style.display != "none") {
            $('dobRecipient').style.display = "none";
            Effect.BlindUp('dobRecipient');
        }*/
        
        // hide parent question
        if($('parentQuestion').style.display != "none") {
            $('parentQuestion').style.display = "none";
            Effect.BlindUp('parentQuestion');    
        }
        
        // about and cause of loss show for every case except 'A Child'
        if($('aboutLoss').style.display == "none") {
            Effect.BlindDown('aboutLoss');
            $('aboutLoss').style.display = "";
        }
        if($('causeLoss').style.display == "none") {
            Effect.BlindDown('causeLoss');
            $('causeLoss').style.display = "";
        }
        
        if($('aChild').style.displayl != "none") {
            $('aChild').style.display = "none";
            Effect.BlindUp('aChild');
        }
    }
    else {
        if($('deviceInfo').style.display == "none") {
            Effect.BlindDown('deviceInfo');
            $('deviceInfo').style.display = "";
        }
        
        if(!profile) {
            if($('gender').style.display == "none") {
                Effect.BlindDown('gender');
                $('gender').style.display = "";
            }
        }
        
        /*if (type == '') { // it says Please Select... for membertype
            // date of birth does not show if they haven't selected a membertype
            if($('dobRecipient').style.display != "none") {
                $('dobRecipient').style.display = "none";
                Effect.BlindUp('dobRecipient');
            }
        }
        else {
            // show DOB of recipient
            if ($('dobRecipient').style.display == "none") {
                $('dobRecipient').style.display = "";
                Effect.BlindDown('dobRecipient');
            }
        }*/
        
        // hide parent question
        if($('parentQuestion').style.display != "none") {
            $('parentQuestion').style.display = "none";
            Effect.BlindUp('parentQuestion');    
        }

        // about and cause of loss show for every case except 'A Child'
        if($('aboutLoss').style.display == "none") {
            Effect.BlindDown('aboutLoss');
            $('aboutLoss').style.display = "";
        }
        if($('causeLoss').style.display == "none") {
            Effect.BlindDown('causeLoss');
            $('causeLoss').style.display = "";
        }
        
        // don't show aChild div        
        if($('aChild').style.display != "none") {
            $('aChild').style.display = "none";
            Effect.BlindUp('aChild');
        }
    }
}

function changeText(id,ddd) {
	$(id).innerHTML = ddd;
}

function showLateral(ddd,eee,fff) {
	if($('deviceStepsLateral').style.display == "none") {
		Effect.BlindDown('deviceStepsLateral');
	}
	
	changeText('device1',ddd);
	changeText('device2',ddd);
	changeText('device3',ddd);
	
	changeText('type1',eee);
	changeText('type2',fff);
}

function showUnilateral(ddd,eee) {
	if($('deviceStepsUnilateral').style.display == "none") {
		Effect.BlindDown('deviceStepsUnilateral');
	}
	if($('deviceStepsBilateral').style.display != "none") {
		Effect.BlindUp('deviceStepsBilateral');
	}
	changeText('side1',ddd);
	changeText('side2',eee);
}

function showBilateral(ddd,eee) {
	if($('deviceStepsUnilateral').style.display == "none") {
		Effect.BlindDown('deviceStepsUnilateral');
	}
	if($('deviceStepsBilateral').style.display == "none") {
		Effect.BlindDown('deviceStepsBilateral');
	}
	changeText('side1',ddd);
	changeText('side2',eee);
}

function hideDeviceSteps(ddd) {
	if(ddd == "all") {
		if($('deviceStepsLateral').style.display != "none") {
			Effect.BlindUp('deviceStepsLateral');
		}
		if($('deviceStepsUnilateral').style.display != "none") {
			Effect.BlindUp('deviceStepsUnilateral');
		}
		if($('deviceStepsBilateral').style.display != "none") {
			Effect.BlindUp('deviceStepsBilateral');
		}
		
	} else {
		if($('deviceSteps' + ddd).style.display != "none") {
			Effect.BlindUp('deviceSteps' + ddd);
		}
	}
	
}

function uploadIconCheck() {
	var alerts = '';
	
	if($("iconfile").value == '') {
		alerts = "You must specify a file to upload!";
	} else if($("icondescription").value == '') {
		alerts = "You must provide a tagline for the new image!";
	} else if(!$("agree").checked) {
		alerts = "You must agree to the uploading terms!";
	}
	
	if(alerts != '') {
		alert(alerts);
		return false;
	} else {
		return true;
	}
}

function checkPasswords() {
	if(document.profile.password.value != document.profile.cpassword.value) {
		document.profile.cpassword.value = '';
		document.profile.password.value = '';
		alert('Your passwords do not match, please re-enter them.');
		return false;
	} else {
		return true;
	}
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
function setFooter() {
	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			var contentHeight = document.getElementById('container').offsetHeight;
			var footerElement = document.getElementById('footer');
			var footerHeight  = footerElement.offsetHeight;
			if (windowHeight - (contentHeight + footerHeight) >= 0) {
				footerElement.style.position = 'relative';
				footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
			}
			else {
				footerElement.style.position = 'static';
			}
		}
	}
}
window.onload = function() {
	setFooter();
}
window.onresize = function() {
	setFooter();
}

/** 
 * This function is used to validate the first registration form.
 * 
 * @return true if form is valid; false otherwise
 */
function validateRegFormOne() {
    var password = trim($('password').value);
    var cpassword = trim($('cpassword').value);
	var email = trim($('email').value);
	var email2 = trim($('email2').value);
	
    if(password != cpassword) {
        alert('Your passwords do not match. Please re-enter them.');
        return false;
    }
	
	if(email != email2) {
        alert('Your emails do not match. Please re-enter them.');
        return false;
    }
    
    if(password == null || password == '') {
        alert('Your passwords are empty');
        return false;
    }
    
    var adult = isOfAge($('birthdate_month').value, $('birthdate_day').value, $('birthdate_year').value);
    if( !adult ) {
        alert('The birthdate you have entered indicates you are under the age of 18. If this is true, please have your parents sign up and you can participate in the community with their permission.');
        return false;
    }
    
    if(!$('agree').checked) {
        alert('You must agree to the terms and conditions');
        return false;
    }

    return true;

}

/** 
 * This function is used to validate the second registration form.
 * 
 * @return true if form is valid; false otherwise
 */
function validateRegFormTwo() {
    if($('privacy').checked) {
        return true;
    }
    else {
        alert('You must agree to the Notice of Privacy Practices and Disclosure of Personal Health Infomation Notice to continue.');
        return false;
    }
}

/** 
 * This validates the information in the compose message area (template messages_compose.tpl).
 * 
 * @return true to send the message; false if there are errors where the message should not be sent.
 */
function validateComposeMessage() {
    if($('new_msg_to').value == "") {
        alert('Please select a person to whom the message should be sent.');
        return false;
    }

    if($('new_msg_body').value == "") {
        return confirm('Do you want to send this message with an empty message body?');
    }
    
    return true;
}

function validateComposeMessage2() {
    if($('new_msg_to').value == "" && $('new_msg_to2').value == "") {
        alert('Please select a person to whom the message should be sent.');
        return false;
    }

    if($('new_msg_body').value == "") {
        return confirm('Do you want to send this message with an empty message body?');
    }
    
    return true;
}