/// version 1.1  accepts an option Object as a single parameter;



function mailForm(optObj){
	CSSRule ('.mailForm',{border:'1px solid black',backgroundColor:'#f8f8f8',padding:'6px'},{insertOnly:true});
	CSSRule ('.e_pop td', {verticalAlign:'middle',fontSize:'12px',fontFamily:'verdana'},{insertOnly:true});
	CSSRule ('.e_pop table', {color:'black',fontSize:'12px'},{insertOnly:true});
	CSSRule ('.fLink', {cursor:'pointer',color:'blue',textDecoration:'underline'},{insertOnly:true});
	CSSRule ('.ast', {color:'red',fontWeight:'bold'},{insertOnly:true});
	var that = $(document.createElement("div")); 
	var defaultObj = {
		msgPrompt:'Enter your message or inquiry here.'
	}

	Object.extend(that,defaultObj);
	if (optObj)
		Object.extend(that,optObj);
	
	that.className = 'mailForm e_pop';

	var t = [
		'<div style="text-align:center;"><span style="font-size:14px;font-weight:bold;border-bottom:1px solid black;">Request More Information</span></div>'
		,'<div style="font-size:10px;padding-bottom:12px;text-align:center;font-style:italic">Items marked with <span style="color:red;font-weight:bold;">*</span> are Required</div>'
		,'<table cellspacing=0 style="font-family:Verdana,Arial;font-size:11px;vertical-align:top;">'
		,'<tr><td></td><td>To:</td><td id="mailTo">'+optObj.sendTo+'</td></tr>'
		,'<tr><td style="color:red;font-weight:bold;">*</td><td>Your Name:</td><td><input id="e_name" size="37" maxlength="50" onkeydown="return checkTab(this,event)"></td></tr>'
		,'<tr><td style="color:red;font-weight:bold;">*</td><td>Your Email:</td><td><input id="e_email" size="37" maxlength="50" onkeydown="return checkTab(this,event)" ></td></tr>'
		,'<tr><td style="color:red;font-weight:bold;">*</td><td>Confirm Email:</td><td><input id="e_email2" size="37" maxlength="50" onpaste="this.value=\'\';return false" onkeydown="return checkTab(this,event)"></td></tr>'
		,'<tr><td></td><td>Your Phone:</td><td><input id="e_phone" size="37" maxlength="30" onkeyup="toggleBT(this)" onkeydown="return checkTab(this,event)"></td></tr>'
		,'<tr id="btRow" style="display:none"><td></td><td style="font-size:9px;">Best Time to Call:</td><td><input id="e_besttime" size="37" maxlength="50" onkeydown="return checkTab(this,event)"></td></tr>'
		,'<tr><td></td><td colspan=2><input type="checkbox" id="haveAgent" value="true" onclick="toggleAgent(this);"><label for="haveAgent">I am currently working with a real estate agent.</label> </td></tr>'
		,'<tr id="agRow" style="display:none"><td></td><td>Agent Name:</td><td><input type="text" id="myAgent" size="37" maxlength="50"> </td></tr>'
		,'<tr><td></td><td>Subject:</td><td id="subj">'+optObj.subject+'</td></tr>'
		,'<tr><td style="color:red;font-weight:bold;vertical-align:top">*</td><td colspan="2"><textarea rows="5" cols="40" id="e_msg" onkeydown="return checkTab(this,event)" onfocus="clearPrompt(this)" onblur="insertPrompt(this)" style="color:blue">'+that.msgPrompt+'</textarea></td></tr>'
		,'<tr><td></td><td colspan="2"><input id="sButton" type="button" onclick="sendEmail(this)" value="Send" onkeydown="return checkTab(this,event)"> '
		,'' // item 14;
		,'</td></tr></table>'
		]

	if (!optObj.insertElm)
		t[14] = '<input id="cButton" type="button" onclick="resetEmail(this)" value="Cancel" onkeydown="return checkTab(this,event)">';

	that.innerHTML = t.join("");

	if (optObj.divStyle)
		that.setStyle(optObj.divStyle);
	if (optObj.tableStyle)
		$(that.getElementsByTagName('table')[0]).setStyle(optObj.tableStyle);
		
		
	that.name = 'mailForm';
	if (optObj.mailFormStyles)
		that.setStyle(optObj.mailFormStyles)
		
	if (optObj.insertElm){
		//Object.extend(that.style,{backgroundColor:'white',border:'0px'})
		optObj.insertElm.appendChild(that);
	}
	else {
		that.style.position='absolute';
		document.body.appendChild(that);
	}
	
	var inputs = that.getElementsByTagName('input')
	that.firstInput = inputs[0];
	that.lastInput = inputs[inputs.length-1];
	
	that.e_name = getElmById('e_name',that)
	that.e_email = getElmById('e_email',that)
	that.e_email2 = getElmById('e_email2',that)
	that.e_phone = getElmById('e_phone',that)
	that.e_besttime = getElmById('e_besttime',that)
	that.cButton = getElmById('cButton',that)
	that.sButton = getElmById('sButton',that)
	that.myAgent = getElmById('myAgent',that)
	that.mailTo = getElmById('mailTo',that)
	that.haveAgent = getElmById('haveAgent',that)
	that.subj = getElmById('subj',that)
	
	//alert(that.subj.value)
	that.agRow = getElmById('agRow',that)
	that.btRow = getElmById('btRow',that)
	
	that.e_msg = that.getElementsByTagName('textarea')[0];

		
	that.e_name.value = readCookie('e_name') || '';
	that.e_email.value = readCookie('e_email') || '';
	that.e_email2.value = readCookie('e_email') || '';
	that.e_phone.value = readCookie('e_phone') || '';
	that.e_besttime.value = readCookie('e_besttime') || '';
	that.myAgent.value = readCookie('myAgent') || '';
	//alert(readCookie('haveAgent'))
	
	if (!readCookie('haveAgent') || readCookie('haveAgent')=='false')
		that.haveAgent.checked = false;
	else
		that.haveAgent.checked = true;
	
	toggleAgent(that.haveAgent);
	toggleBT(that.btRow);

	that.setUser = function (user_id,sendTo){
		that.user_id = user_id;
		that.mailTo.innerHTML = sendTo;
	}

	return that
}

