// SpotSense Scripts
// I detect the correct protocol to use
var protocol = document.location.protocol;
var videoplayerurl;

if (location.host.indexOf('homenet') > -1) {
	videoplayerurl = 'Images/HomeNet/Flash/VideoPlayer.swf';
} else {
	videoplayerurl = 'Images/VideoLink/Flash/VideoPlayer.swf';
}


// I create a console object for Internet Explorer
if (!console) {
	var console = new Object();
	var log = function(string) {}
	console.log = log;
}

// I am a publicly available date stamp
var datestamp = new Date();

// I control Flash transparency - over-ride me inline with tr = 1 to turn off wmode=transparent
var tr = 0;

// I set up the character limit
var initcharlim = 70;

// We show and hide items
function show(d,i) {
	a = document.getElementById(d);
	if (!i) {
		a.className = "on";
	} else {
		a.className = "overon";
	}
}

function hide(d) {
	a = document.getElementById(d);
	a.className = "off";
}

// I handle mouse-over states for images
function switcher(i) {
	var id = document.getElementById(i);
	var s = id.src;
	var type;
	var src;
	var nsrc;

	if (s.indexOf(".gif") >= 0) {
		type = ".gif";
	} else if (s.indexOf(".jpg") >= 0) {
		type = ".jpg";
	} else if (s.indexOf(".png") >= 0) {
		type = ".png";
	} else {
		alert("This is not an image.");
	}

	src = s.replace(type, "");

	if (src.indexOf("-on") == -1) {
		nsrc = src + "-on" + type;
	} else {
		nsrc = src.replace(/-on/, "");
		nsrc += type;
	}

	id.src = nsrc;
}

// I deduce the file name from a URL
function getfilename(url) {
	var removeargs = url.substring(0,url.indexOf('.swf'));
	var removepaths = removeargs.substring(removeargs.lastIndexOf('/')+1,removeargs.length);
	return removepaths;
}

// I invoke Flash headline text
function fh(siteid,head) {
	var h1;
	var w;
	var h;
	if (siteid != 18 && siteid != 21 && siteid != 22 && siteid !=23) {
		h1 = head.toUpperCase();
		if (siteid == 16 || siteid == 15) {
			w = 456;
			h = 25;
		}
		else {
			w = 633;
			h = 21;
		}
	}
	else if (siteid == 18) {
		h1 = head;
		w = 827;
		h = 45;
	}
	else if (siteid == 21) {
		h1 = head;
		w = 640;
		h = 34;
	}
	else if (siteid == 22) {
		h1 = head;
		w = 456;
		h = 30;
	}
	else if (siteid == 23) {
		h1 = head;
		w = 590;
		h = 44;
	}
	var h2 = h1.replace(/&/, "%26");
	var hfl = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="' + protocol + '//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+w+'" height="'+h+'">';
	hfl += '<param name="movie" value="Images/FlashTextHeader'+siteid+'.swf?tt=' + h2 + '" />';
	hfl += '<param name="quality" value="high" />';
	hfl += '<param name="wmode" value="transparent" />';
	hfl += '<embed src="Images/FlashTextHeader'+siteid+'.swf?tt=' + h2 + '" quality="high" pluginspage="' + protocol + '//www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'" wmode="transparent"></embed>';
	hfl +='</object>';

	document.write(hfl);
}

function fh2(siteid,head) {
	var h1;
	var w;
	var h;
	if (siteid == 16) {
		h1 = head.toUpperCase();
		w = 456;
		h = 22;
	}
	else if (siteid != 18 && siteid != 21) {
		h1 = head.toUpperCase();
		w = 633;
		h = 21;
	}
	else if (siteid == 18) {
		h1 = head;
		w = 465;
		h = 30;
	}
	else if (siteid == 21) {
		h1 = head;
		w = 600;
		h = 28;
	}
	var h2 = h1.replace(/&/, "%26");
	var hfl = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="' + protocol + '//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+w+'" height="'+h+'">';
	hfl += '<param name="movie" value="Images/FlashTextHeaderSmall'+siteid+'.swf?tt=' + h2 + '" />';
	hfl += '<param name="quality" value="high" />';
	hfl += '<param name="wmode" value="transparent" />';
	hfl += '<embed src="Images/FlashTextHeaderSmall'+siteid+'.swf?tt=' + h2 + '" quality="high" pluginspage="' + protocol + '//www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+w+'" height="'+h+'" wmode="transparent"></embed>';
	hfl +='</object>';

	document.write(hfl);
}

// I load Flash movies
function fl(url,w,h,t,p,params) {
	var name = getfilename(url);
	var fla = '<object id="' + name + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="' + protocol + '//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + w + '" height="' + h + '">';
	fla += '<param name="movie" value="' + url + '" />';
	fla += '<param name="quality" value="high" />';
	fla += '<param name="allowFullScreen" value="true" />';
	fla += '<param name="AllowScriptAccess" value="always" />';
	fla += '<param name="menu" value="false" />';
	if (params != null) {
		for (attr in params) {
			fla += '<param name="'+attr+'" value="'+params[attr]+'" />';
		}
	}
	if (tr == 0 && (params == null || params["wmode"] == undefined)) {
		fla += '<param name="wmode" value="transparent" />';
	}
	fla += '<embed name="' + name + '" src="' + url + '" quality="high" pluginspage="' + protocol + '//www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" ';
	if (tr == 0 && (params == null || params["wmode"] == undefined)) {
		fla += 'wmode="transparent" ';
	}
	if (params != null) {
		for (attr in params) {
			fla += attr+'="'+params[attr]+'" ';
		}
	}
	fla += 'allowFullScreen="true" AllowScriptAccess="always" menu="false" width="' + w + '" height="' + h + '"></embed>';
	fla +='</object>';

	if ((t == null || t == '') && (p == null || p == 0 || p == '')) {
		document.write(fla);
	} else if (t != null && t != "" && (p == null || p == 0 || p == '')) {
		document.getElementById(t).innerHTML = fla;
	} else if (p!= null && p == 1) {
		parent.document.getElementById(t).innerHTML = fla;
	}
}

// I handle dropdown menu changes
function dropdown(d) {
	var dd = document.getElementById(d);
	var ddlink = dd.options[dd.selectedIndex].value;
	if (ddlink) {
		location.href = ddlink;
	}
}

// I show text
function showtext(source,target) {
	var txtsrc = document.getElementById(source);
	var txttrg = document.getElementById(target);

	txttrg.innerHTML = txtsrc.innerHTML;
	txttrg.className = "on";
}

// I control the character limit text
function initialchars(field,target,length,menu) {
	var dd;
	if (menu) {
		dd = document.getElementById(menu);
	}

	if (dd) {
		var ddsel = dd.selectedIndex;
		var ddval = dd.options[ddsel].value;
		if (ddval == 1077 || ddval == 1079 || ddval == 1081 || ddval == 1094 || ddval == 1110 || ddval == 1118 || ddval == 1119 || ddval == 1122 || ddval == 1123 || ddval == 1124 || ddval == 1134 || ddval == 1135 || ddval == 1138) {
			length = 210;
		}
	}

	initcharlim = length;
	charlim(field,target,initcharlim);
}

