function redirect(url) {
	window.location = url;
}

function doTitleCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'titleCommand';
	mainform.submit();
}

function doAndroidPublishFinalBrochureCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'androidPublishBrochureCommandFinal';
	mainform.submit();
}

function doAndroidPublishTestingBrochureCommand(event){
	var mainform = $('mainform');
	mainform.command.value = 'androidPublishBrochureCommandTesting';
	mainform.submit();
}

function updateRequest(){
	var mainform = $('mainform');
	mainform.updateState.value = "true";
}

function doNewImagesCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'newImagesCommand';
	mainform.submit();
}

function doCopyPagesCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'copyPageCommand';
	mainform.submit();
}


function doNewPagesCommand(event) {	
	if($('isPlatformSelected') && $('isPlatformSelected').value == 1){
		if(undefined != $('title') && !validateBrochureTitle($F('title'))){
			return;	
		}
		var mainform = $('mainform');
		mainform.command.value = 'newPagesCommand';
		mainform.submit();
	}	
	else{
		showWarningMessage('<div align="center">Please select the platform to proceed.</div>');
	}
}

function doUpdateTitleCommand(event) {
	if(validateBrochureTitle($F('title'))){
		var mainform = $('mainform');
		mainform.command.value = 'updateTitleCommand';
		mainform.submit();		
	}
}

function doCreateTitleCommand(event) {
	if(validateBrochureTitle($F('title'))){
		 var mainform = $('mainform');
		 mainform.command.value = 'createTitleCommand';
		 mainform.submit();
	}

}

function doCopyBrochureCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'copyBrochureCommand';
	mainform.submit();	
}

function isPlatformChecked(){		
	return (($('j2me_check') && $F('j2me_check') === 'true')) || (($('android_check') && $F('android_check') === 'true')) || (($('iphone_check') && $F('iphone_check') === 'true')); 	
}


function doCancelCommand() {
    var mainform = $('mainform');
    mainform.action = 'colorschemes.html';
    mainform.colorSchemeId.value = '-1';
    mainform.submit();
}

function editColorScheme(colorschemeId) {
	var mainform = $('mainform');
    mainform.action = 'colorschemes.html';
    mainform.colorSchemeId.value = colorschemeId;
    mainform.submit();
}

function redirectToColorscheme(brochureId) {
    var mainform = $('mainform');
    mainform.action = 'colorschemes.html?brochureId=' + brochureId;
    // mainform.brochureId.value = brochureId;
    mainform.colorSchemeId.value = '-1';
    mainform.submit();
}

function backToStepOne(brochureId, isCopy) {
	var mainform = $('mainform');
	if (isCopy) {
		mainform.action = "copybrochure.html?brochureId=" + brochureId;
	}
	else {
		if (brochureId != -1) {
			mainform.action = 'newbrochure.html?brochureId=' + brochureId;
		}
		else {
			mainform.action = 'newbrochure.html';
		}
	}
    mainform.submit();
}

function doCopyColorSchemeCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'copyColorSchemeCommand';
	mainform.action = 'colorschemes.do';
	mainform.submit();
}

function doSplashIconCommand(event) {
	if($('isPlatformSelected') && $('isPlatformSelected').value == 1){
		if(undefined != $('title') && !validateBrochureTitle($F('title'))){
			return;	
		}
			var mainform = $('mainform');
			mainform.command.value = 'splashIconCommand';
			mainform.submit();	
	}	
	else{
		showWarningMessage('<div align="center">Please select the platform to proceed.</div>');
	}
}

function doSubMenuCommand(event) {
	if($('isPlatformSelected') && $('isPlatformSelected').value == 1){
		if(undefined != $('title') && !validateBrochureTitle($F('title'))){
			return;	
		}
			var mainform = $('mainform');
			mainform.command.value = 'subMenuCommand';
			mainform.submit();	
	}
	else{
		showWarningMessage('<div align="center">Please select the platform to proceed.</div>');
	}
}

function doUploadImageCommand(event) {
	// var mainform = $('mainform');
	var mainform = document.getElementById('mainform');
	mainform.command.value = 'uploadImageCommand';
	mainform.submit();
}

function doUploadAudioCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'uploadAudioCommand';
	mainform.submit();
}

function doShowTemplateCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'showTemplateCommand';
	mainform.submit();
}

function doPublishTestingBrochureCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'publishBrochureCommandTesting';
	mainform.submit();
}

function doPreviewBrochureCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'previewBrochureCommand';
	mainform.submit();
}

function doPublishFinalBrochureCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'publishBrochureCommandFinal';
	mainform.submit();
}

function doGenerateBrochureCommand(event) {	
	if($('isPlatformSelected') && $('isPlatformSelected').value == 1){
		if(undefined != $('title') && !validateBrochureTitle($F('title'))){
			return;	
		}
			var mainform = $('mainform');
			mainform.command.value = 'generateBrochureCommand';
			mainform.submit();	
	}
	else{
		showWarningMessage('<div align="center">Please select the platform to proceed.</div>');
	}	
}

function doCreatePageCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'createPageCommand';
	mainform.submit();
}

function doUpdatePageCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'updatePageCommand';
	mainform.submit();
}

/**
 * Remove the video
 * 
 * @param {}
 *            videoId
 * @param {}
 *            tagId
 */
function doRemoveVideoCommand(videoId, tagId) {
	new Ajax.Request('checkMediaAvailability.json', {
		method: 'post',
		parameters: {'videoId': videoId,
					 'tagId' : tagId,
					 'command' : 'removeVideoCommand'
					 },
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			if (json.error) {
				var pageName = "";
				/**
				 * In case the image link to page, the page size will be > 0 so
				 * let concate all the name of the pages which store the
				 * selected image
				 */
				if (json.imgpages.length > 0) {
					pageName = "<br/> Pages: ";
				}
				for (var i = 0; i < json.imgpages.length; i++) {
					var page = json.imgpages[i];
					pageName += "<br/>&nbsp;&nbsp;&nbsp; - " + page.name;
				}
				showWarningMessage("The video can't be deleted because it linked to" + pageName);
			}
			else {
				var mainform = $('mainform');
				mainform.videoId.value = videoId;
                mainform.tagId.value = tagId;
				mainform.command.value = 'removeVideoCommand';
				mainform.submit();
			}
		}
	});
}

function doRemoveAudioCommand(audioId, tagId) {
	new Ajax.Request('checkMediaAvailability.json', {
		method: 'post',
		parameters: {'audioId': audioId,
					 'tagId' : tagId,
					 'command' : 'removeAudioCommand'
					 },
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			if (json.error) {
				var pageName = "";
				/**
				 * In case the image link to page, the page size will be > 0 so
				 * let concate all the name of the pages which store the
				 * selected image
				 */
				if (json.imgpages.length > 0) {
					pageName = "<br/> Pages: ";
				}
				for (var i = 0; i < json.imgpages.length; i++) {
					var page = json.imgpages[i];
					pageName += "<br/>&nbsp;&nbsp;&nbsp; - " + page.name;
				}
				showWarningMessage("The audio can't be deleted because it linked to " + pageName);
			}
			else {
				var mainform = $('mainform');
				mainform.audioId.value = audioId;
                mainform.tagId.value = tagId;
				mainform.command.value = 'removeAudioCommand';
				mainform.submit();
			}
		}
	});
}

/**
 * Remove the Image
 * 
 * @param {}
 *            imageId
 * @param {}
 *            tagId
 */
function doRemoveImageCommand(imageId, tagId) {
	new Ajax.Request('checkMediaAvailability.json', {
		method: 'post',
		parameters: {'imageId': imageId,
					 'tagId' : tagId,
					 'command' : 'removeImageCommand'
					 },
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			if (json.error) {
				var brochureName = "";
				if (json.imgbrochures.length > 0) {
					brochureName = "<br/> Icon or Splash on brochures:";
				}
				for (var i = 0; i < json.imgbrochures.length; i++) {
					var brochure = json.imgbrochures[i];
					brochureName += "<br/>&nbsp;&nbsp;&nbsp; - " + brochure.title; 
				}
				var pageName = "";
				/**
				 * In case the image link to page, the page size will be > 0 so
				 * let concate all the name of the pages which store the
				 * selected image
				 */
				if (json.imgpages.length > 0) {
					pageName = "<br/> Pages: ";
				}
				for (var i = 0; i < json.imgpages.length; i++) {
					var page = json.imgpages[i];
					pageName += "<br/>&nbsp;&nbsp;&nbsp; - " + page.name;
				}
				
				var colorSchemeName = "";
				/**
				 * In case the image link to page, the colorscheme size will be >
				 * 0 so let concate all the name of the colorscheme which store
				 * the selected image
				 */
				if (json.colorSchemes.length > 0) {
					colorSchemeName = "<br/> Color Schemes: ";
				}
				for (var i = 0; i < json.colorSchemes.length; i++) {
					var colorScheme = json.colorSchemes[i];
					colorSchemeName += "<br/>&nbsp;&nbsp;&nbsp; - " + colorScheme.label;
				}
				var errorMessage = "The image can't be deleted because it linked to " + brochureName + pageName + colorSchemeName;
				showWarningMessage(errorMessage);
			}
			else {
				var mainform = $('mainform');
				mainform.imageId.value = imageId;
                mainform.tagId.value = tagId;
				mainform.command.value = 'removeImageCommand';
				mainform.submit();
			}
		}
	});
	
}
 
function doRemoveMediaTag(tagId, brochureId, isFromPreviousPage) {
	new Ajax.Request('removeTag.json', {
		method: 'post',
		parameters: {'tagId' : tagId},
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			redirect('imagemanagement.html?brochureId=' + brochureId + '&isFromPreviousPage=' + isFromPreviousPage);
		}
	});
}

function doInstructionPageCommand(event) {
	var mainform = $('mainform');
	mainform.command.value = 'instructionPageCommand';
	mainform.submit();
}

function updateInputText(source,destination){
	destination.innerHTML = previewHtmlEscape($F(source));
	destination.show();
	source.hide();
}

function updateSplitHrIntoRow(id, data){
	// delete all
	for(var i = 0; i <document.getElementById("emulatorPageBody").rows.length; i++)	{
		document.getElementById("emulatorPageBody").deleteRow(i--);
	}
	
	// storeOldText(id, data);
	splitHrIntoRow(data);
}

function storeOldText(id, data){
	tr = document.createElement('tr');
	td = document.createElement('td');	
	
    td.colSpan = '2';
	td.style.width = "240px";
	
	txtHrStore = document.createElement('input');
	txtHrStore.type = 'hidden';
	txtHrStore.id = id;
	txtHrStore.name = txtHrStore.id;
	txtHrStore.value = data;
	
	tr.appendChild(td);
	td.appendChild(txtHrStore);
	
	updateEmulatorPageBody(tr);
}

function splitHrIntoRow(data){
	var index = -1;
	var hrlength = 31;
	var hrtag = '';
	var data1 = '';	
	var data2 = '';	
	var combinddata = '';
	
	if(isContainHr(data)){
		index = data.indexOf('<hr color="#');
		
		while(index >= 0){
			hrtag = data.substring(index, index + hrlength);			
			data1 = data.substring(0, index);
			data2 = data.substring(index + hrlength, data.length);
			
			hrtag = changeHrColor(hrtag);
			
			if(data1 != ''){
				data1 = removeBrStart(data1);				
				updateSplitHrRow(data1, 1);
				updateHrChangeColor(1);
				index = -1;
			}
			else{				
				updateHrChangeColor(1);
				index = -1;
			}
			
			if(data2 != ''){
				if(isContainHr(data2)){
					data2 = removeBrStart(data2);
					data = data2;					
					index = data.indexOf('<hr color="#');
				}
				else{
					data2 = removeBrStart(data2);
					updateSplitHrRow(data2, 2);
					index = -1;
				}
			}		
		}
		
	}	
}

function isContainHr(data){
	var index = -1;	
	
	if(data != ''){
		index = data.indexOf('<hr style="height:1px;border-width:0;color:#');	
	}
	
	return (index >= 0 ? true : false);
}

function removeBrStart(data){
	var index = -1;
	var data1 = '';
	
	if(data != ''){
		index = data.indexOf('<br>');
	}
	
	while(index == 0){	
		data1 = data.substring(4, data.length);
		data = data1;
		if(data != ''){
			index = data.indexOf('<br>');	
		}		
	}	
	
	return data;
}

function updateSplitHrRow(data, index){	
	exten = data.substring(0, 4) + index;
	exten = exten + data.substring(data.length - 4, data.length);
	divdata = document.getElementById('parent_data_' + exten);
	
	if(!divdata){
		tr = document.createElement('tr');
		td = document.createElement('td');	
		
	    td.colSpan = '2';
		td.style.width = "240px";
		
		div = document.createElement('div');
		div.style.maxWidth = "225px";
		
		tr.appendChild(td);
		td.appendChild(div);
				
		div.innerHTML = previewHtmlEscape(data);
		div.id = 'parent_data_' + exten;
		
		updateEmulatorPageBody(tr);	
	}	
}

