/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */

componentMail = function(name) {
	this.componentElement = componentImage;
	this.componentElement(name);
};

componentMail.prototype = new componentImage();
componentMail.prototype.type = "componentMail";

componentMail.prototype.onBaseCreate = function(obj)
{
    this.resourcesURL = unescape(this.resourcesURL);
	this.captureMouseEvents();
};

componentMail.prototype.openWin = function(urlname,xpos,ypos,w,h,windowParam,windowName) 
{
	var newWindowPointer=null;
	x = xpos;
	y = ypos;

	if(!xpos) x = 100;
	if(!ypos) y = 100;
	if(!w) w = 400;
	if(!h) h = 300;

	if(!windowParam) windowParam = "resizable=yes";
	if(!windowName) windowName = "subWindow";

	if (newWindowPointer != null && newWindowPointer.closed==false) {
		newWindowPointer.close();
	}

	newWindowPointer=window.open(urlname,windowName,"width="+w+",height="+h+",screenX="+x+",screenY="+y+","+windowParam+"");
	newWindowPointer.moveTo(x,y);
	newWindowPointer.focus();
	return (newWindowPointer);
};

componentMail.prototype.sendMail = function() 
{
	var emailText;	
	var fieldName;
	var formValue;
	var str;	
	var counter		= 1;
	var mailWindow	= null;
	var nextLine		= "\n";

	appVer = navigator.appVersion;
	var mailF = "";
	if (appVer.indexOf("MSIE 5.0") > -1) nextLine="%0A";
	mailF = this.mailFormat;
	var textStart = mailF.replace( /(.*)(\[START\])(.*)(\[END\])(.*)/ig ,"$1");
	var textEl    = mailF.replace( /(.*)(\[START\])(.*)(\[END\])(.*)/ig ,"$3");
	var textEnd   = mailF.replace( /(.*)(\[START\])(.*)(\[END\])(.*)/ig ,"$5");
	var elementsAll = "";

	var val = "";
	emailText = textStart;
  
	if (!this.noTextInMail) {
	for (var i=0; i<page.children.length; i++)
	{
		if (page.children[i].type == 'componentText' || page.children[i].type == 'componentTextarea' || page.children[i].type == 'componentInput' || page.children[i].type == 'componentCheckBox' || page.children[i].type == 'componentRadioButton' || page.children[i].type == 'componentCheckBoxLight' || page.children[i].type == 'componentRadioButtonLight' || page.children[i].type == 'componentCombo' || page.children[i].type == 'componentList' || page.children[i].type == 'componentChoice' || page.children[i].type == 'componentSingleChoice' || page.children[i].type == "componentChoiceLight" || page.children[i].type == "componentSingleChoiceLight")
		{
		if (!this.isOnTheList(page.children[i].id))
		{
			switch(page.children[i].type)
			{
				case 'componentText':
				{
					val = page.children[i].text.replace(/(<[^<>]*>)/ig,"");
				}
				break;
				case 'componentTextarea':
				{
					val = document.getElementById(page.children[i].id+"Form").elements[0].value;
				}
				break;
				case 'componentInput':
				{
					val = document.getElementById(page.children[i].id+"_contentObj").value;
				}
				break;
				case ('componentCheckBox'):
				{
					val = page.children[i].checked;
				}
				break;
				case 'componentRadioButton':
				{
					val = page.children[i].checked;
				}
				break;
				case 'componentCheckBoxLight':
				{
					val = document.getElementById("inputCheckBox_"+page.children[i].id).checked;
				}
				break;
				case 'componentRadioButtonLight':
				{
					val = document.getElementById("inputRadioButton_"+page.children[i].id).checked;
				}
				break;
				case 'componentCombo':
				{
					var obj = document.getElementById(page.children[i].id+"Select");
					if (obj.selectedIndex != -1)
						val = obj.options[obj.selectedIndex].text;
					else val = "";
				}
				break;
				case 'componentList':
				{
					var obj = document.getElementById(page.children[i].id+"Select");
					if (obj.selectedIndex != -1)
						val = obj.options[obj.selectedIndex].text;
					else val = "";
				}
				break;
				case 'componentChoice':
				{
					val = "";
					var imgChecked = resourcesURL + page.children[i].imgChecked;
					for (var j in document.getElementById(page.children[i].id).getElementsByTagName("img"))
						if (j != 'length')
							if (document.getElementById(page.children[i].id).getElementsByTagName("img")[j].src == imgChecked)
							{
								var nameID = document.getElementById(page.children[i].id).getElementsByTagName("img")[j].name.match(/[0-9]*$/);
								if (val == "")
									val += document.getElementById(page.children[i].id+"_text_"+nameID).innerHTML;
								else val += ", "+document.getElementById(page.children[i].id+"_text_"+nameID).innerHTML;
							}
				}
				break;
				case 'componentSingleChoice':
				{
					val = "";
					var imgChecked = resourcesURL + page.children[i].imgChecked;
					for (var j in document.getElementById(page.children[i].id).getElementsByTagName("img"))
						if (j != 'length')
							if (document.getElementById(page.children[i].id).getElementsByTagName("img")[j].src == imgChecked)
							{
								var nameID = document.getElementById(page.children[i].id).getElementsByTagName("img")[j].name.match(/[0-9]*$/);
								val += document.getElementById(page.children[i].id+"_text_"+nameID).innerHTML;
							}
				}
				break;
				case 'componentChoiceLight':
				{
					val = "";
					for (var j in document.getElementById(page.children[i].id).getElementsByTagName("input"))
						if (j != 'length')
							if (document.getElementById(page.children[i].id).getElementsByTagName("input")[j].checked)
							{
								var nameID = document.getElementById(page.children[i].id).getElementsByTagName("input")[j].name.match(/[0-9]*$/);
								if (val == "")
									val += document.getElementById(page.children[i].id+"_text_"+nameID).innerHTML;
								else val += ", "+document.getElementById(page.children[i].id+"_text_"+nameID).innerHTML;
							}
				}
				break;
				case 'componentSingleChoiceLight':
				{
					val = "";
					var obj = document.getElementById(page.children[i].id).getElementsByTagName("input");
					for (var j = 0; j < obj.length; j++)
					{
						if (j != 'length')
							if (obj[j].checked)
							{
								var nameID = obj[j].id.match(/[0-9]*$/);
								val += document.getElementById(page.children[i].id+"_text_"+nameID).innerHTML;
							}
					}
				}
				break;
			}
			
			var textElBefore = textEl.replace(/(.*)(\[ELEMENT\])(.*)/ig,"$1");
			var textElAfter = textEl.replace(/(.*)(\[ELEMENT\])(.*)/ig,"$3");
			
			var objName = page.children[i].id.replace('__bckg_', '');
			emailText = emailText + textElBefore + objName + " = " + val + textElAfter + '\n\n';	
			counter++;
		}
	}
  }
	}
  emailText += textEnd;
  

	emailText = emailText.replace(/<BR>/ig, nextLine);
	emailText = emailText.replace(/&/ig, 'and');
	if (top.isWBTServer && !this.noTextInMail)
	{
		mailWindow = this.openWin("/4ses/servlet/MainServlet?method=lms.SendMail&email="+this.email+"&title=" + "E-mail from "+page.name + "&mailBody=" + emailText,5,5,100,100,"resizable=yes","mailWindow");
	}
	else
	{
		window.location="mailto:" + this.email + "?subject=" + "E-mail from "+page.name + "&body=" + emailText,5,5,100,100,"resizable=yes","mailWindow";
	}
};	

componentMail.prototype.componentMailBaseEventMouseOver = componentMail.prototype.eventMouseOver;
componentMail.prototype.eventMouseOver = function(e)
{
	if (this.srcIn) this.setValue(this.resourcesURL + this.srcIn);
	return this.componentMailBaseEventMouseOver(e);
};

componentMail.prototype.componentMailBaseEventMouseOut = componentMail.prototype.eventMouseOut;
componentMail.prototype.eventMouseOut = function(e)
{
	if (this.src) this.setValue(this.resourcesURL + this.src);
	return this.componentMailBaseEventMouseOut(e);
};

componentMail.prototype.isOnTheList = function(text)
{	
	var select = this.elementsToMail.split("||");
	for (var i=0; i < select.length; i++)
	{
		if (select[i] == text) return true;
	}
	return false;
};
componentMail.prototype.eventClick = function(e)
{
  this.sendMail();
  return false;
};
/********************************************************************/
