// JavaScript Document

function roll(a){if (a.className.substr((a.className.length-1),1) == 'n') {b='o';}else {b='n';}a.className=a.className.substr(0,(a.className.length-1))+b;}

function rollId(a) {
	if (a.id.substr((a.id.length-1),1) == 'n') {b='o';}
	else {b='n';}
	a.id=a.id.substr(0,(a.id.length-1))+b;
}

function rollimg(a) {
	a.style.cursor='pointer';
	ext = a.src.substr((a.src.length-4),4);
	state = a.src.substr((a.src.length-6),2);
	if (state == '_o')
		a.src=a.src.substr(0,(a.src.length-6))+ext;
	else
		a.src=a.src.substr(0,(a.src.length-4))+'_o'+ext;
}

function delConfurm() {
	a=confirm('ARE YOU SURE YOU WANT TO DELETE THIS RECORDS?');
	if(a) {
		return true;
	}
	else {
		return false;
	}
}

function setInput(a,b) {
	document.getElementById(b).value = a;
	return true;
}

function request_ok(a) {
	var b = request_ok.arguments;
	var formname = a.form.name;
	for(var i=1;i<b.length;i++) {
		var c = '';
		c = eval('a.form.'+b[i]+'.value');
		if(c == '' || c == 'fill this' || c == '--') {
			if(eval('a.form.'+b[i]+'.tagName')=='SELECT')
				eval("a.form."+b[i]+".focus()");
			else {
				eval("a.form."+b[i]+".value = 'fill this'");
				eval("a.form."+b[i]+".select()");
			}
			return false;
		}
	}
	return true;
}

function check_numbers_input(a) {
	b = '';
	for (i=0;i<a.value.length;i++) {
		if ( (!isNaN(a.value.substr(i,1)) && a.value.substr(i,1) != ' ') || (a.value.substr(i,1) == '.' && a.value.indexOf('.')==i) ) {
			b += a.value.substr(i,1);
		}
	}
	if(a.value != b)
		a.value = b;
}

function check_dates(a,dateA,dateB,dateAtitle,dateBtitle,same) {
	data_A = document.getElementById(dateA).value.replace(/-/g, "/");
	dateObjA = new Date(data_A);
	data_B = document.getElementById(dateB).value.replace(/-/g, "/");
	dateObjB = new Date(data_B);
	if(same==true) {
		if(dateObjA > dateObjB) {
			alert('"'+dateAtitle+'" must be before or same as "'+dateBtitle+'" !');
			return false;
		}
	}
	else {
		if(dateObjA >= dateObjB) {
			alert('"'+dateAtitle+'" must be before "'+dateBtitle+'" !');
			return false;
		}
	}
	return true;
}

function define_one_day(a,dateA,dateB) {
	var today = new Date();
	var data_A = document.getElementById(dateA).value.replace(/-/g, "/");
	var dateObjA = new Date(data_A);
	if(dateObjA < today) {
		dateObjA = today;
		var A_month = dateObjA.getMonth()+1;
		A_month = A_month<=9 ? '0'+A_month.toString() : A_month.toString();
		var A_day = dateObjA.getDate();
		A_day = A_day<=9 ? '0'+A_day.toString() : A_day.toString();
		var A_year = dateObjA.getFullYear();
		document.getElementById(dateA).value = A_month +'-'+ A_day +'-'+ A_year;
	}
	data_B = document.getElementById(dateB).value.replace(/-/g, "/");
	dateObjB = new Date(data_B);
	if(dateObjA >= dateObjB) {
		var new_dateB = dateObjA.setDate(dateObjA.getDate()+1);
		var new_dateObjB = new Date(new_dateB);
		var B_month = new_dateObjB.getMonth()+1;
		B_month = B_month<=9 ? '0'+B_month.toString() : B_month.toString();
		var B_day = new_dateObjB.getDate();
		B_day = B_day<=9 ? '0'+B_day.toString() : B_day.toString();
		var B_year = new_dateObjB.getFullYear();
		document.getElementById(dateB).value = B_month +'-'+ B_day +'-'+ B_year;
	}
}

/*--------[ SELECT tag ]-------------*/
function addAmenity(a) {
	var listField = a.form.amenities_selected;
	var testField = a.form.amenities_list;
	for(var i=0;i<testField.length;i++) {
		if(a.form.amenities_list.options[i].selected==true) {
			var newValue = a.form.amenities_list.options[i].value;
			var newText = amenity_list[newValue];
			addToSelect(listField, newText, newValue);
		}
	}
}