function updateHrWithoutBr(data){
	var hrlength = 31;
	var hrtag = '';
	var index = -1;	
	var data1 = '';	
	var data2 = '';	
	var combinddata = '';
	
	if(isContainHr(data)){
		index = data.indexOf('<hr color="#');
		
		while(index >= 0){						
			hrtag = data.substring(index, index + hrlength);			
			data1 = data.substring(0, index);
			data2 = data.substring(index + hrlength, data.length);
			hrtag = changeHrColor(hrtag);
			
			if(data1 != ''){
				data1 = removeBrStart(data1);
				combinddata +=  data1 + hrtag;
				index = -1;
			}
			else{
				combinddata +=  hrtag;
				index = -1;
			}			
			
			if(data2 != ''){
				if(isContainHr(data2)){
					data2 = removeBrStart(data2);
					data = data2;					
					index = data.indexOf('<hr color="#');
				}
				else{
					data2 = removeBrStart(data2);
					combinddata +=  data2;
					index = -1;					
				}
			}			
			
		}
	}
	
	return combinddata;
}

function updateHrChangeColor(index){
	var color = '';
	
	var rulerColer = document.getElementById('txtcolor');
	if(rulerColer){
		color = rulerColer.value;
	}
	
	tr = document.createElement('tr');
	td = document.createElement('td');	
	
    td.colSpan = '2';
	td.style.width = "240px";
	
	hr = document.createElement('hr');
    hr.setAttribute('id','hr_text_' + index);
  	hr.setAttribute('color','#' + rulerColer.value);
	hr.setAttribute('size','1');
	
	tr.appendChild(td);
	td.appendChild(hr);
	
	updateEmulatorPageBody(tr);
}

function changeHrColor(data){
	var color = '';
	
	var rulerColer = document.getElementById('txtcolor');
	if(rulerColer){
		color = rulerColer.value;
	}
	
	var part1hr = data.substring(0,12);
	var part2hr = data.substring(18,31);
	var colorhr = part1hr + color + part2hr;
	
	return colorhr;
}

function isParagraphData(data){
	var index = -1;
	if(data != ''){
		data = data.split("\n");
		index = data.length;
	}
	
	return (index > 0 ? true : false);
}

function previewHtmlEscape(source){
	var allowed = [
        {tag:'p', close:true},
        {tag:'b', close:true},
        {tag:'u', close:true},
        {tag:'i', close:true},
        {tag:'br', close:false},
        {tag:'c', close:true},
        {tag:'font', close:true}
	];
	// alert(source);
	source = source.escapeHTML();
	for(var i = 0; i < allowed.length; ++i){
		if(allowed[i].tag == 'c'){
			var resulta = [];
			
			for(var j=0; j<source.length - '&lt;c'.length;j++){
				if(source.substring(j,j + '&lt;c'.length) == '&lt;c'){
					var startIndex = j;
					var endIndex = source.indexOf('&gt;', startIndex) + '&gt;'.length;
					var startCTag = source.substring(startIndex,endIndex);
					var colorAttribute = trim(startCTag.substring('&lt;c'.length,startCTag.indexOf('&gt;')));
					var textTranslate = "";
					if(colorAttribute.length == 1){
						if(colorAttribute == 1){
							textTranslate = "<" + "font" + " color=\"" + "#FF0000" + "\">";
						}
					} else {
						if(isColorValid(colorAttribute)){
							colorAttribute = colorAttribute.gsub('0x', '#');
							if(colorAttribute.startsWith("#")){
								textTranslate = "<" + "font" + " color=\"" + colorAttribute + "\">";								
							} else {
								textTranslate = "<" + "font" + " color=\"#" + colorAttribute + "\">";
							}
						} else {
							if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
								colorAttribute = colorAttribute.gsub('0x','#');								
								textTranslate = "<font color=\"" + ltrimIE(colorAttribute) + "\">";								
								
							}else{
								textTranslate = "<" + "font" + ">";	
							}							
						}
					}
					var tmp = [startIndex,endIndex,textTranslate];
					resulta.push(tmp);
				}
			}
			
			for(var k = resulta.length - 1 ; k >= 0 ; k--){
				source = source.substring(0,resulta[k][0]) + resulta[k][2] + source.substring(resulta[k][1],source.length);
			}
			
			if(allowed[i].close){
				source = source.gsub('&lt;/' + allowed[i].tag + '&gt;', '</' + "font" + '>');
			}
			
		} else {
			source = source.gsub('&lt;' + allowed[i].tag + '&gt;', '<' + allowed[i].tag + '>');
			if(allowed[i].close){
				source = source.gsub('&lt;/' + allowed[i].tag + '&gt;', '</' + allowed[i].tag + '>');
			}
		}
	}
	source = source.gsub('&lt;p center', '<p align="center" style="margin:0px"');	
	source = source.gsub('&lt;p right', '<p align="right" style="margin:0px"');	
	source = source.gsub('&lt;p left', '<p align="left" style="margin:0px"');	
	source = source.gsub('&gt;', '>');
	source = source.gsub('&lt;', '<');
	return source;
}


function convertFontTag(source) {
	var allowed = [
           {tag:'p', close:true},
           {tag:'b', close:true},
           {tag:'u', close:true},
           {tag:'i', close:true},
           {tag:'br', close:false},
           {tag:'font', close:true}
   	];

   	// source = replaceFontClass(source);
   	source = source.escapeHTML();   	
   	for(var i = 0; i < allowed.length; ++i){
   		if(allowed[i].tag == 'font'){
   			var resulta = [];
   			var needTranslate = false;
   			
   			for(var j=0; j<source.length - '&lt;font'.length;j++){
   				if(source.substring(j,j + '&lt;font'.length) == '&lt;font'){   					
   					var startIndex = j;
   					var endIndex = source.indexOf('&gt;', startIndex) + '&gt;'.length;
   					var startCTag = source.substring(startIndex,endIndex);
   					var colorAttribute = trim(startCTag.substring('&lt;font'.length,startCTag.indexOf('&gt;')));   					
   					colorAttribute = colorAttribute.substring(7, colorAttribute.length -1);   			
   					var textTranslate = "";
   					if(colorAttribute.length == 1){
   						if(colorAttribute == 1){
   							textTranslate = "<c 0xFF0000>";
   						}
   					} else {
   						if(isFontColorValid(colorAttribute)){
   							needTranslate = true;
   							colorAttribute = colorAttribute.gsub('#', '0x');
   							if(colorAttribute.startsWith("0x")){
   								textTranslate = "<c " + colorAttribute + ">";								
   							} else {
   								textTranslate = "<c 0x" + colorAttribute + ">";
   							}
   						} else {   							
   							if (colorAttribute.substring(0, "font-size:".length) != "font-size:") {
   								needTranslate = true;
   								if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
   	   								colorAttribute = colorAttribute.gsub('#','0x');								
   	   								textTranslate = "<c " + ltrimIE(colorAttribute) + ">";								
   	   								
   	   							}else{
   	   								textTranslate = "<c>";	
   	   							}
   							}
   						}
   					}
   					
   					var tmp = [startIndex,endIndex,textTranslate];
   					resulta.push(tmp);
   				}
   			}
   			
   			if (needTranslate) {
   				for(var k = resulta.length - 1 ; k >= 0 ; k--){
   	   				source = source.substring(0,resulta[k][0]) + resulta[k][2] + source.substring(resulta[k][1],source.length);
   	   			}
   				
   				if(allowed[i].close){   	   				
   					source = source.gsub('&lt;/' + allowed[i].tag + '&gt;', '</' + "c" + '>');   					
   	   			}
   			}
   			else {
   				for(var k = resulta.length - 1 ; k >= 0 ; k--){
   	   				source = source.substring(0,resulta[k][0]) + resulta[k][2] + source.substring(resulta[k][1],source.length);
   	   			}
   				
   				if(allowed[i].close){
   	   				source = source.gsub('&lt;/' + allowed[i].tag + '&gt;', "");
   	   			}
   			}
   			
   		} else {
   			source = source.gsub('&lt;' + allowed[i].tag + '&gt;', '<' + allowed[i].tag + '>');
   			if(allowed[i].close){
   				source = source.gsub('&lt;/' + allowed[i].tag + '&gt;', '</' + allowed[i].tag + '>');
   			}
   		}
   	}
   	source = source.gsub('&lt;p align="center"', '<p center');
   	source = source.gsub('&lt;p align="right"', '<p right');
   	source = source.gsub('&lt;p align="left"', '<p left');
   	source = source.gsub('&gt;', '>');
   	source = source.gsub('&lt;', '<');
   	source = source.gsub('&amp;nbsp;', ' ');
   	source = source.gsub('&amp;quot;', '"');
   	source = source.gsub('&amp;', '&');   	
   	
   	return source;
}

function replaceFontClass(source){
	var fontClass = '<font class=\"Apple-style-span\" size=\"3\">';
	var closeFont = '</font>';
	
	if(source.indexOf(fontClass) != -1){
		var startIndex = source.indexOf(fontClass) + fontClass.length;
		var endIndex = source.length - closeFont.length;		
		source = source.substring(startIndex, endIndex);		
	}
	
	return source;
}

function getFontSize(source) {
	source = source.escapeHTML();
	var fontSize;
	for(var j=0; j<source.length - '&lt;font'.length;j++){
		if(source.substring(j,j + '&lt;font'.length) == '&lt;font'){
			var startIndex = j;
			var endIndex = source.indexOf('&gt;', startIndex) + '&gt;'.length;
			var startCTag = source.substring(startIndex,endIndex);
			var colorAttribute = trim(startCTag.substring('&lt;font'.length,startCTag.indexOf('&gt;')));
			colorAttribute = colorAttribute.substring(7, colorAttribute.length -1);
			if (colorAttribute.substring(0, "font-size:".length) == "font-size:") {
				fontSize = colorAttribute.substring("font-size: ".length, colorAttribute.length - 3);
			}
		}
	}
	return fontSize;
}
function isColorValid(colorcode) {
	var regColorcode = /^(0x)?([0-9a-fA-F]{3})([0-9a-fA-F]{3})?$/;
    return regColorcode.test(colorcode); 
}