function charlim(field,target,length) {
	charlimit(field,target,length, 0);
}

function charlimit(field,target,length,initialValue) {
	var thetarget = document.getElementById(target);
	var text = document.getElementById(field).value;
	var textlength = text.length + initialValue;
	length = Number(length);

	if (length > initcharlim) {
		length = initcharlim;
	} else if (length < initcharlim) {
		length = initcharlim;
	}

	var remainder = length - textlength;
	var remove = textlength - length;

	if (remainder > 0) {
		thetarget.innerHTML = "You have " + remainder + " characters remaining";
	} else if (remainder == 0)  {
		thetarget.innerHTML = "You have 0 characters remaining";
	} else {
		thetarget.innerHTML = "<font color='#ff0000'>You have have exceeded the limit by " + remove + " characters</font>";
	}
}

// I handle the stock photo
function SetStockPhotoID(index) {	
	document.LTV_MAIN_FORM_69.StockPhotoID.value = index;
	document.LTV_MAIN_FORM_69.submit;
}

function checkfield(r,f) {
	var field = document.getElementById(f).value;
	if (field.length > 5) {
		r.checked = true;
	} else {
		r.checked = false;
	}
}

// I tell Image Catalog what stockphoto has been selected
function selectStock(stockphoto){	
	var stock = document.forms[0].elements['StockPhotoID'];
	stock.value = stockphoto;
	document.forms[0].submit()
}

// We handle the Comcast demo player
var videos = new Array();
var videoindex = 0;

function getvideos(siteid) {
	var videodivs = document.getElementsByTagName('div');
	for (v = 0; v < videodivs.length; v++) {
		if (videodivs[v].className == "videooff") {
			videos[videoindex] = videodivs[v];
			videoindex = videoindex+1;
		}
	}
}

function loadvideo(videoid, autoplay, ssid, siteid, width, height) {
	if (!siteid) {
		siteid = "Comcast";
	}
	for (vi = 0; vi < videos.length; vi++) {
		if (videos[vi].id == videoid) {
			videos[vi].className = "videoon";
		} else {
			videos[vi].className = "videooff";
		}
	}
	if (autoplay == null) {
		autoplay = "t";
	}
	if (ssid == null) {
		ssid = "";
	}
	if (width == null) {
		width = 450;
	}
	if (height == null) {
		height = 350;
	}
	fl('Images/'+siteid+'/Flash/VideoPlayer.swf?videoFile=Demo'+videoid+'.flv&sts='+autoplay+'&screenFile='+ssid+'',''+width+'',''+height+'','flashplayer'); 
}

// We handle hiding and revealing blocks
var block = new Array();
var blocklist;
var activeblock = 0;

function getformblocks() {
	var divs = document.getElementsByTagName('div');
	var currentblock = 0;
	for (var a = 0; a < divs.length; a++) {
		if (divs[a].className == "blockcol") {
			block[currentblock] = divs[a];
			currentblock = currentblock+1;
		}
	}
	blocklist = document.getElementById('blocklist');
	var chooseoption = document.createElement("option");
	chooseoption.setAttribute("value", "0");
	chooseoption.innerHTML = "Choose...";
	blocklist.appendChild(chooseoption);
	for (var i = 0; i < block.length; i++) {
		var option = document.createElement("option");
		option.setAttribute("value", i+1);
		option.innerHTML = i+1;
		blocklist.appendChild(option);
		if (activeblock > 0 && i+1 <= activeblock) {
			block[i].className = "blockcol on";
		} else {
			block[i].className = "blockcol off";
		}
	}
}

function updateformblocks(blockcount) {
	if (blockcount) {
		activeblock = blockcount;
	}
	for (var i = 0; i < block.length; i++) {
		if (activeblock > 0 && i+1 <= activeblock) {
			block[i].className = "blockcol on";
		} else {
			block[i].className = "blockcol off";
		}
	}
}

function formblockchange(id) {
	var selectedblocks = blocklist.options[blocklist.selectedIndex].value;
	updateformblocks(selectedblocks);
}

// We handle the file upload module
function LTV_FILE_LOADED(LTV_FILE_ID, FileURL, FilePath, FileName, FileExtension, FileMIME, FileChecksum, FileTimeCreation, FileTimeChanged, FileTimeAccess, FileSize, FileGuid) {
	var Animation = document.getElementById(LTV_FILE_ID + '_LTV_FILE_ANIMATION');
	Animation.style.visibility = 'hidden';
	Animation.style.display = 'none';

	var DeleteButton = document.getElementById(LTV_FILE_ID + '_LTV_FILE_DELETECOMMAND');
	DeleteButton.style.visibility = 'hidden';
	DeleteButton.style.display = 'none';

	var DeleteCheckbox = document.getElementById(LTV_FILE_ID + '_LTV_FILE_DELETE');
	if (DeleteCheckbox != null) {
		DeleteCheckbox.checked = true;
	}
	var FileTemporal = document.getElementById(LTV_FILE_ID + '_Temporal');
	FileTemporal.value = FileGuid;

	var ErrorChecking = document.getElementById(LTV_FILE_ID);
	ErrorChecking.value = FileGuid;

	var NewCommands = document.getElementById(LTV_FILE_ID + '_LTV_FILE_NEW');
	NewCommands.style.visibility = '';
	NewCommands.style.display = '';

	var PreviewCommands = document.getElementById(LTV_FILE_ID + '_LTV_FILE_COMMANDS');
	PreviewCommands.style.visibility = '';
	PreviewCommands.style.display = '';

	var Preview = document.getElementById(LTV_FILE_ID + '_LTV_FILE_PREVIEW');
	Preview.src = FileURL + '?r=r' + Math.floor(Math.random() * 99999);
	Preview.style.visibility = '';
	Preview.style.display = '';
}

function LTV_FILE_LOADING(LTV_FILE_ID) {
	//alert("animation " + LTV_FILE_ID)
	var Animation = document.getElementById(LTV_FILE_ID + '_LTV_FILE_ANIMATION');
	Animation.style.visibility = '';
	Animation.style.display = '';

	var IFrame = document.getElementById(LTV_FILE_ID + '_LTV_FILE_IFRAME');
	IFrame.style.visibility = 'hidden';
	IFrame.style.display = 'none';
}

function LTV_FILE_LOADING_FAIL(LTV_FILE_ID) {
	var Animation = document.getElementById(LTV_FILE_ID + '_LTV_FILE_ANIMATION');
	Animation.style.visibility = 'hidden';
	Animation.style.display = 'none';

	var IFrame = document.getElementById(LTV_FILE_ID + '_LTV_FILE_IFRAME');
	IFrame.style.visibility = '';
	IFrame.style.display = '';
}