function getMyObj(elm){
	while (elm.name != 'mailForm')
		elm = elm.parentNode;
	return elm;
}

function clearPrompt(elm){
	elm.style.color = 'black';
	if (elm.value == getMyObj(elm).msgPrompt){
		elm.value = '';
	}
}

function insertPrompt(elm){
	if (trim(elm.value) == ''){
		elm.value = getMyObj(elm).msgPrompt;
		elm.style.color = 'blue';
	}
}



function toggleAgent(elm){
	var myObj = getMyObj(elm)
	if (!elm.checked){
		myObj.agRow.style.display='none';
	}
	else {
    	if(window.ActiveXObject) 
			myObj.agRow.style.display='block';
		else if(window.XMLHttpRequest) 
			myObj.agRow.style.display='table-row';
	}
}
function toggleBT(elm){
	var myObj = getMyObj(elm)
	if (elm.value == "")
		myObj.btRow.style.display='none';
	else {
    	if(window.ActiveXObject) 
			myObj.btRow.style.display='block';
		else if(window.XMLHttpRequest) 
			myObj.btRow.style.display='table-row';
	}
}


function mailto(optObj){
	if (!window.myMailForm){
		myMailForm = mailForm(optObj)
	}
	
	Object.extend(myMailForm,optObj)
	myMailForm.setUser(optObj.user_id,optObj.sendTo);
	myMailForm.style.visibility='visible';
	myMailForm.subj.innerHTML = optObj.subject;

	toggleBT(myMailForm.e_phone);
	toggleAgent(myMailForm.haveAgent);
	insertPrompt(myMailForm.e_msg)
	pushForm(myMailForm);
	return;
	
}
function checkTab(elm,event){
	var myObj = getMyObj(elm)
	if (event.keyCode != 9)
		return specChars(event)

	if (elm == myObj.firstInput && event.shiftKey){
		myObj.lastInput.focus()
		return false
	}
	if (elm == myObj.lastInput && !event.shiftKey){
		myObj.firstInput.focus()
		return false
	}
	return specChars(event)
}
function specChars(event){
	if (event.keyCode > 52 && event.keyCode < 56 & event.shiftKey)
		return false

}

