// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function openWin(thewindow) {
remote=window.open(thewindow,"remotewin","toolbar=no,width=450,height=333,status=no,scrollbars=no,resizable=no,menubar=no");
if (remote.opener == null) remote.opener = window;
}

// Requires prototype library
function loadMapImage(region) {
	new Ajax.Request('/search_criteria/update_location_criteria', 
					{asynchronous:true, evalScripts:true, parameters:'market_region='+region})
}

function license_ok() { 
	if( $('license_valid').value == 'Y') { 
		return true; 
	} else {
		alert('Your license number has not been verified. Please check the number and try again or contact us at 1-877-257-6880, if you continue to experience difficulty.');
		return false;
	}
}

function email_ok() { 
	if( $('email_contact_valid').value == 'Y') { 
		return true; 
	} else {
		alert('Your email has not been verified as corresponding to a contact in our system. Please check and try again or contact us at 1-877-257-6880, if you continue to experience difficulty.');
		return false;
	}
}

function given_permission() {
	if( Form.Element.getValue('permission') != undefined) {
		return true;
	} else {
		alert('You must give us permission to e-mail you our newsletter once per week.  We promise not to bother you any more than that and you can easily unsubscribe at any time.');
		return false;
	}
}



function license_changed() {
	$('license_valid') = 'N';
}

function agent_check() {
	if (! given_permission())
		return false;
	if (! MM_validateForm('license_number','','R','user_email','','RisEmail','password','','R','password_confirmation','','R'))
		return false;
	if (! license_ok())
		return false;
	if (! verifyPasswords())
		return false;
	// If we get here, all checks passed--submit the form
	return true;
}

function other_check() {
	if (! given_permission())
		return false;
	if (! MM_validateForm('license_number','','R','user_email','','RisEmail','password','','R','password_confirmation','','R','user_zip','','R'))
		return false;
	if (! email_ok())
		return false;
	if (! verifyPasswords())
		return false;
	// If we get here, all checks passed--submit the form
	return true;
}


function verifyPasswords()
{
	if ($('password').value != $('password_confirmation').value)
	{
		alert('The passwords do not match.');
		return false;
	}
	else if ($('password').value == "" && $('password_confirmation').value == "")
	{
		alert('Please choose a password.');
		return false;
	}
	else { return true; }
}

// A few of the old Dreamweaver functions for convience.  Their client-side validation was helpful.
// We should get rid of this eventually.  We do server-side validation for license checking but client side is more user-friendly.
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) { alert('The following error(s) occurred:\n'+errors);  return false; } else { return true; }
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function get_index_of_option_matching(select_tag_id, value_to_match) {
	var options = $(select_tag_id).options
	for(var i=0;i<options.length;i++) {
		if(options[i].innerHTML == value_to_match) { return i; };
		if(options[i].value == value_to_match) { return i; };
	}
}


function check_and_change_status_type() {
	if( ($F('project_maximum_percent_sold') == '100') && ($F('project_status_type_id') != '14')) {
		alert('This project will be marked as \'Sold Out and Closed\'.');  
		$('project_status_type_id').selectedIndex = get_index_of_option_matching('project_status_type_id', 'Sold Out and Closed');
		
	}
	if( ($F('project_maximum_percent_sold') != '100') && ($F('project_status_type_id') == '14')) {
		alert('This project will be marked as \'Immediate Occupancy\'.  If this is not correct, you may change it.');  
		$('project_status_type_id').selectedIndex = get_index_of_option_matching('project_status_type_id', 'Immediate Occupancy');
	}
	return true;
}

function check_and_change_percent_sold() {
	if( ($F('project_maximum_percent_sold') == '100') && ($F('project_status_type_id') != '14')) {
		alert('This project will be marked as \'95-99% Sold\'. If this is incorrect, you may change it.');  
		$('project_maximum_percent_sold').selectedIndex = get_index_of_option_matching('project_maximum_percent_sold', '99');
		
	}
	if( ($F('project_maximum_percent_sold') != '100') && ($F('project_status_type_id') == '14')) {
		alert('This project will be marked as \'100% Sold\'.  If this is not correct, you may change it.');  
		$('project_maximum_percent_sold').selectedIndex = get_index_of_option_matching('project_maximum_percent_sold', '100');
	}
	return true;
}

