
var winopts = "dependent=yes,toolbar=no,resizable=yes,width=900,height=700,scrollbars=yes";

function submitLoginForm(){
	document.getElementById('login').submit();
}

function submitStockForm(){
	document.getElementById('stock').submit();
}

function getViewportSize()
{
		  var size = [0, 0];

		  if (typeof window.innerWidth != 'undefined')
		  {
					 size = [
								window.innerWidth,
								window.innerHeight
										  ];
		  }
		  else if (typeof document.documentElement != 'undefined'
								&& typeof document.documentElement.clientWidth !=
								'undefined' && document.documentElement.clientWidth != 0)
		  {
					 size = [
								document.documentElement.clientWidth,
					 document.documentElement.clientHeight
								];
		  }
		  else
		  {
					 size = [
								document.getElementsByTagName('body')[0].clientWidth,
					 document.getElementsByTagName('body')[0].clientHeight
								];
		  }

		  return size;
}

	function toggle_element(id){
		if(document.getElementById(id).style.display == "inline"){
			document.getElementById(id).style.display = "none";
		}else{
			document.getElementById(id).style.display = "inline";
		}
	}

	function show_element(id,type){
		document.getElementById(id).style.display = type;
	}

	function hide_element(id){
		document.getElementById(id).style.display = 'none';
	}

	function addToId(id,str){
		if(el = document.getElementById(id)){
			el.innerHTML = el.innerHTML + str;
		}
	}

	function addContent(type, id, contentId){
		switch(type){
			case "Binary":
				if(a = prompt("Name this link")){
					change = '<a href="/data/get/'+id+'">'+a+'</a> ';
				}
				break;
			case "Flash":
				if(x = prompt("Width?")) {
					if(y = prompt("Height?")){
						change = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+x+'" height="'+y+'"><param name="movie" value="/data/get/'+id+'"> <param name="quality" value="high"> <embed src="/data/get/'+id+'" quality="high" width="'+x+'" height="'+y+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>';
					}
				}
				break;
			case "Images":
				if(a = prompt("Type outgoing link (leave empty for none)")){
					change = '<a href="'+a+'">'+'<img src="/data/get/'+id+'"></a> ';
				}else{
					change = '<img src="/data/get/'+id+'">';
				}
				break;
			case "Video":
				change = '<!--content:'+id+'--><!--startignore--><img id="'+id+'" src="/i/video_placeholder.png"><!--ignore-->';
				break;
			case "Content":
				if(a = prompt("Name this link")){
					change = '<a href="/content/node/'+id+'">'+a+'</a> ';
				}
				break;
			case "Products":
				if(a = prompt("Name this link")){
					change = '<a href="/products/get/'+id+'">'+a+'</a> ';
				}
				break;
		}
		if(change){
			opener.tinyMCE.execInstanceCommand("content"+contentId,"mceInsertContent",false,change);
		}
	}

	var moreFiles = '<p><input type="file" name="file[]"></p>';
	var moreFilesFile = '<p>Name it: <input type="text" name="name[]"><input type="file" name="file[]"></p>';

	var products = new Array(
		'product',
		'stock',
		'photos',
		"salePeriod",
		'relationship',
		'priceBreak'
	);

	function manageTabs(tabVar, tabSelected){
		for(i=0;i<tabVar.length;i++){
			if(tabVar[i] != tabSelected){
				document.getElementById(tabVar[i]).style.display = 'none';
			}else{
				document.getElementById(tabVar[i]).style.display = 'block';
			}
		}
	}

function allowRegexpChar(event, object, regexpstr) {
		  var code = (event.which == null) ? event.keyCode : event.which;
		  if (code < 0x20 || code > 0x7e)
					 return true;
		  var regexp = eval("/" + regexpstr + "/");
		  var str = String.fromCharCode(code);
		  if (str.match(regexp) == null)
					 return false;
		  return true;
}

function checkACL(){
}