function delAmenity(a) {
	listField = a.form.amenities_selected;
	removeFromSelect(listField);
}

function sendAmenities(a) {
	amenities_list='';
	select_tag = a.form.amenities_selected;
		for(i=0;i<select_tag.length;i++)
			amenities_list += select_tag.options[i].value + ',';
	a.form.new_amenity_list.value = amenities_list;
}

function addToSelect(listField, newText, newValue) {
   if ( ( newValue == "" ) || ( newText == "" ) ) {
//      alert("You cannot add blank values!");
   } else {
      var exist=false;
		for(var i=0;i<listField.length;i++) {
			if(newValue==listField.options[i].value)
				exist=true;
		}
		if(!exist) {
			var len = listField.length++; // Increase the size of list and return the size
			listField.options[len].value = newValue;
			listField.options[len].text = newText;
			listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
		}
   } // Ends the check to see if the value entered on the form is empty
}

function removeFromSelect(listField) {
   if ( listField.length == -1) {  // If the list is empty
//      alert("There are no values which can be removed!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
//         alert("You must select an entry to be removed!");
      } else {  // Build arrays with the text and values to remain
         var replaceTextArray = new Array(listField.length-1);
         var replaceValueArray = new Array(listField.length-1);
         for (var i = 0; i < listField.length; i++) {
            // Put everything except the selected one into the array
            if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
            if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
            if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
            if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
         }
         listField.length = replaceTextArray.length;  // Shorten the input list
         for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
            listField.options[i].value = replaceValueArray[i];
            listField.options[i].text = replaceTextArray[i];
         }
      } // Ends the check to make sure something was selected
   } // Ends the check for there being none in the list
}
/*--------[ SELECT tag end ]-------------*/

/*--------[ bubble help ]-------------*/
var Xoffset = -80;
var Yoffset = 20;
var timedelay, skn, b_height, b_width, timekilldelay; 
var ns4 = document.layers;
var ns6 = document.getElementById&&!document.all;
var ie4 = document.all;
var bubble_start_working = false;

function bubble_set() {
//	document.body.innerHTML = '<div style="visibility:hidden; width:1px; height:1px; display:none; left:0px; top:0px; position:absolute; z-index:200;" id="bubble"></div>' + document.body.innerHTML;
document.onmousemove = get_mouse;
	if (ns4)
		skn=document.bubble;
	else if (ns6)
		skn=document.getElementById("bubble").style;
	else if (ie4)
		skn=document.all.bubble.style;
	if(ns4)
		document.captureEvents(Event.MOUSEMOVE);
	else{
		skn.visibility="visible";
		skn.display="none";
	}
	bubble_start_working = true;
}

function bubble(msg,width,height) {
	if(bubble_start_working) {
		var content = msg;
		if(ns4) {
			skn.document.write(content);
			skn.document.close();
			skn.visibility="visible";
		}
		if(ns6) {
			document.getElementById("bubble").innerHTML=content;
			skn.display='';
		}
		if(ie4) {
			document.all("bubble").innerHTML=content;
			skn.display='';
		}
		bubble_obj=document.getElementById('bubble');
		b_height = bubble_obj.offsetHeight;
		b_width = bubble_obj.offsetWidth;
	}
}

function get_mouse(e) {
	if(bubble_start_working) {
		var x = (ns4||ns6) ? e.pageX : event.x + document.body.scrollLeft;
		temp_x = x + Xoffset;
		if(temp_x < 1) temp_x = 1;
		if((temp_x+b_width) > document.body.clientWidth) temp_x = document.body.clientWidth - b_width;
		skn.left = temp_x;
		var y = (ns4||ns6) ? e.pageY : event.y + document.body.scrollTop;
		temp_y = y + Yoffset;
		if((temp_y+b_height) > document.body.clientHeight) temp_y = y - b_height - 10;
		skn.top = temp_y;
	}
}

function bubble_kill() {
	if(bubble_start_working) {
//		clearTimeout(timedelay);
		timekilldelay = setTimeout('bubble_real_kill()',100);
	}
}
function bubble_real_kill() {
	if(bubble_start_working) {
		if(ns4)
			skn.visibility="hidden";
		else if (ns6||ie4)
			skn.display="none"
	}
}
/*--------[ bubble help end ]-------------*/

