// Number of times the user gets a 'free' view
var jay_free_views = 3;

function regval(form) {

// var error used to store the error message
var error = "";

//var i used to count the number of messages
var i = 0;

//file_name can't be empty
if (document.getElementById('name').value == ""){
	error += 'You must enter a Name.\n';
	i++;
 }
//link_name caan't be empty


 if (document.getElementById('email').value == ""){
	error += 'You must enter an email address.\n';
	i++;
 	}	
if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('email').value) && document.getElementById('email').value != "") {
	error += "You must enter a valid email.\n";
	i++;
    }    
    
    
if (i > 0) { //if there more then '0' errors..  
	//set top of error message
	var errorMessage = 'The form was not submitted due to the following problem' + ((i > 1) ? 's' : '') + ':\n\n';	   
	//add in the error message 
	errorMessage += error;
	//add in the bottom of the message
	errorMessage += '\nPlease fix ' + ((i > 1) ? 'these' : 'this') + ' problem' + ((i > 1) ? 's' : '') + ' and resubmit the form.';
	//alert the user
	alert(errorMessage);
	//return false, stop the form
	return false;    
}
//else return ture
return true;
 
}

/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return null
}

function signin(num,area){
	var cookie = getCookie('registered');
/*	if (cookie == '' || cookie == null)
	//document.location.href="/"+area+"/sign-in.php?id="+num+"";
	document.location.href="/sign-in?id="+num+"&section="+area;
	else
	document.location.href="/"+area+"/mls-details-"+num+".php";*/
   if (cookie == '' || cookie == null)
	  document.location.href="http://jaybanks.ca/sign-in?id="+num+"&section="+area;
	else
	  document.location.href="/"+area+"/mls-details-"+num+".php";
}


function myfunction(num,area){
/*	if (area != undefined && area != '')
	document.location.href="/"+area+"/sign-in.php?id="+num+"";
	else
	document.location.href="sign-in.php?id="+num+"";
*/
  if (area != undefined && area != '')
	  document.location.href="http://jaybanks.ca/sign-in?id="+num+"&section="+area;
   else
	  document.location.href="http://jaybanks.ca/sign-in";
}

function check_reg (item)
{
  var cookie = getCookie ('registered');
  
  if (cookie == '' || cookie == null)
  {
    var counter = getCookie ('regviews');
    if (counter == null || counter == '')
      counter = 0;
    else
      counter = parseInt (counter);
      
    if (counter < jay_free_views)
    {
      if (counter == 'NaN')
        counter = 0;
      createCookie ('regviews', counter + 1, 7);
      return true;
    }
    else if (jay_page != undefined && jay_page != '')
//  	  document.location.href="/"+jay_page+"/sign-in.php?id="+item.id+"";
  	  document.location.href="http://jaybanks.ca/sign-in?id="+item.id+"&section="+jay_page;
  	else
  	//  document.location.href="sign-in.php?id="+item.id+"";
  	  document.location.href="http://jaybanks.ca/sign-in";
  	return false;
  }
  
  return true;
}

function load(area){
  var cookie = getCookie('registered');
  
  if (cookie == '' || cookie == null)
  {
    var objLinks = getElementsByClassName(document, "a", "info-links");
    
  	for (var iCounter = 0; iCounter < objLinks.length; iCounter++)
  	{
      var mlsnum = objLinks[iCounter].getAttribute('id'); 
      
      objLinks[iCounter].onclick = function()
      {
        myfunction(this.id, area);
        return false;
      }; 
  	}    
  }
}


 function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
function setcookie(){
    var maxp = document.getElementById('maxPrice');
    var minp = document.getElementById('minPrice');
    var beds = document.getElementById('bedRooms');
    var themaxp = maxp.options[maxp.selectedIndex].value + "|" + minp.options[minp.selectedIndex].value + "|" + beds.options[beds.selectedIndex].value;
	createCookie('searchoptions',themaxp,7)
}

function setcookieindex(){
    var maxp = document.getElementById('maximumPrice');
    var minp = document.getElementById('minimumPrice');
    var beds = document.getElementById('beds');
    var themaxp = maxp.options[maxp.selectedIndex].value + "|" + minp.options[minp.selectedIndex].value + "|" + beds.options[beds.selectedIndex].value;
	createCookie('searchoptions',themaxp,7)
}

function gocontact(nums){
	window.open("/mda/inquire.php?pop=yes&mid="+nums,'','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=425,left = 100,top = 100');
	return false;

}

function dynamicSelect(id1, id2) {
	// Feature test to see if there is enough W3C DOM support
	if (document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		// Clone the dynamic select box
		var clone = sel2.cloneNode(true);
		// Obtain references to all cloned options 
		var clonedOptions = clone.getElementsByTagName("option");
		// Onload init: call a generic function to display the related options in the dynamic select box
		refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
		sel1.onchange = function() {
			refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
		};
	}
}
function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
	// Delete all options of the dynamic select box
	while (sel2.options.length) {
		sel2.remove(0);
	}
	// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
	var pattern1 = /( |^)(select)( |$)/;
	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
	// Iterate through all cloned options
	for (var i = 0; i < clonedOptions.length; i++) {
		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
			// Clone the option from the hidden option pool and append it to the dynamic select box
			sel2.appendChild(clonedOptions[i].cloneNode(true));
		}
	}
}
// Attach our behavior onload
/*window.onload = function() {
	dynamicSelect("main-area", "sub-area");
}*/

