/* -*- javascript -*- 
     Copyright 2007 Artific Industries.
     All Rights Reserved
     System        : COMMON_JS : 
     Object Name   : $RCS_FILE$
     Revision      : $REVISION$
     Date          : Mon Oct 1 00:20:01 2007
     Created By    : Ed Costello, Artific Industries
     Created       : Mon Oct 1 00:20:01 2007

     Last Modified : <090306.1355>
     ID            : $Id: common.js,v 1.14 2011/06/22 09:11:35 icpadmin Exp $
     Source        : $Source: /usr/home/icpadmin/sites/iconicpostcards.com/etc/RCS/common.js,v $
     Description	
     Notes
*/
function icptopicselect(topics) {
	var topicform = document.getElementById('topiclist');
	var oSelect=document.getElementById('topicselect');
	var sPrompt = 'Select a topic';
	var oPrompt =document.createElement('option');
	oPrompt.setAttribute('label',sPrompt);
	oPrompt.setAttribute('value',0);
	oPrompt.appendChild(document.createTextNode(sPrompt));
	oSelect.appendChild(oPrompt);
	delete sPrompt,oPrompt;

	for (var topic_id in topics.ix) {
		var cssClass = 'top-topic';
		var opt = document.createElement('option');
		var topic_ix = topics['ix'][topic_id]
		if (topics[topic_ix].c.length > 0||topics[topic_ix].p ==0) {cssClass='parent-topic';}
		if (topics[topic_ix].p == 0 && topics[topic_ix].i >0) {
			var topic_ln = topics[topic_ix].l;
			var uc_topic_ln = topic_ln.substr(0,1).toUpperCase() + topic_ln.substr(1,topic_ln.length);

			var txt = document.createTextNode(uc_topic_ln);
			opt.appendChild(txt);
			opt.setAttribute('value',topics[topic_ix].i);
			opt.setAttribute('class',cssClass);
			oSelect.appendChild(opt);
			}
		delete opt;
		}
	delete oSelect;
	}
              

function onTopicSelect(s) {s.form.submit();}

function popup(u,w,h) {
    var options = "width=" + 1.07 * w + ",height=" + 1.07 * h + ",left=240,top=240";
    options += ",alwaysRaised=yes,directories=no,location=no,menubar=no,resizable=yes,status=no,scrollbars=yes,toolbar=no,z-lock=no";
    window.open(u,"thumbnail",options)
          }

function addTopicSelect(id) {
	var oSelect = document.createElement('select');
	oSelect.setAttribute('class','topiclist');
	oSelect.setAttribute('id','topicselect');
	oSelect.setAttribute('name','topic');
	oSelect.setAttribute('style','overflow:auto;width:90%;');
	oSelect.setAttribute('onChange', 'this.form.submit();');
	new Ajax.Request('/scripts/topics/list/brief/', {
		method:'get',
		parameters: {f:'raw'},
		onSuccess: function(transport){
			var TOPICCACHE = transport.responseText.evalJSON(true);
			var sPrompt = 'Select a topic';
			var oPrompt =document.createElement('option');
			oPrompt.setAttribute('label',sPrompt);
			oPrompt.setAttribute('value',0);
			oPrompt.appendChild(document.createTextNode(sPrompt));
			oSelect.appendChild(oPrompt);
			delete sPrompt,oPrompt;
			for (var topic_id in TOPICCACHE.ix) {
				var topic_ix = TOPICCACHE['ix'][topic_id];
				if ((TOPICCACHE[topic_ix].p) == 0 && (TOPICCACHE[topic_ix].i>0)) {
					var topic_ln = TOPICCACHE[topic_ix].l;
					var oOption = document.createElement('option');
					oOption.setAttribute('value',topic_id);
					if (TOPICCACHE[topic_ix].c.length > 0) {
						oOption.setAttribute('class','parent-topic');
						}
					var tLabel = document.createTextNode(topic_ln);
					oOption.setAttribute('style','font-size:x-small;line-height:1.0');
					oOption.appendChild(tLabel);
					oSelect.appendChild(oOption);
					delete oOption,tLabel;
					}
				}
			}
		});
	var oForm = document.getElementById(id);
	oForm.appendChild(oSelect);
	delete oSelect;
	}
	
