var curobj=0;

var Helps = {};
Helps.Welcome='Welcome to business card designer. We\'re proud to present unique opportunity for your business to design own cards the easiest possible way.<p>You can design cards using our templates or you can create your own template and reuse it later for all your employees.';

Helps.TextClick='You can move text around by holding left mouse button and moving mouse at same time. You can also change text font or color. Click on desired color on a left in order to change color. Use pull down menu in order to change font size or style please. Text attributes such as bold, italic or underline can be set or unset by clicking on <b>B</b> or <b>I</b> or <b>U</b> icons below of pull down font menu';
Helps.Colors = 'In order to change color of the text please click on text line you want to change first. You can only change whole line at a time.';
Helps.Image = 'Click on any of these pictures if you\'d like to change background imgage. You may also use your own background image, but method is under development yet';
Helps.BoldFont = 'You can set or unset bold font by clicking on icon, however effect can be invisible if font is very small';
Helps.ItalicFont = 'By clicking on this icon you can set or unset italic font style for your text. Style can only be set for whole line, not single charachter';
Helps.UnderlineFont = 'By clicking on this icon you can make underline text or remove underline effect. Style can only be set for whole line, not single charachter';
Helps.AddNewText = 'Add new text to your card.';
Helps.NewImage = 'New Image options is not yet available';
Helps.Delete = 'To delete text or image please click on object first';

Helps.Empty = '';

function HtmlToObject(Card) {
	for(var i = 0; i < Card.Items.length; i++) {
		if( Card.Items[i] != null && Card.Items[i].text  ) {
			var obj = eval( 'document.all.textfield' + i );
			
			Card.Items[i].text = obj.value ? obj.value : Card.Items[i].text;
			Card.Items[i].left = obj.style.left ? obj.style.left : Card.Items[i].left;
			Card.Items[i].top = obj.style.top ? obj.style.top : Card.Items[i].top;
			Card.Items[i].fontsize = obj.style.fontsize ? obj.style.fontsize : Card.Items[i].fontsize;
			Card.Items[i].fontFamily = obj.style.fontFamily ? obj.style.fontFamily : Card.Items[i].fontFamily;
			Card.Items[i].fontWeight = obj.style.fontWeight ? obj.style.fontWeight : Card.Items[i].fontWeight;
			Card.Items[i].textDecoration = obj.style.textDecoration ? obj.style.textDecoration : Card.Items[i].textDecoration;
			Card.Items[i].fontStyle = obj.style.fontStyle ? obj.style.fontStyle : Card.Items[i].fontStyle;
			Card.Items[i].color = obj.style.color ? obj.style.color : Card.Items[i].color;
		}
	}
}


function SendForm(Card) {
	var Final='';
        Final += 'FirstName=' + sendme.FirstName.value + ',';
        Final += 'LastName=' + sendme.LastName.value + ',';
        Final += 'Email=' + sendme.Email.value + ',';
        Final += 'Company=' + sendme.Company.value + ',';
        Final += 'Street=' + sendme.Street.value + ',';
        Final += 'City=' + sendme.City.value + ',';
        Final += 'State=' + sendme.State.value + ',';
        Final += 'Zip=' + sendme.Zip.value + ',';
        Final += 'Phone=' + sendme.Phone.value + ',';
        Final += 'Fax=' + sendme.Fax.value + ',';
        Final += 'Notes=' + sendme.Notes.value + ',';
        Final += 'image=' + sendme.image.value + ',';  
        HtmlToObject(Card);
	for(var i = 0; i < Card.Items.length; i++) {
		if( Card.Items[i] != null && Card.Items[i].text  ) {
			var obj = eval( 'document.all.textfield' + i );
			Final += 'id=' + i + ',';
			Final += 'text=' + escape(obj.value ? obj.value : Card.Items[i].text,1) + ',';
			Final += 'left=' + escape(obj.style.left ? obj.style.left : Card.Items[i].left,1) + ',';
			Final += 'top=' + escape(obj.style.top ? obj.style.top : Card.Items[i].top,1) + ',';
			Final += 'fontsize=' + escape(obj.style.fontsize ? obj.style.fontsize : Card.Items[i].fontsize,1) + ',';
			Final += 'fontFamily=' + escape(Card.Items[i].fontFamily,1) + ',';
			Final += 'fontWeight=' + escape(Card.Items[i].fontWeight,1) + ',';
			Final += 'textDecoration=' + escape(Card.Items[i].textDecoration,1) + ',';
			Final += 'fontStyle=' + escape(Card.Items[i].fontStyle,1) + ',';
			Final += 'color=' + escape(Card.Items[i].color,1);
			Final += ':';
		}
	}
	document.all.ready.value=Final;
	alert(Final);
	document.form[0].submit;
}

