var emailImagesLightboxName = "";
var emailImagesLightboxClient = "";
var emailGuid = "";
var getEmailGuidImageIds = "";

function emailRequestImages(type, fromName, fromEmail, targetContainer, lightboxId)
{
	emailImagesLightboxName = "";
	emailImagesLightboxClient = "";
	
	divHide('popLayer',true);
	
	if(type == "email" && readCookie("TrunkAccount") == "0")
	{
		location.href = "LogOn.aspx";
		return true;
	}
	
	if(type == "email" && lightboxId)
		dataRequest(serverUrl + "ajax/Lightbox.aspx?action=info&content=null&lightboxId=" + lightboxId, emailImagesGetLightboxInfo);

	var popObject = new Object();
	
	popObject.closeLowerAccess = true;
	popObject.darkOutBackground = true;
	
	popObject.content =  '<table width="525" height="410" border="0" cellspacing="0" cellpadding="5">';
	popObject.content += '	<tr>';
	popObject.content += '		<td valign="top"><div style="overflow: hidden;"><div style="position: relative; width: 515px; height: 400px; overflow: hidden;" id=\"plScrollArea\">';
	
	var container, containerNode, imageId;
	
	container = document.getElementById(targetContainer);
	
	var imageItem = null;
	
	for(var i=0; i<container.childNodes.length; i++)
	{
		containerNode = container.childNodes[i];

		if(containerNode.nodeType == 1)
		{
			imageId = containerNode.id.replace("div","").replace("Lightbox","").replace("lightboxPageDiv","");
			
			if(imageArray[imageId] != null)
				imageItem = imageArray[imageId];
			else
				imageItem = lightboxImageArray[imageId];
		
			popObject.content += '		<div style="position: relative; margin: 0; float:left; height: 130px;">';
			popObject.content += '		<table width="90" height="125" border="0" cellspacing="0" cellpadding="3">';
			popObject.content += '			<tr>';
			popObject.content += '				<td valign="bottom" align="left"><a href="JavaScript: toogleCheckbox(\'requestChb' + imageId + '\');" onfocus="blur()"><img src="' + imageDir + '89s/' + imageItem.no + '.jpg" alt="" border="0"/></a></td>';
			popObject.content += '			</tr><tr valign="bottom" height="20"><td align="left"><table cellspacing="0" cellpadding="0"><tr>';			
			popObject.content += '				<td><input type="checkbox" value="' +  imageId + '" name="requestChb' + imageId + '" id="requestChb' + imageId + '" checked></td>';
			popObject.content += '				<td><a href="JavaScript: toogleCheckbox(\'requestChb' + imageId + '\');" onfocus="blur()">' + imageItem.no + '</a></td>';
			popObject.content += '			</tr></table></td></tr>';
			popObject.content += '		</table>';
			popObject.content += '		</div>';
		}
	}
	
	popObject.content += '		</div></div></td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';
	popObject.content += '<table width="525" border="0" cellspacing="0" cellpadding="10">';
	popObject.content += '	<tr>';
	
	if(type == "lightbox")
	{
		popObject.content += '		<td width="50%" align="left" valign="top">Target lightbox: ';
		
		popObject.content += "                    <div style=\"overflow: hidden; width: 191px; height: 20px; background-image: url(gfx/combo_191.gif);\" onclick=\"comboSelect(document.getElementById('addToLightboxSelected'));\" onmouseover=\"comboSelectOnMouseOver('addToLightboxSelected');\" onmouseout=\"comboSelectOnMouseOut('addToLightboxSelected');\">";
        popObject.content += '                        <nobr>';
        popObject.content += '                            <input id="addToLightboxSelected" type="text" style="width: 155px; margin-top: 3px;" class="input_box" onfocus="comboSelect(this);" value="" />';
        popObject.content += '                            <select id="addToLightbox" class="select_combo_table" onchange="javascript:comboUpdateField(this);">';        
		
		var selDdl = document.getElementById('lightboxSelectorDdl');
		
		for(i=0; i<selDdl.options.length; i++)
		{
			if(selDdl.options[i].value == selDdl.value)
				popObject.content += '<option selected="selected" value="' + selDdl.options[i].value + '">' + selDdl.options[i].text + '</option>';
			else
				popObject.content += '<option value="' + selDdl.options[i].value + '">' + selDdl.options[i].text + '</option>';
		}
		
		popObject.content += '                            </select>';
        popObject.content += '                        </nobr>';
        popObject.content += '                    </div>';
		
				
		popObject.content += '		<br>';
	}
	else
		popObject.content += '		<td width="50%" align="left" valign="top">';
	
	popObject.content += '		<a href="JavaScript:toogleAllReqestImageCheckboxes(true, \'' + targetContainer + '\');" onfocus="blur()">Select all images</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	popObject.content += '		<a href="JavaScript:toogleAllReqestImageCheckboxes(false, \'' + targetContainer + '\');" onfocus="blur()">Deselect all images</a></td>';
	popObject.content += '		<td width="50%" align="right" valign="bottom">';
	popObject.content += '		<table border="0" cellspacing="0" cellpadding="0">';
	popObject.content += '			<tr>';
	popObject.content += '				<td><a href="JavaScript:divHide(\'popLayer\',' + popObject.closeLowerAccess + ');" onfocus="blur()"><img src="gfx/button_cancel.gif" alt="" width="52" height="20" border="0"/></a></td>';
	popObject.content += '				<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
	
	if(type == "request")
		popObject.content += '				<td><a href="Javascript:requestImagesSubmit(\'' + targetContainer + '\');" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="66" height="20" border="0"/></a></td>';
	
	if(type == "email")
		popObject.content += '				<td><a href="Javascript:emailImages(\'' + fromName + '\',\'' + fromEmail + '\', null, \'' + targetContainer + '\');" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="66" height="20" border="0"/></a></td>';
	
	if(type == "lightbox")
		popObject.content += '				<td><a href="Javascript:addSelectedImagesToLightbox(\'' + targetContainer + '\');" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="66" height="20" border="0"/></a></td>';
	
	if(type == "otherUsage")
		popObject.content += '				<td><a href="Javascript:createOrderByLightbox(\'' + targetContainer + '\', \'otherUsage\');" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="66" height="20" border="0"/></a></td>';
	
	if(type == "license")
		popObject.content += '				<td><a href="Javascript:createOrderByLightbox(\'' + targetContainer + '\', \'license\');" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="66" height="20" border="0"/></a></td>';

	popObject.content += '			</tr>';
	popObject.content += '		</table>';
	popObject.content += '		</td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';
	
	document.getElementById("popLayerContent").innerHTML = popObject.content;
	
	var alertBoxHtml = '<div id="popLayerAlert"></div>';
	
	if(type == "request")
		document.getElementById("popLayerHeadline").innerHTML = "<b>Request images - Select images</b>" + alertBoxHtml;
		
	if(type == "email")
		document.getElementById("popLayerHeadline").innerHTML = "<b>Email images - Select images</b>" + alertBoxHtml;
	
	if(type == "lightbox")
		document.getElementById("popLayerHeadline").innerHTML = "<b>Add images to lightbox - Select images</b>" + alertBoxHtml;
	
	if(type == "otherUsage")
		document.getElementById("popLayerHeadline").innerHTML = "<b>Create order (Other usage) - Select images</b>" + alertBoxHtml;
	
	if(type == "license")
		document.getElementById("popLayerHeadline").innerHTML = "<b>Create order (License) - Select images</b>" + alertBoxHtml;
	
	document.getElementById('popLayerTopSizer').width = 1;		
	ArtistWebEffects.set_opacity(0,"popLayer");				
	divShow('popLayer', 1, 1, popObject.closeLowerAccess, popObject.darkOutBackground);	
	popLayerReposition();		
	ArtistWebEffects.set_opacity(100,"popLayer");					
	var pls = document.getElementById("plScrollArea");	
	pls.style.overflow = "auto";
	
	if (type == "lightbox") {
	
	    comboUpdateField(document.getElementById("addToLightbox"));
	}
}