function LTV_FILE_LOAD(LTV_FILE_ID) {
	var PreviewCommands = document.getElementById(LTV_FILE_ID + '_LTV_FILE_COMMANDS');
	PreviewCommands.style.visibility = 'hidden';
	PreviewCommands.style.display = 'none';

	var PreviewCancel = document.getElementById(LTV_FILE_ID + '_LTV_FILE_CANCEL');
	PreviewCancel.style.visibility = '';
	PreviewCancel.style.display = '';

	var IFrame = document.getElementById(LTV_FILE_ID + '_LTV_FILE_IFRAME');
	IFrame.style.visibility = '';
	IFrame.style.display = '';
}

function LTV_FILE_CLEAR(LTV_FILE_ID) {
	var PreviewCommands = document.getElementById(LTV_FILE_ID + '_LTV_FILE_COMMANDS');
	PreviewCommands.style.visibility = 'hidden';
	PreviewCommands.style.display = 'none';

	var PreviewCancel = document.getElementById(LTV_FILE_ID + '_LTV_FILE_CANCEL');
	PreviewCancel.style.visibility = 'hidden';
	PreviewCancel.style.display = 'none';

	var IFrame = document.getElementById(LTV_FILE_ID + '_LTV_FILE_IFRAME');
	IFrame.style.visibility = '';
	IFrame.style.display = '';
}

function LTV_FILE_CANCEL(LTV_FILE_ID) {
	var PreviewCommands = document.getElementById(LTV_FILE_ID + '_LTV_FILE_COMMANDS');
	PreviewCommands.style.visibility = '';
	PreviewCommands.style.display = '';

	var IFrame = document.getElementById(LTV_FILE_ID + '_LTV_FILE_IFRAME');
	IFrame.style.visibility = 'hidden';
	IFrame.style.display = 'none';
}

// I handle the Primary Contact functionality
var radioarray = new Array();
var currentsubmit;

function primarychecker() {
	var inputarray = document.getElementsByTagName("input");

	for (i = 0; i < inputarray.length; i++) {
		if (inputarray[i].name == "ProfilePropertyID2" && inputarray[i].checked == true) {
			inputarray[i].click();
		}
	}
}

function checkradioarray() {
	var inputarray = document.getElementsByTagName('input');
	var submitform = false;
	if (radioarray.length > 0) {
		for (i = 0; i < radioarray.length; i++) {
			var radioname = radioarray[i][0];
			var radiovalue = radioarray[i][1];
			var textfield = document.getElementById(radioarray[i][2]);
			var hiddentextfield = document.getElementById(radioarray[i][3]);

			for (j = 0; j < inputarray.length; j++) {
				if (inputarray[j].name == radioname && inputarray[j].value == radiovalue) {
					if (inputarray[j].checked == true && textfield.value != "") {
						hiddentextfield.value = "Confirmed";
						submitform = true;
					} else if (inputarray[j].checked == true && textfield.value == "") {
						inputarray[j].checked = false;
						hiddentextfield.value = "";
					}
				}
			}
		}
	}
	if (submitform == true) {
		return true;
	} else {
		return false;
	}
	currentsubmit;
}

// I handle Save As New Profile
function saveasnewprofile() {
	document.getElementById('ProfileID').value = 0;	
}

// I am a simple page switcher
var pageid;

function setpage(pid) {
	pageid = pid;
}

function gotopage(listingid,productid) {
	location.href = "?SitePageID=" + pageid + "&ListingID=" + listingid + "&ProductID=" + productid;
}


// I clear the Starburst text box
function ClearField(field,target) {
	thefield = document.getElementById(field);
	targetfield = document.getElementById(target);
	if (field.checked == false) {
		targetfield.value = "";
		targetfield.disabled = true;
	}
	else {
		targetfield.disabled = false;
	}
}

// I set a hidden value on a form and submit 
function SetFieldAndSubmit(field,value){	
	thefield = document.getElementById(field);
	thefield.value = value;
	theForm.submit();
}

// I clear the target field (Eg. in new profile, resets the profileID to '')
function ClearTargetField(target){
	targetfield = document.getElementById(target);
	targetfield.value = "";
}

// I sort grids
var gridsortarray = new Array();

function sortgrid(g,said,b) {
	for (i = 0; i < gridsortarray.length; i++) {
		if (i == said) {
			try {
				var dropdown = document.getElementById(gridsortarray[i][0]);
				var currentitem = dropdown.options[dropdown.selectedIndex].value;
				var image = document.getElementById(gridsortarray[i][3]);
				var field = document.getElementById(gridsortarray[i][2]);

				if (b == 1 && gridsortarray[i][1] == true) {
					gridsortarray[i][1] = false;
				} else {
					gridsortarray[i][1] = true;
				}

				var sortdir = gridsortarray[i][1];

				g.sort(parseInt(currentitem),sortdir);
				field.value = currentitem;

				if (sortdir == true) {
					image.src = gridsortarray[i][4].toString();
				} else {
					image.src = gridsortarray[i][5].toString();
				}
			} catch(e) {
				if (console) {
					console.log(e);
				}
			}
		}
	}
}

// I handle the Manage My Ads functionality
var shoppingcartpage = 0;

