
// filmcards
var test = 0;
var submittable = true;

var imgArray = new Array();
imgArray[0] = new Image();
imgArray[0] = imgDir + "/common/loader_macish.gif"

imgArray[1] = new Image();
imgArray[1] = imgDir + "/ecards/bg_about_page.jpg"

var loadingGraphic2 = '<img src="' + imgDir + '/common/loader_macish.gif" alt="loading"/>';

var loadingElement = '<div style="margin:20px 0px 10px 0px;"><span style="font-size:25px;height:26px;">' + loadingGraphic2 + '<span style="margin-left:12px;font-size:12px;vertical-align:text-top;">Loading...</span></span></div>';

var timeDiff = {
	setStartTime:function (){
		d = new Date();
		time = d.getTime();
	},

	getDiff:function (){
		d = new Date();
		return (d.getTime()-time);
	}
}


function toggleAboutThisPage(){

	if(document.getElementById('aboutThisPage').style.display=='block'){
		document.getElementById('aboutThisPage').style.display='none'
		document.getElementById('aboutThisPageBttn').src=imgDir+'/ecards/bttn_filmcards_about_page_open.gif'
	}else{
		document.getElementById('aboutThisPage').style.display='block'
		document.getElementById('aboutThisPageBttn').src=imgDir+'/ecards/bttn_filmcards_about_page_close.gif'
	}

}

/**
*
*	Replaces initial text in text field
*
*/
function configInput(el){
	switch(el.value){
		case "Sender's Email":
		case "Sender's Name":
		case "Recipient's Email":
		case "Recipient's Name":
				el.value = '';
				el.style.color = '#000000'
		break;
		default:
			// do nothing
		break;
	}
}

function str_replace(search, replace, subject) {

	var f = search, r = replace, s = subject;
	var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

	while (j = 0, i--) {
		if (s[i]) {
			while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
		}
	};

	return sa ? s : s[0];

}

function getRecip() {

	return document.getElementById('recipients').value;

}

function importHelpPopup () {

	window.open(importHelpPopupURL, 'importHelpPopup', 'height = 600, width = 400, scrollbars = 1');

}

function chooseContacts(){

	/*document.getElementById('fcSummary').style.display='none';*/

	un = document.getElementById('un').value;

	loadContacts(un);

}

function loadContacts(un){

		//trackClick(storeURL + '/ajax/productdetail/' + code);

		var title = this.title || this.name || null;
		var loadUrl = siteURL+'/members/'+un+'/contacts/choose?width=430&height=460&closer=no';
		var imageGroup = this.rel || false;
		var elToHide = new Array();
		//document.getElementById('player').style.display='none'
		tb_show(title, loadUrl, imageGroup, elToHide);
		return;
	
	xmlHttp=getXmlHttpObject();

	xmlHttp.onreadystatechange = function() {

		if (xmlHttp.readyState == 4) {

			//setContactsChoosePanelLoading();
			//alert(xmlHttp.responseText);
			document.getElementById('contactsChooseData').innerHTML = xmlHttp.responseText;

			//openContactsChoosePanel();

			//openContactFields();

		} else {

			//setContactsChoosePanelLoading();

			//closeContactFields();

		}
	}

	var recip = escape(document.getElementById('recipients').value);

	// Automatic HTML character encoding is the devil.
	recip = str_replace('&lt;', '', recip);
	recip = str_replace('&gt;', '', recip);

	var url = siteURL+'/members/'+un+'/contacts/choose';

	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('recip=' + recip);

}

function chooseContactsNon(){

	/*document.getElementById('fcSummary').style.display='none';*/

	//un = document.getElementById('un').value;
	un = 'spiritclips';

	setContactsChoosePanelLoading();

	loadContactsNon(un);

}

function loadContactsNon(un){

	xmlHttp=getXmlHttpObject();

	xmlHttp.onreadystatechange = function() {

		if (xmlHttp.readyState == 4) {

			setContactsChoosePanelLoading();

			document.getElementById('contactsChooseData').innerHTML = xmlHttp.responseText;

			openContactsChoosePanel();

			openContactFields();

		} else {

			setContactsChoosePanelLoading();

			closeContactFields();

		}
	}

	var recip = escape(document.getElementById('recipients').value);

	// Automatic HTML character encoding is the devil.
	recip = str_replace('&lt;', '', recip);
	recip = str_replace('&gt;', '', recip);

	var url = siteURL+'/members/'+un+'/contacts/choose?tempID=' + tempID;

	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('tempID=' + tempID + '&recip=' + recip);

}