function isFontColorValid(colorcode) {
	var regColorcode = /^(#)?([0-9a-fA-F]{3})([0-9a-fA-F]{3})?$/;
    return regColorcode.test(colorcode); 
}

function trim(s) {
	return rtrim(ltrim(s));
}

function ltrim(s) {
	var l=0;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	return s.substring(l, s.length);
}

function ltrimIE(s) {
	var re = /\s*((\S+\s*)*)/;
	return s.replace(re, "$1");
}

function rtrim(s) {
	var r=s.length -1;
	while(r > 0 && s[r] == ' ')
	{	r-=1;	}
	return s.substring(0, r+1);
}


function inspect(obj, maxLevels, level)
{
  var str = '', type, msg;

    // Start Input Validations
    // Don't touch, we start iterating at level zero
    if(level == null)  level = 0;

    // At least you want to show the first level
    if(maxLevels == null) maxLevels = 1;
    if(maxLevels < 1)     
        return '<font color="red">Error: Levels number must be > 0</font>';

    // We start with a non null object
    if(obj == null)
    return '<font color="red">Error: Object <b>NULL</b></font>';
    // End Input Validations

    // Each Iteration must be indented
    str += '<ul>';

    // Start iterations for all objects in obj
    for(property in obj)
    {
      try
      {
          // Show "property" and "type property"
          type =  typeof(obj[property]);
          str += '<li>(' + type + ') ' + property + 
                 ( (obj[property]==null)?(': <b>null</b>'):('')) + '</li>';

          // We keep iterating if this property is an Object, non null
          // and we are inside the required number of levels
          if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
          str += inspect(obj[property], maxLevels, level+1);
      }
      catch(err)
      {
        // Is there some properties in obj we can't access? Print it red.
        if(typeof(err) == 'string') msg = err;
        else if(err.message)        msg = err.message;
        else if(err.description)    msg = err.description;
        else                        msg = 'Unknown';

        str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
      }
    }

      // Close indent
      str += '</ul>';

    return str;
}

function showInput(source, destination) {
	source.hide();
	destination.show();
	destination.activate();
}

function moveDown(event) {
	var currentLi = event.element();
	var tagName = currentLi.tagName;
	do {
		currentLi = currentLi.parentNode;
		tagName = currentLi.tagName;
	} while (tagName.toUpperCase() != 'LI');
	var ul = currentLi.parentNode;
	if ($(currentLi).nextSiblings().length > 0) {
		var nextT = $(currentLi).nextSiblings()[0];
		ul.insertBefore(nextT, currentLi);
	}
}

function moveUp(event) {
	var currentLi = event.element();
	var tagName = currentLi.tagName;
	do {
		currentLi = currentLi.parentNode;
		tagName = currentLi.tagName;
	} while (tagName.toUpperCase() != 'LI');
	var ul = currentLi.parentNode;
	if ($(currentLi).previousSiblings().length > 0) {
		var nextT = $(currentLi).previousSiblings()[0];
		ul.insertBefore(currentLi, nextT);
	}
}

function updateInputSelect(source,destination){
	new Ajax.Request('findimagefile.json', {
		method: 'post',
		parameters: {'imageId': $F(source)},
		onSuccess: function(transport) {
			var json = transport.responseText.evalJSON();
			destination.src = json.url;
			destination.show();
		}
	});
	source.hide();
}

function reSizeFooter() {
    var width = document.body.clientWidth;
    if (document.documentElement.scrollWidth > width) {
        width = document.documentElement.scrollWidth;
    }
    if (document.documentElement.clientWidth) {
        width = document.documentElement.clientWidth;
    }
    
    var height = document.documentElement.clientHeight;
    if (document.documentElement.scrollHeight > height) {    	
    	if ($('space') != null) {
    		        document.getElementById('space').style.width = "100%";// document.documentElement.scrollHeight
																			// -
																			// height;
    	}
    } else {
        if (document.getElementById('space') != null) {
            document.getElementById('space').style.height = (height - 550) + "px";
        }
    }
}

/**
 * The function to get the image through the tag selection.
 * 
 * @param brochureId
 * @param name
 * @param tabChanged
 *            indicate if the tab is changed.
 * @return
 */
function gallarySelection(brochureId, name, tabChanged) {
    var gallary = document.getElementById(name);    
    var tagId = -1;
    if(gallary){
    	tagId = gallary.value;
    }
    var sampleImage = false;
    if (tabChanged == undefined) {
    	tabChanged =  false;
    }
    var mainform = $('mainform');
    mainform.tagId.value = tagId;
    if(name == 'sampleImage'){
    	sampleImage = true;
    }
    if (brochureId != undefined && brochureId != '' && tagId == -1) {        
    	mainform.action = 'imagemanagement.html?brochureId=' + brochureId + '&isSampleImage=' + sampleImage + '&isTabChanged=' + tabChanged;    
    } else if (brochureId != undefined && brochureId == '' && tagId == -1) {
        mainform.action = 'imagemanagement.html?isSampleImage=' + sampleImage + '&isTabChanged=' + tabChanged;
    } else if (brochureId != undefined && brochureId != '') {
        mainform.action = 'imagemanagement.html?tagId=' + tagId + '&brochureId=' + brochureId + '&isSampleImage=' + sampleImage + '&isTabChanged=' + tabChanged;    
    } else {
        mainform.action = 'imagemanagement.html?tagId=' + tagId + '&isSampleImage=' + sampleImage + '&isTabChanged=' + tabChanged;
    }
    
    if($('selectedTagId')){
    	$('selectedTagId').value = tagId;
    }
     
    mainform.submit();
}

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

function validateEmpty(fld, text) {
	var error = "";
		if (fld.value.length == 0) {
    		fld.style.background = 'Yellow'; 
    	error = "The required field [" + text + "] has not been filled in.\n"
		alert(error);
		return true
	} else {
		fld.style.background = 'White';
	}
	return false;  
}


function validateEmptyNoHighLight(fld, text) {
	var error = "";
		if (fld.value.length == 0) {
    		// fld.style.background = 'Yellow';
    	error = "The required field [" + text + "] has not been filled in.\n"
		alert(error);
		return true
	} else {
		// fld.style.background = 'White';
	}
	return false;  
}

function is_same(f1,f2,text3){
	if(f1.value!=f2.value){
		// f1.style.background = 'Yellow';
		// f2.style.background = 'Yellow';
		alert(text3 + " must be the same value!");
		return false;
	} else {
		// f1.style.background = 'White';
		// f2.style.background = 'White';
	}
	return true;

}

function updateEmulatorTitleFontSize() {
    // $('emulatorTitleText').style.fontSize = $F('title_font_size') + "pt";
    $('j2meEmulatorTitleText').style.fontSize = $F('title_font_size') + "pt";
    $('androidEmulatorTitleText').style.fontSize = $F('title_font_size') + "pt";
    $('iphoneEmulatorTitleText').style.fontSize = $F('title_font_size') + "pt";
}

function updateEmulatorDescriptionFontSize(contentId, font) {
    $(contentId).style.fontSize = font + "pt";
}

function updateEmulatorFontSize(id) {
    var prefix = "txtText";
    var prefix1 = "txtContent";
    var emulatorId = "";
    if (id.indexOf(prefix) != -1) {
        emulatorId = id.substring(prefix.length, id.length);
    } else {
        emulatorId = id.substring(prefix1.length, id.length);
    } 
    
    var fontsize = id + "fontsize";
    var content = "idTxtContent" + emulatorId;
    if ($(content) == null) {
        content = "idTxtContent";
    }
    updateEmulatorDescriptionFontSize(content, $F(fontsize));
    // $(content).style.fontSize = $F(fontsize) + "pt";
}	

function validateEmail(addr) {
	
	if (addr == '' ) {
	    alert('email address is mandatory');
	   return false;
	}
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
	      alert('email address contains invalid characters');
	      return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
	      alert("email address contains non ascii characters.");
	      return false;
	   }
	}

	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	    alert('email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	    alert('email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   alert('email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	    alert('email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	    alert('period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	    alert('period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	    alert('two periods must not be adjacent in email address');
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	    alert('invalid primary domain in email address');
	   return false;
	}
	return true;
}

function updateTitleText (text, startColor , endColor){
	updateJ2meTitleText(text, startColor , endColor);
	updateAndroidTitleText(text, startColor , endColor);
	updateIphoneTitleText(text, startColor , endColor);
	updateBodyHeight();
}

function updateJ2meTitleText(text, startColor , endColor) {
	$('j2meEmulatorTitleText').innerHTML = text;
	/* For WebKit (Safari, Google Chrome etc) */
	if (browserDetect.browser == 'Chrome' || browserDetect.browser == 'Safari') {
       $('j2meEmulatorTitleText').style.cssText="background-image: -webkit-gradient(linear, left top, left bottom, from(" + startColor + "), to(" + endColor +"))";
    }
    /* For Mozilla >= 3.6 */
    else if (browserDetect.browser == 'Mozilla' || browserDetect.browser == 'Firefox') {
    	if (browserDetect.version >= 3.6) {
    		$('j2meEmulatorTitleText').style.cssText="background-image: -moz-linear-gradient(top, " + startColor +  ", " + endColor + ");";
    	}
    	else {
    		$('j2meEmulatorTitleText').style.cssText="background-color: " + startColor +";";
    	}
    
    /* For IE >= 5.5 */
    } else if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
        // If the startColor or endColor contains only # the IE wont work, so
		// use FFFFFF as default color
        if (startColor == "#") {
            startColor = "#FFFFFF";
        }
        if (endColor == "#") {
            endColor = "#FFFFFF";
        }
        $('j2meEmulatorTitleText').style.cssText= "filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=" + startColor + ", endColorstr=" + endColor + ")";
    }
    else{    	
    	$('j2meEmulatorTitleText').style.backgroundColor= startColor;
    }
}

function updateAndroidTitleText(text, startColor , endColor) {	
	$('androidEmulatorTitleText').innerHTML = text;	
	/* For WebKit (Safari, Google Chrome etc) */
	if (browserDetect.browser == 'Chrome' || browserDetect.browser == 'Safari') {       
       $('androidEmulatorTitleText').style.cssText="padding: 6px 0 6px 0;background-image: -webkit-gradient(linear, left top, left bottom, from(" + startColor + "), to(" + endColor +"))";
    }
    /* For Mozilla >= 3.6 */
    else if (browserDetect.browser == 'Mozilla' || browserDetect.browser == 'Firefox') {
    	if (browserDetect.version >= 3.6) {    
    		$('androidEmulatorTitleText').style.cssText="padding: 6px 0 6px 0;background-image: -moz-linear-gradient(top, " + startColor +  ", " + endColor + ");";    		
    	}
    	else {    		
    		$('androidEmulatorTitleText').style.cssText="padding: 6px 0 6px 0;background-color: " + startColor +";";
    	}
    
    /* For IE >= 5.5 */
    } else if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
        // If the startColor or endColor contains only # the IE wont work, so
		// use FFFFFF as default color
        if (startColor == "#") {
            startColor = "#FFFFFF";
        }
        if (endColor == "#") {
            endColor = "#FFFFFF";
        }    
        $('androidEmulatorTitleText').style.cssText= "padding: 6px 0 6px 0;filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=" + startColor + ", endColorstr=" + endColor + ")";
    }
    else{    
    	$('androidEmulatorTitleText').style.backgroundColor= startColor;
    	$('androidEmulatorTitleText').style.paddingTop = '6px';
    	$('androidEmulatorTitleText').style.paddingBottom = '6px';
    }
}

function updateIphoneTitleText(text, startColor , endColor) {	
	$('iphoneEmulatorTitleText').innerHTML = text;
	/* For WebKit (Safari, Google Chrome etc) */
	if (browserDetect.browser == 'Chrome' || browserDetect.browser == 'Safari') {       
       $('iphoneEmulatorTitleText').style.cssText="padding: 6px 0 6px 0;background-image: -webkit-gradient(linear, left top, left bottom, from(" + startColor + "), to(" + endColor +"))";
    }
    /* For Mozilla >= 3.6 */
    else if (browserDetect.browser == 'Mozilla' || browserDetect.browser == 'Firefox') {
    	if (browserDetect.version >= 3.6) {    	
    		$('iphoneEmulatorTitleText').style.cssText="padding: 6px 0 6px 0;background-image: -moz-linear-gradient(top, " + startColor +  ", " + endColor + ");";
    	}
    	else {    	
    		$('iphoneEmulatorTitleText').style.cssText="padding: 6px 0 6px 0;background-color: " + startColor +";";
    	}
    
    /* For IE >= 5.5 */
    } else if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
        // If the startColor or endColor contains only # the IE wont work, so
		// use FFFFFF as default color
        if (startColor == "#") {
            startColor = "#FFFFFF";
        }
        if (endColor == "#") {
            endColor = "#FFFFFF";
        }        
        $('iphoneEmulatorTitleText').style.cssText= "padding: 6px 0 6px 0;filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=" + startColor + ", endColorstr=" + endColor + ")";
    }
    else{    
    	$('iphoneEmulatorTitleText').style.backgroundColor= startColor;
    	$('iphoneEmulatorTitleText').style.paddingTop = '6px';
    	$('iphoneEmulatorTitleText').style.paddingBottom = '6px';
    }
}

function updateTitleImageHidden(image){
	$('j2meEmulatorTitleImage').style.display = 'block';	
	$('androidEmulatorTitleImage').style.display = 'block';
	$('iphoneEmulatorTitleImage').style.display = 'block';
	if(image == -1){
		$('j2meEmulatorTitleImage').style.display = 'none';
		$('androidEmulatorTitleImage').style.display = 'none';
		$('iphoneEmulatorTitleImage').style.display = 'none';
	}
}

function updateTitleImage(image, startColor , endColor, imageId){	
	updateJ2meTitleImage(image, startColor , endColor, imageId);
	updateAndroidTitleImage(image, startColor , endColor, imageId);
	updateIphoneTitleImage(image, startColor , endColor, imageId);	
	updateBodyHeight();
}

function updateJ2meTitleImage(image, startColor , endColor, imageId){
	p = new Image();
	p.src = image;
	
	$('j2meTitleIMG').src = image;	
	imageFitToScreen(imageId, 'j2meTitleIMG');	
	$('j2meEmulatorTitleImage').style.cssText="background-image: -moz-linear-gradient(top, " + startColor +  ", " + endColor + ");";
	$('j2meEmulatorTitleImage').setAttribute('align','center');	
}

function updateAndroidTitleImage(image, startColor , endColor, imageId){
	p = new Image();
	p.src = image;
	
	$('androidTitleIMG').src = image;	
	imageFitToScreen(imageId, 'androidTitleIMG');
	$('androidEmulatorTitleImage').style.cssText="background-image: -moz-linear-gradient(top, " + startColor +  ", " + endColor + ");";
	$('androidEmulatorTitleImage').setAttribute('align','center');
}

function updateIphoneTitleImage(image, startColor , endColor, imageId){
	p = new Image();
	p.src = image;
	
	$('iphoneTitleIMG').src = image;
	imageFitToScreen(imageId, 'iphoneTitleIMG');	
	$('iphoneEmulatorTitleImage').style.cssText="background-image: -moz-linear-gradient(top, " + startColor +  ", " + endColor + ");";
	$('iphoneEmulatorTitleImage').setAttribute('align','center');
}

function titleImageAll(image, txtTitleImage, bannerOption){
	if(bannerOption == 'include'){
		displayBannerOption();
	}
	
	var imageId = image;
	
    if(isUserImage(image)) {
        updateTitleImage('files/' + image,'','', imageId);
     }
     else if (isSampleImage(image)) {
    	 updateTitleImage('filessample/' + image,'','', imageId);
     }
     updateParentCombo(txtTitleImage, image);
     updateTitleImageHidden(image);
}

function updateInitPageBody(){
	updateInitPageBodyContent(getSelectedPlatForm($F('tabSelected')));
}

/**
 * Clear all the page body content depends on the selected emulator tab
 * 
 * @param platForm:
 *            the selected tab number.
 * @return
 */
function clearAllPageBodyContent(platForm) {
	
	if (parseInt(platForm) == 1) {
		clearPageBodyContent('j2meEmulatorPageBody');
	}
	else if (platForm == 2) {
		clearPageBodyContent('androidEmulatorPageBody');
	}
	else if (parseInt(platForm) == 3) {
		clearPageBodyContent('iphoneEmulatorPageBody');
	}
	else {
		alert("Incorrect selected emulator tab");
	}
}

function platFormObject(platForm){
	var platformObj = 'j2meEmulatorPageBody';
	if (parseInt(platForm) == 1) {
		platformObj = 'j2meEmulatorPageBody';
	}
	else if (platForm == 2) {
		platformObj = 'androidEmulatorPageBody';
	}
	else if (parseInt(platForm) == 3) {
		platformObj = 'iphoneEmulatorPageBody';
	}
	else {
		alert("Incorrect selected emulator tab");
	}
	
	return platformObj;
}

/**
 * Update the application image possition
 * 
 * @return
 */
function updateAppImagePos(){
	if($F('imageAlign') == 1){			
		updateImageAlign('left', 1);
	}
	else if($F('imageAlign') == 2){			
		updateImageAlign('center', 1);
	}
	else if($F('imageAlign') == 3){			
		updateImageAlign('right', 1);
	}
}