function toogleAllReqestImageCheckboxes(checked, targetContainer)
{
	var container, containerNode;
	
	container = document.getElementById(targetContainer);

	for(var i=0; i<container.childNodes.length; i++)
	{
		containerNode = container.childNodes[i];

		if(containerNode.nodeType == 1)
			document.getElementById('requestChb' + containerNode.id.replace("div","").replace("Lightbox","").replace("lightboxPageDiv","")).checked = checked;
	}
}

function requestImagesSubmit(targetContainer)
{
	var imageIds = returnSelectedEmailRequestImages(targetContainer);
	
	if(imageIds != "")
		location.href = serverUrl + "RequestPrice.aspx?imageIds=" + imageIds;
	else
		document.getElementById("popLayerAlert").innerHTML = '<b class="red">No images was selected.</b>';
}




function emailImages(fromName, fromEmail, imageId, targetContainer)
{	
	emailGuid = "";
	getEmailGuidImageIds = "";
	
	var isResearcher = false;
	if (readCookie("isEmployee") == "1" || readCookie("isResearcher") == "1" || readCookie("isAgent") == "1")
	{
		isResearcher = true;
		var mailBody = "Mail body text";
	}
	
	if(readCookie("TrunkAccount") == "0")
	{
		location.href = "Image.aspx?image=" + imageId;
		return true;
	}
	
	var imageIds;
	
	if(imageId)
	{
		imageIds = imageId;		
		emailImagesLightboxName = "";
		emailImagesLightboxClient = "";
	}
	else
		imageIds = returnSelectedEmailRequestImages(targetContainer);
	
	if(imageIds != "")
	{
		getEmailGuidImageIds = imageIds;
		divHide('popLayer',true);
		
		var popObject = new Object();
		
		popObject.closeLowerAccess = true;
		popObject.darkOutBackground = true;
		
		popObject.content =  '<table width="400" border="0" cellspacing="0" cellpadding="8">';
		popObject.content += '	<tr>';
		popObject.content += '		<td valign="top" align="left"><div id="popLayerEmailAlert" style="display: none; color: #FF0000;"></div>';
		
		popObject.content += '		From name:<br>';
		popObject.content += '		<input id="popLayerEmailFromName" type="text" style="width: 384px;" value="' + fromName + '"><br>';
		popObject.content += '		<br>';
		//popObject.content += '		From email:<br>';
		//popObject.content += '		<input id="popLayerEmailFromEmail" type="text" style="width: 384;" value="' + fromEmail + '"><br>';
		//popObject.content += '		<br>';
		popObject.content += '		To email: separate email addresses with ";"<br>';
		popObject.content += '		<input id="popLayerEmailToEmail" type="text" style="width: 384px;"><br>';
		popObject.content += '		<br>';
			
		if (isResearcher) {
			popObject.content += '	  <span>Display images as:</span><br>';			
			
			if (isOldIE) {
                popObject.content += '                            <select id="popLayerEmailType">';
                popObject.content += '                                <option value="lightbox" selected="true">Lightbox</option><option value="portfolio">Portfolio</option>';
                popObject.content += '                            </select>';
            } else {
			
			    popObject.content += "                    <div style=\"overflow: hidden; width: 191px; height: 20px; background-image: url(gfx/combo_191.gif);\" onclick=\"comboSelect(document.getElementById('popLayerEmailTypeSelected'));\" onmouseover=\"comboSelectOnMouseOver('popLayerEmailTypeSelected');\" onmouseout=\"comboSelectOnMouseOut('popLayerEmailTypeSelected');\">";
                popObject.content += '                        <nobr>';
                popObject.content += '                            <input id="popLayerEmailTypeSelected" type="text" style="width: 155px; margin-top: 3px;" class="input_box" onfocus="comboSelect(this);" value="Lightbox" />';
                popObject.content += '                            <select id="popLayerEmailType" class="select_combo_table" onchange="javascript:comboUpdateField(this);">';
                popObject.content += '                                <option value="lightbox" selected="true">Lightbox</option><option value="portfolio">Portfolio</option>';
                popObject.content += '                            </select>';
                popObject.content += '                        </nobr>';
                popObject.content += '                    </div>';
    			
			}
			
			popObject.content += '		<br><br>';
		
		
			popObject.content += '	<div id="popLayerEmailTypeDiv" style="display: none;">';
			
			popObject.content += '		Subject:<br>';
			popObject.content += '		<input id="popLayerEmailSubject" type="text" style="width: 384px;" value="' + fromName + ' has sent you art from Trunk Archive" /><br><br>';
			
			popObject.content += '		Mail body:<br>';
			popObject.content += '		<textarea id="popLayerEmailBody" style="width: 384px; height: 200px;"></textarea>';
			popObject.content += '	</div>';
					
		}
		
		popObject.content += '	<div id="popLayerEmailCommentDiv">';			
		popObject.content += '		Comment:<br>';
		
		if(emailImagesLightboxName != "")
			popObject.content += '		<textarea id="popLayerEmailComment" style="width: 384px; height: 200px;">Lightbox: ' + emailImagesLightboxName + '\r\n</textarea>';
		else
			popObject.content += '		<textarea id="popLayerEmailComment" style="width: 384px; height: 200px;"></textarea>';	
		
		popObject.content += '	</div>';
		
		popObject.content += '		</td>';
		popObject.content += '	</tr>';
		popObject.content += '</table>';
		popObject.content += '<table width="400" border="0" cellspacing="0" cellpadding="10">';
		popObject.content += '	<tr>';
		popObject.content += '		<td align="right">';
		popObject.content += '		<table border="0" cellspacing="0" cellpadding="0">';
		popObject.content += '			<tr>';
		popObject.content += '				<td><a href="JavaScript:divHide(\'popLayer\',' + popObject.closeLowerAccess + ');" onfocus="blur()"><img src="gfx/button_cancel.gif" alt="" width="52" height="20" border="0"/></a></td>';
		popObject.content += '				<td><img src="gfx/_blank.gif" alt="" width="10" height="1" border="0"/></td>';
		popObject.content += '				<td><a href="Javascript:emailImagesSubmit(\'' + imageIds +'\');" onfocus="blur()"><img src="gfx/button_submit.gif" alt="" width="52" height="20" border="0"/></a></td>';
		popObject.content += '			</tr>';
		popObject.content += '		</table>';
		popObject.content += '		</td>';
		popObject.content += '	</tr>';
		popObject.content += '</table>';
		
		document.getElementById("popLayerContent").innerHTML = popObject.content;
		document.getElementById("popLayerHeadline").innerHTML = "<b>Email images - Submit</b>";
		
		document.getElementById('popLayerTopSizer').width = 1;
		
		ArtistWebEffects.set_opacity(0,"popLayer");				
		divShow('popLayer', 1, 1, popObject.closeLowerAccess, popObject.darkOutBackground);
		popLayerReposition();
		ArtistWebEffects.set_opacity(100,"popLayer");				
	}
	else
	{
		document.getElementById("popLayerAlert").innerHTML = '<b class="red">No images was selected.</b>';
	}
}

