// form validation function //
var URL = "http://www.portlandmyway.com";

function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
cntfield.value = maxlimit - field.value.length;
}


function fnValidate(form){
		  var emailexp=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;	
		  var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;

		  var fname = Trim(form.dsar_1.value);
		  var lname = Trim(form.dsar_2.value);
		  var txtEmail = Trim(form.dsar_18.value);
		  
		  var phone = Trim(form.dsar_13.value);
		  var price_range = Trim(form.dsar_2467.value);
		  
		  var city = Trim(form.dsar_2466.value);
		  var comments = Trim(form.dsar_2432.value);


		  
		  if(fname == "") {
			inlineMsg('dsar_1','You must enter the First Name.',2);
			return false;
		  }
		  
		  if(lname == "") {
		   inlineMsg('dsar_2','You must enter the Last Name.',2);
		   return false;
		  }
		  
		  if(txtEmail == ""){
		   inlineMsg('dsar_18','You must enter the Email.',2);
		   return false;
		  }
		  
		  if(!txtEmail.match(emailexp)) {
		   inlineMsg('dsar_18','You have entered invalid Email.',2);
		   return false;
		  }
		
		  
		  if(price_range == "") {
			inlineMsg('dsar_2467','You must select the Price range.',2);
			return false;
		  }
		  
		  if(city == "") {
			inlineMsg('dsar_2466','You must enter the City / Neighbourhood.',2);
			return false;
		  }
		  
		
  //return true;
}


function fnDownloadValidate(form){
		  var emailexp=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;	
		  var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;

		  var fname = Trim(form.dsar_1.value);
		  var lname = Trim(form.dsar_2.value);
		  var txtEmail = Trim(form.dsar_18.value);
		  


		  
		  if(fname == "") {
			inlineMsg('dsar_1','You must enter the First Name.',2);
			return false;
		  }
		  
		  if(lname == "") {
		   inlineMsg('dsar_2','You must enter the Last Name.',2);
		   return false;
		  }
		  
		  if(txtEmail == ""){
		   inlineMsg('dsar_18','You must enter the Email.',2);
		   return false;
		  }
		  
		  if(!txtEmail.match(emailexp)) {
		   inlineMsg('dsar_18','You have entered invalid Email.',2);
		   return false;
		  }
		
		  
  //return true;
}

function fnValidateWidget(form){
		  var emailexp=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;	
		  var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;

		  if(form.dsar_1.value=="") {
			inlineMsg('dsar_1w','You must enter the First Name.',2);
			return false;
		  }
		  
	  
		  if(form.dsar_2.value=="") {
		   inlineMsg('dsar_2w','You must enter the Last Name.',2);
		   return false;
		  }
		  
		  if(form.dsar_18.value==""){
		   inlineMsg('dsar_18w','You must enter the Email.',2);
		   return false;
		  }
		  
		  if(!form.dsar_18.value.match(emailexp)) {
		   inlineMsg('dsar_18w','You have entered invalid Email.',2);
		   return false;
		  }
		  
		 /*var post_var = {
			 'fname' : form.dsar_1.value,
			 'lname' : form.dsar_2.value,
			 'email' : form.dsar_18.value
		 };
		 $.post('http://90.0.0.150:5051/portland/setSession.php', post_var,
			function(contents) {
			}
		 ); */
		  
		 Set_Cookie( 'dsar_1', form.dsar_1.value, '', '/', '', '' );
	 	 Set_Cookie( 'dsar_2', form.dsar_2.value, '', '/', '', '' ); 	
		 Set_Cookie( 'dsar_18', form.dsar_18.value, '', '/', '', '' );
		 
}

function fnDelete(){
	Delete_Cookie('dsar_1','/','');	
	Delete_Cookie('dsar_2','/','');	
	Delete_Cookie('dsar_18','/','');	
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function Get_Cookie( name ) {

var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}


function Trim(nStr){return nStr.replace(/(^\s*)|(\s*$)/g, "");}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  if(target=="selPostCode"){
  	msg.style.left = -160 +leftposition + 'px';
  }else if(target=="dsar_1w"){
  	msg.style.left = -160 +leftposition + 'px';
  }else if(target=="dsar_2w"){
  	msg.style.left = -160 +leftposition + 'px';
  }else if(target=="dsar_18w"){
  	msg.style.left = -160 +leftposition + 'px';
  }else{
	msg.style.left = leftposition + 'px'; 
  }
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}



