var pb_IattachmentNo, pb_VattachmentNo;
pb_IattachmentNo = 0;
pb_VattachmentNo = 0;

function EmbedFromPhotobucket()
{
	var url = "embedfrom_pb/default.asp";
	displayMessage(url, 620, 580);
}

function photobucket_complete(inurl)
{
	if(IsPBImage(inurl) == 0)
		AttachPBimageIntoClient(inurl);
	else if(IsPBVideo(inurl) == 0)
		AttachPBvideoIntoClient(inurl);
}

function IsPBImage( htmlSrc )
{
	if(htmlSrc.toLowerCase().indexOf("<img ") != -1)
		return 0;
	else
		return 1;
}

function IsPBVideo( htmlSrc )
{
	if(htmlSrc.toLowerCase().indexOf("<embed ") != -1)
		return 0;
	else
		return 1;
}

function AttachPBimageIntoClient( html )
{
	var imageHTML = GetSrcImage(html);
	html = imageHTML;
	if(document.getElementById("hdNoOfImagesCSOIntf").value >= 5)
	{
		alert("You can embed one video or one audio or five images only in a nafnote.");
		return false;
	}

	html = AlterOrCreateAttribute(html, "width", maxImageWidth);
	html = AlterOrCreateAttribute(html, "height", maxImageHeight);

	pb_IattachmentNo += 1;
	document.getElementById("hdPBimgAttachments").value += imageHTML + "|";
	document.getElementById("hdNoOfImagesCSOIntf").value = eval(document.getElementById("hdNoOfImagesCSOIntf").value) + 1;
	htmlSrc = "<span id=\"PBiattachment" + pb_IattachmentNo + "\">";
	htmlSrc += "<table width=\"" + maxImageWidth + "\" height=\"" + maxImageHeight + "\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"imgfile\">" + html + "</td></tr></table>";
	if (!boolPreview)
	{
		htmlSrc += "<a href=\"javascript:void(0);\" onclick=\"CancelPBiAttachment('" + pb_IattachmentNo + "');\" class=\"smallstandardlink\">Remove</a><br/>";
	}
	htmlSrc += "</span>";	

	AddAttachmentAsHtml( htmlSrc );	
}

function AttachPBvideoIntoClient( html )
{
	var videoHTML = html;
	if(document.getElementById("hdNoOfVideoAudioCSOIntf").value >= 1)
	{
		alert("You can embed one video or one audio or five images only in a nafnote.");
		return false;
	}
	
	html = AlterOrCreateAttribute(html, "width", maxImageWidth);
	html = AlterOrCreateAttribute(html, "height", maxImageHeight);

	pb_VattachmentNo += 1;
	document.getElementById("hdPBvideoAttachments").value += videoHTML + "|";
	document.getElementById("hdNoOfVideoAudioCSOIntf").value = eval(document.getElementById("hdNoOfVideoAudioCSOIntf").value) + 1;
	htmlSrc = "<span id=\"PBvattachment" + pb_VattachmentNo + "\">";
	htmlSrc += html + "<input type=\"text\" style=\"display:none;\"/>";
	if (!boolPreview)
	{
		htmlSrc += "<br/><a href=\"javascript:void(0);\" onclick=\"CancelPBvAttachment('" + pb_VattachmentNo + "');\" class=\"smallstandardlink\">Remove</a><br/>";
	}
	htmlSrc += "</span>";	

	AddAttachmentAsHtml( htmlSrc );
}

function AlterOrCreateAttribute( htmlSrc, attrName, attrVal )
{
	var arrAttr, entityElement, i, modHtml, isAttrExits;
	i = 0;
	arrAttr = htmlSrc.split(" ");
	entityElement = arrAttr[0];
	modHtml = entityElement;
	isAttrExits = IsAttrExists(htmlSrc.toLowerCase(), attrName);

	if(isAttrExits == 1)
	{
		modHtml += " " + attrName + "=\"" + attrVal + "\"";
	}

	while (i < arrAttr.length-1)
	{
		i++;
		if(isAttrExits == 0 && arrAttr[i].indexOf(attrName + "=") != -1)
		{
			modHtml += " " + attrName + "=\"" + attrVal + "\"";
			isAttrExits = 1;
			continue;
		}

		modHtml += " " + arrAttr[i];
	}

	return modHtml;
}

