function swapImg(img, file) {
	img.src = '/images/'+file+'.gif';
}

function popWin(url, id) {
	var newWin;
	newWin = window.open(url+'?id='+id, 'popped', 'width=460,height=350,resizeable=yes,scrollbars=yes');
	newWin.focus();
}

function popTour(url) {
	var newWin;
	newWin = window.open(url, 'tour', 'width=420,height=410,resizeable=yes,scrollbars=no');
	newWin.focus();
}

function updStatus(fld, id) {
	window.location.href='preview.status.php?id='+id+'&listing_status='+fld.options[fld.selectedIndex].value;
}

function floor(number) {
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function dosum() {
 	string = document.temps.LA.value;
	rExp = /,/gi;
	document.temps.LA.value = string.replace(rExp, '');
 	var mi = document.temps.IR.value / 1200;
  var base = 1;
  var mbase = 1 + mi;
  
	if (document.temps.YR.value == "int") {
		var mp = ((document.temps.LA.value * (document.temps.IR.value/100))/12);
		document.temps.MP.value = floor(mp);
		
	} else {	
		for (i=0; i<document.temps.YR.value * 12; i++)
		{
			base = base * mbase
		}
		document.temps.PI.value = floor(document.temps.LA.value * mi / ( 1 - (1/base)))
		document.temps.MT.value = floor(document.temps.AT.value / 12)
		document.temps.MI.value = floor(document.temps.AI.value / 12)
		var dasum = document.temps.LA.value * mi / ( 1 - (1/base)) +
					document.temps.AT.value / 12 + 
					document.temps.AI.value / 12;
		document.temps.MP.value = floor(dasum);
	}
}


var rotate_delay = 5000; // delay in milliseconds (5000 = 5 secs)
current = 0;
function next() {
if (document.slideform.slide[current+1]) {
document.images.show.src = document.slideform.slide[current+1].value;
document.slideform.slide.selectedIndex = ++current;
   }
else first();
}
function previous() {
if (current-1 >= 0) {
document.images.show.src = document.slideform.slide[current-1].value;
document.slideform.slide.selectedIndex = --current;
   }
else last();
}
function first() {
current = 0;
document.images.show.src = document.slideform.slide[0].value;
document.slideform.slide.selectedIndex = 0;
}
function last() {
current = document.slideform.slide.length-1;
document.images.show.src = document.slideform.slide[current].value;
document.slideform.slide.selectedIndex = current;
}
function ap(text) {
document.slideform.slidebutton.value = (text == "Stop") ? "Start" : "Stop";
rotate();
}
function change() {
current = document.slideform.slide.selectedIndex;
document.images.show.src = document.slideform.slide[current].value;
}
function rotate() {
if (document.slideform.slidebutton.value == "Stop") {
current = (current == document.slideform.slide.length-1) ? 0 : current+1;
document.images.show.src = document.slideform.slide[current].value;
document.slideform.slide.selectedIndex = current;
window.setTimeout("rotate()", rotate_delay);
   }
}

// TOURS ---------------------------------------------
function open_tour() {
var tour = document.getElementById("tour_applet");
var tourCode = 
	"<applet code=\"panoapplet\" archive=\"http://www.circlepix.com/includes/java/panoapplet.jar\" width=320 height=240>"
+ "<param name=\"file\" value=\"http://www.circlepix.com/site/tour/vt.ivr.php?file="+document.slideform.tour_slide[0].value+"\">"
+ "<param name=\"initialView\" VALUE=\"0.000000, 0.000005, 50.000005\">"
+ "<param name=\"altSplash\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"autospin\" value=\"-80\">"
+ "<param name=\"infoPage\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"minZoomAngle\" value=\"0\">"
+ "<param name=\"toolbar\" value=\"on\">"
+ "<param name=\"toolbarstyle\" value=\"floating\">"
+ "<param name=\"backgroundColor\" value=\"40, 44, 112\">"
+ "</applet>";
tour.innerHTML = tourCode;
document.slideform.tour_slide.selectedIndex = ++current;
}

function next_tour() {
if (document.slideform.tour_slide[current+1]) {
var tour = document.getElementById("tour_applet");
var tourCode = 
	"<applet code=\"panoapplet\" archive=\"http://www.circlepix.com/includes/java/panoapplet.jar\" width=320 height=240>"
+ "<param name=\"file\" value=\"http://www.circlepix.com/site/tour/vt.ivr.php?file="+document.slideform.tour_slide[current+1].value+"\">"
+ "<param name=\"initialView\" VALUE=\"0.000000, 0.000005, 50.000005\">"
+ "<param name=\"altSplash\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"autospin\" value=\"-80\">"
+ "<param name=\"infoPage\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"minZoomAngle\" value=\"0\">"
+ "<param name=\"toolbar\" value=\"on\">"
+ "<param name=\"toolbarstyle\" value=\"floating\">"
+ "<param name=\"backgroundColor\" value=\"40, 44, 112\">"
+ "</applet>";
tour.innerHTML = tourCode;
document.slideform.tour_slide.selectedIndex = ++current;
   }
else first_tour();
}
function previous_tour() {
if (current-1 >= 0) {
var tour = document.getElementById("tour_applet");
var tourCode = 
	"<applet code=\"panoapplet\" archive=\"http://www.circlepix.com/includes/java/panoapplet.jar\" width=320 height=240>"
+ "<param name=\"file\" value=\"http://www.circlepix.com/site/tour/vt.ivr.php?file="+document.slideform.tour_slide[current-1].value+"\">"
+ "<param name=\"initialView\" VALUE=\"0.000000, 0.000005, 50.000005\">"
+ "<param name=\"altSplash\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"autospin\" value=\"-80\">"
+ "<param name=\"infoPage\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"minZoomAngle\" value=\"0\">"
+ "<param name=\"toolbar\" value=\"on\">"
+ "<param name=\"toolbarstyle\" value=\"floating\">"
+ "<param name=\"backgroundColor\" value=\"40, 44, 112\">"
+ "</applet>";
tour.innerHTML = tourCode;
document.slideform.tour_slide.selectedIndex = --current;
   }
else last_tour();
}
function first_tour() {
current = 0;
var tour = document.getElementById("tour_applet");
var tourCode = 
	"<applet code=\"panoapplet\" archive=\"http://www.circlepix.com/includes/java/panoapplet.jar\" width=320 height=240>"
+ "<param name=\"file\" value=\"http://www.circlepix.com/site/tour/vt.ivr.php?file="+document.slideform.tour_slide[0].value+"\">"
+ "<param name=\"initialView\" VALUE=\"0.000000, 0.000005, 50.000005\">"
+ "<param name=\"altSplash\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"autospin\" value=\"-80\">"
+ "<param name=\"infoPage\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"minZoomAngle\" value=\"0\">"
+ "<param name=\"toolbar\" value=\"on\">"
+ "<param name=\"toolbarstyle\" value=\"floating\">"
+ "<param name=\"backgroundColor\" value=\"40, 44, 112\">"
+ "</applet>";
tour.innerHTML = tourCode;
document.slideform.tour_slide.selectedIndex = 0;
}
function last_tour() {
current = document.slideform.tour_slide.length-1;
var tour = document.getElementById("tour_applet");
var tourCode = 
	"<applet code=\"panoapplet\" archive=\"http://www.circlepix.com/includes/java/panoapplet.jar\" width=320 height=240>"
+ "<param name=\"file\" value=\"http://www.circlepix.com/site/tour/vt.ivr.php?file="+document.slideform.tour_slide[current].value+"\">"
+ "<param name=\"initialView\" VALUE=\"0.000000, 0.000005, 50.000005\">"
+ "<param name=\"altSplash\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"autospin\" value=\"-80\">"
+ "<param name=\"infoPage\" value=\"http://www.circlepix.com/images/loading.gif\">"
+ "<param name=\"minZoomAngle\" value=\"0\">"
+ "<param name=\"toolbar\" value=\"on\">"
+ "<param name=\"toolbarstyle\" value=\"floating\">"
+ "<param name=\"backgroundColor\" value=\"40, 44, 112\">"
+ "</applet>";
tour.innerHTML = tourCode;
document.slideform.tour_slide.selectedIndex = current;
}

function popImg() {
	url = unescape(document.images.show.src);
	var imgWin;
	imgWin = window.open('/buyer/image_pop.php?img='+url+'&h='+screen.availHeight+'&w='+screen.availWidth, 'img_pop', 'status=no,width=100,height=100,resizable=yes');
	imgWin.focus();
}

function enable_radio(fld1, fld2) {
	fld1.disabled = true;
	fld2.disabled = true;
}

function disable_radio(fld1, fld2) {
	fld1.disabled = false;
	fld2.disabled = false;
}

function add(fld, increment) {
	ct = parseFloat(fld.value);
	if (isNaN(ct)) ct = 0;
	inc = parseFloat(increment);
	
	ct = ct + inc;
	fld.value = ct;
}

function subtract(fld, increment) {
	ct = parseFloat(fld.value);
	if (isNaN(ct)) ct = 0;
	inc = parseFloat(increment);
	
	if (ct > 1) {
		ct = (ct - inc);
		fld.value = ct;
	}
}
//This is where the code was fucked... i took out some code which i think fixed everything
function status_view(thediv) {
	var allcontent = Array('price','range','empty');
	
	if (thediv == "price") {
		document.listingForm.price.disabled=false;
		document.listingForm.price_low.disabled=true;
		document.listingForm.price_high.disabled=true;
	}
	if (thediv == "range") {
		document.listingForm.price.disabled=true;
		document.listingForm.price_low.disabled=false;
		document.listingForm.price_high.disabled=false;
	}
	
	/*for (i=0; i<allcontent.length; i++) {
		var div_name = allcontent[i] + "_div";
		
		if (allcontent[i] == thediv) {
			document.getElementById(div_name).style.visibility = "visible";
			document.getElementById(div_name).style.display = "block";
		} else {
			document.getElementById(div_name).style.visibility = "hidden";
			document.getElementById(div_name).style.display = "none";
		}
	}*/
}

function media_swap(thediv) {
	var xcontent = Array('emptymedia','photo','tour');
	
	for (i=0; i<xcontent.length; i++) {
		var div_name = xcontent[i] + "_div";
		
		if (xcontent[i] == thediv) {
		if (xcontent[i] == "tour") {
			first_tour();
		}
			document.getElementById(div_name).style.visibility = "visible";
			document.getElementById(div_name).style.display = "block";
		} else {
			document.getElementById(div_name).style.visibility = "hidden";
			document.getElementById(div_name).style.display = "none";
		}
	}
}

function set_price() {
	var final_price;
	
	str = document.forms[0].calc_price.value;
	numstr = str.replace(/\$|,/g, "");
	document.forms[0].calc_realtor.value = Math.floor(Number(numstr) * 0.06);
	
	final_price = Number(document.forms[0].calc_realtor.value) - Number(document.forms[0].calc_total.value);
	
	price_html = document.getElementById('final_price_span');
	price_html.innerHTML = InsertCommas(final_price);
}

function swap_calculator(thediv) {
	var allcontent = Array('pricebox','calculation');
	
	if (thediv == "calculation") {
		str = document.forms[0].calc_price.value;
		numstr = str.replace(/\$|,/g, "");
		document.forms[0].calc_realtor.value = Math.floor(Number(numstr) * 0.05);
		
		set_price();
	}
	
	for (i=0; i<allcontent.length; i++) {
		var div_name = allcontent[i] + "_div";
		
		if (allcontent[i] == thediv) {
			document.getElementById(div_name).style.visibility = "visible";
			document.getElementById(div_name).style.display = "block";
		} else {
			document.getElementById(div_name).style.visibility = "hidden";
			document.getElementById(div_name).style.display = "none";
		}
	}
}

function swap_package(thediv, price) {
	var allcontent = Array('package_1','package_2','package_3','package_4');
	
	for (i=0; i<allcontent.length; i++) {
		var div_name = allcontent[i];
		
		if (allcontent[i] == thediv) {
			document.forms[0].calc_total.value = Number(document.forms[0].calc_total.value) - Number(document.forms[0].calc_package.value);
			document.forms[0].calc_package.value = price;
			document.forms[0].calc_total.value = Number(document.forms[0].calc_total.value) + Number(document.forms[0].calc_package.value);
			document.getElementById(div_name).style.visibility = "visible";
			document.getElementById(div_name).style.display = "block";
		} else {
			document.getElementById(div_name).style.visibility = "hidden";
			document.getElementById(div_name).style.display = "none";
		}
	}
	
	set_price();
}

function swap_service(chkbox, thediv, price) {
	
	if (chkbox.checked == true) {
		document.forms[0].calc_service.value = price;
		document.forms[0].calc_total.value = Number(document.forms[0].calc_total.value) + Number(document.forms[0].calc_service.value);
		document.getElementById(thediv).style.visibility = "visible";
		document.getElementById(thediv).style.display = "block";
	} else {
		document.forms[0].calc_service.value = price;
		document.forms[0].calc_total.value = Number(document.forms[0].calc_total.value) - Number(document.forms[0].calc_service.value);
		document.getElementById(thediv).style.visibility = "hidden";
		document.getElementById(thediv).style.display = "none";
	}
	
	set_price();
}

function InsertCommas(x) {

var x=x;

x+="";

iLen=x.length;

pos=x.indexOf(".");

if (pos>-1) //there are decimals

{

iLen=pos;

}

//add the decimal point

temp="";

//add the decimal part to begin

// with 45.123, we add the .123

temp=x.substring(iLen,x.length);

//iLen-1 is the rightmost non-decimal digit (5 in 98745.123)

for (var i=iLen-1;i>=0;i--)

//we add a separator when the expression (iLen-i-1)%3==0 is true...

//except when i is (iLen-1), or the first digit

//eg (98745.12). i is iLen-1, and the digit pos is next the decimal, 

//it is 5. From here, we decrement i...iLen-2, iLen-3, iLen-4 ... when i is a multiple of

//3, (i=iLen-iLen+4-1). This point is just before the number 7

if ((iLen-i-1)%3==0&&i!=iLen-1)

temp=x.charAt(i)+","+temp;

else

temp=x.charAt(i)+temp;

return temp;

}//end of addseps(x)


function viewListing(id) {
	if (id.length > 0) {
		window.location.href='/buyer/homes/'+id+'.php';
	} else {
		alert("You must enter an ID!");
	}
}

















/* ------------------ validation --------------------- */

function isEmail(fld, name) {
	if (fld.value.length) {
		var isEmail = ((fld.value.indexOf("@") != -1) && (fld.value.indexOf(".") != -1));
		if (!isEmail) {
			alert(name+' does not appear to be a valid format.  E-mail addresses must by in the xxx@xxx.com format.');
			fld.focus();
			fld.select();
			return false;
		}
	}
	
	return true;
}

function isNumeric(fld, name) {
	if (fld.value.length) {
		if (isNaN(fld.value)) { 
			alert(name+' must be a number.  Please do not include commas or non-numeric characters in your submission.');
			fld.focus();
			fld.select();
			return false;
		}
	}
	
	return true;
}

function isMoney(fld, name, xmatch) {
	// strip all the currency stuff
	money = _removeCurrency(fld.value);
	if (fld.value.length) {
		if (isNaN(money) || !money.length) {
			alert(name+' appears to be an invalid money format.  Please do not include commas or dollar signs in your submission.');
			fld.focus();
			fld.select();
			return false;
		} else {
			fld.value = money;
		}

	}
	
	return true;
}

function isRequired(fld, name) {
	if (fld.disabled) return true;
	if (!fld.value.length) {
		alert(name+' is a required field.');
		fld.focus();
		if (!fld.options) {
			fld.select();
		}
		return false;
	}
	
	return true;
}

function isChecked(fld, name) {
	if (fld.disabled) return true;
	if (!fld.checked == true) {
		alert(name+' must be checked.');
		fld.focus();
		if (!fld.options) {
			fld.select();
		}
		return false;
	}
	
	return true;
}

function isMinimum(fld, name, minimum) {
	if (fld.disabled) return true;
	if (fld.value.length < minimum) {
		alert(name+' requires a minimum character count of '+minimum);
		fld.focus();
		fld.select();
		return false;
	}
	return true;
}

function isRequiredSelect(fld, name) {
	if (fld.disabled == true) return true;
	if (fld.options[fld.selectedIndex].value == '') {
		alert(name+ ' is a required field.');
		fld.focus();
		return false;
	} else {
		//alert(fld.options[fld.selectedIndex].value);
	}

	return true;

}

function isDate(fld, name) {
	if (fld.value.length) {
	
		date = _validateUSDate(fld.value);
		
		if (!date.length) {
			alert(name+' appears to be invalid.  Please use the mm/dd/yyyy format.');
			fld.focus();
			fld.select();
			return false;
		} else {
			fld.value = date;
		}

	}
	
	return true;
}

function isIdentical(fld1, fld2, label1, label2) {

	if (fld1.value !== fld2.value) {
		alert(label1+' does not match '+label2+'.');
		fld2.focus();
		fld2.select();
		return false;
	} else {
		if (!isRequired(fld1, label1)) return false;
	}
	
	return true;
}

function isReserved(fld, name, value) {

	if (fld.value.length) {
		reserved = Array('guest','admin');
	
		val = fld.value.toLowerCase();
		val = escape(val);
		
		for (i=0;i<reserved.length;i++) {
			if (value != reserved[i]) {
				if (val == reserved[i]) {
					if (val != value) {
						alert(name+' is a reserved word and cannot be used!');
						fld.focus();
						fld.select();
						return false;
					}
				}
			} else {
				fld.value = value;
			}
		}
	
		if (val.indexOf('%20', 0) >= 0) {
			alert(name+' must not contain spaces.');
			fld.focus();
			fld.select();
			return false;
		}

	}
	
	// if (!isRequired(fld, name)) return false;
	
	return true;
}





function _removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';

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

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

function _validateUSDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2,4}$/

  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return ''; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) //find date separator
		if (!isNaN(strSeparator)) {
			var strSeparator = strValue.substring(1,2) //find date separator
		}
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = arrayDate[1]; // if (intDay < 10) { intDay = '0' + intDay; }
    var intYear = arrayDate[2]; 
    var intMonth = arrayDate[0]; // if (intMonth < 10) { intMonth = '0' + intMonth; }

    if (intYear == '00') { intYear = '2000'; } else if (intYear < 50) { intYear = parseInt(intYear) + 2000; } else if (intYear > 50 && intYear < 100) { intYear = parseInt(intYear) + 1900; }
    
    //check if month value and day value agree
    if(arrayLookup[intMonth] != null) {
      if(intDay <= arrayLookup[intMonth] && intDay != 0)
        return intMonth+'/'+intDay+'/'+intYear; //found in lookup table, good date
    }

    //check for February
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return intMonth+'/'+intDay+'/'+intYear; //Feb. had valid number of days
  }
  return ''; //any other values, bad date
}