var frmHelp=new Array();

function $(id){
	return document.getElementById(id);
}

function url(str){
	u=str.replace(/[^a-z0-9]/ig, '_');
	u=u.replace(/_{2,}/g, '-');
	u=u.replace(/[_-]+$/, '');
	return u;
}

function absPos(obj){
	var pos=new Array();
		pos.left=obj.offsetLeft;
		pos.top=obj.offsetTop;
		pos.width=obj.offsetWidth;
		pos.height=obj.offsetHeight;
	while(obj.offsetParent){
		obj=obj.offsetParent;
		pos.left+=obj.offsetLeft;
		pos.top+=obj.offsetTop;
	}
	return pos;
}

function addEvent(obj, evt, exec, capture){
	try{
		obj.addEventListener(evt, exec, capture);
	}
	catch(e){
		try{
			obj.attachEvent('on'+evt, exec);
		}
		catch(e){
			throw new Error('Cannot attach event '+evt+' to object');
		}
	}
}

function initHelp(divId, fromTop, maxFromTop){
	$(divId).style.right=((screen.width-1016)/2+20)+'px';
	$(divId).style.visibility='visible';
	var iTxt=$(divId).innerHTML;
	$(divId).style.top=fromTop+'px';

	window.onscroll=function(){
		var top=fromTop+document.documentElement.scrollTop;
		if(maxFromTop>0 && top>maxFromTop-$(divId).offsetHeight)
			top=maxFromTop-$(divId).offsetHeight;
		$(divId).style.top=top+'px';
	}

	var tBlur=new Array();
	for(var id in frmHelp){
		$(id).onfocus=function(){
			$(divId).innerHTML=frmHelp[this.id];
			if($(this.id+'p')){
				$(this.id+'p').className='bxp_a';
			}
		}

		addEvent($(id), 'blur', function(){
			if(navigator.appName=="Microsoft Internet Explorer")
				var tmp=event.srcElement;
			else
				var tmp=this;
			$(divId).innerHTML=iTxt;
			if($(tmp.id+'p')){
				$(tmp.id+'p').className='bxp_i';
			}
		}, false);

		if($(id).type=='checkbox' || $(id).type=='radio'){
			var tmp=document.getElementsByTagName('LABEL');
			for(var i=0;i<tmp.length;i++){
				if(tmp[i].htmlFor==id){
					tmp[i].onmouseover=function(){ $(divId).innerHTML=frmHelp[this.htmlFor]; }
					tmp[i].onmouseout=function(){ $(divId).innerHTML=iTxt; }
					break;
				}
			}
			$(id).onmouseover=function(){ $(divId).innerHTML=frmHelp[this.id]; }
			$(id).onmouseout=function(){ $(divId).innerHTML=iTxt; }
		}
	}
}

function checkDomain(domain, noalert){
	if(domain.substring(0, 4).toLowerCase()=='www.')
		domain=domain.substring(4);
	if(!domain)
		return;
	else if(!noalert){
		alert('Domain "'+domain+'" is available');
		window.open ("ajax_request.php?domain="+domain,"Chack Domain","location=no,status=no,scrollbars=no,width=300,height=150,top=0,left=0");
		$('availableDomain').value=domain;
	}
	else
		return true;
}

function replaceDomains(txt){
	document.sitef.domain.value=txt;
	checkDomainAjax(txt);
}