function getAttributeVal( htmlSrc, attrName )
{
	var arrAttr, entityElement, i, isAttrExits, attrVal;
	i = 0;
	arrAttr = htmlSrc.split(" ");
	entityElement = arrAttr[0];
	isAttrExits = IsAttrExists(htmlSrc.toLowerCase(), attrName);

	if(isAttrExits == 1)
	{
		return;
	}

	while (i < arrAttr.length-1)
	{
		i++;
		if(isAttrExits == 0 && arrAttr[i].indexOf(attrName + "=") != -1)
		{
			attrVal = arrAttr[i].substring(arrAttr[i].indexOf(attrName + "=") + attrName.length + 2, arrAttr[i].lastIndexOf("\""));
			isAttrExits = 1;
			continue;
		}
	}

	return attrVal;
}

function IsAttrExists(htmlSrc, attr)
{
	if(htmlSrc.indexOf(attr + "=") != -1)
		return 0;
	else
		return 1;
}

function CancelPBiAttachment( attachmentNo )
{
	var sPBresources = document.getElementById("hdPBimgAttachments").value;
	var arrImagesColl = sPBresources.split("|");
	var i = 0;
	var resrc;
	document.getElementById("PBiattachment" + attachmentNo).innerHTML = "";
	document.getElementById("hdPBimgAttachments").value = "";
	while(i < arrImagesColl.length)
	{
		resrc = arrImagesColl[i];
		if(i == eval(attachmentNo-1))
		{
			resrc = "";
			document.getElementById("hdNoOfImagesCSOIntf").value = eval(document.getElementById("hdNoOfImagesCSOIntf").value) - 1;
		}
		document.getElementById("hdPBimgAttachments").value += resrc + "|";
		i++;
	}
}

function CancelPBvAttachment( attachmentNo )
{
	var sPBresources = document.getElementById("hdPBvideoAttachments").value;
	var arrVideosColl = sPBresources.split("|");
	var i = 0;
	var resrc;
	document.getElementById("PBvattachment" + attachmentNo).innerHTML = "";
	document.getElementById("hdPBvideoAttachments").value = "";
	while(i < arrVideosColl.length)
	{
		resrc = arrVideosColl[i];
		if(i == eval(attachmentNo-1))
		{
			resrc = "";
			document.getElementById("hdNoOfVideoAudioCSOIntf").value = eval(document.getElementById("hdNoOfVideoAudioCSOIntf").value) - 1;
		}
		document.getElementById("hdPBvideoAttachments").value += resrc + "|";
		i++;
	}
}

function LoadPBEmbeds( boolEditPreview )
{
	var sSelFiles = document.getElementById("hdPBimgAttachments").value;
	var noOfFilesSelected;
	var arrFilesColl;
	boolPreview = boolEditPreview;

	arrFilesColl = sSelFiles.split("|");
	noOfFilesSelected = arrFilesColl.length - 1;
	document.getElementById("hdPBimgAttachments").value = "";

	for(var i = 0;i < noOfFilesSelected;i++)
	{
		if(arrFilesColl[i] == "")
		{
			continue;
		}
		AttachPBimageIntoClient( arrFilesColl[i] );
	}

	document.getElementById("hdPBimgAttachments").value = sSelFiles;

	LoadPBvideos( boolEditPreview );
}

function LoadPBvideos( boolEditPreview )
{
	var sSelFiles = document.getElementById("hdPBvideoAttachments").value;
	var noOfFilesSelected;
	var arrFilesColl;
	boolPreview = boolEditPreview;

	arrFilesColl = sSelFiles.split("|");
	noOfFilesSelected = arrFilesColl.length - 1;
	document.getElementById("hdPBvideoAttachments").value = "";

	for(var i = 0;i < noOfFilesSelected;i++)
	{
		if(arrFilesColl[i] == "")
		{
			continue;
		}
		AttachPBvideoIntoClient( arrFilesColl[i] );
	}

	document.getElementById("hdPBvideoAttachments").value = sSelFiles;
}

function GetSrcImage( html )
{
	if(html.lastIndexOf("</a>") > 0)
		return html.substring(html.indexOf("<img "), html.lastIndexOf("</a>"));
	else
		return html.substring(html.indexOf("<img "));
}