/**
 * General javascript functions
 * 
 * @package		framework
 * @subpackage	base
 * @author		Roland Karle <rk@ateliervision.de>
 * @version 	1.0
 * @revision	$Id: functions.js 240 2007-11-09 17:49:43Z rk $
 * @since		25.10.2007
 */
 

 
/**
 * Returns the filename of a path
 * 
 * @return 	string
 */
function basename(v)
	{
	if (isset(v))
		{
		var pos = v.lastIndexOf("\\");
		var r = v.substring(pos+1);
		return r;
		}
	else
		{
		return false;
		}
	}
/**
 * Returns the browser type
 * 
 * ie		-> Internet Explorer 
 * opera	-> Opera
 * safari	-> Safari
 * gecko	-> Gecko based browsers (Firefox, Netscape)
 * 
 * @return 	string
 */
function browser()
	{
	switch (true)
		{
		case (window.attachEvent && !window.opera):
			return "ie";
			break;
		case (window.opera):
			return "opera";
			break;
		case (navigator.userAgent.indexOf('AppleWebKit/') > -1):
			return "safari";
			break;
		case (navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1):
			return "gecko";
			break;
		default:
			return false;
			break;
		}
	}
/**
 * Converts a decimal number to hexadecimal 
 * 
 * @param	int		v
 * @return	string
 */
function dec_hex(v)
	{
	return v.toString(16);
	}
/**
 * Converts a hexadecimal string to a decimal number
 * 
 * @param	string	v
 * @return	int
 */
function hex_dec(v) 
	{
	return parseInt(v,16);
	}
/**
 * Returns if a element is defined or undefined
 * 
 * @param	mixed 	v	value
 * @return 	bool
 */
function isset(v)
	{
	return (typeof v != "undefined" && v != null) ? true : false;
	}
/**
 * Returns if a element is null or not
 * 
 * @param	mixed 	v	value
 * @return 	bool
 */
function is_null(v)
	{
	return (v == null);
	}
/**
 * Opens a popup window
 * 
 * Possible options are:
 * - location		Show the location input field 
 * - menubar		Show menu of the browser
 * - resizable		Popup is resizable by the user
 * - scrollbars		Show scrollbars if needed
 * - status			Show the status bar
 * - toolbar		Show the toolbar
 * 
 * Default options are: "resizable,status,scrollbars"
 * 
 * @param	string	name
 * @param	int		width
 * @param	int		height
 * @param	int		left
 * @param	int		top
 * @param	string	options		
 */
function popup(name, url, width, height, left, top, options)
	{
	if (isset(name) && isset(url))
		{
		var option_string = "width=" + width + ",height=" + height;
		if (isset(left) && left != 0) option_string += ",left=" + left; 
		if (isset(top) && top != 0) option_string += ",top=" + top;
		if (!isset(options)) options = "resizable,status,scrollbars";
		if (options.indexOf("location") != -1) option_string += ",location=yes";
		if (options.indexOf("menubar") != -1) option_string += ",menubar=yes";
		if (options.indexOf("resizable") != -1) option_string += ",resizable=yes";
		if (options.indexOf("scrollbars") != -1) option_string += ",scrollbars=yes";
		if (options.indexOf("status") != -1) option_string += ",status=yes";
		if (options.indexOf("toolbar") != -1) option_string += ",toolbar=yes";
		option_string += ",dependent=yes";
		return window.open(url, name, option_string);
		}
	else
		{
		return false;
		}
	}
function rand(min, max)
	{
	var r = Math.round(Math.random() * (max - min)) + min;
	return r;
	}	
/**
 * Redirects the browser
 * 
 * @param	string	v	url to redirect
 * @return 	void
 */
function redirect(v)
	{
	if (isset(v))
		{
		window.location.href = v;			
		}
	}
/**
 * Strip script of a string
 * 
 * @param	string	v	string
 * @return 	string
 */
function strip_scripts(v)
	{
	return v.replace(/<script[^>]*>([\u0001-\uFFFF]*?)<\/script>/g, "");
	}
/**
 * Strip html tags of a string
 * 
 * @param	string	v	string
 * @return 	string
 */
function strip_tags(v)
	{
	return v.replace(/<[^>]*>/g, "");
	}

/* ---------------------------------------------------------------------------------- */

function control_image_clear(id)
	{
	try
		{
		new e.input(id).value(''); 
		new e(id + ".image").Element.src = "";
		}
	catch (e) {}
	}



function insert_image()
	{
	var src		= new e.input("system.media.url").Value;
	var alt 	= new e.input("system.media.image.alt").Value;
	var align 	= new e.select("system.media.image.align").Value;
	var border	= new e.input("system.media.image.border").Value;
	if (border == "") border = 0;
	var hspace	= new e.input("system.media.image.hspace").Value;
	if (hspace == "") border = 0;
	var vspace	= new e.input("system.media.image.vspace").Value;
	if (vspace == "") border = 0;
	var image 	= new e("system.media.image.preview");
	height		= image.Height;
	width		= image.Width;
	tinyMCE.themes['advanced']._insertImage(src, alt, border, hspace, vspace, width, height, align);			
	}