function addHomeTopicSelect(id) {
	var oSelect = document.createElement('select');
	oSelect.setAttribute('class','topiclist');
	oSelect.setAttribute('id','topicselect');
	oSelect.setAttribute('name','topic');
	oSelect.setAttribute('style','overflow:auto;width:auto;');
	oSelect.setAttribute('onChange', 'this.form.submit();');
	new Ajax.Request('/scripts/topics/list/brief/', {
		method:'get',
		parameters: {f:'raw'},
		onSuccess: function(transport){
			var TOPICCACHE = transport.responseText.evalJSON(true);
			var sPrompt = 'Select a topic';
			var oPrompt =document.createElement('option');
			oPrompt.setAttribute('label',sPrompt);
			oPrompt.setAttribute('value',0);
			oPrompt.appendChild(document.createTextNode(sPrompt));
			oSelect.appendChild(oPrompt);
			delete sPrompt,oPrompt;
			for (var topic_id in TOPICCACHE.ix) {
				var topic_ix = TOPICCACHE['ix'][topic_id];
				if ((TOPICCACHE[topic_ix].p) == 0 && (TOPICCACHE[topic_ix].i>0)) {
					var topic_ln = TOPICCACHE[topic_ix].l;
					var oOption = document.createElement('option');
					oOption.setAttribute('value',topic_id);
					if (TOPICCACHE[topic_ix].c.length > 0) {
						oOption.setAttribute('class','parent-topic');
						}
					var tLabel = document.createTextNode(topic_ln);
					oOption.setAttribute('style','font-size:x-small;line-height:1.0');
					oOption.appendChild(tLabel);
					oSelect.appendChild(oOption);
					delete oOption,tLabel;
					}
				}
			}
		});
	var oForm = document.getElementById(id);
	oForm.appendChild(oSelect);
	delete oSelect;
	}

	
function addTopicOptions(id) {
	var oSelect = document.getElementById(id);
	oSelect.setAttribute('style','overflow:auto;font-size:small;width:12em;');
	new Ajax.Request('/scripts/topics/list/brief/', {
		method:'get',
		parameters: {f:'raw'},
		onSuccess: function(transport){
			var TOPICCACHE = transport.responseText.evalJSON(true);
			var sPrompt = 'Select a topic';
			var oPrompt =document.createElement('option');
			oPrompt.setAttribute('label',sPrompt);
			oPrompt.setAttribute('value',0);
			oPrompt.appendChild(document.createTextNode(sPrompt));
			oSelect.appendChild(oPrompt);
			delete sPrompt,oPrompt;
			for (var topic_id in TOPICCACHE.ix) {
				var topic_ix = TOPICCACHE['ix'][topic_id];
				if ((TOPICCACHE[topic_ix].p) == 0 && (TOPICCACHE[topic_ix].i>0)) {
					var topic_ln = TOPICCACHE[topic_ix].l;
					var oOption = document.createElement('option');
					oOption.setAttribute('value',topic_id);
					if (TOPICCACHE[topic_ix].c.length > 0) {
						oOption.setAttribute('class','parent-topic');
						}
					var tLabel = document.createTextNode(topic_ln);
					oOption.setAttribute('style','font-size:x-small;line-height:1.0');
					oOption.appendChild(tLabel);
					oSelect.appendChild(oOption);
					delete oOption,tLabel;
					}
				}
			}
		});
	}