/*
function chooseContactsNon(){

	un = document.getElementById('un').value;

	setContactsChoosePanelLoading();

	document.getElementById('contactsChooseContainer').style.display='block';

	closeContactFields();

	// Allow loading graphic to render before we start loading contact data.
	setTimeout("loadContactsNonProcess(un)", timeoutStandardDelay);

}

function loadContactsNonProcess(un) {

	var toContactsChooseData = '<div style=""><div style="margin:10px 0px 20px 0px;"><span style="padding:0px 0px 8px 0px;border-bottom:1px solid #95baca;font-size:12px;">check the contacts to add to your recipients list</span></div><div class="innerMenu" style="margin-bottom:10px;"><a href="javascript:checkAllContacts(\'check\')" title="Check all contact checkboxes">check all</a><span class="sep">|</span><a href="javascript:checkAllContacts(\'clear\')" title="Clear all contact checkboxes">clear all</a></div><form id="contactForm" action="" method="post"><div id="contactList">';

	var numContactsAdded = 0;

	var i = 0;
	var f = function () {

		if (i < contactsNon.length) {

			var parts = contactsNon[i].split('^');

			var toData = '<div style="margin:0px 0px 10px 0px"><input type="checkbox" name="contact_' + i + '" value="' + parts[1] + ' &lt;' + parts[0] + '&gt;" /> ' + parts[0];

			if (parts[1] != '') {
				toData += ' (' + parts[1] + ')';
			}

			toData += '</div>';

			toContactsChooseData += toData;
			numContactsAdded += 1;

			i++;

			setTimeout( f, timeoutStandardDelay )

		} else {

			toContactsChooseData += '</div></form><div class="innerMenu"><a href="javascript:checkAllContacts(\'check\')" title="Check all contact checkboxes">check all</a><span class="sep">|</span><a href="javascript:checkAllContacts(\'clear\')" title="Clear all contact checkboxes">clear all</a></div><div style="margin-top:10px"><img src="' + imgDir + '/ecards/send/bttn_add_to_recipients.gif" alt="" onclick="addContactToRecips()" style="cursor:pointer" /></div></div>';

			if (numContactsAdded == 0) {
				document.getElementById('contactsChooseData').innerHTML = '<div style="margin-top:10px;">All contacts in the submitted file have already been added as recipients for this filmcard.</div>';
			} else {
				document.getElementById('contactsChooseData').innerHTML = toContactsChooseData;
			}

			openContactsChoosePanel();

			openContactFields();

		}

	};
	f();

}
*/

function addContactToRecips(){

	un = userName; //document.getElementById('un').value;

	xmlHttp=getXmlHttpObject();

	xmlHttp.onreadystatechange = function() {

		if (xmlHttp.readyState == 4) {

			//setContactsChoosePanelLoading();

			var r = xmlHttp.responseText;
			r = str_replace('&lt;', '<', r);
			r = str_replace('&gt;', '>', r);

			document.getElementById('recipients').value = r;

			//openContactFields();
			// Reset the contacts panel.
			closeContactsChoosePanel();

		} else {

			setContactsChoosePanelLoading();

			closeContactFields();

		}
	}

	removeDefaultText();
	var recip = escape(document.getElementById('recipients').value);

	// Automatic HTML character encoding is the devil.
	recip = str_replace('&lt;', '', recip);
	recip = str_replace('&gt;', '', recip);

	inputs = document.getElementById('contactForm').elements

	var contacts = '';

	for(i=0; i<inputs.length; i++){
		if(inputs[i].checked==true){
			contacts += escape(inputs[i].value) + ',';
		}
	}

	// If nothing was selected in the contacts form, we should bail out.
	if (contacts.length < 1) {
		closeContactsChoosePanel();
		return;
	}

	var url = siteURL+'/members/'+un+'/contacts/addctr';

	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('recip=' + recip + '&contacts=' + contacts);

}

function addContact(lineNum){

	xmlHttp=getXmlHttpObject()

	xmlHttp.onreadystatechange = function(){

		if(xmlHttp.readyState == 4){
			document.getElementById('addInfo'+lineNum).innerHTML = xmlHttp.responseText;
		}else{
			document.getElementById('addInfo'+lineNum).innerHTML = loadingElement;
		}
	}

	newemail = document.getElementById('recipientEmail'+lineNum).value
	newname = document.getElementById('recipientName'+lineNum).value

	var url = siteURL;

	url = url+'/members/'+document.getElementById('un').value+'/contacts/add?e='+urlencode(newemail)+'&n='+urlencode(newname);

	xmlHttp.open("POST",url,true);
	xmlHttp.send(null);

}