function resetEmail(elm){
	var myObj = getMyObj(elm);
	myObj.e_msg.value = '';
	if (myObj.style.position == 'absolute')
		popForm();
}


function sendEmail(elm){
	var myObj = getMyObj(elm);
	var eMsg = new Array("Please fix the following errors...");
	if (trim(myObj.e_name.value)=="")
		eMsg[eMsg.length] = "* Your Name is Required.";
	if (trim(myObj.e_email.value)=="")
		eMsg[eMsg.length] = "* Your Email Address is Required.";
	else if (!testEmail(trim(myObj.e_email.value)))
		eMsg[eMsg.length] = "* The Email address entered is not valid.";
	else if (trim(myObj.e_email2.value)=="")
		eMsg[eMsg.length] = "* Please re-type your email in the 'Confirm Email' field.";
	else if (trim(myObj.e_email.value)!=trim(myObj.e_email2.value))
		eMsg[eMsg.length] = "* The 'Confirm' email does not match 'Your' email.";
	if (trim(myObj.e_msg.value)=="" || trim(myObj.e_msg.value)==myObj.msgPrompt)
		eMsg[eMsg.length] = "* Please type a message to send.";
	if (eMsg.length > 1){
		alert(eMsg.join("\n\n"))
		return;
	}

	if (!window.did)
		var did = '';
		
	var a = [
		'/wwwroot/lib/mailto/1.1/email.cfm?did='+did
		,'&e_name='+myObj.e_name.value
		,'&e_phone='+myObj.e_phone.value
		,'&e_email='+myObj.e_email.value
		,'&e_besttime='+myObj.e_besttime.value
		,'&e_msg='+myObj.e_msg.value
		,'&search='+escape(document.location.search)
		,'&pathname='+escape(document.location.pathname)
		,'&myAgent='+myObj.myAgent.value
		,'&haveAgent='+myObj.haveAgent.checked
	]
	
	if (myObj.boardid)
		a.push('&boardid='+myObj.boardid);
	if (myObj.idxcomp_id)
		a.push('&idxcomp_id='+myObj.idxcomp_id);
	else if (myObj.idxuser_id)
		a.push('&idxuser_id='+myObj.idxuser_id);
	else if (myObj.user_id)
		a.push('&user_id='+myObj.user_id);
		
	if (myObj.classid)	
		a.push('&classid='+myObj.classid);
	else if (myObj.propid)	
		a.push('&propid='+myObj.propid);

	var urlstring = a.join('');
	
	new Ajax.Request(urlstring)
	createCookie('e_name',myObj.e_name.value,365) ;
	createCookie('e_email',myObj.e_email.value,365) ;
	createCookie('e_phone',myObj.e_phone.value,365) ;
	createCookie('e_besttime',myObj.e_besttime.value,365) ;
	createCookie('myAgent',myObj.myAgent.value,365) ;
	createCookie('haveAgent',myObj.haveAgent.checked,365);
	resetEmail(elm);
}



function forward(obj){
	urlstring = '/wwwroot/remaint/ajax/forward.cfm?did='+did;
	urlstring += '&from_search='+escape(document.location.search);
	urlstring += '&from_pathname='+escape(document.location.pathname);
	urlstring += '&to_url='+escape(obj.url);
	if (obj.user_id)
		urlstring += '&user_id='+obj.user_id;
	if (obj.comp_id)
		urlstring += '&comp_id='+obj.comp_id;
	if (obj.classid)
		urlstring += '&classid='+obj.classid;
	if (obj.adid)
		urlstring += '&adid='+obj.adid;
	
	/*initializeBoxes();
	myAppBox.innerHTML = urlstring;
	myAppBox.style.visibility='visible';
	centerbox(myAppBox)
	return;*/
	
	if(window.XMLHttpRequest) 
		var myxmlhttp = new XMLHttpRequest();
    else if(window.ActiveXObject) 
		var myxmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	myxmlhttp.open("POST", urlstring,true);
	myxmlhttp.send(null)
	var mystring = 'document.location = "'+obj.url+'"';
	//alert(mystring);
	setTimeout(mystring,50)
}