var suggest=new Array();
function suggestDomains(txt){
	// get the first number and the first word longer than 3 chars
	var w=txt.split(/[^a-z0-9]+/i);
	if(w.length==0)
		return;

	var words=new Array();
	var numfound=false;
	for(var i=0;i<w.length;i++){
		if(!numfound && !isNaN(parseInt(w[i]))){
			words[words.length]=w[i].toLowerCase();
			numfound=true;
		}
		else if(w[i].length>1){
			words[words.length]=w[i].toLowerCase();
		}
	}

	suggest=new Array();

	if(words.length==0)
		return;

	if(words.length==1){
		suggest=['<a href="javascript://" onClick="replaceDomains(\'www.'+words[0]+'.com\')">www.'+words[0]+'.com</a>', '<a href="javascript://" onClick="replaceDomains(\'www.'+words[0]+'house.com\')">www.'+words[0]+'house.com</a>', '<a href="javascript://" onClick="replaceDomains(\'www.'+words[0]+'home.com\')">www.'+words[0]+'home.com</a>', '<a href="javascript://" onClick="replaceDomains(\'www.'+words[0]+'sale.com\')">www.'+words[0]+'sale.com</a>'];
	}
	else if(words.length==2){
		suggest=['<a href="javascript://" onClick="replaceDomains(\'www.'+words[0]+words[1]+'.com\')">www.'+words[0]+words[1]+'.com</a>', '<a href="javascript://" onClick="replaceDomains(\'www.'+words[1]+words[0]+'.com\')">www.'+words[1]+words[0]+'.com</a>', '<a href="javascript://" onClick="replaceDomains(\'www.'+words[0]+words[1]+'house.com\')">www.'+words[0]+words[1]+'house.com</a>', '<a href="javascript://" onClick="replaceDomains(\'www.'+words[1]+words[0]+'house.com\')">www.'+words[1]+words[0]+'house.com</a>', '<a href="javascript://" onClick="replaceDomains(\'www.'+words[0]+words[1]+'home.com\')">www.'+words[0]+words[1]+'home.com</a>', '<a href="javascript://" onClick="replaceDomains(\'www.'+words[1]+words[0]+'home.com\')">www.'+words[1]+words[0]+'home.com</a>'];
	}
	else{
		for(var i=0;i<words.length-1;i++)
			for(var j=i+1;j<words.length;j++){
				suggest[suggest.length]='<a href="javascript://" onClick="replaceDomains(\'www.'+words[i]+words[j]+'.com\')">www.'+words[i]+words[j]+'.com</a>';
				suggest[suggest.length]='<a href="javascript://" onClick="replaceDomains(\'www.'+words[j]+words[i]+'.com\')">www.'+words[j]+words[i]+'.com</a>';
			}
		var tmp='www.'+words.join('')+'.com';
		suggest[suggest.length]='<a href="javascript://" onClick="replaceDomains(\''+tmp+'\')">'+tmp+'</a>';
	}
	$('suggestDomain').innerHTML='<b>Suggested Web Site Addresses:</b><br>Click on the above suggestions or fill up a domain name<br><div style="padding-top: 2px; padding-left: 60px; font-size:11px;"><b>'+suggest.join('<br>')+'</b></div>';
	//$('suggestDomain').innerHTML='<b>Suggested Domains:</b><br><div style="padding-top: 2px; padding-left: 60px; font-size:11px;"><b>'+suggest.join('<br>')+'</b></div>';
}

function ajaxStart(){
	if (navigator.appName=="Microsoft Internet Explorer")
	    return new ActiveXObject("Microsoft.XMLHTTP");
	else{
		try{
			netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesRead");
		}
		catch(err){}
		return new XMLHttpRequest();
	}
}

function ajaxGet(link){
	var objHTTP=ajaxStart();
	try{
		objHTTP.open('GET',link,false);
	}
	catch(err){
		try{
			objHTTP.open('GET',localPath+'ajax_remote.php?link='+escape(link),false);
		}
		catch(e){
			alert('Cannot open remote link because the browser won\'t let me.\nPlease use another browser');
			return "\tBad Browser";
		}
	}
	objHTTP.send(null);
	return objHTTP.responseText;
}


function checkDomainAjax(id){
	var fld_url=document.sitef.domain.value;
	var camp=ajaxGet('ajax_request.php?domain='+fld_url);
	camp=camp.split("\t");

	document.getElementById('fld_url').innerHTML=camp[1];
	document.getElementById('fld_url').style.display='';

	if(camp[0]=="no"){
		document.sitef.domain.value='';

		var j=0;
		for(var i=0;i<suggest.length; i++){
			if(suggest[i].indexOf(fld_url)>0){
				for(j=i;j<suggest.length-1;j++)
					suggest[j]=suggest[j+1];
				suggest.length=suggest.length-1;
			}
		}
		$('suggestDomain').innerHTML='<b>Suggested Web Site Addresses:</b><br>Click on the above suggestions or fill up a domain name<br><div style="padding-top: 2px; padding-left: 60px; font-size:11px;"><b>'+suggest.join('<br>')+'</b></div>';
		//$('suggestDomain').innerHTML='Suggested Domains:<br><div style="padding-top: 2px; padding-left: 60px; font-size:11px;"><b>'+suggest.join('<br>')+'</b></div>';
	}
}

function confirm_active_user(uid) {
	var pass = prompt("Insert the password !")
	if (pass){
		window.location = "users.php?uid="+uid+"&activare="+pass;
	}
}

function trim(strText)
{
    while (strText.substring(0,1) == ' ')
	strText = strText.substring(1, strText.length);

    while (strText.substring(strText.length-1,strText.length) == ' ')
	strText = strText.substring(0, strText.length-1);

    return strText;
}