function updateImageAlign(align, index){    
	if($('tabSelected').value == 1){    	    
	    $('appImgJ2me').setAttribute('align',align);
	    if(align == 'right' && index == 0){
	      $('appImgJ2me').style.width = '234px';
	    }			
	}
	else if($('tabSelected').value == 2){			
		$('appImgAndroid').setAttribute('align',align);
	    if(align == 'right' && index == 0){
	      $('appImgAndroid').style.width = '227px';
	    }
	}
	else if($('tabSelected').value == 3){		
		$('appImgIphone').setAttribute('align',align);
	    if(align == 'right' && index == 0){
	      $('appImgIphone').style.width = '230px';
	    }
	}
}

function updateImageBody(platform){		
	if ($F('header_img_id') != "-1" && $F('header_img_id') != "") {			
		$('idCboIMG' + platform).style.display = 'inline';
		sampleimage($F('header_img_id'), $('idCboIMG' + platform), $('cboImage'), false);
	} else{
		$('idCboIMG' + platform).style.display = 'none';
	}
	
	updateBodyHeight();		
}

function getRowAndImageWidth(platform){
	var widthArray = new Array(2);
	if(platform == 'J2me' || platform == 'j2me'){
		widthArray[0] = 240;
		widthArray[1] = 234;
	}
	else if(platform == 'Android' || platform == 'android'){
		widthArray[0] = 233;
		widthArray[1] = 227;
	}
	else if(platform == 'Iphone' || platform == 'iphone'){
		widthArray[0] = 237;
		widthArray[1] = 230;
	}
	
	return widthArray;
}

function getWidthHeightBgImage(platform){
	var widthArray = new Array(2);
	if(platform == 'j2meEmulator' || platform == 'j2me' || platform == '1'){
		widthArray[0] = 248;
		widthArray[1] = 310;
	}
	else if(platform == 'androidEmulator' || platform == 'android' || platform == '2'){
		widthArray[0] = 238;
		widthArray[1] = 390;
	}
	else if(platform == 'iphoneEmulator' || platform == 'iphone' || platform == '3'){
		widthArray[0] = 241;
		widthArray[1] = 360;
	}
	
	return widthArray;
}

function updateBodyHeight(){
	updateJ2MEBodyHeight();
	updateAndroidBodyHeight();
	updateIphoneBodyHeight();
} 

function updateJ2MEBodyHeight() {
	if($('j2meEmulatorPageHead').offsetHeight >= 310){
		$('j2meEmulatorBody').hide();
		$('j2meEmulator').style.overflow = 'hidden';
		$('j2meEmulatorPageBodyBackground').hide();
	} else {
		$('j2meEmulatorBody').style.height = (310 - $('j2meEmulatorPageHead').offsetHeight) + 'px';
		$('j2meEmulatorBody').show();
		$('j2meEmulatorPageBodyBackground').show();
		$('j2meEmulatorPageBodyBackground').style.height = (310 - $('j2meEmulatorPageHead').offsetHeight) + 'px';
	}
}

function updateAndroidBodyHeight() {
	if($('androidEmulatorPageHead').offsetHeight >= 390){
		$('androidEmulatorBody').hide();
		$('androidEmulator').style.overflow = 'hidden';
		$('androidEmulatorPageBodyBackground').hide();
	} else {
		$('androidEmulatorBody').style.height = (390 - $('androidEmulatorPageHead').offsetHeight) + 'px';
		$('androidEmulatorBody').show();
		$('androidEmulatorPageBodyBackground').show();
		$('androidEmulatorPageBodyBackground').style.height = (390 - $('androidEmulatorPageHead').offsetHeight) + 'px';
	}
}

function updateIphoneBodyHeight() {
	if($('iphoneEmulatorPageHead').offsetHeight >= 357){
		$('iphoneEmulatorBody').hide();
		$('iphoneEmulator').style.overflow = 'hidden';
		$('iphoneEmulatorPageBodyBackground').hide();
	} else {
		$('iphoneEmulatorBody').style.height = (357 - $('iphoneEmulatorPageHead').offsetHeight) + 'px';
		$('iphoneEmulatorBody').show();
		$('iphoneEmulatorPageBodyBackground').show();
		$('iphoneEmulatorPageBodyBackground').style.height = (357 - $('iphoneEmulatorPageHead').offsetHeight) + 'px';
	}
}

function updateContentText(contentTextColor, background, contentBGTran){
    var platform = getSelectedPlatForm($F('tabSelected')).toLowerCase();

    $(platform + 'EmulatorPageBodyBackground').style.width = "245px";
    $(platform + 'EmulatorPageBodyBackground').style.position = 'absolute';  
    $(platform + 'EmulatorPageBodyBackground').style.backgroundColor = "#"+background;     
    $(platform + 'EmulatorPageBodyBackground').style.opacity = contentBGTran/100;
    $(platform + 'EmulatorPageBodyBackground').style.filter = 'alpha(opacity=' + contentBGTran + ')';     
    $(platform + 'EmulatorBodyContent').style.color = "#"+contentTextColor;        
}

function j2meUpdateContentText(contentTextColor, background, contentBGTran){
	$('j2meEmulatorPageBodyBackground').style.width = "245px";
    $('j2meEmulatorPageBodyBackground').style.position = 'absolute';  
    $('j2meEmulatorPageBodyBackground').style.backgroundColor = "#"+background;     
    $('j2meEmulatorPageBodyBackground').style.opacity = contentBGTran/100;
    $('j2meEmulatorPageBodyBackground').style.filter = 'alpha(opacity=' + contentBGTran + ')';
    
    $('j2meEmulatorBodyContent').style.color = "#"+contentTextColor;
}

function updateEmulatorPageBody(parentTag, childTag){
	$(parentTag).appendChild(childTag);
}

function updateSelectedEmulatorPageBody(platform, element) {
	// In case the give platform is number we should use getSelectedPlatForm to
	// get it into string
	if (platform == 'J2me') {
		$('j2meEmulatorPageBody').appendChild(element);
	}
	else if (platform == 'Android') {
		$('androidEmulatorPageBody').appendChild(element);
	}
	else if (platform == 'Iphone') {
		$('iphoneEmulatorPageBody').appendChild(element);
	}
}

function updateJ2meEmulatorPageBody(element){
	$('j2meEmulatorPageBody').appendChild(element);
}

function updateAndroidEmulatorPageBody(element){
	$('androidEmulatorPageBody').appendChild(element);
}

function updateIphoneEmulatorPageBody(element){
	$('iphoneEmulatorPageBody').appendChild(element);
}

function isEmunlatorContentExist(platform) {
	var divlist;
	if (platform == 'J2me') {
		divlist = $('j2meEmulatorPageBody').getElementByTagName('tr');
	}
	else if (platform == 'Android') {
		$('androidEmulatorPageBody').appendChild(element);
	}
	else if (platform == 'Iphone') {
		$('iphoneEmulatorPageBody').appendChild(element);
	}
}

function clearPageBodyContent(element){	
	for(var i = 0; i <document.getElementById(element).rows.length; i++)	{		
		document.getElementById(element).deleteRow(i--);
	}
}

function updateEmulatorSoundSection(emulatorPlatform){	
	// removed all
	for(var i = 0; i <document.getElementById(emulatorPlatform).rows.length; i++)	{
		document.getElementById(emulatorPlatform).deleteRow(i--);
	}	
	
	var rulerColer = document.getElementById('txtcolor');
	
	var pageListSound = $('pagelist').getElementsByTagName('li');
	
	for ( var i = 0; i < pageListSound.length; ++i) {
		var selects = pageListSound[i].getElementsByTagName('select');

		var inputs = pageListSound[i].getElementsByTagName('input');

		var imageId = -1;
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('bullet_') > -1) {
				imageId = inputs[j].value;
				break;
			}
		}
		
		var divdisplay = 'none';
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('txt_addhr_sound') > -1) {
				divdisplay = inputs[j].value;
				break;
			}
		}
		// read resize image option
		var isResized = false;
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('resize_image_option_sound_id_') > -1) {
				isResized = inputs[j].value == 'true';
				break;
			}
		}
	
		tr = document.createElement('tr');
		td1 = document.createElement('td');
		td2 = document.createElement('td');
		td1.style.width = "35px";
		td2.style.width = "205px";
		td1.style.paddingLeft = "3px";
		td1.style.paddingTop = "1px";
		td1.style.paddingBottom = "1px";
		td2.style.paddingTop = "1px";
		td2.style.paddingBottom = "1px";
		
		tr.appendChild(td1);
		tr.appendChild(td2);
		
		// image cell
		img = document.createElement('img');
		sampleimage(imageId, img, selects[0]);
		
		if(isResized){
			img.style.width='30px';
			img.style.height='30px';
		}
		img.style.textAlign='left';
		
		// resize image to fit screen in case resize_image = false &&
		// image_Width > Emulator screen width
		if(false == isResized){
			if(emulatorPlatform == 'j2meEmulatorSound1' && img.width > 281){
				img.style.width= '240px';
			}
			else if(emulatorPlatform == 'androidEmulatorSound1' && img.width > 240){
				img.style.width= '220px';
			}
		}
		
		if (imageId !=-1) {
			td1.appendChild(img); // append img to td
			var txtimg = document.createElement('input');
			txtimg.type = 'hidden';
			txtimg.id = 'txt_store_img_sound_' + i;
			txtimg.name = txtimg.id;
			txtimg.value = imageId;
			td1.appendChild(txtimg);
		} else {
			td1.style.width = "20px";
            td2.style.width = "220px";
			td1.innerHTML="";
		}
		
		var text = "";
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('text') > -1) {
				text = inputs[j].value;
			}

		}
		// td2.innerHTML=text;
		
		div = document.createElement('div');
		div.setAttribute('id','div_sound_data_' + i);
		div.innerHTML = text;
		td2.appendChild(div);
		
		$(emulatorPlatform).appendChild(tr);
		
		divsoundid = '';
		addhr_sound = document.getElementById('addhr_sound_' + i);
		if(addhr_sound){
			divsoundid = addhr_sound.id;
		}
		else{
			divsoundid = 'addhr_sound_' + i;
		}
		
		rowidsoundvalue = '-1';
		rowId1 = document.getElementById('rowid_' + i);
		if(rowId1){
			rowidsoundvalue = rowId1.value;
		}
		
		audiovalue = '-1';
		audio = document.getElementById('audio_' + i);
		if(audio){
			audiovalue = audio.value;
		}
		
		if(divsoundid != ''){
			
			tr1 = document.createElement('tr');
	        td1 = document.createElement('td');
	        
			div_hr = document.createElement('div');	      
		    div_hr.setAttribute('id','div_hr_' + divsoundid);
	    	div_hr.style.maxWidth = '240px';	    	
	    	div_hr.style.display = divdisplay;
	    	
	    	hr = document.createElement('hr');
	        hr.setAttribute('id','hr_sound_' + count);	      	
			hr.style.height = '1px';
			hr.style.borderWidth = '0px';
			hr.style.color = '#' + rulerColer.value;
			hr.style.backgroundColor = '#' + rulerColer.value;
			
			rowidsound = document.createElement('input');
			rowidsound.setAttribute('type', 'hidden');
			rowidsound.setAttribute('name', 'rowId_sound_' + i);
			rowidsound.setAttribute('id', 'rowId_sound_' + i);
			rowidsound.value = rowidsoundvalue;
			
			audiosound = document.createElement('input');
			audiosound.setAttribute('type', 'hidden');
			audiosound.setAttribute('name', 'audio_sound_' + i);
			audiosound.setAttribute('id', 'audio_sound_' + i);
			audiosound.value = audiovalue;
			
			td1.colSpan = '2';  
	       	div_hr.appendChild(hr);
	       	td1.appendChild(div_hr);
	       	td1.appendChild(rowidsound);
	       	td1.appendChild(audiosound);
	       	tr1.appendChild(td1);
	       	$(emulatorPlatform).appendChild(tr1);
		}
		
	}
	
}
/*******************************************************************************
 * 
 * Video Section for Emulator preview
 */
