/**
 * Rock, Paper, Scissors llc.
 *
 * scripts.js
 *
 * General javascripts needed for each project
 *
 * LICENSE: This source file is subject to version 1.0 of the 
 * Rock, Paper, Scissors llc. license that is available through 
 * the world-wide-web at the following URI:
 * http://www.123shoot.com/license/1_0.txt.  If you did not receive 
 * a copy of the Rock, Paper, Scissors llc. License and are unable 
 * to obtain it through the web, please send a note to 
 * support@123shoot.com so we can mail you a copy immediately.
 *
 * @category   js
 * @author     Scott Lively <scott@123shoot.com>
 * @copyright  2009 Rock, Paper, Scissors llc.
 * @license    http://www.123shoot.com/license/1_0.txt  RPS License 1.0
 */


/**
 * function dayfill
 *
 * fills the day id dropdown select box with the correct number of days
 * depending on which month the user selected from the month dropdown select box.
 */
function dayfill(monthid, dayid, yearid)
{
    var month = document.getElementById(monthid).value;
    var day = document.getElementById(dayid).value;
    var year = document.getElementById(yearid).value;
    if (month == 2) {
        if(year%4 == 0) {
            var days = 29;
        } else {
            var days = 28;
        }
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        var days = 30;
    } else {
        var days = 31;
    }
    
    document.getElementById(dayid).options.length=1;
    
    if(day > days) {
        day = days;
    }
    
    var i=1;
    for (i=1;i<=days;i++) {
        if(i == day) {
            document.getElementById(dayid).options[i-1] = new Option(i,i, true, true);							
        } else {
            document.getElementById(dayid).options[i-1] = new Option(i,i);							
        }
    }
}

/**
 * function fillMailing
 *
 * copies permanent address to mailing address on application form
 */
function fillMailing(f) 
{
	if(f.copyperm.checked == true) {
	    f.addressMail.value = f.addressPerm.value;
	    f.cityMail.value = f.cityPerm.value;
	    f.stateMail.value = f.statePerm.value;
	    f.zipMail.value = f.zipPerm.value;
    }
}

/**
 * function fillBusinessPg1
 *
 * copies permanent address to business address on application form
 */
function fillBusinessPg1(f) 
{
	if(f.copypermPg1.checked == true) {
	    f.pg1AddressBusiness.value = f.pg1AddressPerm.value;
	    f.pg1CityBusiness.value = f.pg1CityPerm.value;
	    f.pg1StateBusiness.value = f.pg1StatePerm.value;
	    f.pg1ZipBusiness.value = f.pg1ZipPerm.value;
    }
}

/**
 * function fillBusinessPg2
 *
 * copies permanent address to business address on application form
 */
function fillBusinessPg2(f) 
{
	if(f.copypermPg2.checked == true) {
	    f.pg2AddressBusiness.value = f.pg2AddressPerm.value;
	    f.pg2CityBusiness.value = f.pg2CityPerm.value;
	    f.pg2StateBusiness.value = f.pg2StatePerm.value;
	    f.pg2ZipBusiness.value = f.pg2ZipPerm.value;
    }
}

/**
 * function wordCount
 *
 * counts the number of words the user enters
 */
function wordCount(valfield, infofield) 
{
    var fullStr = valfield.value + " ";
    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
    var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
    var splitString = cleanedStr.split(" ");
    var word_count = splitString.length -1;
    
    var r = 0;
    for (z=0; z<splitString.length; z++) {
        if (splitString[z].length > 1) {
            r++;
        }
    }
    infofield.value=r;
}

/**
 * function toggleShow
 *
 * this function shows a child container based onmouseover of parent container
 *
 */
function toggleShow(child)
{
    document.getElementById(child).style.display = '';
}

/**
 * function toggleHide
 *
 * this function hides a child container based onmouseover of parent container
 *
 */
function toggleHide(child)
{
    document.getElementById(child).style.display = 'none';
}

/**
 * function toggleSelectShow
 *
 * this function shows a child container based on a parent (select box) container's selection
 *
 */
function toggleSelectShow(parent, child, myselection) 
{
	var selectCheck = document.getElementById(parent).value;
	if(selectCheck == myselection) {
		document.getElementById(child).style.display = '';
	} else {
		document.getElementById(child).style.display = 'none';
	}
}

/**
 * function toggleSelectHide
 *
 * this function hides a child container based on a parent (select box) container's selection
 *
 */
function toggleSelectHide(parent, child, myselection) 
{
	var selectCheck = document.getElementById(parent).value;
	if(selectCheck == myselection) {
		document.getElementById(child).style.display = 'none';
	} else {
		document.getElementById(child).style.display = '';
	}
}

/**
 * function toggleCheckbox
 *
 * toggles the view on a hiddenField, depending on whether the user checked the checkField or not
 */
function toggleCheckbox (checkField, displayField)
{
    var myCheckbox = document.getElementById(checkField);
    
    if (myCheckbox.checked == true) {
        document.getElementById(displayField).style.display = '';
    } else {
        document.getElementById(displayField).style.display = 'none';
    }
}

/**
 * function toggleRelation
 *
 * toggles the view on the Recommendation Form,
 * depending on whether the user selected Supervisor or Teacher
 */
function toggleRelation() 
{
	var relationCheck = document.getElementById('recStudentRelation').value;
	if(relationCheck == "Supervisor") {
		document.getElementById("supervisorContainer").style.display = '';
		document.getElementById("teacherContainer").style.display = 'none';
	} else {
		document.getElementById("supervisorContainer").style.display = 'none';
		document.getElementById("teacherContainer").style.display = '';
	}
}

/**
 * function toggleHouseholdIncome
 *
 * toggles the view on the StepUp form,
 * depending on whether the user selected Under $49,000 for Household Income
 */
function toggleHouseholdIncome() 
{
	var incomeCheck = document.getElementById('pgHouseholdIncome').value;
	if((incomeCheck == "Less than $9,999")||(incomeCheck == "$10,000 - $14,999")||(incomeCheck == "$15,000 - $24,999")||(incomeCheck == "$25,000 - $34,999")||(incomeCheck == "$35,000 - $49,999")) {
		document.getElementById("under_fifty_k").style.display = '';
		document.getElementById("over_fifty_k").style.display = 'none';
	} else {
		document.getElementById("under_fifty_k").style.display = 'none';
		document.getElementById("over_fifty_k").style.display = '';
	}
}

/**
 * function toggleFreshmanCheckbox
 *
 * toggles the view on the Student Application form,
 * depending on whether the user selected current grade of 8 or not
 */
function toggleFreshmanCheckbox() 
{
	var eightCheck = document.getElementById('currentGrade').value;
	if(eightCheck == "8") {
		document.getElementById("checkboxContainerLt50").style.display = '';
		document.getElementById("checkboxContainerGt50").style.display = '';
	} else {
		document.getElementById("checkboxContainerLt50").style.display = 'none';
		document.getElementById("checkboxContainerGt50").style.display = 'none';
	}
}
