function initAuctionForm(controller) {
//	alert('controller = '+controller);
	$("form").each(function() {
		var id = this.id;
//		alert('id = '+id);
		var name = controller+'['+id+']';
//		alert($.cookie(name));
		if($.cookie(name)) {
//			alert('name = '+name);
			var data = JSON.parse(unescape($.cookie(name).replace(/\+/g, " ")));
			$("#"+id).find(":input[type!=hidden][type!=image]").not("button").each(function() {
//				alert('tagName = '+this.tagName+', id = '+this.id+', val = '+data[id]+', type = '+this.type);
				if(data[this.id]) {
					if(this.type == 'checkbox') {
						this.checked = (data[this.id] == 1 ? true : false);
					} else{
						this.value = data[this.id];
					}
				}
			});
		}
	});
}

$(document).ready(function(){
//	alert('uname = '+$.cookie('username'));
//	alert('PHPSESSID = '+$.cookie('PHPSESSID'));
//	alert('GAN.user = '+$.cookie('GAN.user'));
	if($.cookie('GAN.user')) {
		var user = JSON.parse(unescape($.cookie('GAN.user').replace(/\+/g, " ")));
//		alert(user.username);
		$("#uname").append(user.first_name+' '+user.last_name);
		$("#optiZip").append(user.zip);

		if($.cookie('UserInfo')) {
			var messages = JSON.parse(unescape($.cookie('UserInfo').replace(/\+/g, " ")));
			$.each(messages, function(i,n) {
				$("#systemMessage").children().filter("table").append("<tr><td>"+n.text+"</td></tr>");
			});
			$("#systemMessage").css({display:'block'});
		}
	
		var path = location.pathname.split("/");
//		alert('path[1] = '+path[1]+', path[2] = '+path[2]);
		if(path[1] == 'auth' && (path[2] == 'editAccount' || path[2] == 'changeAccount' || path[2] == 'manageAlert')) {
			$("form").find(":input[type!=password]").not("button").each(function() {
				if(user[this.id]) {
					this.value = user[this.id];
				}
			});
			
		} else if(path[1] == 'index' && path[2] == 'support') {
			$("form").find(":input").not("button").each(function() {
				if(user[this.id]) {
					this.value = user[this.id];
				}
			});
		} else if(path[1] == 'RealEstateAuctions') {
			initAuctionForm('RealEstateAuctions');
		} else if(path[1] == 'RealEstateFeed') {
			initAuctionForm('RealEstateFeed');
		}

		if(path[1]) {
			var key = path[1]+'[model]';
			if($.cookie(key)) {
				var name = $.cookie(key);
				var formname = name.substring(0, name.indexOf('Auctions'));
//				alert('formname = '+formname);
				$("#"+formname+"OPTIONID").attr('checked','checked');
//				$("input[type=radio][onclick=showForm('"+formname+"FORMID')]").attr('checked','checked');
				$("form[name=searchForm]").addClass("searchForm");
				$("#"+formname+"FORMID").addClass("searchFormDefault");
			}
		}
	}
});

function showForm(formName) {
//	alert('formName = '+formName);
	var allforms=document.getElementsByTagName("form");
	for(var i=0;i<allforms.length;i++) {
		allforms[i].parentNode.parentNode.style.display = "none";
	}
	document.getElementById(formName).style.display = "block";
//	alert(document.getElementById(formName).className);
}

function changeButtonLabel(name, label1, label2) {
	if(document.getElementById(name).value == label1) {
		document.getElementById(name).value = label2;
	} else {
		document.getElementById(name).value = label1;
	}
}

function switchVisibility(e, type) {
//	e.src
//	alert('type = '+type);
//	alert('display = '+document.getElementById(type).style.display);
	var div = document.getElementById(type);
	if(div.style.display == 'block') {
		e.src = e.src.replace('Opened', 'Closed');
		e.alt = 'Open List';
		div.style.display = 'none'
	} else {
		e.src = e.src.replace('Closed', 'Opened');
		e.alt = 'Close List';
		div.style.display = 'block'
	}
//	alert(e.src);
}

$("#usual1 ul").idTabs(); 

jQuery.jPrintArea=function(el) {
	var iframe=document.createElement('IFRAME');
	var doc=null;
	$(iframe).attr('style','position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
	document.body.appendChild(iframe);
	doc=iframe.contentWindow.document;
//	alert(doc);
	var links=window.document.getElementsByTagName('link');
	for(var i=0;i<links.length;i++) {
		if(links[i].rel.toLowerCase()=='stylesheet') {
			doc.write('<link type="text/css" rel="stylesheet" href="'+links[i].href+'"></link>');
		}
	}
	doc.write('<div class="'+$(el).attr("class")+'">'+$(el).html()+'</div>');
//	alert(doc);
	doc.close();
	iframe.contentWindow.focus();
	iframe.contentWindow.print();
//	setTimeout("alert('Printing...')", 1000);
//	wait(1);
	document.body.removeChild(iframe);
}

function printScreen() {
	window.print();
//	$.jPrintArea('#content');
}