function updateEmailForm() {
	
	var typeDiv = document.getElementById("popLayerEmailTypeDiv");
	var typeSelect = document.getElementById("popLayerEmailType");
	var commentDiv = document.getElementById("popLayerEmailCommentDiv");
	
	if (typeSelect.options[typeSelect.selectedIndex].value == "portfolio") {		
			
		if (emailGuid == "") {
		
			emailImagesGetGuid(getEmailGuidImageIds);
			
		} else {
			commentDiv.style.display = "none";
			typeDiv.style.display = "block";
		}				

	} else {
	
		typeDiv.style.display = "none";
		commentDiv.style.display = "block";
	}
	
	
}

function emailImagesGetGuid(imageIds)
{
	var error = "";
	
	var fromName = document.getElementById('popLayerEmailFromName');
	var fromEmail = "";//document.getElementById('popLayerEmailFromEmail');
	var toEmail = "";
	var typeSelect = document.getElementById('popLayerEmailType');
	
	var type = "guid"; 		
	var comment = "";
	
	//if(fromName.value == "")
	//	error += '"From name" is missing or contains none alpha characters.<br>';
			
	if(error == "")
	{
		dataRequest(serverUrl + "ajax/Mail.aspx?action=getEmailGuid&imageIds=" + imageIds + "&type=" + type + "&fromName=" + encodeURI(fromName.value) + "&fromEmail=&toEmail=&comment=", emailImagesGetEmailGuid);		
	}
	else
	{
		document.getElementById('popLayerEmailAlert').innerHTML = "<b>" + error + "</b><br>";
		document.getElementById('popLayerEmailAlert').style.display = "block";
	}
}