function listingupdate(listingid,action,productid) {
	console.log(listingid,action);
	if (productid) {
		theForm.ProductID.value = productid;
	}
	if (action == -1) {
		var answer = confirm("Do you wish to delete this listing?");
		if (answer) {
			theForm.ListingID.value = listingid;
			theForm.ListingStatusID.value = -1;
			theForm.submit();
		}
	} else if (action == -2) {
		var answer = confirm("Do you wish to delete all listings for this profile?");
		if (answer) {
			theForm.ListingID.value = listingid;
			theForm.ListingStatusID.value = -2;
			theForm.submit();
		}
	} else if (action == -3) {										  // human voice over 
			theForm.VoiceOverOrderID.value = listingid;
			theForm.ListingStatusID.value = -3;
			theForm.submit();
	} else if (action == 0) {
		var answer = confirm("Do you wish to activate this listing?");
		if (answer) {
			theForm.ListingID.value = listingid;
			theForm.ListingStatusID.value = 1;
			theForm.submit();
		}
	} else if (action == 1) {
		var answer = confirm("Do you wish to deactivate this listing?")
		if (answer) {
			theForm.ListingID.value = listingid;
			theForm.ListingStatusID.value = 0;
			theForm.submit();
		}
	} else if (action == 2) {
		var answer = confirm("Do you wish to add this listing to your shopping cart?");
		if (answer) {
			if (theForm.SitePageNextID && shoppingcartpage != 0) {
				theForm.SitePageNextID.value = shoppingcartpage;
			}
			theForm.ListingID.value = listingid;
			theForm.ListingStatusID.value = 2;
			theForm.submit();
		}
	} else if (action == 3) {
		var answer = confirm("Do you wish to add all unpaid ads to your shopping cart?");
		if (answer) {
			if (theForm.SitePageNextID && shoppingcartpage != 0) {
				theForm.SitePageNextID.value = shoppingcartpage;
			}
			theForm.ListingID.value = 0;
			theForm.ListingStatusID.value = 3;
			theForm.submit();
		}
	} else if (action == 4) {
		var answer = confirm("Do you wish to activate all of your paid ads?");
		if (answer) {
			theForm.ListingID.value = 0;
			theForm.ListingStatusID.value = 4;
			theForm.submit();
		}
	} else if (action == 5) {
		if (listingid==1) {
			var answertext = "display";
		} else {
			var answertext = "hide";
		}
		var answer = confirm("Do you wish to " + answertext + " photos?");
		if (answer) {
			theForm.ListingStatusID.value = 5;
			theForm.submit();
		}
	} else if (action == 6) {
		var answer = confirm("Do you wish to apply this promo code?");
		if (answer) {
			theForm.ListingID.value = listingid;
			theForm.ListingStatusID.value = 6;
			theForm.submit();
		}
	} else if (action == 7) {
		var answer = confirm("Do you wish to remove this listing from your cart?");
		if (answer) {
			theForm.ListingID.value = listingid;
			theForm.ListingStatusID.value = 7;
			theForm.submit();
		}
	} else if (action == 9) {
		var answer = confirm("Do you wish to deactivate all of your paid ads?");
		if (answer) {
			theForm.ListingID.value = 0;
			theForm.ListingStatusID.value = 9;
			theForm.submit();
		}
	} else if (action == 10) {
		var answer = confirm("Do you wish to update these ads?");
		if (answer) {
			theForm.ListingID.value = 0;
			theForm.ListingStatusID.value = 10;
			theForm.submit();
		}
	} else if (action == 11) {
		var answer = confirm("Do you wish to update these ads?");
		if (answer) {
			theForm.ListingID.value = 0;
			theForm.ListingStatusID.value = 11;
			theForm.submit();
		}
	} else if (action == 12) {
		var answer = confirm("Do you wish to empty your cart?");
		if (answer) {
			theForm.ListingID.value = 0;
			theForm.ListingStatusID.value = 12;
			theForm.submit();
		}
	} else {
		alert("The status of this listing is undefined.");
	}
}

// We create edit drop-down menus for the Shopping Cart
var carteditarray = new Array();
var justlisting = 0;

function shoppingcarteditpopulator(channelid,productid,listingid) {
	var currentarrayitem;
	var dropdown = '<select id="scdd' + listingid + '" onchange="dropdown(\'scdd' + listingid + '\');" style="width: 160px;">';
	dropdown += '<option>Edit...</option>';

	for (i = 0; i < carteditarray.length; i++) {
		if (carteditarray[i][0] == channelid) {
			currentarrayitem = carteditarray[i];
		}
	}

	for (j = 1; j < currentarrayitem.length; j++) {
		dropdown += '<option value="?SitePageID=' + currentarrayitem[j][0] + '&ListingID=' + listingid;
		if (justlisting == 0) {
			dropdown += '&ProductID=' + productid + '&Edit=1">' + currentarrayitem[j][1] + '</option>';
		} else if (justlisting == 1) {
			dropdown += '">' + currentarrayitem[j][1] + '</option>';
		}
	}

	dropdown += '</select>';
	return dropdown;
}

// I attach an event to an object
function eventattach(object,newevent,eventaction) {
	var fullevent;
	var fullobject = document.getElementById(object);
	var currentaction;
	var fullaction;

	if (newevent == "click") {
		currentaction = fullobject.onclick;
	} else if (newevent == "submit") {
		currentaction = fullobject.onsubmit;
	} else if (newevent == "change") {
		currentaction = fullobject.onchange;
	}

	if (currentaction) {
		fullaction = currentaction + eventaction;
	} else {
		fullaction = eventaction;
	}

	if (window.attachEvent) {
		fullevent = "on" + newevent;
		fullobject.attachEvent(fullevent, fullaction);
	} else if (window.addEventListener) {
		fullevent = newevent;
		fullobject.addEventListener(fullevent, fullaction, false);
	}
}

// I use the VIN Tool
var VINAJAX;
var VINAJAXVariables;
function VINTool(VIN, Variables)
{	VINAJAX=null;
	VINAJAXVariables=Variables;

	if (window.XMLHttpRequest)
	{ VINAJAX=new XMLHttpRequest(); }
	else if (window.ActiveXObject)
	{ VINAJAX=new ActiveXObject("Microsoft.XMLHTTP"); }

	if (VINAJAX!=null)
	{	VINAJAX.onreadystatechange=VINAJAXResponse;
		VINAJAX.open("GET",'PageVIN.aspx?VIN=' + VIN + '&Variables=' + Variables,true);
		VINAJAX.send(null);	}
	else
	{	alert("Your browser does not support AJAX."); }
}

function VINAJAXState(obj)
{	if(obj.readyState == 4)
	{	if(obj.status == 200)
		{	return true;	}
		else
		{	alert("Problem retrieving vehicle data");	}
	}
}

function VINAJAXResponse()
{	if(VINAJAXState(VINAJAX))
	{	var VINResponse = VINAJAX.responseXML.documentElement;
		if(VINResponse.getElementsByTagName("Status")[0].firstChild.data=="FOUND")
		{	var Variables = VINAJAXVariables.split(",");
			for (var i=0;i<Variables.length;i++)
			{	var VariablePair = Variables[i].split(":");
				if(VariablePair[0]=="Make")
				{	for(var j=0;j<theForm.elements[VariablePair[1]].length;j++)
					{	if(theForm.elements[VariablePair[1]][j].value==VINResponse.getElementsByTagName([VariablePair[1]])[0].firstChild.data)
						{ theForm.elements[VariablePair[1]][j].selected = true; }
					}
				}
				else
				{ theForm.elements[VariablePair[1]].value = VINResponse.getElementsByTagName([VariablePair[1]])[0].firstChild.data;}
			}
		}
		else
		{	alert("Problem retrieving vehicle data");	}
	}
}

// I use the Features Tool
var FeaturesAJAX;
var FeaturesAJAXFieldIDs;
var FeaturesAJAXFieldKewords;
var FeaturesAJAXField;
var FeaturesAJAXCharLimTarget;
var FeaturesAJAXCharLim;

