//------------------------------------------------------------------------------------------------
//	Show/hide state/province dropdowns based on country
//------------------------------------------------------------------------------------------------
function clearPulldown( id )
{
	obj = document.getElementById(id);
	
	if (obj.selectedIndex)
	{
		obj.selectedIndex = 0;
	}
}

//------------------------------------------------------------------------------------------------
//	Based upon user action, show or hide a required field star
//------------------------------------------------------------------------------------------------
function showstar( id )
{
	document.getElementById(id).style.display = 'inline';
}

function hidestar( id )
{
	document.getElementById(id).style.display = 'none';
}

//------------------------------------------------------------------------------------------------
//	Based upon user action, show or hide a particular part of the form
//------------------------------------------------------------------------------------------------
function showDiv( id )
{
	document.getElementById(id).style.display = 'block';
}
function hideDiv( id )
{
	document.getElementById(id).style.display = 'none';
}

//------------------------------------------------------------------------------------------------
//	Field Entry Validation
//------------------------------------------------------------------------------------------------
// onKeyUp - allow entry of numeric number only in the field
function validateNumericOnly(field)
{
    var valid		= "0123456789";
    var errors		= false;
    var temp		= "";
	var newString	= "";

    for (var i=0; i<field.value.length; i++)
    {
		temp = "" + field.value.substring(i, i+1);
		
		if (valid.indexOf(temp) == "-1")
		{
			errors = true;
		}

		else
		{
			newString += temp;
		}
	}

	if (errors == true)
	{
		field.value = newString;
		return;
	}
}

// onKeyUp - allow entry of numeric number and dashes only in a field
//         - used for sponsored child number fields and phone number fields
function validateNumericDash(field)
{
    var valid		= "0123456789-";
    var errors		= false;
    var temp		= "";
	var newString	= "";

    for (var i=0; i<field.value.length; i++)
    {
		temp = "" + field.value.substring(i, i+1);
		
		if (valid.indexOf(temp) == "-1")
		{
			errors = true;
		}

		else
		{
			newString += temp;
		}
	}

	if (errors == true)
	{
		field.value = newString;
		return;
	}
}

// onKeyUp - allow entry of numeric number and slashes / only in a field - used for date fields
function validateNumericSlash(field)
{
    var valid		= "0123456789/";
    var errors		= false;
    var temp		= "";
	var newString	= "";

    for (var i=0; i<field.value.length; i++)
    {
		temp = "" + field.value.substring(i, i+1);
		
		if (valid.indexOf(temp) == "-1")
		{
			errors = true;
		}

		else
		{
			newString += temp;
		}
	}

	if (errors == true)
	{
		field.value = newString;
		return;
	}
}

