// Version 1.0.1 trustallenmortgage.com from Armando Romero
// License: GPL

// Version: 2008.01.28: Updated the scripts to handle any given elementID and not send off empty form submissions

// Form validation 2008.01.01 Added by Armando
function validate_required(field,alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
  	{alert(alerttxt);return false;}
		else {return true}
	}
}
// 2008.01.01 This one is actually used by the forms themselves unique to each page such as content.php and index.php
/*
function validate_form( thisform )
{
	if( validate_required( thisform.email , "Email must be filled out!" ) == false )
  {
  	thisform.email.focus() ;
  	return false ;
  }
}*/

// UI
// 2008.02.01: Submit a form
function formSubmit(id)
{
	document.getElementById(id).submit();
}

// 2008.03.27: Does a POST to a new window
function formSubmitPopUp(aForm, WIDTH, HEIGHT)
{
	day = new Date();
	id = day.getTime();

	eval( "page" + id + " = window.open('', '" + id + "','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + WIDTH + ",height=" + HEIGHT + ",left = 0,top = 120');");
	//return true ;
	aForm.target= id;
	aForm.submit();
	return false;
}

function popUp(URL,WIDTH,HEIGHT)
{
	day = new Date();
	id = day.getTime();
	eval( "page" + id + " = window.open(URL, '" + id + "','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + WIDTH + ",height=" + HEIGHT + ",left = 0,top = 120');");
	//return true ;
} ;

// 2008.01.04 This script now requires the full compliant URL like http:// or https:// whatever in order to work 100%
function publicPopUp(URL)
{
	day = new Date();
	id = day.getTime();
// 	eval( "page" + id + " = window.open('http://' + URL, '" + id + "');");
	eval( "page" + id + " = window.open(URL, '" + id + "');");
	//return true ;
} ;

function redirect(URL)
{
	window.location=URL;
	//return true ;
} ;

function switchImage( imageid , name )
{
	document.getElementById( imageid ).setAttribute( 'src' , name ) ;
	//return true ;
} ;

// 2008.02.01: Hide or display an element
function switchVisible(id)
{
	$element = document.getElementById(id);
	switch($element.style.display)
	{
		case 'none':
			$element.style.display = 'block';
			break;
		default:
			$element.style.display = 'none';
			break;
	}
}

function warn()
{
	return window.confirm("Are you sure you want to do that?");
}

/* Below is AJAX stuff */

var request = false ;

function clearInnerCell()
{
	document.getElementById( 'innerCell' ).innerHTML = '' ;
} ;

function createRequest()
{
	try
	{
		request = new XMLHttpRequest() ;
	}
	catch( msTwo )
	{
		try
		{
			request = new ActiveXObject( 'Msxml2.XMLHTTP' ) ;
		}
		catch( msOne )
		{
			try
			{
				request = new ActiveXObject( 'Microsoft.XMLHTTP' ) ;
			}
			catch( failure )
			{
				request = false ;
			}
		}
	}
	if( !request )
	{
		alert( 'Could not start up proper page navigation' ) ;
	}
} ;

function updateElement(url, cellId)
{
	createRequest() ;
	request.open( 'GET' , url , true ) ;
	request.onreadystatechange = function(){ updateElementContent( cellId ) ; }  ;
	request.send( null ) ;
} ;
function updateElementContent( cellId )
{
	if( request.readyState == 4 )
	{
		if( request.status == 200 )
		{
			document.getElementById(cellId).innerHTML = request.responseText ;
		}
	}

	else
	{
		document.getElementById(cellId).innerHTML = "<div class='text'><h2>Please wait</h2></div>" ;
	}
} ;

/* Below is AJAX and form POST */

/* The following function will always prepare post variables into utf-8 , so it is necessary to convert on the server */
// Updated 2008.01.28
function buildPOST( theFormName )
{
	theForm = document.getElementById( theFormName ) ;
	// theForm = document.forms[theFormName];
	var qs = ''
	for( e = 0 ; e < theForm.elements.length ; e++ )
	{
		if( theForm.elements[e].name != '' )
		{
			if(theForm.elements[e].value != '')
			{
				switch(theForm.elements[e].type)
				{
					case "checkbox" :
					case "radio" :
						if(theForm.elements[e].checked)
						{
							qs += ( qs =='' ) ? '' : '&' ;
							qs += theForm.elements[e].name + '=' + encodeURIComponent( theForm.elements[e].value ) ;
						}
						break ;
					default :
						qs += ( qs =='' ) ? '' : '&' ;
						qs += theForm.elements[e].name + '=' + encodeURIComponent( theForm.elements[e].value ) ;
						break ;
				}
			}
		}
	}
	qs += "\n" ;
	return qs ;
}

function send_post( theFormName , url )
{
	var xmlMessage = buildPOST( theFormName ) ;
	request.open( "POST" , url , false )
	request.setRequestHeader('Content-Type','application/x-www-form-urlencoded') ;
	request.send( xmlMessage )
}

function display_response(cellId) {
    var optionDiv = document.getElementById(cellId);
    optionDiv.innerHTML = request.responseText;
//     optionDiv.innerHTML = 'no';
}

function contactSubmit()
	{
		var contactName = document.getElementById( 'contact_name' ).value ;
		var contactText = document.getElementById( 'contact_textarea' ).innerHTML ;
		alert  ( contactText ) ;
		createRequest() ;
		request.open( 'POST' , 'contact.php' , true ) ;
		request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		request.onreadystatechange = updateInnerCell() ;
		request.send( "ajax=yes&contact_name=" + contactName + "&contact_textarea=" + contactText ) ;
	} ;