function Features(IDs, Keywords, Field, CharLimTarget, CharLim)
{	var Comma = "";
	var FieldText = "";
	for(var i=0;i<theForm.elements[IDs].length;i++)
	{	if(theForm.elements[IDs][i].checked)
		{	FieldText = FieldText + Comma + theForm.elements[IDs][i].value;
			Comma = ", "; }
	}
	FieldText = FieldText + Comma + theForm.elements[Keywords].value;

	FeaturesAJAX=null;
	FeaturesAJAXFieldIDs = IDs;
	FeaturesAJAXFieldKewords = Keywords;
	FeaturesAJAXField = Field;
	FeaturesAJAXCharLimTarget = CharLimTarget;
	FeaturesAJAXCharLim = CharLim;

	if (window.XMLHttpRequest)
	{ FeaturesAJAX=new XMLHttpRequest(); }
	else if (window.ActiveXObject)
	{ FeaturesAJAX=new ActiveXObject("Microsoft.XMLHTTP"); }

	if (FeaturesAJAX!=null)
	{	FeaturesAJAX.onreadystatechange=FeaturesAJAXResponse;
		FeaturesAJAX.open("GET",'PageFeatures.aspx?Description=' + FieldText.replace(" ", "+") + '&rnd=' + Math.random(), true);
		FeaturesAJAX.send(null);	}
	else
	{	alert("Your browser does not support AJAX."); }
}

function FeaturesAJAXState(obj)
{	if(obj.readyState == 4)
	{	if(obj.status == 200)
		{	return true;	}
	}
}

function FeaturesAJAXResponse()
{	if(FeaturesAJAXState(FeaturesAJAX))
	{	var FeaturesResponse = FeaturesAJAX.responseXML.documentElement;
		var FeaturesIDs; 

		try
		{	FeaturesIDs = FeaturesResponse.getElementsByTagName("Names")[0].firstChild.data.split("||"); }
		catch(e)
		{	FeaturesIDs = null; }

		if(FeaturesIDs!=null)
		{	for (var i=0;i<FeaturesIDs.length;i++)
			{	for(var j=0;j<theForm.elements[FeaturesAJAXFieldIDs].length;j++)
				{	if(theForm.elements[FeaturesAJAXFieldIDs][j].value==FeaturesIDs[i])
					{	theForm.elements[FeaturesAJAXFieldIDs][j].checked = true; }
				}
			}
		}

		try
		{ theForm.elements[FeaturesAJAXFieldKewords].value = FeaturesResponse.getElementsByTagName("Keywords")[0].firstChild.data;}
		catch(e)
		{ theForm.elements[FeaturesAJAXFieldKewords].value = ""; }

		try
		{ theForm.elements[FeaturesAJAXField].value = FeaturesResponse.getElementsByTagName("Description")[0].firstChild.data;}
		catch(e)
		{ theForm.elements[FeaturesAJAXField].value = ""; }

		charlim(FeaturesAJAXField, FeaturesAJAXCharLimTarget, FeaturesAJAXCharLim);
	}
}

function charlimFeatures(IDs, Keywords, CharLimTarget, CharLim)
{	var CharTotal = 0;
	for(var i=0;i<theForm.elements[IDs].length;i++)
	{	if(theForm.elements[IDs][i].checked)
		{	CharTotal = CharTotal + theForm.elements[IDs][i].value.length; }
	}
	charlimit(Keywords, CharLimTarget, CharLim, CharTotal)
}

// I change the profile image when the populator changes
function showprofilepicture(imagename,id) {
	var fileid;

	if (id) {
		fileid = id;
	} else {
		fileid = '1';
	}

	var FileURLTmp = document.getElementById('FileURL'+fileid).value;
	var host = location.host;

	if (host.indexOf('.') == -1) {
		host += '/LTV2';
	}

	document.getElementById(imagename+'_ProfileFileID').value = document.getElementById('ProfileFileID'+fileid).value;

	if (document.getElementById(imagename+'_LTV_FILE_PREVIEW').tagName.toLowerCase() == 'img') {
		document.getElementById(imagename+'_LTV_FILE_PREVIEW').src = FileURLTmp;
	} else if (document.getElementById(imagename+'_LTV_FILE_PREVIEW').tagName.toLowerCase() == 'div' && FileURLTmp.indexOf('.flv') > -1) {
		var fileconversionstatus = document.getElementById('FileConversionStatus' + id).value;

		if (fileconversionstatus == '') {
			fl(videoplayerurl+'?videoFile='+escape('http://' + host + '/' + FileURLTmp), 300, 233, imagename+'_LTV_FILE_PREVIEW', 0);
		} else {
			document.getElementById(imagename+'_LTV_FILE_PREVIEW').innerHTML = fileconversionstatus;
		}
	}

	if (FileURLTmp != '') {
		document.getElementById(imagename+'_LTV_FILE_PREVIEW').style.visibility = '';
		document.getElementById(imagename+'_LTV_FILE_PREVIEW').style.display = '';
		if (document.getElementById(imagename+'_LTV_FILE_PREVIEW').tagName.toLowerCase() == 'img') {
			LTV_FILE_LOADED(imagename, document.getElementById(imagename+'_LTV_FILE_PREVIEW').src, '', '', '', '', '', '', '', '', '', '');
		} else if (document.getElementById(imagename+'_LTV_FILE_PREVIEW').tagName.toLowerCase() == 'div') {
			LTV_FILE_LOADED(imagename, FileURLTmp, '', '', '', '', '', '', '', '', '', '');
		}
		document.getElementById(imagename+'_LTV_FILE_DELETECOMMAND').style.visibility = '';
		document.getElementById(imagename+'_LTV_FILE_DELETECOMMAND').style.display = '';
		document.getElementById(imagename+'_LTV_FILE_DELETE').checked = false;
		document.getElementById(imagename).value = "Exists";
		document.getElementById(imagename+'_LTV_FILE_IFRAME').style.visibility = 'hidden';
		document.getElementById(imagename+'_LTV_FILE_IFRAME').style.display = 'none';
	} else {
		document.getElementById(imagename+'_LTV_FILE_PREVIEW').style.visibility = 'hidden';
		document.getElementById(imagename+'_LTV_FILE_PREVIEW').style.display = 'none';
		document.getElementById(imagename+'_LTV_FILE_DELETECOMMAND').style.visibility = 'hidden';
		document.getElementById(imagename+'_LTV_FILE_DELETECOMMAND').style.display = 'none';
		document.getElementById(imagename+'_LTV_FILE_COMMANDS').style.visibility = 'hidden';
		document.getElementById(imagename+'_LTV_FILE_COMMANDS').style.display = 'none';
		document.getElementById(imagename+'_LTV_FILE_CANCEL').style.visibility = 'hidden';
		document.getElementById(imagename+'_LTV_FILE_CANCEL').style.display = 'none';
		document.getElementById(imagename+'_LTV_FILE_IFRAME').style.visibility = '';
		document.getElementById(imagename+'_LTV_FILE_IFRAME').style.display = '';
	}
}

// Show Profile default TTS
function showtts(fieldname) {
	var ttsvalue = parseInt(document.getElementById(fieldname).value);
	if (ttsvalue == undefined || ttsvalue == '') {setscriptvalues(0);} else {setscriptvalues(ttsvalue);}
}

// I create VO
var VOAJAX;
var VOAJAXTTSID;
var VOAJAXPlayer;
var VOWidth = 354;
var VOHeight = 45;

