/* - Custom JS Scripts -----------------------------------
Functions Outlined Below
* printversion(template)
* reorder(orderBy)
* popUpBox(linkInfo, boxName)
* InspectBox(linkInfo, boxName)
* PictureBox(linkInfo, size)
* SizeDropdown - Split single size dropdown into 2 variables
---------------------------------------------------------- */

// Print Version Script used from OFI, Aution and Search Pages
function printversion(template){
	var newurl = location.href;
	//tmpl check and replace
	if(newurl.indexOf('tmpl=') > 0){
		if(newurl.indexOf('&tmpl=&') > 0){
			newurl = newurl.replace('&tmpl=&', '&tmpl='+template+'_printlist'+'&');	
		}else{
			newurl = newurl.replace('tmpl='+template, 'tmpl='+template+'_printlist');						
		}
	}else{
		newurl = newurl+'&tmpl='+template+'_printlist';
	}
	//rps check and replace
	if(newurl.indexOf('rps=') > 0){
		if(newurl.indexOf('&rps=&') > 0){
			newurl = newurl.replace('&rps=&', '&rps=250&');	
		}
		if(newurl.search(/&rps=\d{1,3}/) > 0){
			newurl = newurl.replace(/&rps=\d{1,3}/, '&rps=250');
		}
	}else{
		newurl = newurl+'&rps=250';
	}
	//p check and replace (page)
	if(newurl.indexOf('p=') > 0){
		if(newurl.search(/&p=\d{1,1}/) > 0){
			newurl = newurl.replace(/&p=\d{1,1}/, '&');
		}
	}
	var finalurl=newurl.replace('#','');
	window.open(finalurl,'','');
}

// Reorder the current search to what is needed from a link (<a>).
function reorder(orderBy){
	if(location.href.indexOf('f_OrderedBy=') > 0){//If OrderBy Exists					
		var getIndex = window.location.href.indexOf('&f_OrderedBy=');	//Find OrderBy		
		var preOrderBy = window.location.href.substr(0, getIndex); //Get URL Before OrderBy
		var removeAmp = window.location.href.substring(getIndex+1); //Remove &amp; Before OrderBy
		var postOrderBy = "";
		if(removeAmp.indexOf('&') > 0){
			var getIndexOfAmp = removeAmp.indexOf('&'); //Find &amp; After OrderBy
			var postOrderBy = removeAmp.substring(getIndexOfAmp); //Get URL After &amp;					
		}
		var newurl = preOrderBy + "&f_OrderedBy=" + orderBy + postOrderBy; //Put It All Together						
	}else{
		var newurl = location.href+'&f_OrderedBy='+orderBy;
	}
	
	var finalurl=newurl.replace('#',''); //Remove # from URL				
	location.href = finalurl;
}

//Popups for Start Sites
function popUpBox(linkInfo, boxName) {
	winOptions = "alwaysRaised=yes,location=no,status=no,titlebar=no,"+
	"toolbar=no, scrollbars=no,height=560,width=620,left=0,top=0,resizable=yes";
	ordWin = window.open(linkInfo, boxName, winOptions);
	}
function InspectBox(linkInfo, boxName) {
	winOptions = "alwaysRaised=yes,location=no,status=no,titlebar=no,"+
	"toolbar=no, scrollbars=yes,height=500,width=650,left=0,top=0,resizable=no";
	ordWin = window.open(linkInfo, boxName, winOptions);
	}
function PictureBox(linkInfo, size, url) {
	size = size.replace(',', ',height=')
	linkOptions = "alwaysRaised=yes,location=no,status=no,titlebar=no,toolbar=no,scrollbars=yes,width=" + size + ",left=0,top=0,resizable=yes";
	linkURL = url + "/pol/Property/search.asp?" + linkInfo
	window.open(linkURL, 'pic', linkOptions);
	}
	
//Used to split commercial size from one dropdown to 2 hidden inputs
$(document).ready(function(){
	$("select[name=sizeRange]").change(function(){
		var findSplit = $(this).val().indexOf('-');
		var sizeMin = $(this).val().substr(0, findSplit);
		var sizeMax = $(this).val().substr(findSplit+1);
		$("input[name=f_minimum_size]").attr("value", sizeMin);
		$("input[name=f_maximum_size]").attr("value", sizeMax);
	});
});