function setFormDate(year,month,day,prefix){
	if(!prefix){
		prefix = '';
	}else{
		prefix = prefix + "_";
	}
	if(year && month && day){
		setSelectToOption(prefix + 'month',month);
		setSelectToOption(prefix + 'day',day);
		setSelectToOption(prefix + 'year',year);
	}else{
		var d = new Date();
		setSelectToOption(prefix + 'month',d.getMonth()+1);
		setSelectToOption(prefix + 'day',d.getDate());
		setSelectToOption(prefix +'year',d.getFullYear());
	}
}


function setSelectToOption(id,value){
		  for (var idx=0;idx<document.getElementById(id).options.length;idx++) {
					 if (value==document.getElementById(id).options[idx].value) {
								document.getElementById(id).selectedIndex=idx;
								document.getElementById(id).options[idx].selected=true;
								break;
					 }
		  }      

}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
		  var output = "";
		  var chr1, chr2, chr3;
		  var enc1, enc2, enc3, enc4;
		  var i = 0;

		  do {
					 chr1 = input.charCodeAt(i++);
					 chr2 = input.charCodeAt(i++);
					 chr3 = input.charCodeAt(i++);

					 enc1 = chr1 >> 2;
					 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
					 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
					 enc4 = chr3 & 63;

					 if (isNaN(chr2)) {
								enc3 = enc4 = 64;
					 } else if (isNaN(chr3)) {
								enc4 = 64;
					 }

					 output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
								keyStr.charAt(enc3) + keyStr.charAt(enc4);
		  } while (i < input.length);

		  return output;
}

function testLayout(id,isPreview){
	if(!isPreview){
		if(document.getElementById('alias').value != '' || document.getElementById('sta').value == 'On hold'){
			for(var i in ai){
				if( document.getElementById('alias').value == ai[i] && id != i){
					setSelectToOption('sta','On hold');	
					alert('The alias you have chosen is already in use by a different page. The status was changed back to "On Hold". All other changes have been saved. In order to publish this page, please make sure that no other page is using the same alias.');
				}
			}
			document.getElementById('noteEdit').submit()
		} else { 
			setSelectToOption('sta','On hold');	
			alert('All your changes have been saved, but the page was put back on hold. Please enter an alias for this page first before you publish it.'); 
			document.getElementById('noteEdit').submit()
		}
	} else {
		if(document.getElementById('sta').value == 'On hold'){
			alert('Please change the page status before you try to view this page.');
			return false;
		}
		if(document.getElementById('alias').value == ''){
			alert('Please enter a path for this	page before you try to view it.');
			return false;
		}
		for(var i in ai){
			if( document.getElementById('alias').value == ai[i] && id != i){
				alert('The alias you have chosen is already in use by a different page.');
				return false;
			}
		}
		if(confirm('Before this page is shown, any changes will be saved permanently. Do you want to continue?')){
			changeFormTo('noteEdit','/layout/pstore/'+id);
			document.getElementById('noteEdit').submit();
		}
	}
}

function setCData(countryId, stateId, countryGadget, stateGadget){

	cSelect = document.getElementById(countryGadget);
	for (country in cData){
		var elOptNew = document.createElement('option');
		elOptNew.value = country; 
		elOptNew.text = cData[country]; 
		if(countryId == country){
			elOptNew.selected = true;
		}
		try {
			cSelect.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
			cSelect.add(elOptNew); // IE only
		}
	}
	if(stateId){
		var cState = document.getElementById(stateGadget);
		if(cState.options.length > 0){
			for(oItems in cState){
				cState.remove(oItems);
			}
		}
		for(state in pData[countryId]){
			var elOptStateNew = document.createElement('option');
			elOptStateNew.value = state;
			elOptStateNew.text = pData[countryId][state];
			if(stateId == state){
				elOptStateNew.selected = true;
			}
			try {
				cState.add(elOptStateNew, null); // standards compliant; doesn't work in IE
			}
			catch(ex) {
				cState.add(elOptStateNew); // IE only
			}	
		}
	}
}
