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

function trim(text) {
  	text = text.replace(/^\s*/, '');
  	text = text.replace(/\s*$/, '');
  	return text;
}

function isInt(text)
{
	testString = /\D/;
	if (testString.test(text)==false) {
		return true;
	}
	else
	{
		return false;
	}
}

function validate_signupForm()
{
   if ($('user_login'))
   {
      $('user_login').value = trim($('user_login').value);
   }
   if(!$('terms').checked) {
       alert("Please agree to Terms and Conditions.");
       return false;
   }
   return true; 
}

function validate_vip_signupForm()
{
   if ($('user_login'))
   {
      $('user_login').value = trim($('user_login').value);
   }
   var code = trim($('vip_code_code').value);
   if (code == '') {
   	   alert("VIP Promotional Code can not be empty.");
	   return false;
   }
   if(!$('terms').checked) {
       alert("Please agree to Terms and Conditions.");
       return false;
   }
   return true; 
}

function validate_cartTermsChecked()
{
   if($('monthly-terms') != undefined && !$('monthly-terms').checked) {
       alert("Please click on checkbox to agree on monthly recurring charge.");
       return false;
   }
   return true; 
}

function copy_billing_to_shipping_address()
{
    if ($('same_ship_address') && $('same_ship_address').checked)
    {
        $('ship_address_address1').value = $('bill_address_address1').value;
        $('ship_address_address2').value = $('bill_address_address2').value;
        $('ship_address_city').value = $('bill_address_city').value;
        $('ship_state').innerHTML = $('bill_state').innerHTML;
        $('ship_address_state').value = $('bill_address_state').value;
        $('ship_address_zip').value = $('bill_address_zip').value;
        $('ship_address_country_id').value = $('bill_address_country_id').value;
    }
}

function validate_test(num_questions)
{
	//Validate firt and last name first
	if ($('name_first') && $('name_last'))
	{
		if ( trim($('name_first').value) == '' )
		{
			alert('Please enter first name.');
			$('name_first').focus();
			return false;
		}
		if ( trim($('name_last').value) == '' )
		{
			alert('Please enter last name.');
			$('name_last').focus();
			return false;
		}		
	}	
	
	// Iterate through each question and see if any one choice is selected.
	// If not, show an alert box
	for (var i=0; i<num_questions; i++)
	{
		choice_radios_array = document.getElementsByName('selected_choice_'+i);
		if (choice_radios_array.length != undefined)
		{
			checked = false;
			for (var j=0; j<choice_radios_array.length; j++)
			{
				if (choice_radios_array[j].checked)
				{
					checked = true;
					break;
				}
			}
			if (!checked)
			{
				alert("You have not answered all questions yet. Please verify and re-submit.")
				return false;
			}
		}
	}
	//return true; //do not return true for AJAX validation to work
}
function validate_first_last_name()
{

	return true;
}

function validate_exam_form()
{
	var num_questions = $('exam_num_questions').value;
	if (!isInt(num_questions))
	{
		alert("No. of Questions is invalid.");
		$('exam_num_questions').focus();
		return false;
	}
	var questions_to_pass = $('exam_questions_to_pass').value;
	if (!isInt(questions_to_pass))
	{
		alert("No. of Questions Required to Pass is invalid.");
		$('exam_questions_to_pass').focus();
		return false;
	}
	
	if (parseInt(num_questions) < parseInt(questions_to_pass))
	{
		alert("No. of Questions should not be less than No. of Questions Required to Pass.");
		$('exam_num_questions').focus();
		return false;
	}
	return true;
}

function validate_question_form()
{
	choice_radios_array = document.getElementsByName('selected_choice');
	if (choice_radios_array.length != undefined)
	{
		checked = false;
		for (var j=0; j<choice_radios_array.length; j++)
		{
			if (choice_radios_array[j].checked)
			{
				checked = true;
				break;
			}
		}
		if (!checked)
		{
			alert("Please select which choice is the correct answer.")
			return false;
		}
	}
}

function validate_comment_form()
{
	if ( trim($('comment_comment').value) == '' )
	{
		alert('Please enter the comment.');
		$('comment_comment').focus();
		return false;
	}
	return true;
}

function validate_promo_code_form()
{
	
	return true;
}

function validate_full_name()
{
	if ( trim($('name_first').value) == '' )
	{
		alert('Please enter first name.');
		$('name_first').focus();
		return false;
	}
	if ( trim($('name_last').value) == '' )
	{
		alert('Please enter last name.');
		$('name_last').focus();
		return false;
	}
	return true;
}

function toggle_delete_links(callingLink)
{
	var delete_links = getElementsByClass(document, 'delete-link', 'a');
	if (delete_links)
	{
		for (var j=0; j<delete_links.length; j++)
		{
			// show the links
			Element.toggle(delete_links[j]);
		}
		// toggle text of the calling link 
		callingLink.innerHTML == 'Show Delete Links' ? callingLink.innerHTML='Hide Delete Links' : callingLink.innerHTML='Show Delete Links';
	}
}

function getElementsByClass(node,searchClass,tag) 
{
	var classElements = new Array();
	var els = node.getElementsByTagName(tag); // use "*" for all elements
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
	for (i = 0, j = 0; i < elsLen; i++) 
	{
		if ( pattern.test(els[i].className) ) 
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