function updateEmulatorVideoSection(emulatorPlatform){	
	 // removed all
	for(var i = 0; i <document.getElementById(emulatorPlatform).rows.length; i++)	{
		document.getElementById(emulatorPlatform).deleteRow(i--);
	}
	
	var rulerColer = document.getElementById('txtcolor');
	
	var pageListVideo = $('pagelist').getElementsByTagName('li');
	
	for ( var i = 0; i < pageListVideo.length; ++i) {
		var selects = pageListVideo[i].getElementsByTagName('select');

		var inputs = pageListVideo[i].getElementsByTagName('input');

		var imageId = -1;
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('bullet') > -1) {
				imageId = inputs[j].value;
			}
		}
		
		var divdisplay = 'none';
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('txt_addhr_video') > -1) {
				divdisplay = inputs[j].value;
				break;
			}
		}
		
		// read resize image option
		var isResized = false;
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('resize_image_option_video_id_') > -1) {
				isResized = inputs[j].value == 'true';
				break;
			}
		}
	
		tr = document.createElement('tr');
		td1 = document.createElement('td');
		td2 = document.createElement('td');
		td1.style.width = "35px";
		td2.style.width = "205px";
		td1.style.paddingLeft = "3px";
		td1.style.paddingTop = "1px";
		td1.style.paddingBottom = "1px";
		td2.style.paddingTop = "1px";
		td2.style.paddingBottom = "1px";
		
		tr.appendChild(td1);
		tr.appendChild(td2);
		
		// image cell
		img = document.createElement('img');
		sampleimage(imageId, img, selects[0]);
		if(isResized){
			img.style.width='30px';
			img.style.height='30px';
		}
		img.style.textAlign='left';
		
		// resize image to fit screen in case resize_image = false &&
		// image_Width > Emulator screen width
		if(false == isResized){
			if(emulatorPlatform == 'j2meEmulatorVideo' && img.width > 281){
				img.style.width= '240px';
			}
			else if(emulatorPlatform == 'androidEmulatorVideo' && img.width > 240){
				img.style.width= '220px';
			}
		}
		
		if (imageId !=-1) {
			td1.appendChild(img); // append img to td
			var txtimg = document.createElement('input');
			txtimg.type = 'hidden';
			txtimg.id = 'txt_store_img_video_' + i;
			txtimg.name = txtimg.id;
			txtimg.value = imageId;
			td1.appendChild(txtimg);
		} else {
			td1.style.width = "20px";
            td2.style.width = "220px";
			td1.innerHTML="";
		}
		
		var text = "";
		var link = "";
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('text') > -1) {
				text = inputs[j].value;
			}
		}
		// td2.innerHTML=text;
		
		div = document.createElement('div');
		div.setAttribute('id','div_video_data_' + i);
		div.innerHTML = text;
		td2.appendChild(div);
		
		$(emulatorPlatform).appendChild(tr);
		
		divvideoid = '';
		addhr_video = document.getElementById('addhr_video_' + i);
		if(addhr_video){
			divvideoid = addhr_video.id;
		}
		else{
			divvideoid = 'addhr_video_' + i;
		}
		
		rowidvideovalue = '-1';
		rowid = document.getElementById('rowid_' + i);
		if(rowid){
			rowidvideovalue = rowid.value;
		}
		
		video_value = '-1';
		video = document.getElementById('video_' + i);
		if(video){
			video_value = video.value;
		}
		
		if(divvideoid != ''){
			
			tr1 = document.createElement('tr');
	        td1 = document.createElement('td');
	        
			div_hr = document.createElement('div');	      
		    div_hr.setAttribute('id','div_hr_' + divvideoid);
	    	div_hr.style.maxWidth = '240px';	    	
	    	div_hr.style.display = divdisplay;
	    	
	    	hr = document.createElement('hr');
	        hr.setAttribute('id','hr_video_' + count);	      	
			hr.style.height = '1px';
			hr.style.borderWidth = '0px';
			hr.style.color = '#' + rulerColer.value;
			hr.style.backgroundColor = '#' + rulerColer.value;
			
			rowidvideo = document.createElement('input');
			rowidvideo.setAttribute('type', 'hidden');
			rowidvideo.setAttribute('name', 'rowId_video_' + i);
			rowidvideo.setAttribute('id', 'rowId_video_' + i);
			rowidvideo.value = rowidvideovalue;
			
			video_file = document.createElement('input');
			video_file.setAttribute('type', 'hidden');
			video_file.setAttribute('name', 'video_file_' + i);
			video_file.setAttribute('id', 'video_file_' + i);
			video_file.value = video_value;
			
			td1.colSpan = '2';  
	       	div_hr.appendChild(hr);
	       	td1.appendChild(div_hr);
	       	td1.appendChild(rowidvideo);
	       	td1.appendChild(video_file);
	       	tr1.appendChild(td1);
	       	$(emulatorPlatform).appendChild(tr1);
		}		
		
	}	
	
}

function updateEmulatorPageCallLinkSMS(){
	// removed all
	for(var i = 0; i <document.getElementById("emulatorRowLink").rows.length; i++)	{
		document.getElementById("emulatorRowLink").deleteRow(i--);
	}
	
	if ($('pageListLinkCall') == null  ) return;
	
	var pageName = '';	
	
	var rulerColer = document.getElementById('txtcolor');
	var txtpage = document.getElementById('txtpage');
	if(txtpage){
		pageName = txtpage.value;
	}
	
	var pageListLinkCall = $('pageListLinkCall').getElementsByTagName('li');
	
	for ( var i = 0; i < pageListLinkCall.length; ++i) {
		var selects = pageListLinkCall[i].getElementsByTagName('select');

		var inputs = pageListLinkCall[i].getElementsByTagName('input');

		var imageId = -1;
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('bullet1') > -1) {
				imageId = inputs[j].value;
				break;
			}
		}
		
		var link1_call = '';
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('link1') > -1) {
				link1_call = inputs[j].value;
				break;
			}
		}
		
		var callTypeValue = '';
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('call_type_') > -1) {
				callTypeValue = inputs[j].value;
				break;
			}
		}
		
		var txtsmsbody = '';
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('body1_') > -1) {
				txtsmsbody = inputs[j].value;
				break;
			}
		}
		
		var rowidcallValue = '-1';
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('rowid1call_') > -1) {
				rowidcallValue = inputs[j].value;
				break;
			}
		}
		
		var divdisplay = 'none';
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('txt_addhr_call') > -1) {
				divdisplay = inputs[j].value;
				break;
			}
		}
	
	    var isImageLink = false;
	    for (var j = 0; j < inputs.length; ++j) {
	       if (inputs[j].name.indexOf('img_link') > -1) {
	           isImageLink = true;
	       }
	    }	  
	    
	    // resize image option
	    var isResizedImageOption = false;
	    for(var j=0;j<inputs.length;++j){
	    	if (inputs[j].name.indexOf('resize_image_option_linkcallsms_id') > -1) {
	    		isResizedImageOption = true;
	    		break;
		       }
	    }
        
        tr = document.createElement('tr');
        td1 = document.createElement('td');
        
	    if (isImageLink) {
	       // td1.setAttribute("colspan", "2"); // for Firefox
	       td1.colSpan = "2"; // for IE + others
	       td1.setAttribute("align", "center");
	       tr.appendChild(td1);
	    }
	    else {
	       td2 = document.createElement('td');
           td1.style.width = "35px";
           td2.style.width = "205px";
           td1.style.paddingLeft = "3px";
   		   td1.style.paddingTop = "1px";
   		   td1.style.paddingBottom = "1px";
   		   td2.style.paddingTop = "1px";
   		   td2.style.paddingBottom = "1px";
           tr.appendChild(td1);
           tr.appendChild(td2);
	    }
		
		// image cell
		img = document.createElement('img');
		sampleimage(imageId, img, selects[0]);
		if (isImageLink) {
			img.setAttribute("id", "imgLink_"+i);
			img.setAttribute("name", "imgLink_"+i);
		}
		
		if (!isImageLink) {
			if(isResizedImageOption){
	            img.style.width='30px';
	            img.style.height='30px';
			}
            img.style.textAlign='left';
        } else {
            resizeImageLink(imageId, img,isResizedImageOption == 'true');
        }    
		
		if (imageId !=-1) {
			td1.appendChild(img); // append img to td
			var txtimg = document.createElement('input');
			txtimg.type = 'hidden';
			txtimg.id = 'txt_store_img_call_' + i;
			txtimg.name = txtimg.id;
			txtimg.value = imageId;
			td1.appendChild(txtimg);
		} else {
		  if (!isImageLink) {
		      td1.style.width = "20px";
              td2.style.width = "220px";
              td1.innerHTML="";
		  }
		}
		
		var text = "";		
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('text1') > -1) {
				text = inputs[j].value;
				break;
			}
		}
		
		if (!isImageLink) {
		  // td2.innerHTML=text;
		  
		  div = document.createElement('div');
		  div.setAttribute('id','div_call_data_' + i);
		  div.innerHTML = text;
		  td2.appendChild(div);		  
		}				
		
		// check type when call_type is null
		if(isImageLink){
			if(link1_call == 'http://'){
				callTypeValue = '4';				
			}
		}
		else{
			if(link1_call == 'http://'){
				callTypeValue = '1';
			}
			else if(link1_call == 'call:'){
				callTypeValue = '2';
			}
			else if(link1_call == 'sms:'){
				callTypeValue = '3';
			}
		}		
		
		var callType = document.createElement('input');
		callType.setAttribute('type', 'hidden');
		callType.setAttribute('name', 'type_call_' + i);
		callType.setAttribute('id', 'type_call_' + i);
		callType.value = callTypeValue;
		td1.appendChild(callType);
		  
		$('emulatorRowLink').appendChild(tr);
		if (isImageLink) {
			resizeImageLink(imageId, img,isResizedImageOption == 'true');
// // if(browserDetect.browser != 'Explorer') {
// // window.setInterval('updateImageLinkSize()', 1000);
// // }
		}
		
		if(pageName != ''){
			
			divcalid = '';
			addhr_call = document.getElementById('addhr_call_' + i);
			if(addhr_call){
				divcalid = addhr_call.id;
			}
			else{
				divcalid = 'addhr_call_' + i;
			}			
			
			if(divcalid != ''){
				
				tr1 = document.createElement('tr');
		        td1 = document.createElement('td');
		        
				div_hr = document.createElement('div');	      
			    div_hr.setAttribute('id','div_hr_' + divcalid);
		    	div_hr.style.maxWidth = '240px';	    	
		    	div_hr.style.display = divdisplay;
		    	
		    	hr = document.createElement('hr');
		        hr.setAttribute('id','hr_call_' + count);
		      	// hr.setAttribute('color','#' + rulerColer.value);
				// hr.setAttribute('size','1');
				hr.style.height = '1px';
				hr.style.borderWidth = '0px';
				hr.style.color = '#' + rulerColer.value;
				hr.style.backgroundColor = '#' + rulerColer.value;
				
				rowidcall = document.createElement('input');
				rowidcall.setAttribute('type', 'hidden');
				rowidcall.setAttribute('name', 'rowId1_call_' + i);
				rowidcall.setAttribute('id', 'rowId1_call_' + i);
				rowidcall.value = rowidcallValue;	
				
				linkcall = document.createElement('input');
				linkcall.setAttribute('type', 'hidden');
				linkcall.setAttribute('name', 'link_call_' + i);
				linkcall.setAttribute('id', 'link_call_' + i);
				linkcall.value = link1_call;
				
				textsmsbody = document.createElement('input');
				textsmsbody.setAttribute('type', 'hidden');
				textsmsbody.setAttribute('name', 'sms_text_body_' + i);
				textsmsbody.setAttribute('id', 'sms_text_body_' + i);
				textsmsbody.value = txtsmsbody;
				
				td1.colSpan = '2';  
		       	div_hr.appendChild(hr);
		       	td1.appendChild(div_hr);
		       	td1.appendChild(rowidcall);
		       	td1.appendChild(linkcall);
		       	td1.appendChild(textsmsbody);
		       	tr1.appendChild(td1);
		       	$('emulatorRowLink').appendChild(tr1);
			}
			
		}
	}	
	
	// if (intervalId != undefined && intervalId != "") {
		// clearTimeout(intervalId);
	// }
}
	