function VOTool(TTSID, Player, Script, Width, Height) {
	VOAJAX = null;
	VOAJAXTTSID = TTSID;
	VOAJAXPlayer = Player;

	if (isNaN(Width) == false) {
		VOWidth = Width;
	}
	if (isNaN(Height) == false) {
		VOHeight = Height;
	}

	if (window.XMLHttpRequest) {
		VOAJAX = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		VOAJAX=new ActiveXObject("Microsoft.XMLHTTP");
		}

	if (VOAJAX != null) {
		VOAJAX.onreadystatechange = VOAJAXResponse;
		VOAJAX.open("GET",'PageVO.aspx?TTSID=' + TTSID + '&Script=' + Script + '&Random=' + Math.random(), true);
		document.getElementById('VoiceOverScriptPreview').innerHTML = 'PageVO.aspx?TTSID=' + TTSID + '&Script=' + Script;
		VOAJAX.send(null);
	} else {
		alert("Your browser does not support AJAX.");
	}
}

function VOAJAXResponse() {
	if(VOAJAX.readyState == 4) {
		if(VOAJAX.status == 200) {
			var PreviewURL = VOAJAXPlayer + '?audioFILE=' + escape('http://tts.liquidustv.com/VoiceOver/Listing/' + VOAJAXTTSID + '.mp3');
			fl(PreviewURL,VOWidth,VOHeight,'VoiceOverMP3Preview');
		} else {
			alert("There was a problem retrieving listing information.");
		}
	}
}

function Toggle(ElementList, Value) {
	var ElementArray = ElementList.split(',');

	for (i = 0; i < ElementArray.length; i++) {
		if (Value) {
			show(ElementArray[i]);
		} else {
			hide(ElementArray[i]);
		}	
	}
}

// I handle the check-out process
function setprocess(source) {
	var process = document.getElementById('LTV_CHECKOUT_FREE');
	if (process && source.checked == true) {
		process.value = 1;
	} else if (process && source.checked == false) {
		process.value = 0;
	}
}

function checkout(termsbox) {
	var terms = document.getElementById(termsbox);
	var process = document.getElementById('LTV_PROCESS_CARD');
	if (terms && terms.checked == true) {
		theForm.submit();
	} else {
		alert('You must accept the terms and conditions before proceeding.');
		return false;
	}
}

// I over-write the GridFooter information with a shopping cart summary
function summaryinformation(gridid) {
	var subtotal = 0;
	var discount = 0;
	var totalprice = 0;
	var count = 0;
	try {
		subtotal = document.getElementById('SubTotal').value;
		discount = document.getElementById('Discount').value;
		totalprice = document.getElementById('TotalPrice').value;
		count = document.getElementById('ListingCount').value;
		document.getElementById('CCAmount').value = totalprice;
	}
	catch(e) {
		alert('Your order is not valid. Please add listings to your shopping cart');
		location.href = '?SitePageID='+cartpageid;
	}
	var summary = '<div class="subtotal"><div class="left">' + count + ' Total Ads<br /><a href="?SitePageID=' + cartpageid +'">View Entire Cart</a></div><div class="right">Subtotal: <strong>$' + subtotal + '</strong><br />Discount: <strong>$' + discount + '</strong></div></div><div class="total"><div class="left">&nbsp;</div><div class="right">Total: <strong>$' + totalprice + '</strong></div></div>';
	document.getElementById('LTV_GridTest'+gridid+'_Footer').innerHTML = summary;
	document.getElementById('LTV_GridTest'+gridid+'_Footer').className = "billingsummary";
}

// I allow the product field to be required
function productrequired(radio,field) {
	var required = document.getElementById(field);
	if (radio && radio.value > 0) {
		required.value = "Confirmed.";
	}
}

// I make the decision whether to display an uploaded or a stock photo for jobs
function stockuploaded(count,fileid,stockid,path) {
	if (count != 0) {
		if (fileid != 0 && path.indexOf('StockPhoto') == -1) {
			return "<div style='width: 100px; margin-right: 10px; float: left; display: inline;'><img src='Images/Listing/" + path + fileid + ".jpg' width='100' align='left' /></div>";
		} else if (stockid != 0) {
			return "<div style='width: 100px; margin-right: 10px; float: left; display: inline;'><img src='Images/StockPhoto/" + stockid + "t.jpg' width='100' align='left' /></div>";
		}
	}
}

// I display the shopping cart warning
function logoff(listingcount) {
	if (listingcount != 0) {
		LTV_SHOPPING_CART_WARNING.set_alignment("TopCentre");
		LTV_SHOPPING_CART_WARNING.Show();
	} else {
		location.href = "?AccountLogoff=True";
	}
}

function hidephotos() {
	theForm.ListingStatusID.value = 5;
	theForm.submit();
}

function StockPhotoSwitch(Photo)
{	if(Photo.className=='active')
	{Photo.className='';}
	else
	{Photo.className='active';}
}

// I am the new character limit function
var inchar = new Array();

function incharcheck(referer,initval) {
	for (i = 0; i < inchar.length; i++) {
		var sourcefield;
		var targetfield;
		var startlength;
		var sourcemenu;
		var sourcemenuselected;
		var sourcemenuselectedvalue;
		var sourcefieldlength;
		var initial;
		var under;
		var over;
		if (i == referer) {
			sourcefield = document.getElementById(inchar[i][0]);
			targetfield = document.getElementById(inchar[i][1]);
			startlength = inchar[i][2];
			if (inchar[i][3]) {
				sourcemenu = document.getElementById(inchar[i][3]);
			}
			if (sourcemenu) {
				sourcemenuselected = sourcemenu.selectedIndex;
				sourcemenuselectedvalue = sourcemenu.options[sourcemenuselected].value;
				for (j = 0; j < inchar[i][4].length; j++) {
					if (sourcemenuselectedvalue == inchar[i][4][j][0]) {
						startlength = Number(inchar[i][4][j][1]);
					}
				}
			}
			if (!initval) {
				initial = 0;
			} else {
				initial = Number(initval);
			}
			sourcefieldlength = sourcefield.value.length + initial;
			under = startlength - sourcefieldlength;
			over = sourcefieldlength - startlength;
			if (under > 0) {
				targetfield.innerHTML = "You have " + under + " characters remaining";
			} else if (under == 0)  {
				targetfield.innerHTML = "You have 0 characters remaining";
			} else {
				targetfield.innerHTML = "<font color='#ff0000'>You have have exceeded the limit by " + over + " characters</font>";
			}
		}
	}
}

// I display the default agency logo
var logorequest;
var logosrc;
var logofield;

function agencylogo(variable,field) {
	var agencydropdown = document.getElementById(variable);
	var selectedagency = agencydropdown.selectedIndex;
	var selectedagencyvalue = agencydropdown.options[selectedagency].value;
	getimage(selectedagencyvalue,field);
}