// onKeyUp - validate the number of characters allowed in a textarea
function ismaxlength(obj) 
{
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

//------------------------------------------------------------------------------------------------
//	If the phone field is 10 digits, reformat it
//------------------------------------------------------------------------------------------------
function formatPhone(field) 
{ 
	var digits = field.value.replace(/[^0-9]/ig, ''); 

	if (!digits) 
	{ 
		return; 
	} 

	if (digits.length == 10) 
	{ 
		field.value = '(' + digits.substring(0, 3) + ') ' +  
		digits.substring(3, 6) + '-' +  
		digits.substring(6, 10); 
	} 
	
	else 
	{ 
		field.value = digits; 
	} 
}

//------------------------------------------------------------------------------------------------
//	If source is current sponsor then a sponsor number must be entered on form
//------------------------------------------------------------------------------------------------
function validateSrc(src,sponsor)
{
	if (src.value == "cs" && sponsor.value.length == 0)
	{
		alert("You seleted current donor/sponsor but did not enter your sponsor number.");
		sponsor.focus();
		sponsor.select();
	}  
}

//------------------------------------------------------------------------------------------------
//	validate the Write to child and Direct Comm form fields after the user hits the submit button
//------------------------------------------------------------------------------------------------
function validateChildLetters(details)
{
	var msg = "";

	// if the form is the direct communications form
	if (details["fields[Form]"].value == "WriteDC")
	{
		control = details["fields[Field Office]"];
		dropdown = control.options;
		dropdown_value	= dropdown[dropdown.selectedIndex].value;
		if ( dropdown_value == "" )
		{
			msg = msg + "Field Office is required.\n";
		}	

		control = details["fields[FO Number]"];
		if ( !control.value )
		{
			msg = msg + "";
		}
	}

	control = details["fields[SC Number]"];
	if ( !control.value )
	{
		msg = msg + "Sponsored Childs Number is required.\n";
		control.focus();
	}

	control = details["fields[SC Name]"];
	if ( !control.value )
	{
		msg = msg + "Sponsored Childs Name is required.\n";
		control.focus();
	}

	control = details["fields[Sponsor Number]"];
	if ( !control.value )
	{
		msg = msg + "Sponsor Number is required.\n";
		control.focus();
	}

	control = details["fields[Sponsor Name]"];
	if ( !control.value )
	{
		msg = msg + "Sponsor Name is required.\n";
		control.focus();
	}

	control = details["fields[Email]"];
	if ( !control.value )
	{
		msg = msg + "Email is required.\n";
		control.focus();
	}
	if ( (control.value) && !IsEmail( control.value ) )
	{
		msg = msg + "Email format is invalid.\n";
		control.focus();
	}

	// if the form is the write by answering the questions form
	if (details["fields[Form]"].value == "WriteQs")
	{
		if ( !details["fields[question1]"].value && !details["fields[question2]"].value && !details["fields[question3]"].value )
		{
			msg = msg + "Please fill in at least one response to the questions.\n";
			control.focus();
		}
	}
	else {
		control = details["fields[Message]"];
		if ( !control.value )
		{
			msg = msg + "Message is required.";
			control.focus();
		}
	}

	if ( msg )
	{
		alert(msg);
		return false;
	}
	else return true;
}

//------------------------------------------------------------------------------------------------
//	validate the eCard form fields after the user hits the Preview eCard button
//------------------------------------------------------------------------------------------------
function validateeCards(details)
{
	var msg = "";

	//	If the Ecard ID field is a radio button
	if ($('input.ecard-radio:radio').length)
	{
		val = $('input.ecard-radio:checked').val();

		if (!val) {
			msg = msg + "Please choose an E-Card\n";
		};
		
	};
	
	control = details["fields[Your Name]"];
	if ( !control.value )
	{
		msg = msg + "Your name is required.\n";
		control.focus();
	}

	control = details["fields[Your Email]"];
	if ( !control.value )
	{
		msg = msg + "Your Email is required.\n";
		control.focus();
	}
	else if(!IsEmail( control.value ))
	{
		msg = msg + "Your Email format is invalid.\n";
		control.focus();
		
	}

	control = details["fields[Recipient Name]"];
	if ( !control.value )
	{
		msg = msg + "Recipient Name is required.\n";
		control.focus();
	}

	control = details["fields[Recipient Email]"];
	if ( !control.value )
	{
		msg = msg + "Recipient email is required.\n";
		control.focus();
	}
	emails = control.value.split(',');
	if (emails) {
		for(var i in emails)
		{
			value = jQuery.trim( emails[i] );
		    if(!IsEmail(value)) {
				msg = msg + "Recipient email '"+emails[i]+"' is invalid.\n";
				control.focus();
			}
		}
	}

	control = details["fields[Subject Line]"];
	if ( !control.value )
	{
		msg = msg + "Subject line is required.\n";
		control.focus();
	}

	control = details["fields[Message]"];
	if ( !control.value )
	{
		msg = msg + "Message is required.\n";
		control.focus();
	}

	if ( msg )
	{
		alert(msg);
		return false;
	}
	else return true;
}

//------------------------------------------------------------------------------------------------
//	Used in the Child selection tool on SAC forms
//------------------------------------------------------------------------------------------------
function toggleLongest()
{
	longestObj = document.getElementById( 'longestcheckbox' );
	
	if (!document.getElementById( 'area' ).value && !document.getElementById( 'age' ).value && !document.getElementById( 'gender' ).value)
	{
		longestObj.checked = true;
	}

	else
	{
		longestObj.checked = false;
	}
}

function toggleOther()
{
	document.getElementById( 'area' ).value = '';
	document.getElementById( 'age' ).value = '';
	document.getElementById( 'gender' ).value = '';
	
}

function testCookies()
{
	make_cookie( 'validatecookies', true );
	
	validate_value = find_cookie('validatecookies');
	
	if ( !validate_value || "" + validate_value == 'undefined' )
	{
		document.getElementById('no_cookies_msg').style.display="block";
	}
	else
	{
		document.getElementById('child-picker').style.display="block";
	}
}

function toggleLongestOLD()
{
	checkarea	= 0;
	checkage	= 0;
	checkgender = 0;
	
	longestObj		= document.getElementById( 'longestcheckbox' );
	other_selection = false;

	if (document.getElementById( 'area' ) != null)
	{
		checkarea = document.getElementById( 'area' ).selectedIndex;
	}

	if (document.getElementById( 'age' ) != null)
	{
		checkage = document.getElementById( 'age' ).selectedIndex;
	}
	
	if (document.getElementById( 'gender' ) != null)
	{
		checkgender = document.getElementById( 'gender' ).selectedIndex;
	}
	
	if (checkgender || checkage || checkarea)
	{
		longestObj.checked = false;
	}

	else
	{
		longestObj.checked = true;
	}
}

function toggleOtherOLD()
{
	if (document.getElementById( 'area' ))
	{
		document.getElementById( 'area' ).selectedIndex = 0;
	}

	if (document.getElementById( 'age' ))
	{
		document.getElementById( 'age' ).selectedIndex = 0;
	}
	
	if (document.getElementById( 'gender' ))
	{
		document.getElementById( 'gender' ).selectedIndex = 0;
	}
}

//------------------------------------------------------------------------------------------------
//	validate the Mobile number on the sign up forms
//------------------------------------------------------------------------------------------------
function validateMobile(details)
{
	var msg = "";

	control = details["fields[Mobile Number]"];
	if ( !control.value )
	{
		msg = msg + "Mobile Number is required";
	}
	
	if ( msg ) {
		alert(msg);
		control.focus();
		return false;
	}
	else {
		window.open('/give/mobilesignup.php', '','width=500,height=180'); 
		return true;
	}
}

// Change the matchkey value on the SAC forms based on the how_did_you_hear_about_us dropdown
function MKChange() 
{
	var dropdownValue = document.getElementById('howdidyouhearaboutus').value;
	var mk = document.getElementById('matchkey'); 
	
	if  (dropdownValue == "tv") {
       mk.value = "2012D0045";
	}
	else {
       mk.value = "2012Y0007";
    }
}

