function InitAjax()
{
var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function toempty(elem, value)
{
	if( elem.value == value )
	{
		elem.value = "";
	}
	else if( elem.value == "" )
	{
		elem.value = value;
	}
}

function gdbShowStates( country )
{
  var StateSelector = document.getElementById( 'state_control' );
  var NoStateSelector = document.getElementById( 'nostate_control' );
  if( country == 'US' || country == 'Us' )
  {
    var advertising = document.getElementById( 'advertising' );
    advertising.rowSpan = 3;
    StateSelector.style.display = '';
    NoStateSelector.style.display = 'none';
    var td_city = document.getElementById( 'td_city' );
    td_city.innerHTML = "";
    var td_city_us = document.getElementById( 'td_city_us' );
    td_city_us.innerHTML = "<input type='text' name='city' class='text_input3' id='CourseCitysearch' />";
    
  }
  else
  {
    var advertising = document.getElementById( 'advertising' );
    advertising.rowSpan = 4;
    StateSelector.style.display = 'none';
    NoStateSelector.style.display = '';
    var td_city_us = document.getElementById( 'td_city_us' );
    td_city_us.innerHTML = "";
    var td_city = document.getElementById( 'td_city' );
    td_city.innerHTML = "<input type='text' name='city' class='text_input1' id='CourseCitysearch' />";
    
  }
}

function submitRating( Rating, CourseId ) 
{
	//var gridG = document.getElementById('CourseRatingResult');
	var xmlhttp = InitAjax();
		if (xmlhttp)
		{
			//gridG.innerHTML = '<br /><br /><center><img src="/images/main/ajax-loader.gif"/></center>';
			xmlhttp.open("GET", "/ajax.php?Action=RateCourse&Rating="+ Rating +"&CourseId="+CourseId);
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState==4)
				{
					gridG.innerHTML = xmlhttp.responseText;
				}
		}
		xmlhttp.send(null);
	}
}


function addCourseReview(Body, CourseId) 
{
	var gridG = document.getElementById('ReviewResult');
	var xmlhttp = InitAjax();
	if(Body != '')
	{
		if(xmlhttp)
		{
			sURL = '/ajax.php';
			sParams = 'Action=ReviewCourse&CourseId='+CourseId+"&Body="+Body;
			gridG.innerHTML = '<img src="/images/main/ajax-loader.gif" style="padding-left:12px;" />';
			xmlhttp.open('POST', sURL, true);
			xmlhttp.setRequestHeader('Method', 'POST '+sURL+' HTTP/1.1');
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.setRequestHeader('Content-Length', sParams.length);
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState==4)
				{
					gridG.innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(sParams);
		}
	}
}


function checkregisterform () {
	var returnValue = true;
	var RegEmail = document.getElementById('RegEmail').value;
	var RegPassword = document.getElementById('RegPassword').value;
	var RegConfirmPassword = document.getElementById('RegConfirmPassword').value;
	var formresult = document.getElementById('formresult');
	formresult.innerHTML = '';
	var errorMsg = '';

	var AtPos = RegEmail.indexOf("@")
	var StopPos = RegEmail.lastIndexOf(".")
	if (AtPos == -1 || StopPos == -1) {
		errorMsg = "Email Address not valid.";
	}

	if (RegPassword != RegConfirmPassword) {	errorMsg = "Passwords do not match.";	}
	if (RegPassword.length < 5) {	errorMsg = "Password must be at least 5 characters long.";	}
	if (RegEmail == "") {	errorMsg = "Email field is blank.";	}

	if (errorMsg != "") {
		formresult.innerHTML = '<span style="color:red;">'+ errorMsg +'. Please correct and re-submit form.</span>';
		returnValue = false;
	}
	return returnValue;
}

function checkcontactform() {
	var returnValue = true;
	var fname = document.getElementById('fname').value;
	var femail = document.getElementById('femail').value;
	var subject = document.getElementById('fsubject').value;
	var body = document.getElementById('fbody').value;
	var formresult = document.getElementById('formresult');
	formresult.innerHTML = '';
	var errorMsg = '';

	var AtPos = femail.indexOf("@")
	var StopPos = femail.lastIndexOf(".")
	if (AtPos == -1 || StopPos == -1) {
		errorMsg = "Email Address not valid.";
	}

	if (fname == "Your Name" || femail == "Your Email" || subject == "Subject" || body == "") {
		errorMsg = "Required fields are blank.";
	}

	if (errorMsg != "") {
		formresult.innerHTML = '<span style="color:red; font-size:11px">Error: '+ errorMsg +'. Please correct and re-submit.</span';
		returnValue = false;
	}
	return returnValue;
}

function submitsearchform() {
	var country = document.getElementById('CourseCountrysearch').value;
  var state = document.getElementById('CourseStatesearch').value;
	var city = rawurlencode( document.getElementById('CourseCitysearch').value );
	var areacode = rawurlencode( document.getElementById('CourseAreaCodesearch').value );
  var name = rawurlencode( document.getElementById('CourseNamesearch').value );
  var value = '/search/'; 
  if( country || state || city || areacode || name )
  {
    value += ( country ? country : 'any' ) + ( country == 'Us' || country== 'US' ? state ? '-' + state : '' : '' ) + '/';
  }  
  if( name )
  {
   value += ( city ? city : 'any' ) + '/' + ( areacode ? areacode : 'any' )  + '/' + ( name ? name + '/' : '' );
  } 
  else if( areacode )
  {
   value += ( city ? city : 'any' ) + '/' + ( areacode ? areacode : 'any' )  + '/';
  } 
  else if( city )
  {
   value += ( city ? city : 'any' ) + '/';
  } 

  //document.CourseFormsearch.action = value;
  //document.CourseFormsearch.submit();
  document.location.href=value;
	return true;
}

String.prototype.trim = function () {
  return this.replace(/^\s*|\s*$/,"");
}

function rawurlencode( str ) {
    var str = str.trim();
    var histogram = {}, tmp_arr = [], unicodeStr='', hexEscStr='';
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urldecode.
   histogram[' ']   = '%20';
   histogram['!']   = '%21';
   histogram['#']   = '%23';
   histogram['$']   = '%24';
   histogram['\\']  = '';
   histogram["'"]   = '';
   histogram['"']   = '%27';
   histogram['(']   = '%28';
   histogram[')']   = '%29';
   histogram['*']   = '%2A';
   histogram['+']   = '%2B';
   histogram[',']   = '%2C';
   histogram['/']   = '';
   histogram[':']   = '%3A';
   histogram[';']   = '%3B';
   histogram['=']   = '%3D';
   histogram['?']   = '%3F';
   histogram['@']   = '%40';
   histogram['~']   = '%7E';


    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    //ret = encodeURIComponent(ret);
    for (unicodeStr in histogram) {
        hexEscStr = histogram[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }

    return ret;
}
