/*---------------------Jquery global---------------------------------------*/

$(document).ready(function() {

  $defaultval = 'Type Location here';
  
  if ($('#loc').val() == '') {
    $('#loc').val($defaultval);
  }

  $('#loc').focus(function() {
	if ($('#loc').val() == $defaultval) {
	  $('#loc').val('');
    }
  });

  $('#loc').blur(function() {
	if ($('#loc').val() == '') {
	  $('#loc').val($defaultval);
    }
  });

})

/*------------------------------------------------------------------*/

function celOver(obj) {
  var s = obj.className;
  s = s.replace(/-over/g, "");
  obj.className = s+"-over";
}

function celOut(obj) {
  var s = obj.className;
  s = s.replace(/-over/g, "");
  obj.className = s;
}

function verifEmail(chp) {
	
  var returnString = "";
  
  if (chp.value.length == 0) {
    returnString += "- E-mail is required\n";
  } else {
	regex = new RegExp("^[a-z0-9_]([.-]?[a-z0-9_]+)+@[a-z0-9_]([.-]?[a-z0-9_]+)+\.([a-z]{2,4}|[a-z]{6})$", "gi");
	if (!regex.exec(chp.value)) {
       returnString += "- E-mail is invalid\n";
	}				
  }
  return returnString
}

function numberok(value) {
	if (isNaN(value)) {
		return false;
	} else {
		return true;
	}
}

function uploadFile() {

  var form = document.form1;
    
  var msg = "";
  
  var Extensions = ".doc .pdf";
  var sFile = form.RespFile.value;
  
  if (sFile == "") {
	//msg += "- No file to uploaded\n";
  } else {
		
    var sExt = sFile.match( /\.[^\.]*$/ ) ;
    sExt = sExt ? sExt[0].toLowerCase() : "." ;

    if ( Extensions.indexOf( sExt ) < 0 ) {
      msg += "- This file type is not allowed for uploading ("+sExt+")\n"; 
    }
  }
  
  return msg;

}

function favoris(txt,url) {
  if ( navigator.appName != 'Microsoft Internet Explorer' ){ 
    window.sidebar.addPanel(txt,url,""); 
  } else { 
    window.external.AddFavorite(url,txt); 
  } 
}

/**********************************************************/

function MM_validateletter() {
  var form = document.newsletter;
  var errors = "";

  if (form.FirstName.value.length == 0) {
	errors += "- First Name is required\n";
  }

  if (form.LastName.value.length == 0) {
	errors += "- Last Name is required\n";
  }

  errors += verifEmail(form.Email);

  var flagCheck = "- Type of properties is required\n";
  for (i=0; i<form.Type.length; i++) {
      if (form.Type[i].checked) {
        flagCheck = "";
        break;
      }
  }  
  errors += flagCheck;

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
}

/*-------gallery system (item)----------*/
function popup(idimg,width,height) { 
  var yp = 10;
  var xp = 10;
  fen = window.open(idimg,'','toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,copyhistory=0,menuBar=0,top='+yp+',left='+xp+',width=' + width + ',height=' + height);
  fen.focus(); 
}

/*-------gallery system (itempopup)----------*/
function checksize() {
 if (document.images[0].complete) {
 var width = 320 -12;
 if (document.images[0].width > 310) {
   width = document.images[0].width;
  }
  window.resizeTo(width+12,document.images[0].height+155);
  window.focus();
 }
}

/*--------------------- Client ADD & Edit -------------------*/
function MM_validateClient() {
  
  var form = document.form1;
  var errors = "";
  
  if (form.FirstName.value.length == 0) {
	errors += "- First Name is required\n";
  }
  
  if (form.Surname.value.length == 0) {
	errors += "- Last Name is required\n";
  }

  if (form.Address1.value.length == 0) {
	errors += "- Address1 is required\n";
  }
  
  if (form.Address2.value.length == 0) {
	errors += "- Address2 is required\n";
  }

  if (form.LocationID.selectedIndex == 0) {
	errors += "- Location is required\n";
  }

  if (form.Tel.value.length == 0) {
	errors += "- Tel is required\n";
  }
  
  if (!numberok(form.Tel.value)) {
	errors += "- Tel requires numbers only\n";
  }

  if (form.Email.value.length == 0) {
	errors += "- Email is required\n";
  } else {
    if (!checkEmail(form.Email.value)) {
       errors += "- Email is invalid\n";
    }
  }
  

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}

/*--------------------- Auctioneer Add & Edit -------------------*/
function MM_validateAuctioneer() {
  
  var form = document.form1;
  var errors = "";
  
  if (form.Name.value.length == 0) {
	errors += "- Name is required\n";
  }
  
  if (form.Address.value.length == 0) {
	errors += "- Address is required\n";
  }
  
  if (form.LocationID.selectedIndex == 0) {
	errors += "- Location is required\n";
  }

  if (form.Tel.value.length == 0) {
	errors += "- Tel is required\n";
  }
  
  if (!numberok(form.Tel.value)) {
	errors += "- Tel requires numbers only\n";
  }

  if (form.Email.value.length == 0) {
	errors += "- Email is required\n";
  } else {
    if (!checkEmail(form.Email.value)) {
       errors += "- Email is invalid\n";
    }
  }
  

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}

/*--------------------- Called by requestresponse.asp -------------------*/

function MM_validateFormResp() {

  var form = document.form1;
  var errors = '';
  var flag = 0;
  
  if (form.RespFile.value.length == 0) {
    
	if (form.RespURL.value.length == 0 && form.RespLetter.value.length == 0) {
	    errors += '- Either an Attached File, a URL or Property Details text is required\n';
    } else {
      flag = 1;
    }
        
  } else {
    var ulf = uploadFile();
    flag = 1;
    if (ulf != "") {
      errors += ulf;
    }
  }

  if (flag == 0) {
     errors += '- You must attach a File, a URL or complete the textfield\n';
  }
  
  if (errors) { alert('The following error(s) occurred:\n'+errors); }
  document.MM_returnValue = (errors == '');
}

/*------------------------------------ Property Search ---------------------------------*/

function XMLHttpBrouser() {
  if (document.all) {
    var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
  } else {
    var XhrObj = new XMLHttpRequest();
  }
  return XhrObj;
}

function sendDataSearch(v1,v2) {
  var XhrObj = XMLHttpBrouser();
  XhrObj.open("POST", "s1.asp");
  XhrObj.onreadystatechange = function() {
    if (XhrObj.readyState == 4 && XhrObj.status == 200) {
      eval(XhrObj.responseText);
    }
  }
  XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  XhrObj.send("id="+v1+"&v2="+v2);
}