var last_pic_file = '';
function pic_bubble(file) {
	clearTimeout(timekilldelay);

	content='<table width="110" height="110" border="0" cellpadding="0" cellspacing="0" style="background-color:#FFF5D7;border:solid 1px #660000;">';
	content+='<tr><td align="center" valign="middle"><img src="../sys/pic.php?img='+file+'&h=100&w=100&q=90" style="border:solid 1px #FFFFFF;" /></td></tr></table>';
	if(file==last_pic_file) bubble(content,110,110);
	else {
		last_pic_file = file;
		bubble(content,110,110);
//		timedelay = setTimeout('bubble(content,110,110)',1000);
	}
}
function thumb_pic_bubble(name, size, date) {
	clearTimeout(timekilldelay);

	content='<table border="0" cellpadding="0" cellspacing="2" style="background-color:#FFF5D7;border:solid 1px #660000;font-size:10px;">';
	content+='<tr valign="top"><td>name:</td><td><b>'+name+'</b></td></tr><tr valign="top"><td>size:</td><td><b>'+size+'</b></td></tr><tr valign="top"><td>date:</td><td><b>'+date+'</b></td></tr></table>';
	bubble(content,220,110);
}
function product_bubble(a) {
	clearTimeout(timekilldelay);

	content='<table border="0" cellspacing="2" cellpadding="0" style="background-color:#FFF5D7;border:solid 1px #660000;font-size:10px;">';
	content+='	<tr valign="top"><td width="100" rowspan="12" align="center" valign="top">picture:<br>'+products_data[a].pic+'</td><td width="140" align="right">name : </td><td><b>'+products_data[a].name+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">description : </td><td><b>'+products_data[a].description+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">model number : </td><td><b>'+products_data[a].model_number+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">box quantity</td><td><b>'+products_data[a].box_quantity+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">weight : </td><td><b>'+products_data[a].weight+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">color : </td><td><b>'+products_data[a].color+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">price group : </td><td><b>'+products_data[a].price_group+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">prices/<br />price per additional color : </td><td><table border="0" cellspacing="0" cellpadding="2" style="font-size:10px;"><tr>';
	var counter = 0;
	for(var i in products_data[a].price_qt) {
		content+='					<td align="center" '+(counter++%2?'bgcolor="#fff2cc"':'bgcolor="#fff8e5"')+'><b>'+products_data[a].price_qt[i]+'</b></td>';
	}
	content+='				</tr><tr>';
	var counter = 0;
	for(var i in products_data[a].prices) {
		content+='					<td align="center" '+(counter++%2?'bgcolor="#fff2cc"':'bgcolor="#fff8e5"')+'><b>'+products_data[a].prices[i]+'</b></td>';
	}
	content+='				</tr><tr>';
	var counter = 0;
	for(var i in products_data[a].addcolor) {
		content+='					<td align="center" '+(counter++%2?'bgcolor="#fff2cc"':'bgcolor="#fff8e5"')+'><b>'+products_data[a].addcolor[i]+'</b></td>';
	}
	content+='				</tr></table></td></tr>';
//	content+='	<tr valign="top"><td align="right">price per additional color : </td><td><b>'+products_data[a].additional_color_price+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">price for optional UV lamination : </td><td><b>'+products_data[a].uv_lamination_price+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">color chart : </td><td><b>'+products_data[a].color_chart+'</b></td></tr>';
	content+='	<tr valign="top"><td align="right">fonts chart : </td><td><b>'+products_data[a].fonts_chart+'</b></td></tr>';
	content+='</table>';
	bubble(content,220,110);
}

/*--------[ filemanager ]-------------*/
var selected_count = 0;
function switch_state(a) {
	c_name = a.id+'_chk';
	c = document.getElementById(c_name);
	if (a.className == 'filelist_o') {
		b = 'selected_o';
		c.checked = true;
		selected_count++;
	}
	else {
		b='filelist_o';
		c.checked = false;
		selected_count--;
	}
	a.className=b;
	document.forms['file_operations'].f_rename.disabled=(selected_count!=1 ? true : false);
	document.forms['file_operations'].f_move.disabled=(!selected_count ? true : false);
	document.forms['file_operations'].f_delete.disabled=(!selected_count ? true : false);
}

function gatherInputs(name) {
	if(!name || name=='') return false;
	var inputs = document.getElementsByTagName("input");
	checkboxes = Array();
	for(i in inputs){
        if (inputs[i].name != null && inputs[i].name.indexOf(name) == 0 && inputs[i].checked)
        {
			checkboxes[checkboxes.length] = inputs[i].value;
        }
	}
	return checkboxes;
}

function del_confurm() {
	if(confirm('You\'re about to delete an important thing !\nAre You sure ?')) {
		return true;
	}
	return false;
}