var selected;
var submitter = null;
function submitFunction()
	{
	submitter = 1;
	}
function popupWindow(url)
	{
	window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
	}  
function selectRowEffect(object, buttonSelect)
	{
	if (!selected)
		{
		if (document.getElementById) {
		selected = document.getElementById('defaultSelected');
    } else {
      selected = document.all['defaultSelected'];
    }
  }

  if (selected) selected.className = 'moduleRow';
  object.className = 'moduleRowSelected';
  selected = object;

// one button is not an array
  if (document.getElementById('payment'[0])) {
    document.getElementById('payment'[buttonSelect]).checked=true;
  } else {
    //document.getElementById('payment'[selected]).checked=true;
  }
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

function popupImageWindow(url) {
  window.open(url,'popupImageWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}
function check_form_optional(form_name) {
	  var form = form_name;

	  var firstname = form.elements['firstname'].value;
	  var lastname = form.elements['lastname'].value;
	  var street_address = form.elements['street_address'].value;

	  if (firstname == '' && lastname == '' && street_address == '') {
	    return true;
	  } else {
	    return check_form(form_name);
	  }
	}
function image_open(v)
{
var e_all = new e("layer.all");
var e_over = new e("overlay");
var e_div = new e("image_viewer.div");
var e_img = new e("image_viewer.img");

e_img.Element.src = v;

var browser_name = browser();

if (browser_name == "ie")
	{
		var links = 0;
		var top = 50 + mouse.ScrolledY;
		e_div.position(links, top);
		
		var resize_width = document.body.clientWidth + mouse.ScrolledX;
		var resize_height = document.body.clientHeight + mouse.ScrolledY;
		alert(document.body.clientWidth);
		alert(document.body.clientHeight);
		//e_over.resize(resize_width - 16, resize_height + 80);
	}
else
	{

		var height = (e_all.Height > mouse.WindowY) ? e_all.Height : mouse.WindowY;
		//height += mouse.ScrolledY; 
		
		var width = (e_all.Width > mouse.WindowX) ? e_all.Width : mouse.WindowX;
		//width += mouse.ScrolledX;
		//var height= screen.height;
		//var width = screen.width;
		//alert(width);
		//alert(height);
		
		//e_div.center();
		var links = 0;
		var top = 250;
		
		e_div.position(links, top);
		e_over.resize(width, height);
		e_over.opacity(0.75);
		alert(document.body.clientWidth);
		alert(document.body.clientHeight);
	}

e_over.show();
e_div.show();
}
function image_close()
{
var e_over = new e("overlay");
var e_div = new e("image_viewer.div");
var e_img = new e("image_viewer.img");
e_over.hide();
e_div.hide();
}

function check_form_confirmation()
	{
	if (!document.getElementById("checkout_confirmation").conditions.checked) 
		{
		var error_message = '* Sofern Sie unsere Allgemeinen Geschäftsbedingungen nicht akzeptieren,\n können wir Ihre Bestellung bedauerlicherweise nicht entgegennehmen!';
		var error = 1;
			}
	if (error == 1) 
		{
		alert(error_message);
		return false;
			}
		else 
			{
		return true;
			}
	}

var current_id 		= 5;
var current_fcolor 	= "000000"; 
var current_bcolor 	= "ffffff"; 
function select_color(id, fcolor)
{
if (!isset(id)) id = current_id;
if (!isset(fcolor)) fcolor 	= current_fcolor;
var opt_colors = document.getElementById("options.color");
for (i = 0; i < opt_colors.length; i++)
	{
 	if (opt_colors.options[i].value == id)
 		{
 		var color_name = opt_colors.options[i].text;
 		opt_colors.selectedIndex = i;
 		}
	}

var bcolor 		= new e.input("bcolor").Value;
var image_url 	= new e.input("product.image.url").Value;
var src 		= "image.php?image=" + image_url + "&options=alpha_colorize(" + fcolor.replace(/#/, "") + "," + bcolor.replace(/#/, "") + ")";
var src2 		= "image.php?image=" + image_url + "&options=alpha_colorize(" + fcolor.replace(/#/, "") + "," + bcolor.replace(/#/, "FFFFFF") + ");resize(200,200);";
var src3 		= "image.php?image=" + image_url + "&options=alpha_colorize_trans(" + fcolor.replace(/#/, "") + ")";	
var image 		= new e("product.image");
var image2 		= new e("product.image2");
var image3 		= new e("product.image3");
image.Element.src = src;
image2.Element.src = src2;
image3.Element.src = src3;
var label 		= new e("product.color.label");

if (matches = fcolor.match(/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/))
	{
	red 	= hex_dec(matches[1]);
	green 	= hex_dec(matches[2]);
	blue	= hex_dec(matches[3]);
	med = (red + green + blue) / 3;
	var label_font_color = (med > 127) ? "black" : "white";
	}
else
	{
	if (fcolor == "black")
		{
		var label_font_color = "white";
		}
	else
		{
		var label_font_color = "black";
		}
	}

label.style("color", label_font_color);
label.style("background", fcolor);
label.update(color_name);
current_id 		= id;
current_fcolor 	= fcolor; 
current_bcolor 	= bcolor; 
}