function checkAllContacts(ref) {

	var checkVal = false;

	if ( ref == 'check' ) {
		var checkVal = true;
	}

	inputs = document.getElementById('contactForm').elements;

	for (i=0; i < inputs.length; i++){
		if (inputs[i].type != 'checkbox') {
			continue;
		}

		inputs[i].checked = checkVal;
	}

}

function setContactsChoosePanelLoading() {

	document.getElementById('contactsChooseData').style.display='none';

	document.getElementById('contactsFunctionButton').innerHTML = '<a href="javascript:closeContactsChoosePanel()" title="close contacts">- close</a>';

	document.getElementById('contactsChooseLoading').innerHTML = loadingElement;
	document.getElementById('contactsChooseLoading').style.display='block';

	document.getElementById('contactsChoosePanel').style.display='block';

	document.getElementById('contactsChooseContainer').style.display='block';

}

function openContactsChoosePanel() {

	document.getElementById('contactsChooseLoading').style.display='none';
	document.getElementById('contactsChooseData').style.display='block';

	document.getElementById('contactsFunctionButton').innerHTML = '<a href="javascript:closeContactsChoosePanel()" title="close contacts">- close</a>';

	document.getElementById('contactsHeader').innerHTML = '<a href="javascript:closeContactsChoosePanel()" title="close contacts">choose contacts</a>';

	document.getElementById('contactsChoosePanel').style.display='block';
	document.getElementById('contactsChooseContainer').style.display='block';

}

function closeContactsChoosePanel() {
	tb_remove();
	return;
	document.getElementById('contactsChooseLoading').style.display='none';
	document.getElementById('contactsChooseData').style.display='block';

	document.getElementById('contactsChooseLoading').innerHTML = '';
	document.getElementById('contactsChooseData').innerHTML = '';

	document.getElementById('contactsFunctionButton').innerHTML = '<a href="javascript:chooseContacts()" title="open contacts">+ open</a>';

	document.getElementById('contactsHeader').innerHTML = '<a href="javascript:chooseContacts()" title="open contacts">choose contacts</a>';

	document.getElementById('contactsChoosePanel').style.display='none';
	//document.getElementById('fcSummary').style.display='block';

	if (isNon == 1) {
		document.getElementById('contactsChooseContainer').style.display='none';
	}

}

function closeImportPanel() {

	document.getElementById('importPanel').style.display='none';
	//document.getElementById('importPanelStartButton').style.display='block';

	document.getElementById('importFunctionButton').innerHTML = '<a href="javascript:openImportPanel()" title="click to start">+ open</a>';

	document.getElementById('importHeader').innerHTML = '<a href="javascript:openImportPanel()" title="click to start">import contacts</a>';

}

function openImportPanel() {

	// Reset the importFrame iframe to its default location.
	var f = document.getElementById('importFrame');
	f.src = importFrameDefaultSrc;

	document.getElementById('importPanel').style.display='block';
	//document.getElementById('importPanelStartButton').style.display='none';

	document.getElementById('importFunctionButton').innerHTML = '<a href="javascript:closeImportPanel()" title="cancel importing contacts">- close</a>';

	document.getElementById('importHeader').innerHTML = '<a href="javascript:closeImportPanel()" title="cancel importing contacts">import contacts</a>';

}

function closeContactFields() {

	document.getElementById('contactFields').style.display="none";
	document.getElementById('contactFieldsLoading').innerHTML = loadingElement;
	document.getElementById('contactFieldsLoading').style.display="block";

}

function openContactFields() {

	document.getElementById('contactFieldsLoading').style.display="none";
	document.getElementById('contactFields').style.display="block";

}

function closeImageBox() {

	document.getElementById('imageBox').style.display="none";
	document.getElementById('imageBoxFunctionButton').innerHTML = '<a href="javascript:openImageBox()" title="show image upload">click here</a>';

}

function openImageBox() {

	document.getElementById('imageBox').style.display="block";
	document.getElementById('imageBoxFunctionButton').innerHTML = '<a href="javascript:closeImageBox()" title="hide image upload">close</a>';

}

function submitRecipientForm(nextStep){
	jQuery('#nextStepEl').val(nextStep);
	document.getElementById('enterRecipients').submit();
}

function showWelcomeMessage(){

	var t = this.title || this.name || null;
	var a = siteURL+'/welcome?height=580&amp;width=670&amp;closer=no';
	var g = this.rel || false;

	tb_show(t,a,g);

}

function homeShowDiv(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.display = "block";
	}
}

function homeHideDiv(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.display = "none";
	}
}

function switchGraphic(id, src) {
	if (document.getElementById(id)) {
		document.getElementById(id).src = src;
	}
}