function emailImagesSubmit(imageIds)
{
	var error = "";
	
	var fromName = document.getElementById('popLayerEmailFromName');
	var fromEmail = "";//document.getElementById('popLayerEmailFromEmail');
	var toEmail = document.getElementById('popLayerEmailToEmail');
	var typeSelect = document.getElementById('popLayerEmailType');
	var subject = "";		
	
	var type = "lightbox"; 
	if (typeSelect != null) type = typeSelect.options[typeSelect.selectedIndex].value;
	
	var comment = document.getElementById('popLayerEmailComment');

    /*	
	if (type == "portfolio") {
		subject = document.getElementById('popLayerEmailSubject');
		comment = document.getElementById('popLayerEmailBody');
	}
	*/
		
	// if(!isValidEmail(toEmail.value))
	//	error += '"To email" is not a valid email<br>';

	var re = new RegExp("[<>]", "g");
	
	if(comment.value != comment.value.replace(re, ""))
			error += 'Only characters and numbers are allowed in "comment"<br>';
	

	if(error == "")
	{	    
		dataRequest(serverUrl + "ajax/EmailImages.aspx?action=emailImages&imageIds=" + imageIds + "&type=" + type + "&fromName=" + encodeURI(fromName.value) + "&fromEmail=" + fromEmail.value + "&toEmail=" + toEmail.value + "&comment=" + encodeURI(comment.value) + "&subject=" + encodeURI(subject.value), doSendEmailedImages);		
	}
	else
	{
		document.getElementById('popLayerEmailAlert').innerHTML = "<b>" + error + "</b><br>";
		document.getElementById('popLayerEmailAlert').style.display = "block";
	}
}