function addArtistOptions(id) {
	var oSelect = document.getElementById(id);
	oSelect.setAttribute('style','overflow:auto;font-size:small;text-transform:capitalize;width:12em;');

	new Ajax.Request('/scripts/artists/', {
		method:'get',
		onSuccess: function(transport){
			var JSONCACHE = transport.responseText.evalJSON(true);
//			window.alert(transport.responseText);
			var sPrompt = 'Select an artist';
			var oPrompt =document.createElement('option');
			oPrompt.setAttribute('label',sPrompt);
			oPrompt.setAttribute('value',0);
			oPrompt.appendChild(document.createTextNode(sPrompt));
			oSelect.appendChild(oPrompt);
//			delete sPrompt,oPrompt;
			for (var a in JSONCACHE.ix) {
			var artist_ix = JSONCACHE['ix'][a];
			// window.alert(JSONCACHE[artist_ix].artist_surname);
				var oOption= document.createElement('option');
				oOption.setAttribute('value',a);
				var tSurname = JSONCACHE[artist_ix].artist_surname;
				var tName = JSONCACHE[artist_ix].artist_name;
				if (tName != '') {
					var oLabel = document.createTextNode(tSurname + ',' + tName);
					}
				else {
					var oLabel = document.createTextNode(tSurname);	
					}
				oOption.appendChild(oLabel);
				oSelect.appendChild(oOption);
				}
			}
		});
	}
	
function addLanguageOptions(id) {
	var oSelect = document.getElementById(id);
	oSelect.setAttribute('style','overflow:auto;font-size:small;text-transform:capitalize;width:12em;');
	new Ajax.Request('/scripts/languages/', {
		method:'get',
		onSuccess: function(transport){
			var JSONCACHE = transport.responseText.evalJSON(true);
			var sPrompt = 'Select a language';
			var oPrompt =document.createElement('option');
			oPrompt.setAttribute('label',sPrompt);
			oPrompt.setAttribute('value',0);
			oPrompt.appendChild(document.createTextNode(sPrompt));
			oSelect.appendChild(oPrompt);
			for (var langid in JSONCACHE.ix) {
				var lang_ix = JSONCACHE['ix'][langid];
				var oOption= document.createElement('option');
				oOption.setAttribute('value',langid);
				var tLangName = JSONCACHE[lang_ix].lang_name;
				var tLangCode = JSONCACHE[lang_ix].lang_code;
				var oLabel = document.createTextNode(tLangName + '  (' + tLangCode + ')');
				oOption.appendChild(oLabel);
				oSelect.appendChild(oOption);
				}
			}
		});
	}
	
function addPublisherOptions(id) {
	var oSelect = document.getElementById(id);
	oSelect.setAttribute('style','overflow:auto;font-size:small;text-transform:capitalize;width:12em;');
	new Ajax.Request('/scripts/publishers/', {
		method:'get',
		onSuccess: function(transport){
			var JSONCACHE = transport.responseText.evalJSON(true);
			var sPrompt = 'Select a publisher';
			var oPrompt =document.createElement('option');
			oPrompt.setAttribute('label',sPrompt);
			oPrompt.setAttribute('value',0);
			oPrompt.appendChild(document.createTextNode(sPrompt));
			oSelect.appendChild(oPrompt);
			for (var pubid in JSONCACHE.ix) {
			var publ_ix = JSONCACHE['ix'][pubid];
				var oOption= document.createElement('option');
				oOption.setAttribute('value',pubid);
				var tPublisherName = JSONCACHE[publ_ix].publisher_ln;
				var oLabel = document.createTextNode(tPublisherName);
				oOption.appendChild(oLabel);
				oSelect.appendChild(oOption);
				}
			}
		});
	}

function activatePlaceholders() {
var detect = navigator.userAgent.toLowerCase(); 
if (detect.indexOf("safari") > 0) return false;
if (detect.indexOf("webkit") > 0) return false;
var inputs = document.getElementsByTagName("input");
for (var i=0;i<inputs.length;i++) {
  if ((inputs[i].getAttribute("type") == "text")||
	(inputs[i].getAttribute('type') == 'search')) {
   if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
    inputs[i].value = inputs[i].getAttribute("placeholder");
    inputs[i].onclick = function() {
     if (this.value == this.getAttribute("placeholder")) {
      this.value = "";
     }
     return false;
    }
    inputs[i].onblur = function() {
     if (this.value.length < 1) {
      this.value = this.getAttribute("placeholder");
     }
    }
   }
  }
}
}