function ask_for_name(the_name) {
	if(!the_name || the_name=='') the_name = 'type_new_name';
	if( !(name = prompt('Type the name for the new DIR', the_name)) ) { return false; }
	if(name!=''&&name!=the_name) {
		if(name.indexOf('*') > -1 ||
		   name.indexOf('"') > -1 ||
		   name.indexOf('<') > -1 ||
		   name.indexOf('>') > -1 ||
		   name.indexOf("'") > -1 ||
		   name.indexOf('?') > -1) {
			alert('The name can\'t contain [<], [>], ["], [\'], [*] or [?] symbols !');
			name = ask_for_name(the_name);
		}
		return name;
	}
	return false;
}

function add_dir() {
	name = ask_for_name();
	if(name!=false && name!='false') {
		document.forms['dir_operations'].add_new_dir.value = name;
		document.forms['dir_operations'].submit();
	}
}

function del_dir() {
	if(del_confurm()) {
		document.forms['dir_operations'].del_curr_dir.value = 'yes';
		document.forms['dir_operations'].submit();
	}
}

function ren_dir(old_name) {
	name = ask_for_name(old_name);
	if(name!=false && name!='false') {
		document.forms['dir_operations'].ren_curr_dir.value = name;
		document.forms['dir_operations'].submit();
	}
}

function ren_file() {
	checkboxes = gatherInputs('filelist_chk');
	old_name = checkboxes[0];
	name = ask_for_name(old_name);
	if(name!=false && name!='false') {
		document.forms['file_operations'].ren_curr_file.value = name;
		return true;
	}
	return false;
}
// for file browser
function select_pic(a) {
	if(a=='')
		return false;
	opener.pic_holder.value=a;
	opener.pic_show(a);
	window.close();
}
// for file browser
function pic_show(picture) {
	var picObj = document.getElementById('picture_container');
	if(picture==''){
		picObj.innerHTML = "";
		return false;
	}
	var pic_path = "../sys/pic.php?w=120&h=120&q=50&img=../UserFiles/Image/";
	picObj.innerHTML = '<img src="' + pic_path + picture + '" />';
	return true;
}

var pic_holder;
var picBrowser;
function get_picture(a) {
	if (typeof(picBrowser) != 'undefined' && !(picBrowser.closed)) 
		picBrowser.close();
	pic_holder = a;
	width = 820;
	height = 400;
	picBrowser = window.open('file_browser.php','picBrowser', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width='+width+',height='+height+'');
}
/*--------[ filemanager end ]-------------*/

function insert_flash (){
	parameters = '';
	file = '';
	id = '';
	width = '';
	height = '';
	bgcolor = '#ffffff';
	wmode = 'transparent';
	scale = 'showAll';
	
	for(var i=0; i<arguments.length; i++){
	    arg = arguments[i].split('=');
	  	if (arg[0] == 'file'){file = arg[1]}
		else if (arg[0] == 'id'){id = arg[1]}
		else if (arg[0] == 'width'){width = arg[1]}
		else if (arg[0] == 'height'){height = arg[1]}
		else if (arg[0] == 'bgcolor'){bgcolor = arg[1]}
		else if (arg[0] == 'wmode'){wmode = arg[1]}
		else if (arg[0] == 'scale'){scale = arg[1]}
		else {parameters += arguments[i]}
	}
	//alert(file + '\n' + HTMLobj + '\n' + width + '\n' +height + '\n' +bgcolor + '\n' +wmode + '\n' +parameters);
	document.getElementById(id).innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="temp" align="middle">'+
	'<param name="allowScriptAccess" value="sameDomain" />'+
	'<param name="movie" value="'+file+'" />'+
	'<param name="quality" value="high" />'+
	'<param name="bgcolor" value="'+bgcolor+'" />'+
	'<param name="wmode" value="'+wmode+'" />'+
	'<param name="scale" value="'+wmode+'" />'+
	'<param name="FlashVars" value="'+parameters+'">'+
	'<embed src="'+file+'" FlashVars="'+parameters+'" quality="high" wmode="'+wmode+'" scale="'+scale+'" bgcolor="'+bgcolor+'" width="'+width+'" height="'+height+'" name="temp" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
	'</object>';
}

function print_round(a) {
	b = a.toString();
	c = b.split('.');
	d = c[0] +'.'+ (c.length>1 ? c[1]+(c[1].length<2 ? '0' : '') : '00');
	return d;
}

var in_the_end = "done";