function updateEmulatorPageCallLinkSMSPlatform(element){
		// removed all
		for(var i = 0; i <document.getElementById(element).rows.length; i++)	{
			document.getElementById(element).deleteRow(i--);
		}
		
		if ($('pageListLinkCall') == null  ) return;
		
		var pageName = '';	
		
		var rulerColer = document.getElementById('txtcolor');
		var txtpage = document.getElementById('txtpage');
		if(txtpage){
			pageName = txtpage.value;
		}
		
		var pageListLinkCall = $('pageListLinkCall').getElementsByTagName('li');
		
		for ( var i = 0; i < pageListLinkCall.length; ++i) {
			var selects = pageListLinkCall[i].getElementsByTagName('select');

			var inputs = pageListLinkCall[i].getElementsByTagName('input');

			var imageId = -1;
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('bullet1') > -1) {
					imageId = inputs[j].value;
					break;
				}
			}
			
			var link1_call = '';
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('link1') > -1) {
					link1_call = inputs[j].value;
					break;
				}
			}
			
			var callTypeValue = '';
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('call_type_') > -1) {
					callTypeValue = inputs[j].value;
					break;
				}
			}
			
			var txtsmsbody = '';
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('body1_') > -1) {
					txtsmsbody = inputs[j].value;
					break;
				}
			}
			
			var rowidcallValue = '-1';
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('rowid1call_') > -1) {
					rowidcallValue = inputs[j].value;
					break;
				}
			}
			
			var divdisplay = 'none';
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('txt_addhr_call') > -1) {
					divdisplay = inputs[j].value;
					break;
				}
			}
		
		    var isImageLink = false;
		    for (var j = 0; j < inputs.length; ++j) {
		       if (inputs[j].name.indexOf('img_link') > -1) {
		           isImageLink = true;
		       }
		    }	   
	        
		    // resize image option
		    var isResizedImageOption = false;
		    for(var j=0;j<inputs.length;++j){
		    	if (inputs[j].name.indexOf('resize_image_option_linkcallsms_id') > -1) {
		    		isResizedImageOption = inputs[j].value;
		    		break;
			       }
		    }
		    
	        tr = document.createElement('tr');
	        td1 = document.createElement('td');
	        
			   if (isImageLink) {
				 td1.setAttribute("colSpan", "2");
				 td1.setAttribute("align", "left");
				 tr.appendChild(td1);
			    }
			   else {
			       td2 = document.createElement('td');
		           td1.style.width = "35px";
		           td2.style.width = "205px";
		           td1.style.paddingLeft = "3px";
		   		   td1.style.paddingTop = "1px";
		   		   td1.style.paddingBottom = "1px";
		   		   td2.style.paddingTop = "1px";
		   		   td2.style.paddingBottom = "1px";
		           tr.appendChild(td1);
		           tr.appendChild(td2);
	           }
			
			// image cell
			img = document.createElement('img');
			sampleimage(imageId, img, selects[0]);
			img.style.textAlign='left';
			// if (isImageLink) {
			// img.setAttribute("id", "imgLink_"+i);
			// img.setAttribute("name", "imgLink_"+i);
			// }
			// no image link
			// if (!isImageLink) {
			// if('true' == isResizedImageOption){
			// img.style.width='30px';
			// img.style.height='30px';
			// }
			// img.style.textAlign='left';
			// } else {
			// resizeImageLink(imageId, img,isResizedImageOption == 'true');
			// }
			
			 if('true' == isResizedImageOption){
				 img.style.width='30px';
				 img.style.height='30px';
			 }
			
			 // resize image to fit screen in case resize_image = false &&
			// image_Width > Emulator screen width
			if('false' == isResizedImageOption){
				if(element == 'j2meEmulatorRowLink' && img.width > 281){
					img.style.width= '247px';
				}
				else if(element == 'androidEmulatorRowLink' && img.width > 240){
					img.style.width= '240px';
				}
			}
			
			
			if (imageId !=-1) {
				td1.appendChild(img); // append img to td
				var txtimg = document.createElement('input');
				txtimg.type = 'hidden';
				txtimg.id = 'txt_store_img_call_' + i;
				txtimg.name = txtimg.id;
				txtimg.value = imageId;
				td1.appendChild(txtimg);
			} 
			// else {
			  // if (!isImageLink) {
			  // td1.style.width = "20px";
	          // td2.style.width = "220px";
	          // td1.innerHTML="";
			  // }
			// }
			
			var text = "";		
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('text1') > -1) {
					text = inputs[j].value;
					break;
				}
			}
			
			// if (!isImageLink) {
			if(!isImageLink && "" != text){
			  div = document.createElement('div');
			  div.setAttribute('id','div_call_data_' + i);
			  div.innerHTML = text;
			  td2.appendChild(div);		  
			}				
			
			// check type when call_type is null
			if(isImageLink){
				if(link1_call == 'http://'){
					callTypeValue = '4';				
				}
			}
			else{
				if(link1_call == 'http://'){
					callTypeValue = '1';
				}
				else if(link1_call == 'call:'){
					callTypeValue = '2';
				}
				else if(link1_call == 'sms:'){
					callTypeValue = '3';
				}
			}		
			
			var callType = document.createElement('input');
			callType.setAttribute('type', 'hidden');
			callType.setAttribute('name', 'type_call_' + i);
			callType.setAttribute('id', 'type_call_' + i);
			callType.value = callTypeValue;
			td1.appendChild(callType);
			  
			$(element).appendChild(tr);
			// if (isImageLink) {
			// resizeImageLink(imageId, img,isResizedImageOption == 'true');
			// }
			
			if(pageName != ''){
				
				divcalid = '';
				addhr_call = document.getElementById('addhr_call_' + i);
				if(addhr_call){
					divcalid = addhr_call.id;
				}
				else{
					divcalid = 'addhr_call_' + i;
				}			
				
				if(divcalid != ''){
					
					tr1 = document.createElement('tr');
			        td1 = document.createElement('td');
			        
					div_hr = document.createElement('div');	      
				    div_hr.setAttribute('id','div_hr_' + divcalid);
			    	div_hr.style.maxWidth = '240px';	    	
			    	div_hr.style.display = divdisplay;
			    	
			    	hr = document.createElement('hr');
			        hr.setAttribute('id','hr_call_' + count);
			      	// hr.setAttribute('color','#' + rulerColer.value);
					// hr.setAttribute('size','1');
					hr.style.height = '1px';
					hr.style.borderWidth = '0px';
					hr.style.color = '#' + rulerColer.value;
					hr.style.backgroundColor = '#' + rulerColer.value;
					
					rowidcall = document.createElement('input');
					rowidcall.setAttribute('type', 'hidden');
					rowidcall.setAttribute('name', 'rowId1_call_' + i);
					rowidcall.setAttribute('id', 'rowId1_call_' + i);
					rowidcall.value = rowidcallValue;	
					
					linkcall = document.createElement('input');
					linkcall.setAttribute('type', 'hidden');
					linkcall.setAttribute('name', 'link_call_' + i);
					linkcall.setAttribute('id', 'link_call_' + i);
					linkcall.value = link1_call;
					
					textsmsbody = document.createElement('input');
					textsmsbody.setAttribute('type', 'hidden');
					textsmsbody.setAttribute('name', 'sms_text_body_' + i);
					textsmsbody.setAttribute('id', 'sms_text_body_' + i);
					textsmsbody.value = txtsmsbody;
					
					td1.colSpan = '2';  
			       	div_hr.appendChild(hr);
			       	td1.appendChild(div_hr);
			       	td1.appendChild(rowidcall);
			       	td1.appendChild(linkcall);
			       	td1.appendChild(textsmsbody);
			       	tr1.appendChild(td1);
			       	$(element).appendChild(tr1);
			}
				
		}
	}
}	

function resizeImageLink(imageId, img,resizeImage) {
	var screenWidth = getWidthHeightBgImage($F('tabSelected'));	
	new Ajax.Request('titleImage.json', {
        method: 'post',
        parameters: {'imageId': imageId,
		             'screenWidth': screenWidth[0],
		             'resize': resizeImage,
	                },
        onSuccess: function(transport) {
            var json = transport.responseText.evalJSON()
            if (json.isSampleImage) {
            	img.src = 'filessample/'  + imageId;
            }
            else {
            	img.src = 'files/'  + imageId;
            }
            
            img.style.width= json.widthImage + 'px';
            img.style.height= json.heightImage + 'px';
        }
    });
}

function imageFitToScreen(imageId, platform){
	var screenWidth = getWidthHeightBgImage($F('tabSelected'));
	
	new Ajax.Request('scaleImage.json', {
        method: 'post',
        parameters: {'imageId': imageId,
                     'screenWidth': screenWidth[0]},
        onSuccess: function(transport) {
            var json = transport.responseText.evalJSON();            
            $(platform).setAttribute('width',json.widthImage + 'px');
        }
    });
}

function updateEmulatorInternalPagePlatform(element) {
	
	// removed all'';
	for(var i = 0; i <document.getElementById(element).rows.length; i++)	{
		document.getElementById(element).deleteRow(i--);
	}
	if ($('internalpagelist') == null  ) return;
	
	var pageName = '';
	var isResizeImage = 'false';
	var rulerColer = document.getElementById('txtcolor');
	var txtpage = document.getElementById('txtpage');
	if(txtpage){
		pageName = txtpage.value;
	}
	
	var pagelist = $('internalpagelist').getElementsByTagName('tr');
	for(var i = 0; i < pagelist.length; i++){
		var selects = pagelist[i].getElementsByTagName('select');
		var inputs = pagelist[i].getElementsByTagName('input');
		
		var imageId = -1;
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('internal_img_id_') > -1) {
				imageId = inputs[j].value;
				break;
			}
		}

		var divdisplay = 'none';
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('txt_addhr_link_') > -1) {
				divdisplay = inputs[j].value;
				break;
			}
		}
		
		tr = document.createElement('tr'); // create tr
		td1 = document.createElement('td');// create td
		td2 = document.createElement('td');// create td
		td1.style.width = "35px";
		td2.style.width = "205px";
		td1.style.paddingLeft = "3px";
		td1.style.paddingTop = "1px";
		td1.style.paddingBottom = "1px";
		td2.style.paddingTop = "1px";
		td2.style.paddingBottom = "1px";
		tr.appendChild(td1);
		tr.appendChild(td2);
		
		var pagecombo = '';
		for(var j = 0; j < inputs.length; ++j){
			if(inputs[j].id.indexOf('page_comment_display_') > -1) {
				pagecombo = inputs[j].innerHTML;
				break;
			}			
		}
		
		var pagedescription = '';
		var text = "";
		for(var j = 0; j < inputs.length; ++j){
			if(inputs[j].id.indexOf('page_comment_') > -1) {
				if(inputs[j].value.length > 0){
					text = inputs[j].value;
					pagedescription = text;
				}
				break;
			}
		}
		
		// if (text=="") continue;
		// image cell
		img = document.createElement('img');
		for(var j = 0; j < inputs.length; ++j){
			if(inputs[j].id.indexOf('resize_image_option_id_') > -1) {
				if(inputs[j].value.length > 0){
					isResizeImage = inputs[j].value;
				}
				break;
			}
		}
		if('true' == isResizeImage){
			img.style.width='30px';
			img.style.height='30px';			
		}
		img.style.textAlign='left';
		if (imageId !=-1) {
			sampleimage(imageId, img);
			td1.appendChild(img); // append img to td
		} else {
			td1.style.width = "20px";
            td2.style.width = "220px";
			td1.innerHTML="";
		}	
		
		// resize image to fit screen in case resize_image = false &&
		// image_Width > Emulator screen width
		if('false' == isResizeImage){
			if(element == 'j2meEmulatorInternalLink' && img.width > 281){
				img.style.width= '240px';
			}
			else if(element == 'androidEmulatorInternalLink' && img.width > 240){
				img.style.width= '220px';
			}
		}
		
		div = document.createElement('div');	      
	    div.setAttribute('id','div_link_data_' + i);
	    div.innerHTML = text;
	    td2.appendChild(div);
	    
		// td2.innerHTML=text;
		$(element).appendChild(tr);
		
		if(pageName != ''){
			addhr_link = document.getElementById('addhr_link_' + i);
			divlinkid = '';
			if(addhr_link){
				divlinkid = addhr_link.id;
			}
			else{
				divlinkid = 'addhr_link_' + i;
			}
			
			rowidlink = '-1';
			rowId1= document.getElementById('rowId1_' + i);
			if(rowId1){
				rowidlink = rowId1.value;
			}
			
			if(divlinkid != ''){
				
				tr1 = document.createElement('tr');
		        td1 = document.createElement('td');
		        
				div_hr = document.createElement('div');	      
			    div_hr.setAttribute('id','div_hr_' + divlinkid);
		    	div_hr.style.maxWidth = '240px';	    	
		    	div_hr.style.display = divdisplay;
		    	
		    	hr = document.createElement('hr');
		        hr.setAttribute('id','hr_link_' + count);
		      	// hr.setAttribute('color','#' + rulerColer.value);
				// hr.setAttribute('size','1');
				hr.style.height = '1px';
				hr.style.borderWidth = '0px';
				hr.style.color = '#' + rulerColer.value;
				hr.style.backgroundColor = '#' + rulerColer.value;
				
				rowidlinkpage = document.createElement('input');
				rowidlinkpage.setAttribute('type', 'hidden');
				rowidlinkpage.setAttribute('name', 'rowId1_link_' + i);
				rowidlinkpage.setAttribute('id', 'rowId1_link_' + i);
				rowidlinkpage.value = rowidlink;
				
				td1.colSpan = '2';  
		       	div_hr.appendChild(hr);
		       	td1.appendChild(div_hr);
		       	td1.appendChild(rowidlinkpage);
		       	tr1.appendChild(td1);
		       	$(element).appendChild(tr1);
			}
			
		}
		
	}
}
/*
function updateEmulatorPageCallLinkSMSImageText(heightText){
	var isImgExist = false;
	var storeHeight = 0;		
	if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
		heightText = parseInt(heightText);
		storeHeight = heightText + 5;
	}
	
		// removed all
		for(var i = 0; i <document.getElementById("emulatorRowLink").rows.length; i++)	{
			document.getElementById("emulatorRowLink").deleteRow(i--);
		}
		
		if ($('pageListLinkCall') == null  ) return;
		
		var pageListLinkCall = $('pageListLinkCall').getElementsByTagName('li');
		
		for ( var i = 0; i < pageListLinkCall.length; ++i) {
			var selects = pageListLinkCall[i].getElementsByTagName('select');

			var inputs = pageListLinkCall[i].getElementsByTagName('input');

			var imageId = -1;
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('bullet1') > -1) {
					imageId = inputs[j].value;
					break;
				}
			}
			
			var isImageLink = false;
		    for (var j = 0; j < inputs.length; ++j) {
		       if (inputs[j].name.indexOf('img_link') > -1) {
		           isImageLink = true;
		       }
		    }
		
			tr = document.createElement('tr');
			td1 = document.createElement('td');
			td2 = document.createElement('td');
			
			if (isImageLink) {
				td1.setAttribute("colspan", "2");
			    td1.setAttribute("align", "center");
			    tr.appendChild(td1);
			}
			else{
				td1.style.width = "35px";
				td2.style.width = "205px";
				td2.setAttribute('align','left');
				td2.setAttribute('id','parent_div_text_link' + i);	
			}			
			
			tr.appendChild(td1);
			tr.appendChild(td2);
			
			div = document.createElement('div');
			// div.style.maxWidth = "100px";
			div.setAttribute('id','div_img_link' + i);		
			if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
			  div.style.top = heightText + "px";	
			  div.style.position = "absolute";
			  div.style.display = "block";
			}
			
			// image cell
			img = document.createElement('img');
			img.src = 'files/'  + imageId;
			if (isImageLink) {
				img.setAttribute("name", "imgLink_"+i);
				img.setAttribute("id", "imgLink_"+i);
			}				
			
			if (!isImageLink) {
	            img.style.width='30px';
	            img.style.height='30px';
	            img.style.textAlign='left';
	        } else {
	            resizeImageLink(imageId, img);
	        }
			
			if (imageId !=-1) {
				td1.appendChild(div);
				div.appendChild(img);
				isImgExist = true;
			} else {
				if (!isImageLink) {
					td1.style.width = "20px";
				    td2.style.width = "220px";
					td1.innerHTML="";
					isImgExist = false;	
				}				
			}
			
			var text = "";
			var link = "";
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('text1') > -1) {
					text = inputs[j].value;
				}
			}
			
			if (!isImageLink) {
				div = document.createElement('div');
				div.style.maxWidth = "220px";
				div.setAttribute('id','div_text_link' + i);
				if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {				
					if(isImgExist){
						div.style.top = (heightText + 5) + "px";
					}
					else{
						div.style.top = heightText + "px";
					}
					
					div.style.position = "absolute";
					div.style.display = "block";
				}		
						
				div.innerHTML = previewHtmlEscape(text);			
				td2.appendChild(div);	
			}						
			
			$('emulatorRowLink').appendChild(tr);
// if (isImageLink) {
// if(browserDetect.browser != 'Explorer') {
// window.setInterval('updateImageLinkSize()', 1000);
// }
// }
			
			if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
				var txtHeight = 0;
				if($('div_text_link' + i)){
					txtHeight = txtHeight = parseInt($('div_text_link' + i).offsetHeight);		
				}								
				
				if(isImgExist){
					var imgHeight = parseInt($('div_img_link' + i).offsetHeight);
					var imgWidth = parseInt($('div_img_link' + i).offsetWidth);
					
					if(isImageLink){
						imgWidth = parseInt(imgWidth/4);
						$('div_img_link' + i).style.left = imgWidth + "px";
					}
					else{
						imgWidth = imgWidth + 5;
						if($('div_text_link' + i)){
							$('div_text_link' + i).style.left = imgWidth + "px";	
						}	
					}										
					
					if(imgHeight > txtHeight){						
						heightText = imgHeight + heightText;
					}
					else{							
						heightText = txtHeight + heightText;							
					}
				}
				else{
					heightText = txtHeight + heightText;
					if($('div_text_link' + i)){
						$('div_text_link' + i).style.left = "15px";	
					}for(var i = 0; i <document.getElementById("emulatorInternalLink").rows.length; i++)	{
						document.getElementById("emulatorInternalLink").deleteRow(i--);
					}
				}					
				
				heightText = heightText + 5;									
				storeHeight = heightText;
			}
		}			
		
		$('txtAllTextStore').value = storeHeight;		
		updateEmulatorInternalPageImageText(storeHeight);
}

function updateEmulatorInternalPageImageText(heightText){		
	var isImgExist = false;
	if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
		heightText = parseInt(heightText);		
	}
	
	// removed all'';
	for(var i = 0; i <document.getElementById("emulatorInternalLink").rows.length; i++)	{			
		document.getElementById("emulatorInternalLink").deleteRow(i--);			
	}
	
	if ($('internalpagelist') == null) return;
	
	var pagelist = $('internalpagelist').getElementsByTagName('tr');
	
	for(var i = 0; i < pagelist.length; i++){
		var selects = pagelist[i].getElementsByTagName('select');
		var inputs = pagelist[i].getElementsByTagName('input');
		
		var imageId = -1;
		for(var j = 0; j < selects.length; ++j){
			if(selects[j].id.indexOf('image_id_') > -1){
				imageId = selects[j].options[selects[j].selectedIndex].value;
				break;
			}
		}
		
		tr = document.createElement('tr'); // create tr
		td1 = document.createElement('td');// create td
		td1.style.width = "30px";
		td2 = document.createElement('td');// create td
		td2.style.width = "210px";
		td2.setAttribute('align','left');
		td2.setAttribute('id','parent_div_text_inlink' + i);
		
		tr.appendChild(td1);
		tr.appendChild(td2);
		
		div = document.createElement('div');
		div.style.maxWidth = "100px";
		div.setAttribute('id','div_img_inlink' + i);		
		if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
		  div.style.top = heightText + "px";	
		  div.style.position = "absolute";
		  div.style.display = "block";		  
		}		
		
		var text = "";
		for(var j = 0; j < inputs.length; ++j){			
			if(inputs[j].id.indexOf('page_comment_') > -1){
				if(inputs[j].value.length > 0){
					text = inputs[j].value;
				}else{
					for(var k = 0; k < selects.length; ++k){
						if(selects[k].id.indexOf('page_id_') > -1){
							var option = selects[k].options[selects[k].selectedIndex];
							if(option.text.length > 0){
								text = option.text;
							}else{
								text = option.value;
							}
							break;
						}
					}
				}
				break;
			}
		}
		if (text=="") continue;
		
		// image cell
		img = document.createElement('img');
		img.src = 'files/'  + imageId;
		img.style.width='30px';
		img.style.height='30px';
		img.style.textAlign='left';
		
		if (imageId !=-1) {
			td1.appendChild(div);
			div.appendChild(img);			
			isImgExist = true;
		} else {
			td1.innerHTML="";
			isImgExist = false;
		}
		
		div = document.createElement('div');
		div.style.maxWidth = "240px";
		div.setAttribute('id','div_text_inlink' + i);
		if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
			if(isImgExist){
				div.style.top = (heightText + 5) + "px";
			}
			else{
				div.style.top = heightText + "px";
			}
			 
			div.style.position = "absolute";
			div.style.display = "block";
		}		
				
		div.innerHTML = previewHtmlEscape(text);			
		td2.appendChild(div);		
		
		$('emulatorInternalLink').appendChild(tr);
		
		if(browserDetect.browser == 'Explorer' && browserDetect.version >= 5.5) {
			var txtHeight = txtHeight = parseInt($('div_text_inlink' + i).offsetHeight);				
			
			if(isImgExist){
				var imgHeight = parseInt($('div_img_inlink' + i).offsetHeight);									
				var imgWidth = parseInt($('div_img_inlink' + i).offsetWidth);
				
				imgWidth = imgWidth + 5;
				$('div_text_inlink' + i).style.left = imgWidth + "px";
				
				if(imgHeight > txtHeight){
					heightText = imgHeight + heightText;
				}
				else{
					heightText = txtHeight + heightText;
				}
			}
			else{
				heightText = txtHeight + heightText;
				$('div_text_inlink' + i).style.left = "15px";
			}
			
			heightText = heightText + 5;
	}	  
		
  }	
}
*/
function isUserImage(imageId) {
	var userImage = false;
	for(var i = 0; i < alluserimages.length; i++) {
		if (imageId == alluserimages[i].id) {
			userImage = true;
			break;
		}
	}
	return userImage;
}

