// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function toggle_subsection_salary_and_skills()
{
	Element.toggle('subsection_salary_and_skills_disclosure_opened');
	Element.toggle('subsection_salary_and_skills_disclosure_closed');
	Effect.toggle('subsection_salary_and_skills', 'blind');		
}

function toggle_subsection_current_position()
{
	Element.toggle('subsection_current_position_disclosure_opened');
	Element.toggle('subsection_current_position_disclosure_closed');
	Effect.toggle('subsection_current_position', 'blind');		
}

function toggle_subsection(type)
{	
	var element_name = 'openstate[' + type + ']';
	$(element_name).value = 1 - $(element_name).value

	Element.toggle('subsection_' + type + '_disclosure_opened');
	Element.toggle('subsection_' + type + '_disclosure_closed');
	Effect.toggle('subsection_' + type, 'blind');		
}

function toggle_text(type)
{	
	Element.toggle('text_' + type + '_disclosure_opened');
	Element.toggle('text_' + type + '_disclosure_closed');
	Element.toggle('text_' + type + '_div_opened');		
	Element.toggle('text_' + type + '_div_closed');		
}

function toggle_all_texts_open(type)
{
	var divs = document.getElementsByTagName("div"); // loop through all div elements 
	// alert(divs.length)
	for (var i=0; i<divs.length; i++){
		var div = divs[i]; 
		if (div != null && div.getAttribute("id") != null) {
			var div_id = div.getAttribute("id");
			if (div_id != null) {
				string_length = type.length + 6;
				id_substring = div_id.substring(0,string_length);
				//alert(id_substring)
				if (id_substring == "text_" + type + "_"){
					// alert("found!")
					new_substring = div_id.substring(5, div_id.length - 11);
					if (div_id =="text_" + new_substring + "_div_closed"){
						// alert(div_id)
					 // take certain action } 
						div.setAttribute("style","display: '';");
						var image_name = "text_" + new_substring + "_disclosure_closed";
						var image = document.getElementById(image_name);
						if (image != null) {
							image.setAttribute("style", "display: none;");
						}						
						var image_name = "text_" + new_substring + "_disclosure_opened";
						var image = document.getElementById(image_name);
						if (image != null) {
							image.setAttribute("style","display: '';");
						}						
					} 
					if (div_id =="text_" + new_substring + "_div_opened"){
						div.setAttribute("style","display: none;");
					}
				} 
			}
		}
	}
}

function toggle_all_texts_closed(type)
{
	var divs = document.getElementsByTagName("div"); // loop through all div elements 
	// alert(divs.length)
	for (var i=0; i<divs.length; i++){
		var div = divs[i]; 
		if (div != null && div.getAttribute("id") != null) {
			var div_id = div.getAttribute("id");
			if (div_id != null) {
				string_length = type.length + 6;
				id_substring = div_id.substring(0,string_length);
				//alert(id_substring)
				if (id_substring == "text_" + type + "_"){
					// alert("found!")
					new_substring = div_id.substring(5, div_id.length - 11);
					if (div_id =="text_" + new_substring + "_div_opened"){
						div.setAttribute("style","display: '';");
					}
					if (div_id =="text_" + new_substring + "_div_closed"){
						// alert(div_id)
					 // take certain action } 
						div.setAttribute("style","display: none;");
						var image_name = "text_" + new_substring + "_disclosure_closed";
						var image = document.getElementById(image_name);
						if (image != null) {
							image.setAttribute("style", "display: '';");
						}						
						var image_name = "text_" + new_substring + "_disclosure_opened";
						var image = document.getElementById(image_name);
						if (image != null) {
							image.setAttribute("style","display: none;");
						}						
					} 
				} 
			}
		}
	}
}

function to_print()
{
	window.print();
}

function set_checkboxes_state(base_element_id, state) 
{
	var allElements=document.getElementsByName(base_element_id);
	var length_string = base_element_id.length;
	for(z=0; z<allElements.length;z++){
		if(allElements[z].type == 'checkbox'){
			if(allElements[z].name.substring(0,length_string) == base_element_id) {
				allElements[z].checked = state;
			}
		}
	}
}

function validate_required(field,alerttxt)
{
with (field)
{
  if (value==null||value=="")
  {
  alert(alerttxt);return false;
  }
  else
  {
  return true;
  }
}
}

function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 
  {alert(alerttxt);return false;}
else {return true;}
}
}

function validate_captcha(captcha_challenge_field, captcha_response_field, alerttxt)
{
	// validates against captcha inline in the code (in rails code)
	if(captcha_challenge_field.value==null||captcha_response_field.value!=captchas()[parseInt(captcha_challenge_field.value)].response) {
		alert(alerttxt);
		return false;
	} else {
		return true;
	}
}

function validate_form(thisform)
{	
with (thisform)
{
if (validate_required(person_firstname,"firstname is not valid")==false)
  {return false}
if (validate_required(person_surname,"surname is not valid")==false)
  {return false}
if (validate_required(address_street,"street is not valid")==false)
  {return false}
if (validate_required(address_suburb,"suburb is not valid")==false)
  {return false}
if (validate_required(address_state,"state is not valid")==false)
  {return false}
if (validate_required(address_postcode,"zip/postcode is not valid")==false)
  {return false}
if (validate_required(contact_phone_country_code,"phone country code is not valid")==false)
  {return false}

if (validate_required(contact_phone_area_code,"phone area code is not valid")==false)
  {return false}
if (validate_required(contact_phone_number,"phone number is not valid")==false)
  {return false}
if (validate_required(person_dob_day,"date of birth (day) is not valid")==false)
  {return false}
if (validate_required(person_dob_month,"date of birth (month) is not valid")==false)
  {return false}
if (validate_email(email,"email is not valid")==false)
  {return false}
if (validate_required(position,"position is not valid")==false)
  {return false}

if (validate_captcha(captcha_challenge, captcha_response,"anti-SPAM question answered incorrectly")==false)
  {return false}

}
}

function set_business_name(this_field, text_field_qualifier)
{
	var length_string = this_field.name.length - text_field_qualifier.length - 2;
	var part_1 = this_field.name.substring(0,length_string);
	var part_2 = this_field.name.substring(length_string);
	if (text_field_qualifier == 'firstname')
		{
		var firstname = this_field.value;
		surname_field_name = part_1 + '[surname]';
		surname_field = document.getElementById(surname_field_name);
		var surname = surname_field.value;
		}
	if (text_field_qualifier == 'surname')
		{
		var surname = this_field.value;
		firstname_field_name = part_1 + '[firstname]';
		firstname_field = document.getElementById(firstname_field_name);
		var firstname = firstname_field.value;
		}
	var name_field_string = part_1 + "[name]";
	name_field = document.getElementById(name_field_string);
	name_field.value = surname + ', ' + firstname;
}