function getimage(agencyid,fieldid) {
	logorequest = null;
	logosrc = 'Images/Agency/Agency' + agencyid + '.jpg';
	logofield = document.getElementById(fieldid);

	if (window.XMLHttpRequest) {
		logorequest = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		logorequest = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (logorequest != null) {
		logofield.innerHTML = 'Loading...';
		logorequest.onreadystatechange = loadimage;
		logorequest.open("GET",logosrc,true);
		logorequest.send(null);
	} else {
		alert("Your browser cannot use this feature.");
	}
}

function loadimage() {
	if (logorequest.readyState == 4) {
		if (logorequest.status == 200) {
			logofield.innerHTML = '<p><img src="' + logosrc + '" /></p><p>If you would prefer to use your own logo, simply upload it below. Any logo you upload will over-ride your agency\'s default.</p>';
		} else {
			logofield.innerHTML = '<p>The agency you selected does not have a default logo. Do you wish to upload your own?</p>';
		}
	} else {
		logofield.innerHTML = '<p>The agency you selected does not have a default logo. Do you wish to upload your own?</p>';
	}
}

function checktext(clicked,text,hiddentext) {
	var textfield = document.getElementById(text);
	var hiddentextfield = document.getElementById(hiddentext);
	if (clicked.checked == true) {
		if (textfield.value == "") {
			clicked.checked = false;
			hiddentextfield.value = "";
		} else {
			clicked.checked = true;
			hiddentextfield.value = "Confirmed";
		}
	}
}

// I format the listing IDs for the activate ads functionality
function setlistingids(newvalue,hidden,e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();

	var hiddenfield = document.getElementById(hidden);
	var hiddenvalue = hiddenfield.value;
	if (hiddenvalue.indexOf(newvalue) == -1) {
			hiddenfield.value = hiddenvalue + newvalue + ",";
	} else if (hiddenvalue.indexOf(newvalue) > -1) {
		hiddenvalue = hiddenvalue.replace(newvalue+",", "");
		hiddenfield.value = hiddenvalue;
	}
	return true;
}

function updatecheckbox(target) {
	target = document.getElementById(target);
	 if(target.checked==true){
		 target.checked=false;
	}
	else{target.checked=true;}
}

// We write the listing counts to the Manage My Ads pages
function displaycounts(element) {
	var activecount = parseInt((document.getElementById('ActiveCount').value != "" ? document.getElementById('ActiveCount').value : 0));
	var inactivecount = parseInt((document.getElementById('InactiveCount').value != "" ? document.getElementById('InactiveCount').value : 0));
	var totalcount = activecount + inactivecount;
	var textstring = totalcount + ' total complete listings: ' + activecount + ' active listings, ' + inactivecount + ' inactive listings.';

	document.getElementById(element).innerHTML = textstring;
}

// I stop events from bubbling and can be used by any clicked item
function stopbubble(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

// I resize the page to specific dimensions
function resize(w,h) {
	var width = 0;
	var height = 0;

	if (navigator.appName.indexOf("Netscape") > -1) {
		width = window.outerWidth - window.innerWidth;
		height = window.outerHeight - window.innerHeight;
	}

	window.resizeTo(width+w,height+h);

	if (navigator.appName.indexOf("Netscape") == -1) {
		var cp = document.createElement("div");
		cp.style.position = "absolute";
		cp.style.width = "0px";
		cp.style.height = "0px";
		cp.style.right = "0px";
		cp.style.bottom = "0px";
		document.body.appendChild(cp);
		var cw = cp.offsetLeft;
		var ch = cp.offsetTop;
		var dw = w - cw;
		var dh = h - ch;

		document.body.removeChild(cp);
		window.resizeBy(dw, dh);
	}
}

// For Auto Profiles re: FSBO
function sellertypeswitch(type) {
	var typenumber;

	if (type == 'Individual Owner') {
		typenumber = '582759';
	} else {
		typenumber = '582652';
	}	

	for (var i=0; i<theForm.ProfilePropertyID7.length; i++) {
		if (theForm.ProfilePropertyID7[i].value == typenumber) {
			theForm.ProfilePropertyID7[i].checked = true;
		} else {
			theForm.ProfilePropertyID7[i].checked = false;
		}
	}
}

function autosellertypechange() {
	showprofilepicture('CompanyLogo','1');
	profiletypeswitcher(document.getElementById("ProfilePropertyValue7").value);
	sellertypeswitch(document.getElementById("ProfilePropertyValue7").value);
}

function profiletypeswitcher(type){
	document.getElementById("ProfilePropertyValue7").value = type;

	if (document.getElementById("ProfilePropertyValue7").value == 'Individual Owner') {
		for (var i=0; i<document.getElementsByTagName("Div").length; i++) {
			if (document.getElementsByTagName("Div")[i].id.substring(0,8) == 'Profile1') {
				document.getElementsByTagName("Div")[i].style.display = 'none';
			} else if (document.getElementsByTagName("Div")[i].id.substring(0,8) == 'Profile2') {
				document.getElementsByTagName("Div")[i].style.display = 'inline';
			}
		}
		for (var i=0; i<document.getElementsByTagName("Span").length; i++) {
			if (document.getElementsByTagName("Span")[i].id.substring(0,8) == 'Profile1') {
				document.getElementsByTagName("Span")[i].style.display = 'none';
			} else if (document.getElementsByTagName("Span")[i].id.substring(0,8) == 'Profile2') {
				document.getElementsByTagName("Span")[i].style.display = 'inline';
			}
		}
	} else {
		for (var i=0; i<document.getElementsByTagName("Div").length; i++) {
			if(document.getElementsByTagName("Div")[i].id.substring(0,8) == 'Profile1') {
				document.getElementsByTagName("Div")[i].style.display = 'inline';
			} else if (document.getElementsByTagName("Div")[i].id.substring(0,8) == 'Profile2') {
				document.getElementsByTagName("Div")[i].style.display = 'none';
			}
		}
		for (var i=0; i<document.getElementsByTagName("Span").length; i++) {
			if(document.getElementsByTagName("Span")[i].id.substring(0,8) == 'Profile1') {
				document.getElementsByTagName("Span")[i].style.display = 'inline';
			} else if(document.getElementsByTagName("Span")[i].id.substring(0,8) == 'Profile2') {
				document.getElementsByTagName("Span")[i].style.display = 'none';
			}
		}
	}
}

// I create a path in the same way the database function does
function path(value,delimiter,position) {
	var splitvalue = value.split("");
	var newpath = "";

	for (i=0; i < splitvalue.length; i++) {
		if (i/position == 1) {
			newpath += delimiter + splitvalue[i];
		} else {
			newpath += splitvalue[i];
		}
	}

	newpath += delimiter + value + delimiter;
	return newpath;
}

// I strip HTML tags from a string
function strip(s) {
	return s.replace(/(<([^>]+)>)/ig,"");
}

// I handle the VideoLink email to a friend functionality
var formset = 0;

function emailtofriend(contentid,subjectid,subject,messageid,message,url,listingid) {
	document.getElementById(subjectid).value = subject;
	document.getElementById(messageid).value = message;
	document.getElementById('Rec1').value = "";
	document.getElementById('Rec2').value = "";
	document.getElementById('Rec3').value = "";
	document.getElementById('Rec4').value = "";
	document.getElementById('Rec5').value = "";
	document.getElementById('U').value = url;
	document.getElementById('ListingID').value = listingid;

	if (formset == 0) {
		var source = document.getElementById(contentid);

		document.getElementById('LTV_SHOPPING_CART_WARNING').appendChild(source);
		source.className = "on";
		formset == 1;
	}

	if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf('6.0') > -1) {
		LTV_SHOPPING_CART_WARNING.set_alignment("TopRight");
	} else {
		LTV_SHOPPING_CART_WARNING.set_alignment("TopCentre");
	}
	LTV_SHOPPING_CART_WARNING.Show();
}

function emailclose(contentid) {
	var source = document.getElementById(contentid);
	source.className = "off";
	document.getElementById('EmailError').innerHTML = "";
	LTV_SHOPPING_CART_WARNING.Close();
}

var emailajax;
var emailurl;
var emailerror;

function sendemail() {
	emailajax = null;
	emailurl = "VideoLinkEmail.aspx?";
	emailerror = "";

	if (document.getElementById('SenderName').value.length < 5) {
		emailerror += "You must enter your name. ";
	} else {
		emailurl += "SenderName="+escape(document.getElementById('SenderName').value)+"&";
	}

	if (document.getElementById('Sender').value.length < 5) {
		emailerror += "You must enter your email address. ";
	} else {
		emailurl += "Sender="+escape(document.getElementById('Sender').value)+"&";
	}

	if (document.getElementById('Subject').value.length > 1) {
		emailurl += "Subject="+escape(document.getElementById('Subject').value)+"&";
	}

	if (document.getElementById('Message').value.length < 5) {
		emailerror += "You must enter a message. ";
	} else {
		var url = "";
		if (document.getElementById('U').value.length > 5) {
			url = "<br /><br />Your VideoLink is available here:<br />" + document.getElementById('U').value;
		}
		emailurl += "Body="+escape(document.getElementById('Message').value + url)+"&";
	}
	if (document.getElementById('ListingID').value.length > 0) {		
		emailurl += "Listing="+escape(document.getElementById('ListingID').value)+"&";
	}

	if (document.getElementById('Rec1').value.length < 5) {
		emailerror += "You must enter a recipient's email address. ";
	} else {
		emailurl += "Recipient="+escape(document.getElementById('Rec1').value);
	}

	if (document.getElementById('Rec2').value.length > 5) {
		emailurl += ","+escape(document.getElementById('Rec2').value);
	}

	if (document.getElementById('Rec3').value.length > 5) {
		emailurl += ","+escape(document.getElementById('Rec3').value);
	}

	if (document.getElementById('Rec4').value.length > 5) {
		emailurl += ","+escape(document.getElementById('Rec4').value);
	}

	if (document.getElementById('Rec5').value.length > 5) {
		emailurl += ","+escape(document.getElementById('Rec5').value);
	}

	if (window.XMLHttpRequest) {
		emailajax = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		emailajax = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (emailajax != null && emailerror.length == 0) {
		document.getElementById('EmailError').innerHTML = "";
		emailajax.onreadystatechange = emailresponse;
		emailajax.open("GET",emailurl,true);
		emailajax.send(null);
	} else if (emailajax != null && emailerror.length > 1) {
		document.getElementById('EmailError').innerHTML = emailerror;
	} else {
		document.getElementById('EmailError').innerHTML = "Your browser does not support this feature.";
	}
}

function emailresponse() {
	if (emailajax.readyState == 4) {
		if (emailajax.status == 200) {
			if (emailajax.responseText == "OK") {
				document.getElementById('EmailError').innerHTML = "Your email was sent.";
			} else {
				document.getElementById('EmailError').innerHTML = "Your email could not be sent. Please try again.";
			}
		} else {
			document.getElementById('EmailError').innerHTML = "Your email could not be sent. Please try again.";
		}
	}
}

// I write VideoLink URLs
function generateurl(sitepageid,listingid,playerid,urlonly) {
	var host = location.host;

	if (host.indexOf('.') == -1) {
		host += '/LTV2';
	}

	var url = 'http://' + host + '/?SitePageID=' + sitepageid + '&ListingID=' + listingid +'&PlayerID=' + playerid;
	if (urlonly) {
		return url;
	} else {
		return '<a href="' + url + '" target="_blank">' + url + '</a>';
	}
}

// I allow previews of FLV files
function flvURL(playername,filename,object,temporal) {
	if (object != null && object) {
		if (object.value != null && object.value != undefined && object.value != '') {
			filename = object.value;
		}
	}

	var host = location.host;
	if (host.indexOf('.') == -1) {
		host = host+'/LTV2';
	}

	var url = playername + '?videoFile=http://' + host;
	if (temporal == null || temporal == 0) {
		url += '/temporal';
	}
	url += '/' + filename;
	return url;
}

// I update a count by turning one field into an array and reporting its length to another field
function updatecount(source,target,text) {
	var sourcestring = document.getElementById(source).value;
	var sourcearray = sourcestring.split(",");
	var targetelement = document.getElementById(target);
	targetelement.innerHTML = parseInt(sourcearray.length-1) + text;
}

// I filter a list module
function filtergrid(grid,column,value,matchdir) {
	var filtercommand;
	if (value == "") {
		filtercommand = "DataItem.GetMember('" + column + "').Value != ''";
	} else {
		filtercommand = "DataItem.GetMember('" + column + "').Value " + matchdir + " '" + value + "'";
	}
	try {
		grid.Filter(filtercommand);
		grid.render();
	} catch(e) {
		if (console) {
			console.log(e);
		}
	}
}

// I check the currently selected template and reveal its colour page to assist the populator module
function revealcolors() {
	var inputarray = document.getElementsByTagName('input');

	for (i = 0; i < inputarray.length; i++) {
		if ((inputarray[i].name == "TemplateID" && inputarray[i].checked == "checked") || (inputarray[i].name == "TemplateID2" && inputarray[i].checked == "checked")) {
			inputarray[i].click();
		}
	}
}

// I'm a jumpto (used in admin for LiquiWiki and most Reports)
function FormJumpTo(link){
	window.location.href = link;
}

// I stop a VideoLink Flash file
function findmovie(moviename) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return document.getElementById(moviename);
	} else {
		return document[moviename];
	}
}

function stopmovie(moviename) {
	var pauseplayback = findmovie(moviename);
	pauseplayback.pausePlayback();
}

// I open the common overlay
function openoverlay() {
	LTV_COMMON_OVERLAY.set_alignment("TopCentre");
	LTV_COMMON_OVERLAY.Show();
}

// I close the common overlay
function closeoverlay() {
	LTV_COMMON_OVERLAY.Close();
}

// I switch CSS classes for an element
function switchclass(target,classA,classB) {
	var target = document.getElementById(target);
	target.className = (target.className == classA ? classB : target.className == classB ? classA : classB);
}