function isSampleImage(imageId) {
	var sampleImage = false;
	for(var i = 0; i < allsampleimages.length; i++) {
		if (imageId == allsampleimages[i].id) {
			sampleImage = true;
			break;
		}
	}
	return sampleImage;
}

function getImage1(imageId) {
	if (imageId == -1) {
		return "";
	}
	var imgObj = "";
	for(var i = 0; i < alluserimages.length; i++) {
		if (imageId == alluserimages[i].id) {
			imgObj = alluserimages[i];
			break;
		}
	}
	if (imgObj == "") {
		for(var i = 0; i < allsampleimages.length; i++) {
			if (imageId == allsampleimages[i].id) {
				imgObj = allsampleimages[i];
				break;
			}
		}
	}
	return imgObj;
}

function getImageAppWidth(platForm){
    var imageSize = getWidthHeightBgImage(platForm.toLowerCase());
    var width = (imageSize[0] * 90)/100;
    width = parseInt(width);
    
    return width;
}

/**
 * Function to check the image, if it is sample or user image.
 * 
 * @param imageId
 * @param img
 * @param parentCombo
 * @param needResize
 *            Optional: this is used for the banner and Header image only
 * @return
 */
function sampleimage(imageId, img, parentCombo, needResize) {
	var imageSize = getWidthHeightBgImage($F('tabSelected'));
	var imgObj = getImage1(imageId);
	if (imgObj != "") {
		if (imgObj.isSample == 0) {
			if (img != null) {				
				if (needResize) {
					img.src = 'files/'  + imageId + "?width=" + imageSize[0] + "&height=" + imageSize[1];
				}
				else {
					img.src = 'files/'  + imageId;
				}
				img.style.display = 'inline';
			}	
		}
		else if (imgObj.isSample == 1) {
			if (img != null) {
				if (needResize) {
					img.src = 'filessample/'  + imageId + "?width=" + imageSize[0] + "&height=" + imageSize[1];
				}
				else {
					img.src = 'filessample/'  + imageId;
				}
				img.style.display = 'inline';
			}
		}
		updateParentCombo(parentCombo, imageId);		
	}
	else {
		img.src = "";
		img.style.display = 'none';
	}
	
}

function updateEmuloaterInternalPage(templateName) {
	if (templateName == "carouselmenu") {
		updateEmulatorInternalPageCarouselPlatform('j2meEmulatorBodyContainer', 'j2meEmulatorInternalLink');
		updateEmulatorInternalPageCarouselPlatform('androidEmulatorBodyContainer', 'androidEmulatorInternalLink');
		updateEmulatorInternalPageCarouselPlatform('iphoneEmulatorBodyContainer', 'iphoneEmulatorInternalLink');
	}
	else if (templateName == "grid") {
		updateEmulatorInternalPageGridPlatform('j2meEmulatorInternalLink');
		updateEmulatorInternalPageGridPlatform('androidEmulatorInternalLink');
		updateEmulatorInternalPageGridPlatform('iphoneEmulatorInternalLink');
	}
	else {
		updateEmulatorInternalPagePlatform('j2meEmulatorInternalLink');
	    updateEmulatorInternalPagePlatform('androidEmulatorInternalLink');
	    updateEmulatorInternalPagePlatform('iphoneEmulatorInternalLink');
	}
}

function updateEmulatorInternalPageCarouselPlatform(parrentTag, childTag){
	var emulatorBody = document.getElementById(parrentTag);
	var emulatorInternalLink = document.getElementById(childTag);
	if(emulatorInternalLink){
		$(emulatorInternalLink).remove();
	}
	var emulatorInternalLink = document.createElement('tfoot');
	emulatorInternalLink.setAttribute('id',childTag);
	emulatorInternalLink.innerHTML='';
	emulatorBody.appendChild(emulatorInternalLink);
	
	var imgPreviewCount=0;
	tr1 = document.createElement('tr'); // create tr
	tr2 = document.createElement('tr'); // create tr
	var pagelist = $('internalpagelist').getElementsByTagName('tr');
	for(var i = 0; i < pagelist.length; i++){
		var selects = pagelist[i].getElementsByTagName('select');
		var inputs = pagelist[i].getElementsByTagName('input');
		
		var imageId = -1;
		for (var j = 0; j < inputs.length; j++) {
			if (inputs[j].id.indexOf('internal_img_id_') > -1) {
				imageId = inputs[j].value;
			}
		}
		
		td1 = document.createElement('td');// create td
		td1.style.width = "70px";
		td1.style.height = "70px";
		td1.style.paddingTop = "90px";
		td1.style.paddingLeft = "10px";
		td1.id="image_" + i;
		tr1.appendChild(td1);
		td2 = document.createElement('td');// create td
		td2.style.width = "70px";
		td2.style.textAlign='center';
		td2.id="text_" + i;
		tr2.appendChild(td2);
		
		var text = "";
		for(var j = 0; j < inputs.length; ++j){
			if(inputs[j].id.indexOf('page_comment_') > -1){
				if(inputs[j].value.length > 0){
					text = inputs[j].value;
				}else{
					for(var k = 0; k < selects.length; ++k){
						if(selects[k].id.indexOf('page_id_') > -1){
							var option = selects[k].options[selects[k].selectedIndex];
							if(option.text.length > 0){
								text = option.text;
							}else{
								text = option.value;
							}
							break;
						}
					}
				}
				break;
			}
		}
		if (text=="") continue;
		
		// image cell
		
		img = document.createElement('img');
		sampleimage(imageId, img, selects[0]);
		
		if(imgPreviewCount == pagelist.length-2){
			img.style.width='50px';
			img.style.height='50px';
			td2.innerHTML=text;
		}
		else{
			img.style.width='30px';
			img.style.height='30px';
			
		}
		img.style.textAlign='center';
		if (imageId !=-1) {
			td1.appendChild(img); // append img to td
			imgPreviewCount++;
		} else {
			td1.innerHTML="";
		}
		
		
		$(childTag).appendChild(tr1);
		$(childTag).appendChild(tr2);
		if(i-2>0){
			var tdr1 = document.getElementById("image_" + eval(i-3));
			var tdr2 = document.getElementById("text_" + eval(i-3));
			tr1.removeChild(tdr1);
			tr2.removeChild(tdr2);
		}
		
	}	
}