function doRegister()
{
    var s="";
	if(trim(document.frmRegister.fname.value)==""){
		s=s+"The field \"First Name\" is empty!\r\n";
	}
	if(trim(document.frmRegister.lname.value)==""){
		s=s+"The field \"Last Name\" is empty!\r\n";
	}
	if(trim(document.frmRegister.public_id.value)==""){
		s=s+"The field \"Public Id\" is empty!\r\n";
	}
	if(trim(document.frmRegister.company.value)==""){
		s=s+"The field \"Company\" is empty!\r\n";
	}
	var rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-z]{2,4}$");
	if(!rx.test(document.frmRegister.email.value)){
		s=s+"Please, verify your email address!\r\n";
	}
	if(document.frmRegister.phone.value.replace(/[^0-9]/g, '').length!=10){
		s=s+"Please, verify your phone number!\r\n";
	}
	if(trim(document.frmRegister.mobile.value)==""){
		s=s+"The field \"Mobile\" is empty!\r\n";
	}
	if(trim(document.frmRegister.address1.value)==""){
		s=s+"The field \"Address\" is empty!\r\n";
	}
	if(trim(document.frmRegister.city.value)==""){
		s=s+"The field \"City\" is empty!\r\n";
	}
	if(trim(document.frmRegister.zip.value)==""){
		s=s+"The field \"Zip\" is empty!\r\n";
	}
	if(!document.frmRegister.agree.checked){
		s=s+"You must agree to our terms of use!\r\n";
	}
	if(s!=""){
		alert(s);
		return;
	}
	document.frmRegister.submit();
}

function doValidate()
{
    var s="";
	if(trim(document.frmData.txtName.value)==""){
		s=s+"The field \"Name\" is empty!\r\n";
	}
	if(trim(document.frmData.txtCompany.value)==""){
		s=s+"The field \"Company\" is empty!\r\n";
	}
	var rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-z]{2,4}$");
	if(!rx.test(document.frmData.txtEmail.value)){
		s=s+"Please, verify your email address!\r\n";
	}
	if(trim(document.frmData.txtState.value)==""){
		s=s+"The field \"State\" is empty!\r\n";
	}
	if(trim(document.frmData.txtDetails.value)==""){
		s=s+"Your message is empty!\r\n";
	}
	if(s!=""){
	alert(s);
	return;
	}
	document.frmData.submit();
}

function doStepOne()
{
    var s="";
	if(trim(document.sitef.address1.value)==""){
		s=s+"The field \"Property Address\" is empty!\r\n";
	}
	if(trim(document.sitef.domain.value)==""){
		s=s+"The field \"Domain Name\" is empty!\r\n";
	}
	if(trim(document.sitef.mls.value)==""){
		s=s+"MLS Code cannot be empty!\r\n";
	}
	if(s!=""){
	alert(s);
	return;
	}
	document.sitef.submit();
}

function popup(mylink, windowname)
{
	if (! window.focus)return true;
		var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;
	window.open(href, windowname, 'width=900,height=700,resizable=1,scrollbars=yes,toolbar=no,status=no,menubar=no,location=no');
	return false;
}

function openPic(img){
	if(!document.getElementById('popPic')){
		var popPic=document.createElement('img');
		popPic.id='popPic';
		document.body.appendChild(popPic);
		var popPicX=document.createElement('div');
		popPicX.id='popPicX';
		popPicX.innerHTML='CLOSE';
		document.body.appendChild(popPicX);
	}
	else{
		var popPic=document.getElementById('popPic');
		var popPicX=document.getElementById('popPicX');
	}

	popPic.src=img;
	popPic.style.display='block';
	popPic.onload=function(){
		var l=(document.documentElement.clientWidth-this.width)/2;
		this.style.left=l>=0?l+'px':'0px';
		var t=document.documentElement.scrollTop+(document.documentElement.clientHeight-this.height)/2;
		this.style.top=t>=0?t+'px':'0px';
		// close image
		var x=document.getElementById('popPicX');
//		x.style.width=this.width+'px';
		x.style.left=this.style.left;
		x.style.top=(t+this.height+9)+'px';
		x.style.display='block';
	}
	popPic.onclick=function(){
		this.style.display='none';
		document.getElementById('popPicX').style.display='none';
	}
	popPicX.onclick=function(){
		this.style.display='none';
		document.getElementById('popPic').style.display='none';
	}
}