
// Various onloiad attachments as required

window.onload = function() { 
	attachShowHide(); 
	attachFormValidation();
}

// First Add Photo page
function attachShowHide() {
	if (document.getElementById('optional_fields')) {
		document.getElementById('optional_fields').style.display='none';
		var openMeString = 'Open this link for more optional fields below . . .';
		if (document.getElementById('open_me')) {
			document.getElementById('open_me').innerHTML = openMeString;
		}
		document.getElementById('open_me').onclick=function() { return showHide(); }
	}
}

function showHide() {
	var v = document.getElementById('optional_fields').style.display;
	document.getElementById('optional_fields').style.display=(v=='block')?'none':'block';
	var openMeString = (document.getElementById('optional_fields').style.display=='block')?'Hide optional fields (data will still submit.)':'Enhance your ad with optional fields (click to open)';
	document.getElementById('open_me').innerHTML = openMeString;
	return false;
}


function attachFormValidation() {
	if (document.getElementById('add-form')) {
		document.getElementById('add-form').onsubmit=function () { return validate_form(this); }
	}
	if (document.getElementById('upload-form')) {
		document.getElementById('upload-form').onsubmit=function () { document.getElementById('bar').style.display='block'; }
	}
}

// Validate the first form, account creation

function validate_form(form) {
	var requireds  = new Array('at','ad_ph','em');
	var plainNames = new Array('ad type','ad phone number','email address');
	var checkThis=itemName=msg="";

	for (i=0; i<requireds.length; i++) {
		var obj = document.getElementById(requireds[i]);
		if (obj.type == 'select-one') { checkThis = obj[obj.selectedIndex].value; }
		else { checkThis = obj.value; }
		itemName =  plainNames[i];

		if ((checkThis == '') || (checkThis == 'none')) {
			msg += '\n  Please provide your ' + itemName + ' before submitting this form.';
		}
	}
	if (msg != '') {
		msg = 'Your form contains the following errors:\n'+msg+'\n Please make the necessary corrections and try again.';
		alert(msg);
	}
	else  { 
		//form.submitButton.disabled = true; 
		form.submit();
	}
	return false;
}

// Generic new window

function newWin(url,img,title,w,h) {

	   if (! (url) && ! (img)) { return; }
	   var day= new Date();
	   var id = day.getTime();
	   var ww = w+75;
	   var wh = h+125;
	   if ((screen.height) && (wh > screen.height-100)) { wh = screen.height-100; }
	   var params = 'width='+ww+',height='+wh+',scrollbars,resizable';
	   var t = (title != '')?title:img;
	   if (url=='') {
	      var msg='<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN" "http:\/\/www.w3.org/TR\/html4\/loose.dtd">\n'+
		          '<meta http-equiv="Content-Type" content="text\/html; charset=iso-8859-1">\n'+
		          '<html><head><title>'+t+'<\/title><\/head>'+
		          '<style type="text\/css">\n'+
				  'html,body { font-family: Arial,Helvetica,Sans-Serif; }\n'+
				  'h3 { text-align: center; }\n'+
				  'td { text-align: center; }\n'+
				  '<\/style><body>\n'+
				  '<h3>'+t+'<\/h3>\n <table align="center" border="0"><tr><td><img src="'+img+'" width="'+w+'" height="'+h+'" border="0" alt="'+title+'"></td></tr>\n'+
				  '<tr><td><hr width="100%" size="1"><form><input type="button" onClick="javascript:window.close();" value="Close Window">\n'+
				  '<hr width="100%" size="1"><\/form><\/td><\/tr><\/table><\/body><\/html>\n';
	      var win = open('',id,params);
		  win.document.write(msg);
		  win.document.close();
	   }
	   else { var win = open(url,id,params); } 
	   return false;
}

// Detail page swap
function swapImage(id,url,w,h) {
	var obj = document.getElementById(id);
	if (obj) {
		obj.src='';
		obj.width=w;
		obj.height=h;
		obj.src=url;
	}
	return false;
}