function updateEmulatorInternalPageGridPlatform(element){
	
	// removed all'';
	for(var i = 0; i <document.getElementById(element).rows.length; i++)	{
		document.getElementById(element).deleteRow(i--);
	}
	
	if ($('internalpagelist') == null  ) return;
	
		
		var imgPreviewCount=0;
		tr1 = document.createElement('tr'); // create tr
		tr2 = document.createElement('tr'); // create tr
		var pagelist = $('internalpagelist').getElementsByTagName('tr');
		for(var i = 0; i < pagelist.length; i++){
			
			if (i>0 && i%3 ==0) {
				tr1 = document.createElement('tr'); // create tr
			}
			
			var selects = pagelist[i].getElementsByTagName('select');
			var inputs = pagelist[i].getElementsByTagName('input');
			
			var imageId = -1;
			for (var j = 0; j < inputs.length; j++) {
				if (inputs[j].id.indexOf('internal_img_id_') > -1) {
					imageId = inputs[j].value;
				}
			}
			td1 = document.createElement('td');// create td
			td1.setAttribute('vAlign','top');
			td1.style.width = "70px";
			td1.style.paddingTop = "10px";
			td1.style.paddingLeft = "5px";
			td1.style.textAlign = "left";
			td1.id="image_" + i;
			tr1.appendChild(td1);			


			p = document.createElement('p');
			p.style.textAlign='left';
			p.id="text_" + i;
			
			var text = "";
			for(var j = 0; j < inputs.length; ++j){
				if(inputs[j].id.indexOf('page_comment_') > -1){
					if(inputs[j].value.length > 0){
						text = inputs[j].value;
					}else{
						for(var k = 0; k < selects.length; ++k){
							if(selects[k].id.indexOf('page_id_') > -1){
								var option = selects[k].options[selects[k].selectedIndex];
								if(option.text.length > 0){
									text = option.text;
								}else{
									text = option.value;
								}
								break;
							}
						}
					}
					break;
				}
			}
			if (text=="") continue;
			
			// image cell
			
			img = document.createElement('img');
			sampleimage(imageId, img, selects[0]);
			// if(imgPreviewCount == pagelist.length-1){
			// img.style.width='70px';
			// img.style.height='70px';
			// //td2.innerHTML=text;
			// }
			// else{
			// img.style.width='60px';
			// img.style.height='60px';
			//				
			// }
			
			img.style.width='60px';
			img.style.height='60px';
			img.style.textAlign='center';
			if (imageId !=-1) {
				td1.appendChild(img); // append img to td
				imgPreviewCount++;
			} else {
				td1.innerHTML="";
			}
			p.innerHTML=text;
			td1.appendChild(p);
			
			$(element).appendChild(tr1);			
		}	
}
	
function doSendEmailCommand(event){
	var mainform = $('mainform');
	mainform.command.value = 'sendEmailCommand';
	mainform.submit();
}

function doSaveEmailCommand(event){
	var mainform = $('mainform');
	mainform.command.value = 'saveEmailCommand';
	mainform.submit();
}

function doCancelEmailCommand(event){
	var mainform = $('mainform');
	mainform.command.value = 'cancelEmailCommand';
	mainform.submit();
}


function updateEmulatorListBullet(bulletListPlatform){
	// removed all
	if ($(bulletListPlatform) != null  ) { 
		for(var i = 0; i <document.getElementById(bulletListPlatform).rows.length; i++)	{
			document.getElementById(bulletListPlatform).deleteRow(i--);
		}
	}
	
	if ($('pagelistBullet') == null  ) return;
	
	var pageListBullets = $('pagelistBullet').getElementsByTagName('li');
	
	for ( var i = 0; i < pageListBullets.length; ++i) {
		var selects = pageListBullets[i].getElementsByTagName('select');

		var inputs = pageListBullets[i].getElementsByTagName('input');

		var imageId = -1;
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('bullet') > -1) {
				imageId = inputs[j].value;
				break;
			}
		}
	    
	    tr = document.createElement('tr');
        td1 = document.createElement('td');
        
        td2 = document.createElement('td');
        td1.style.width = "22px";
        td2.style.width = "218px";
        tr.appendChild(td1);
        tr.appendChild(td2);	
		
		// image cell
		img = document.createElement('img');
		// img.src = 'files/' + imageId;
		sampleimage(imageId, img, selects[0]);
	
        img.style.width='17px';
        img.style.height='17px';
        img.style.textAlign='left';
    	
		if (imageId !=-1) {
			td1.appendChild(img); // append img to td
		} 
		
		var text = "";		
		for ( var j = 0; j < inputs.length; ++j) {
			if (inputs[j].name.indexOf('text') > -1) {
				text = inputs[j].value;
			}
		}
		
		div = document.createElement('div');
		div.style.maxWidth = "220px";
		div.setAttribute('id','div_text_link' + i);
		div.innerHTML = previewHtmlEscape(text);			
		td2.appendChild(div)
		
		$(bulletListPlatform).appendChild(tr);		
	}
	
	if (intervalId != undefined && intervalId != "") {
	   window.clearInterval(intervalId);
	}
}

function updateEmulatorphotoboardGrid(emulatorPlatform){	
	// removed all'';
	for(var i = 0; i <document.getElementById(emulatorPlatform).rows.length; i++)	{
		document.getElementById(emulatorPlatform).deleteRow(i--);
	}
	if ($('internalpagelist') == null  ) return;
	
	
		var imgPreviewCount=0;
		tr1 = document.createElement('tr'); // create tr
		tr2 = document.createElement('tr'); // create tr
		var pagelist = $('internalpagelist').getElementsByTagName('li');
		for(var i = 0; i < pagelist.length; i++){
			
			if (i>0 && i%3 ==0) {
				tr1 = document.createElement('tr'); // create tr
			}
			
			
			var selects = pagelist[i].getElementsByTagName('select');
			var inputs = pagelist[i].getElementsByTagName('input');
			
			var imageId = -1;
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('bullet') > -1) {
					imageId = inputs[j].value;
					break;
				}
			}
			
			
			td1 = document.createElement('td');// create td
			td1.style.width = "70px";
			td1.style.paddingTop = "10px";
			td1.style.paddingLeft = "5px";
			td1.style.textAlign = "left";
			td1.id="image_" + i;
			tr1.appendChild(td1);
			p = document.createElement('p');
			p.style.textAlign='left';
			p.id="text_" + i;
			
			
			var text = "";
			for ( var j = 0; j < inputs.length; ++j) {
				if (inputs[j].name.indexOf('text') > -1) {
					text = inputs[j].value;
				}

			}
			img = document.createElement('img');
			sampleimage(imageId, img, selects[0]);
			// if(imgPreviewCount == pagelist.length-1){
			// img.style.width='50px';
			// img.style.height='50px';
			// // td2.innerHTML=text;
			// }
			// else{
			// img.style.width='50px';
			// img.style.height='50px';
			//				
			// }
			img.style.width='60px';
			img.style.height='60px';
			img.style.textAlign='center';
			if (imageId !=-1) {
				td1.appendChild(img); // append img to td
				imgPreviewCount++;
			} else {
				td1.innerHTML="";
			}
			$(emulatorPlatform).appendChild(tr1);
		}
}

function androidSubMenuHeightAlignment(menuLeft,menuRight) {
	menuLeft.style.width = '119px';
	menuRight.style.width = '119px';
	var height = menuLeft.offsetHeight;
	if (height < menuRight.offsetHeight) {
		height = menuRight.offsetHeight;
	}
	menuLeft.style.height = height + 'px';

	menuRight.style.height = height + 'px';
	
}

function androidSubMenuCheck(emulator){
	var childs = emulator.childElements();
	if (childs.length % 2 == 0){
		for(var i = 0; i<childs.length / 2;i++){
			var j = i * 2;
			var item1 = childs[j];
			var item2 = childs[j + 1];
			androidSubMenuHeightAlignment(item1,item2);
		}		
	} else {
		for(var i = 0; i< (childs.length - 1) / 2;i++){
			var j = i * 2;
			var item1 = childs[j];
			var item2 = childs[j + 1];
			androidSubMenuHeightAlignment(item1,item2);
		}
		childs[childs.length - 1].style.width = '238px';
	}
}

function unescapeHTML(html) {
	var htmlNode = document.createElement("DIV");
	htmlNode.innerHTML = html;
	if(htmlNode.innerText !== undefined)
	return htmlNode.innerText; // IE
	return htmlNode.textContent; // FF
}

function updateEmulator(tabSelected) {
	var platform = getSelectedPlatForm(tabSelected);
	var areas = document.getElementsByTagName('textarea');
    for (i = 0; i < areas.length; i++) {
       if (CKEDITOR.instances[areas[i].name] != null && $("emulatorcontent" + platform + i) != null) {
    	   var rawText = CKEDITOR.instances[areas[i].name].getData();  
           $("emulatorcontent" + platform + i).innerHTML = rawText;
       }
       else if(areas[i] != undefined && areas[i] != null && $("emulatorcontent" + platform + i) != null) {
    	   if (areas[i].innerHTML != "") {
    		   $("emulatorcontent" + platform + i).innerHTML = areas[i].innerHTML;
    	   }
       }
    }
}

/**
 * This function used to redirect from the current page to the imagemanagement
 * page
 * 
 * @param brochureId:
 *            In case user editing the existing brochure
 * @param fromPage:
 *            the current page. It is mandatory when the user want to redirect
 *            back to the specific page
 * @param fromPageId:
 *            the current page id. Used when the user editing the existing
 *            brochure.
 * @return
 */
function redirectToImageManagement(brochureId) {
	var mainForm = $('mainform');
	if (brochureId != undefined) {
		mainForm.action = 'imagemanagement.html?brochureId=' + brochureId;
	}
	else {
		mainForm.action = 'imagemanagement.html';
	}
	mainForm.command.value="commandUploadImage";
	mainForm.submit();
}

function redirectToImageManagementFromColorSchem(colorSchemeId) {
	var mainForm = $('mainform');
	// if (colorSchemeId != undefined) {
		mainForm.action = 'imagemanagement.html';
	// }
		$('command').value="commandUploadImage";
		mainForm.submit();
}

function backToColorScheme(colorSchemeId) {
	var mainform = $('mainform');
	if (colorSchemeId != -1) {
		mainform.action = "colorschemes.html?colorSchemeId=" + colorSchemeId;
	}
	else {
		mainform.action = "colorschemes.html";
	}
	mainform.command.value = "back";
    mainform.submit();
}
/**
 * Back from the image management page or other page to the previous page
 * 
 * @param pageName
 * @param brochureId
 * @param pageId
 * @return
 */
function backToPreviousPage(pageName, brochureId, pageId) {
	var mainform = $('mainform');
	if (brochureId != -1) {
		if (pageId != -1) {
			mainform.action = pageName + '?brochureId=' + brochureId + "&pageId=" + pageId;
		}
		else {
			mainform.action = pageName + '?brochureId=' + brochureId;
		}
	}
	else {
		mainform.action = pageName;
	}
	mainform.command.value = "back";
    mainform.submit();
}

function getSelectedPlatForm(selectedPlatform) {
	var platform;
	if (selectedPlatform == 1) {
		platform = 'J2me';
	}
	if (selectedPlatform == 2) {
		platform = 'Android';
	}
	else if(selectedPlatform == 3 ) {
		platform = 'Iphone';
	}
	return platform;
}

function updateEmulatorAdventCalendarGridPlatform(element){
	// removed all'';
	for(var i = 0; i <document.getElementById(element).rows.length; i++)	{
		document.getElementById(element).deleteRow(i--);
	}
	if ($('adventCalendarGridListItems') == null  ) return;
	
		
		tr1 = document.createElement('tr'); // create tr
		var lists = $('adventCalendarGridListItems').getElementsByTagName('li');
		for(var i = 0; i < lists.length; i++){
			
			if (i>0 && i%4 ==0) {
				tr1 = document.createElement('tr'); // create tr
			}
			
			var inputs = lists[i].getElementsByTagName('input');
			
			var imageId = -1;
			for (var j = 0; j < inputs.length; j++) {
				if (inputs[j].id.indexOf('start_image_') > -1) {
					imageId = inputs[j].value;
				}
			}
			td1 = document.createElement('td');// create td
			td1.setAttribute('vAlign','top');
			td1.style.width = "60px";
			td1.style.padding = "0px";
			td1.style.margin = "0px";
			td1.style.textAlign = "left";
			td1.id="image_" + i;
			tr1.appendChild(td1);			
			
			// image cell
			img = document.createElement('img');
			var imgObj = getImage1(imageId);
			if (imgObj != "") {
				img.style.display = 'inline';
				if (imgObj.isSample == 0) {
					img.src = 'files/'  + imageId;
				}
				else if (imgObj.isSample == 1) {
						img.src = 'filessample/'  + imageId;
					}
			}
			else {
				img.src = "";
				img.style.display = 'none';
			}
			
			img.style.width='60px';
			img.style.height='60px';
			
			img.style.textAlign='center';
			if (imageId !=-1) {
				td1.appendChild(img); // append img to td
			} 
			
			$(element).appendChild(tr1);			
		}	
}

function updateCKBackground(ckEditor,startColor,textColor){
	var css;
    css="background-color:#"+startColor;
    css = "body{"+css+";color:#"+textColor+";}";
    // alert(css);
    ckEditor.addCss(css);
}

function validateTelNumber(phone){
    var regexObj = /^(?:[+0-9] ?){6,14}[0-9]$/;
	
    if (!regexObj.test(phone)) {
    	alert('phone number is invalid! [accept only number ex +85517123456]');
    	return false;
    }
    
    return true;
}
/**
 * Validate application title
 * 
 * @param title
 *            application title
 * @returns boolean
 */
function validateBrochureTitle(title){
	var regularExp = /[<&]/;
	if(regularExp.test(title)){
		alert("An application title doesn't support special characters(<,&).");
		return false;
	}
	return true;
}