function doSendEmailedImages(xmlFeed, status, url) {
    if(status == 0)
	{
		var xmlDoc = xmlFeed.responseXML;
		var toemailnode = xmlDoc.getElementsByTagName('toemail').item(0);
		var subjectnode = xmlDoc.getElementsByTagName('subject').item(0);
		var mailbodynode = xmlDoc.getElementsByTagName('mailbody').item(0);
		
		var subject = subjectnode.firstChild.nodeValue;				
		var mailbody = mailbodynode.firstChild.nodeValue;
		
		var toemail = toemailnode.firstChild.nodeValue;
		
		
				
		if(null != mailbody && mailbody != "" && null != subject && subject != "")
		{
	        var mailToLink = "mailto:" + toemail + "?subject=" + escape(subject) + "&body=" + escape(mailbody);    		
	        
	        document.location.href = mailToLink;
	        
	        divHide('popLayer',true);
		}
	}
}

function returnSelectedEmailRequestImages(targetContainer)
{
	var container = document.getElementById(targetContainer);
	
	var containerNode = "";
	var imageIds = "";

	for(var i=0; i<container.childNodes.length; i++)
	{
		containerNode = container.childNodes[i];

		if(containerNode.nodeType == 1)
		{
			if(document.getElementById('requestChb' + containerNode.id.replace("div","").replace("Lightbox","").replace("lightboxPageDiv","")).checked)
			{
				if(imageIds == "")
					imageIds += containerNode.id.replace("div","").replace("Lightbox","").replace("lightboxPageDiv","");
				else
					imageIds += "," + containerNode.id.replace("div","").replace("Lightbox","").replace("lightboxPageDiv","");
			}
		}
	}
	
	return imageIds;
}