function RegsMe(o) {
	var e = window.event;
	o.LeftMoveOffset = e.clientX - o.offsetLeft;
	o.TopMoveOffset = e.clientY - o.offsetTop;
	curobj=o;
}

function MoveMe(o) {
	var e = window.event;
	if( e.button ) {
		o.style.left=e.clientX - o.LeftMoveOffset;
		o.style.top=e.clientY - o.TopMoveOffset;
	}
}

function SetBgr(how) {
	var bgr = 'url(\'' + how + '\')';
	document.all.mybgr.style.backgroundImage='url(\'' + how + '\')';
	document.all.image.value=how;
}

function SetFontName(how) {
	if(curobj.arrayid) {
		curobj.style.fontFamily=how;
	}
}

function SetFontSize(how) {
	if(curobj.arrayid) {
		curobj.style.fontSize=how;
	}
}

function SetColor(color) {
	if(curobj.arrayid) {
    	curobj.style.color='#' + color;
    }
}

function FlipFont(how) {
	if(curobj.arrayid) {
		if( how == 'b' ) {
			if( curobj.style.fontWeight == 'bold' ) {
				curobj.style.fontWeight='normal';
			} else {
				curobj.style.fontWeight='bold';
			}
		}
		if( how == 'i' ) {
			if( curobj.style.fontStyle == 'italic' ) {
				curobj.style.fontStyle='normal';
			} else {
				curobj.style.fontStyle='italic';
			}
		}
		if( how == 'u' ) {
			if( curobj.style.textDecoration == 'underline' ) {
				curobj.style.textDecoration='none';
			} else {
				curobj.style.textDecoration='underline';
			}
		}
	}
}

function DeleteField(Card) {
	HtmlToObject(Card);
	var x = 0;
	if(curobj.arrayid) {
		delete Card.Items[curobj.arrayid];
		for(var i = 0; i < Card.Items.length; i++) {
			if( Card.Items[i] != null ) {
				Card.Items[x++] = Card.Items[i];
			}
		}
		Card.Items.length=x;
	}
}

function CardToHtml(Card) {
	var out='';
	for(var i = 0; i < Card.Items.length; i++) {
		if( Card.Items[i] != null && Card.Items[i].text  ) {
			out += '<input onClick="RegsMe(this)" onMouseDown="RegsMe(this)" onMouseOver="document.all.Help.innerHTML=Helps.TextClick" onMouseMove="MoveMe(this)" ';
			out += 'type=text name="textfield' + i + '" value="' + Card.Items[i].text + '" arrayid="' + i + '" ';

			Card.Items[i].left = Card.Items[i].left ? Card.Items[i].left : 20;
			out += 'style="left:' + Card.Items[i].left + '; ';

			Card.Items[i].top = Card.Items[i].top ? Card.Items[i].top : (i+1) * 20;
			out += 'top:' + Card.Items[i].top + '; ';
			
			Card.Items[i].fontsize = Card.Items[i].fontsize ? Card.Items[i].fontsize : '10';
			out += 'font-size:' + Card.Items[i].fontsize + '; ';
			
			Card.Items[i].fontFamily = Card.Items[i].fontFamily ? Card.Items[i].fontFamily : 'Arial';
			out += 'font-family:' + ( Card.Items[i].fontFamily ? Card.Items[i].fontFamily : 'Arial') + '; ';
			
			Card.Items[i].fontWeight = Card.Items[i].fontWeight ? Card.Items[i].fontWeight : 'normal';
			out += 'font-Weight:' + Card.Items[i].fontWeight + '; ';
			
			Card.Items[i].textDecoration = Card.Items[i].textDecoration ? Card.Items[i].textDecoration : 'none';
			out += 'text-decoration:' + Card.Items[i].textDecoration + '; ';
			
			Card.Items[i].fontStyle = Card.Items[i].fontStyle ? Card.Items[i].fontStyle : 'normal';
			out += 'font-style:' + Card.Items[i].fontStyle + '; ';
			
			Card.Items[i].color = Card.Items[i].color ? Card.Items[i].color : '#000000';
			out += 'color:' + Card.Items[i].color + '; ';
			
			out += 'position: absolute" class="Card" size="40">';
		}
		if( Card.Items[i] != null && Card.Items[i].image  ) {
		    out += '<img src="' + Card.Items[i].image + ' style="';
		    Card.Items[i].top = Card.Items[i].top ? Card.Items[i].top : (i+1) * 20;
		    out += 'top:' + Card.Items[i].top + '; ';
		    Card.Items[i].left = Card.Items[i].left ? Card.Items[i].left : 20;
		    out += 'style="left:' + Card.Items[i].left + '; "';
		    out += 'border=0>';
		}
	}
	return(out);
}

function AddNewField(Card) {
	HtmlToObject(Card)
	var vname = 'textfield' + Card.Items.length;
	Card.Items[Card.Items.length]={ text: 'This is new text field' };
}
