var expDays = 1;
var expd = new Date(); 
expd.setTime(expd.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset)
 {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
		endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) 
{  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) 
		{    
			var j = i + alen;    
			if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
			i = document.cookie.indexOf(" ", i) + 1;    
			if (i == 0) break;   
		}  
	return null;
}

function SetCookie (name, value) 
{  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
}


function DeleteCookie (name) 
{  
	var expd = new Date();  
	expd.setTime (expd.getTime() - 1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + expd.toGMTString();
}

function CookiesEnabled()
{
	SetCookie("FSBOCookieTest", "1", expd);
	if (GetCookie("PBCookieTest") != null)
		{
			DeleteCookie("FSBOCookieTest");
			if (navigator.appName !="Netscape")
			{
				if (navigator.cookieEnabled==true) return true; 
				else return false;
			}
			else return true;
		}
	else
		return false;		
}
	
function AlertCookieTest()
{
		var result;
		result = CookiesEnabled();
		if(!result) {
			alert("This section of the site requires that cookies are enabled.");
		}
}
/* opens a popup window */
function openPopupWindow(theURL,winName,features) { 
	var s = window.open(theURL,winName,features);
	if(s) s.focus();
}

function Manage(what,wname){
	displayWindow(what, wname, 622, 600, "resize=yes,scrollbars=yes,resizable=yes");
}

function OpenPopup(what,wname,width, height){
	displayWindow(what, wname, width, height, "resize=yes,scrollbars=yes,resizable=yes");
}

function ShowDescription(code,fid){
	displayWindow('plan_description.php?code='+code+'&fid='+fid, 'Description', 500, 300, "resize=yes,scrollbars=yes,resizable=yes");
}

function ShowWaitWindow(){
	displayWindow('common/wait.html', 'Wait', 300, 100, "resize=no,scrollbars=no,resizable=no");
}

function ShowAdminWaitWindow(){
	displayWindow('../common/wait.html', 'Wait', 300, 100, "resize=no,scrollbars=no,resizable=no");
}

function Disclaimer(){
	displayWindow('disclaimer.php', 'disclaimer', 400, 280, "resize=yes,scrollbars=no,resizable=yes");
}

function CloseWaitWindow()
{
 	if  (Wait != null)
 	{
 		if (Wait.opened)
 		{
 			Wait.close();
 		}
 	}	
}

function windowClose(){
	window.close()
}

function displayWindow(theURL,winName,width,height,features) {
    var window_width = width;
    var window_height = height;
    var newfeatures = features;
    var window_top = (screen.height-window_height)/2-20;
    var window_left = (screen.width-window_width)/2;
    newWindow=window.open(''+ theURL + '',''+ winName + '','width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ',' + newfeatures + '');
    newWindow.focus();
}

function OpCancel()
{
	window.close();
	return true;
}

function CloseWnd()
{
	if(opener != null)
		opener.location.href = opener.location.href;
	window.close();
}


function validatedelete(item)
{
	return confirm("Are you sure you want to " + item + "?");
}

function enableschedule(thisForm)
{
	if (sale2.house_is_opened.checked == true)
	{
                document.sale2.house_schedule.disabled = false;
	}
	else
	{
	        document.sale2.house_schedule.disabled = true;
	}
}

function addSeparatorsNF(nStr, inD, outD, sep)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}

function removeCurrency( strValue ) 
{
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function rewritePage() 
{
        var plan, price, rlength = 2;
        
              
        if (saving.price.length=0)
        {
                price = 0;
        }
        else if (!checknumber(removeCurrency(saving.price.value)))
        {
                alert('Invalid number!');
                saving.price.value = '';
                return;
        }
        else
        {
                price = removeCurrency(saving.price.value);
        }
        
        for (i=0; i<saving.paymentplan.length; i++)
        {
                if(saving.paymentplan[i].checked)
                {
                        plan = saving.paymentplan[i].value;
                }
        }
	saving.price.value=addSeparatorsNF(price, '.', '.', ',');
        saving.realtor.value=addSeparatorsNF(Math.round((price*0.06)*Math.pow(10,rlength))/Math.pow(10,rlength), '.', '.', ',');
        saving.savings.value=addSeparatorsNF(Math.round((price*0.06-plan)*Math.pow(10,rlength))/Math.pow(10,rlength), '.', '.', ',');
}

function ShowEmailForm(tip, id, prefix)
{
	displayWindow(prefix+'business_contact.php?tip=' + tip + '&id=' + id, 'contact', 450, 300, "resize=yes,scrollbars=yes,resizable=yes");
}


function ShowMoneySave()
{
	displayWindow('money_save.php', 'money', 410, 240, "resize=yes,scrollbars=yes,resizable=yes");
}

function ShowPicturesPopup(id, prefix, pid)
{
	displayWindow(prefix+'common/pictures_popup.php?recID='+id+'&id='+pid, 'pictures_popup', 500, 500, "resize=yes,scrollbars=yes,resizable=yes");
}

function ShowMCalculator(prefix, price)
{
	displayWindow(prefix + 'mortgage_calculator.php?price=' + price, 'mc', 560, 370, "resize=yes,scrollbars=yes,resizable=yes");
}

function ShowReferForm(prefix, id)
{
	displayWindow(prefix + 'email_house.php?id=' + id, 'emailhouse', 450, 440, "resize=yes,scrollbars=yes,resizable=yes");
}

function ForgotPwd()
{
	displayWindow('forgot_password.php', 'pwd', 410, 280, "resize=no,scrollbars=yes,resizable=no");
}

function  ValidateLogin(thisForm)
{
	if  (!hasValue(thisForm.username, "TEXT" ))
		if  (!onError(thisForm.username, "Please enter your username."))
			return false;

	
	if  (!hasValue(thisForm.password, "TEXT" ))
		if  (!onError(thisForm.password, "Please enter your password."))
			return false;
	return true;
}

function billingSame()
{
     if (document.getElementById("billing_same") != null) {
        var company    = document.getElementById('company').value;
        var address    = document.getElementById('address').value;       
        var city       = document.getElementById('city').value;       
        var zip_code   = document.getElementById('zip_code').value;       
        var state      = document.getElementById('state').value;       
        var phone      = document.getElementById('phone').value;       
        
        if (document.getElementById("billing_same").checked == true) {
            document.getElementById('billing_name').value       = company;
            document.getElementById('billing_address').value    = address;
            document.getElementById('billing_city').value       = city;       
            document.getElementById('billing_zip_code').value   = zip_code;             
            document.getElementById('billing_state').value      = state;       
            document.getElementById('billing_phone').value      = phone;       
        }
    } //end if
} //end function billingSame