function emailImagesSubmitThanks()
{
	divHide('popLayer',true);
	
	var popObject = new Object();
	
	popObject.closeLowerAccess = true;
	popObject.darkOutBackground = true;
	
	popObject.content =  '<table width="400" border="0" cellspacing="0" cellpadding="8">';
	popObject.content += '	<tr>';
	popObject.content += '		<td valign="top" align="left">Email has been sent.</td>';
	popObject.content += '		</td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';
	popObject.content += '<table width="400" border="0" cellspacing="0" cellpadding="10">';
	popObject.content += '	<tr>';
	popObject.content += '		<td align="right">';
	popObject.content += '		<table border="0" cellspacing="0" cellpadding="0">';
	popObject.content += '			<tr>';
	popObject.content += '				<td><a href="JavaScript:divHide(\'popLayer\',' + popObject.closeLowerAccess + ');" onfocus="blur()"><img src="gfx/button_continue.gif" alt="" width="66" height="20" border="0"/></a></td>';
	popObject.content += '			</tr>';
	popObject.content += '		</table>';
	popObject.content += '		</td>';
	popObject.content += '	</tr>';
	popObject.content += '</table>';	
	
	document.getElementById("popLayerContent").innerHTML = popObject.content;
	document.getElementById("popLayerHeadline").innerHTML = "<b>Email images - submit</b>";
	document.getElementById('popLayerTopSizer').width = 1;

    ArtistWebEffects.set_opacity(0,"popLayer");				
	divShow('popLayer', Math.floor((document.body.clientWidth - 400)/2), 100, popObject.closeLowerAccess, popObject.darkOutBackground);		
	popLayerReposition();
	ArtistWebEffects.set_opacity(100,"popLayer");				
}

function emailImagesGetEmailGuid(xmlFeed, status, url)
{
	if(status == 0)
	{
		var xmlDoc = xmlFeed.responseXML;
		var node = xmlDoc.getElementsByTagName('emailGuid').item(0);
		
		var emailGuid = node.getAttributeNode("guid").value;				
		while (emailGuid.indexOf("-") >= 0)
		    emailGuid = emailGuid.replace("-", "");
		    
	    var url = serverUrl.replace("www.", "");
		
		var mailBody = document.getElementById("popLayerEmailBody");
		if(null != mailBody)
		{
	        mailBody.innerHTML = "Click this link to view the portfolio:\n\n";
	        mailBody.innerHTML += serverUrl + "ep/" + emailGuid;
		
    		updateEmailForm();
		}
	}
}


function emailImagesGetLightboxInfo(xmlFeed, status, url)
{
	if(status == 0)
	{
		var xmlDoc = xmlFeed.responseXML;
		var node = xmlDoc.getElementsByTagName('lightbox').item(0);
		emailImagesLightboxName = node.getAttributeNode("lightboxname").value;
		emailImagesLightboxClient = node.getAttributeNode("lightboxclient").value;
	}
}

function addSelectedImagesToLightbox(targetContainer)
{
	var imageIds = returnSelectedEmailRequestImages(targetContainer);
	
	var targetLightBox = document.getElementById('addToLightbox').value;
	var currentLightBox = document.getElementById('lightboxSelectorDdl').value;
	
	dataRequest(serverUrl + "ajax/Lightbox.aspx?action=addImages&lightboxId=" + targetLightBox + "&content=" + imageIds, insureSeverUpdate);
	
	if(targetLightBox == currentLightBox)
	    dataRequest(serverUrl + "ajax/Lightbox.aspx?action=getImages&lightboxId=" + lightboxId + "&content=null", getLightBoxFromServer);

	divHide('popLayer',true);
}
