<?php
  function xtc_href_link($page = '', $parameters = '', $connection = 'NONSSL' ) {
    if ($page == '') {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine the page link!<br><br>Function used:<br><br>xtc_href_link(\'' . $page . '\', \'' . $parameters . '\', \'' . $connection . '\')</b>');
    }

    if ($connection == 'NONSSL') {
      if( stristr($_SERVER['REQUEST_URI'],DIR_WS_ADMIN) )
	      $link = HTTP_SERVER . DIR_WS_ADMIN;
      else
	      $link = HTTP_SERVER;
    } elseif ($connection == 'SSL') {
      if (ENABLE_SSL == 'true') {
	if( stristr($_SERVER['REQUEST_URI'],DIR_WS_ADMIN) )
        	$link = HTTPS_CATALOG_SERVER . DIR_WS_ADMIN;
	else
        	$link = HTTPS_CATALOG_SERVER;
      } else {
	if( stristr($_SERVER['REQUEST_URI'],DIR_WS_ADMIN) )
	        $link = HTTP_SERVER . DIR_WS_ADMIN;
	else
	        $link = HTTP_SERVER;
      }
    } else {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine connection method on a link!<br><br>Known methods: NONSSL SSL<br><br>Function used:<br><br>xtc_href_link(\'' . $page . '\', \'' . $parameters . '\', \'' . $connection . '\')</b>');
    }
    if ($parameters == '') {
      $link = $link . $page . '?' . SID;
    } else {
      $link = $link . $page . '?' . $parameters . '&' . SID;
    }

    while ( (substr($link, -1) == '&') || (substr($link, -1) == '?') ) $link = substr($link, 0, -1);

    return $link;
  }

  function xtc_href_link_admin($page = '', $parameters = '', $connection = 'NONSSL' ) {
    if ($page == '') {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine the page link!<br><br>Function used:<br><br>xtc_href_link(\'' . $page . '\', \'' . $parameters . '\', \'' . $connection . '\')</b>');
    }

    if ($connection == 'NONSSL') {
      $link = HTTP_SERVER . DIR_WS_ADMIN;
    } elseif ($connection == 'SSL') {
      if (ENABLE_SSL == 'true') {
	if( stristr($_SERVER['REQUEST_URI'],DIR_WS_ADMIN) )
        	$link = HTTPS_CATALOG_SERVER . DIR_WS_ADMIN;
	else
        	$link = HTTPS_CATALOG_SERVER;
      } else {
        $link = HTTP_SERVER . DIR_WS_ADMIN;
      }
    } else {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine connection method on a link!<br><br>Known methods: NONSSL SSL<br><br>Function used:<br><br>xtc_href_link(\'' . $page . '\', \'' . $parameters . '\', \'' . $connection . '\')</b>');
    }
    if ($parameters == '') {
      $link = $link . $page . '?' . SID;
    } else {
      $link = $link . $page . '?' . $parameters . '&' . SID;
    }

    while ( (substr($link, -1) == '&') || (substr($link, -1) == '?') ) $link = substr($link, 0, -1);

    return $link;
  }

  function xtc_catalog_href_link($page = '', $parameters = '', $connection = 'NONSSL') {
    if ($connection == 'NONSSL') {
      $link = HTTP_CATALOG_SERVER . DIR_WS_CATALOG;
    } elseif ($connection == 'SSL') {
      if (ENABLE_SSL_CATALOG == 'true') {
        $link = HTTPS_CATALOG_SERVER . DIR_WS_CATALOG;
      } else {
        $link = HTTP_CATALOG_SERVER . DIR_WS_CATALOG;
      }
    } else {
      die('</td></tr></table></td></tr></table><br><br><font color="#ff0000"><b>Error!</b></font><br><br><b>Unable to determine connection method on a link!<br><br>Known methods: NONSSL SSL<br><br>Function used:<br><br>xtc_href_link(\'' . $page . '\', \'' . $parameters . '\', \'' . $connection . '\')</b>');
    }
    if ($parameters == '') {
      $link .= $page;
    } else {
      $link .= $page . '?' . $parameters;
    }

    while ( (substr($link, -1) == '&') || (substr($link, -1) == '?') ) $link = substr($link, 0, -1);

    return $link;
  }

  ////
  // The HTML image wrapper function
  function xtc_image($src, $alt = '', $width = '', $height = '', $params = '') {
    $image = '<img src="' . $src . '" border="0" alt="' . $alt . '"';
    if ($alt) {
      $image .= ' title=" ' . $alt . ' "';
    }
    if ($width) {
      $image .= ' width="' . $width . '"';
    }
    if ($height) {
      $image .= ' height="' . $height . '"';
    }
    if ($params) {
      $image .= ' ' . $params;
    }
    $image .= '>';

    return $image;
  }

  ////
  // The HTML form submit button wrapper function
  // Outputs a button in the selected language
  function xtc_image_submit($image, $alt, $params = '') {

    return '<input type="image" src="' .DIR_WS_LANGUAGES . $_SESSION['language'] . '/admin/images/buttons/' . $image . '" border="0" alt="' . $alt . '"' . (($params) ? ' ' . $params : '') . '>';
  }

  ////
  // Draw a 1 pixel black line
  function xtc_black_line() {
    return xtc_image(DIR_WS_IMAGES . 'pixel_black.gif', '', '100%', '1');
  }

  ////
  // Output a separator either through whitespace, or with an image
  function xtc_draw_separator($image = 'pixel_black.gif', $width = '100%', $height = '1') {
    return xtc_image(DIR_WS_IMAGES . $image, '', $width, $height);
  }

  ////
  // Output a function button in the selected language
  function xtc_image_button($image, $alt = '', $params = '') {

    return xtc_image(DIR_WS_LANGUAGES. $_SESSION['language'] . '/admin/images/buttons/' . $image, $alt, '', '', $params);
  }

  ////
  // javascript to dynamically update the states/provinces list when the country is changed
  // TABLES: zones
  function xtc_js_zone_list($country, $form, $field) {
    $countries_query = xtc_db_query("select distinct zone_country_id from " . TABLE_ZONES . " order by zone_country_id");
    $num_country = 1;
    $output_string = '';
    while ($countries = xtc_db_fetch_array($countries_query)) {
      if ($num_country == 1) {
        $output_string .= '  if (' . $country . ' == "' . $countries['zone_country_id'] . '") {' . "\n";
      } else {
        $output_string .= '  } else if (' . $country . ' == "' . $countries['zone_country_id'] . '") {' . "\n";
      }

      $states_query = xtc_db_query("select zone_name, zone_id from " . TABLE_ZONES . " where zone_country_id = '" . $countries['zone_country_id'] . "' order by zone_name");

      $num_state = 1;
      while ($states = xtc_db_fetch_array($states_query)) {
        if ($num_state == '1') $output_string .= '    ' . $form . '.' . $field . '.options[0] = new Option("' . PLEASE_SELECT . '", "");' . "\n";
        $output_string .= '    ' . $form . '.' . $field . '.options[' . $num_state . '] = new Option("' . $states['zone_name'] . '", "' . $states['zone_id'] . '");' . "\n";
        $num_state++;
      }
      $num_country++;
    }
    $output_string .= '  } else {' . "\n" .
                      '    ' . $form . '.' . $field . '.options[0] = new Option("' . TYPE_BELOW . '", "");' . "\n" .
                      '  }' . "\n";

    return $output_string;
  }

  ////
  // Output a form
  function xtc_draw_form($name, $action, $parameters = '', $method = 'post', $params = '') {
    $form = '<form name="' . $name . '" action="';
    if ($parameters) {
      $form .= xtc_href_link($action, $parameters);
    } else {
      $form .= xtc_href_link($action);
    }
    $form .= '" method="' . $method . '"';
    if ($params) {
      $form .= ' ' . $params;
    }
    $form .= '>';

    return $form;
  }

  ////
  // Output a form input field
  function xtc_draw_input_field($name, $value = '', $parameters = '', $required = false, $type = 'text', $reinsert_value = true) {
    $field = '<input type="' . $type . '" name="' . $name . '"';
    if ( ($GLOBALS[$name]) && ($reinsert_value) ) {
      $field .= ' value="' . htmlspecialchars(trim($GLOBALS[$name])) . '"';
    } elseif ($value != '') {
      $field .= ' value="' . htmlspecialchars(trim($value)) . '"';
    }
    if ($parameters != '') {
      $field .= ' ' . $parameters;
    }
    $field .= '>';

    if ($required) $field .= TEXT_FIELD_REQUIRED;

    return $field;
  }
  // Output a form small input field
  function xtc_draw_small_input_field($name, $value = '', $parameters = '', $required = false, $type = 'text', $reinsert_value = true) {
    $field = '<input type="' . $type . '" size="3" name="' . $name . '"';
    if ( ($GLOBALS[$name]) && ($reinsert_value) ) {
      $field .= ' value="' . htmlspecialchars(trim($GLOBALS[$name])) . '"';
    } elseif ($value != '') {
      $field .= ' value="' . htmlspecialchars(trim($value)) . '"';
    }
    if ($parameters != '') {
      $field .= ' ' . $parameters;
    }
    $field .= '>';

    if ($required) $field .= TEXT_FIELD_REQUIRED;

    return $field;
  }

  ////
  // Output a form password field
  function xtc_draw_password_field($name, $value = '', $required = false) {
    $field = xtc_draw_input_field($name, $value, 'maxlength="40"', $required, 'password', false);

    return $field;
  }

  ////
  // Output a form filefield
  function xtc_draw_file_field($name, $required = false) {
    $field = xtc_draw_input_field($name, '', '', $required, 'file');

    return $field;
  }

  ////
  // Output a selection field - alias function for xtc_draw_checkbox_field() and xtc_draw_radio_field()
  function xtc_draw_selection_field($name, $type, $value = '', $checked = false, $compare = '') {
    $selection = '<input type="' . $type . '" name="' . $name . '"';
    if ($value != '') {
      $selection .= ' value="' . $value . '"';
    }
    if ( ($checked == true) || ($GLOBALS[$name] == 'on') || ($value && ($GLOBALS[$name] == $value)) || ($value && ($value == $compare)) ) {
      $selection .= ' CHECKED';
    }
    $selection .= '>';

    return $selection;
  }

  ////
  // Output a form checkbox field
  function xtc_draw_checkbox_field($name, $value = '', $checked = false, $compare = '') {
    return xtc_draw_selection_field($name, 'checkbox', $value, $checked, $compare);
  }

  ////
  // Output a form radio field
  function xtc_draw_radio_field($name, $value = '', $checked = false, $compare = '') {
    return xtc_draw_selection_field($name, 'radio', $value, $checked, $compare);
  }

  ////
  // Output a form textarea field
  function xtc_draw_textarea_field($name, $wrap, $width, $height, $text = '', $params = '', $reinsert_value = true) {
    $field = '<textarea id="'.$name.'" name="' . $name . '" wrap="' . $wrap . '" cols="' . $width . '" rows="' . $height . '"';
    if ($params) $field .= ' ' . $params;
    $field .= '>';
    if ( ($GLOBALS[$name]) && ($reinsert_value) ) {
      $field .= $GLOBALS[$name];
    } elseif ($text != '') {
      $field .= $text;
    }
    $field .= '</textarea>';

    return $field;
  }

  ////
  // Output a form hidden field
  function xtc_draw_hidden_field($name, $value = '') {
    $field = '<input type="hidden" name="' . $name . '" value="';
    if ($value != '') {
      $field .= trim($value);
    } else {
      $field .= trim($GLOBALS[$name]);
    }
    $field .= '">';

    return $field;
  }

  /**
   * Draw the HTML Form-Element Select 
   *
   * @param String $name The Name for the Input Element
   * @param Array $values The Options Array. Format: array('id'=>id,'text'=>text)
   * @param Mixed $default The default Value for the Selectbox
   * @param String $params Optional HTML Parameters for select-Tag
   * @param Bool $required Required - Optional an not implemented in YES Framework
   * @return HTML Form Element <select>
   */
  function xtc_draw_pull_down_menu($name, $values, $default = '', $params = '', $required = false) {
    $field = '<select name="' . $name . '"';
    if ($params) $field .= ' ' . $params;
    $field .= '>';
    for ($i=0; $i<sizeof($values); $i++) {
      $field .= '<option value="' . $values[$i]['id'] . '"';
      
      // MULTIPLE SELECTBOX ADDON
      if(is_array($default) and sizeOf($default)>0){
      	if ( ((strlen($values[$i]['id']) > 0) && ($GLOBALS[$name] == $values[$i]['id'])) || (in_array($values[$i]['id'],$default) ) ) {
      		$field .= ' SELECTED';
      	}
      }else{ // a single size selectbox
      	if ( ((strlen($values[$i]['id']) > 0) && ($GLOBALS[$name] == $values[$i]['id'])) || ($default == $values[$i]['id']) ) {
      		$field .= ' SELECTED';
      	}
      }
      $field .= '>' . $values[$i]['text'] . '</option>';
    }
    $field .= '</select>';

    if ($required) $field .= TEXT_FIELD_REQUIRED;

    return $field;
  }
  

  /*
   * Draw formated Inputbutton
   * @param String $title The value and Title of Inputbutton
   * @param String $href The Link action, you can add http:// and javascript: Links
   * @param String $access The User access Permission for the linkaction
   * @param String $style You can add additional CSS Parameters
   * @param String $type The Input-Type of the Button. Default: button
   * @param String $css_class The css class of the Button. Default: iButton
   * @param String $target You can change the Linktarget, eg "_blank". Default: false
   */
  function yes_draw_href_button($title,$href,$access,$style='',$type='button',$css_class='iButton',$target=false){
  	global $YES_USER_PERMISSIONS;
	$style_string = ($style != '') ? ' style="'.$style.'" ' : '';
  	if(isset($YES_USER_PERMISSIONS[$access])){
  		if($YES_USER_PERMISSIONS[$access] == 1){
			if(!$target){
				if(!strstr($href,'javascript:')){
					if(strstr($css_class,'lbOn')){
						$css_class = 'iButton';
						$link_string = "onclick=\"lightbox_on_click(this,'".$href."');\"";
					}else{
						$link_string = ($type == 'button') ? 'onclick="window.location.href=\''.$href.'\';"' : '';
					}
				}else{
					$link_string = ($type == 'button') ? 'onclick="'.str_replace('javascript:','',$href) . '"': '';
				}
			}else{
				$link_string = ($type == 'button') ? 'onclick="window.open(\''.$href.'\');"' : '';
			}
  		}else{
  			$link_string = "onclick=\"".yes_draw_js_alert_href(MENU_ACCESS_NOT_ALLOWED)."\""; 
  		}
  	}else{
  		$link_string = "onclick=\"".yes_draw_js_alert_href(MENU_ACCESS_NOT_EXISTS)."\""; 
  	}
  	
	$button = sprintf('<input title="%s" value="%s" %s %s type="%s" class="%s" />',
			$title,$title,$link_string,$style_string, $type,$css_class);
  	return $button;
  }

  /**
   * Draw formated Link with Access-Control
   *
   * @param string $title
   * @param string $href
   * @param string $access
   * @param string $style
   * @param string $css_class
   * @param string $params
   * @return string
   */
  function yes_draw_href_text($title,$href,$access,$style='',$css_class='iLink',$params=false){
  	global $YES_USER_PERMISSIONS;
	$style_string = ($style != '') ? ' style="'.$style.'" ' : '';
	$params_string = ($params) ? $params : '';
  	if(isset($YES_USER_PERMISSIONS[$access])){
  		if($YES_USER_PERMISSIONS[$access] == 1){
			$link_string = 'href="'.$href.'"';
  		}else{
  			$link_string = "href=\"javascript:".yes_draw_js_alert_href(MENU_ACCESS_NOT_ALLOWED)."\""; 
  		}
  	}else{
  		$link_string = "href=\"javascript: ".yes_draw_js_alert_href(MENU_ACCESS_NOT_EXISTS)."\""; 
  	}
	$link = sprintf('<a title="%s" %s %s %s class="%s">%s</a>',
			$title,$params_string, $link_string,$style_string,$css_class,$title);
  	return $link;
  }
  
  /**
   * draws a href-image with access control
   *
   * @param string $image
   * @param string $title
   * @param string $href
   * @param string $access
   * @param string $css_class
   * @param string $params
   * @return string
   */
  function yes_draw_href_image($image,$title,$href,$access,$css_class='iLink',$params=false){
  	global $YES_USER_PERMISSIONS;
	$params_string = ($params) ? $params : '';
  	if(isset($YES_USER_PERMISSIONS[$access])){
  		if($YES_USER_PERMISSIONS[$access] == 1){
			$link_string = 'href="'.$href.'"';
  		}else{
  			$link_string = "href=\"javascript:".yes_draw_js_alert_href(MENU_ACCESS_NOT_ALLOWED)."\""; 
  		}
  	}else{
  		$link_string = "href=\"javascript: ".yes_draw_js_alert_href(MENU_ACCESS_NOT_EXISTS)."\""; 
  	}
	$link = sprintf('<a title="%s" %s class="%s"><img src="%s" border="0" /></a>',
			$title, $link_string,$css_class,$image);
  	return $link;
  }
  
  function yes_draw_href_image_confirm($js_text,$image,$title,$href,$access,$css_class='iLink',$params=false){
  	global $YES_USER_PERMISSIONS;
	$params_string = ($params) ? $params : '';
  	if(isset($YES_USER_PERMISSIONS[$access])){
  		if($YES_USER_PERMISSIONS[$access] == 1){
  			$link_string = "href=\"".yes_draw_js_confirm_href($js_text,$href)."\""; 
  		}else{
  			$link_string = "href=\"javascript:".yes_draw_js_alert_href(MENU_ACCESS_NOT_ALLOWED)."\""; 
  		}
  	}else{
  		$link_string = "href=\"javascript: ".yes_draw_js_alert_href(MENU_ACCESS_NOT_EXISTS)."\""; 
  	}
	$link = sprintf('<a title="%s" %s class="%s"><img src="%s" border="0" /></a>',
			$title, $link_string,$css_class,$image);
  	return $link;
  }
  
  function yes_db_error_handler($msg,$sql){

	die('<div style="width:80%;border: 2px solid #000000; font-family:arial,helvetica,sans-serif; font-size:14px; color:yellow;background-color:#FF0000;" align="center"><font color="#FFFFFF"><b>ERROR:</b></font> '.$msg.'<p align="left"><font style="font-size:12px;color:#FFFFFF;">'.$sql.'</font></p></div>');

  }

  function yes_draw_js_alert_href($message,$width=400,$height=200){
	return sprintf("javascript:void( show_yes_alertbox('%s',%s,%s) );",htmlentities($message),$width,$height);
  }
  
  function yes_draw_js_popup_href($link,$width=800,$height=600){
	return sprintf("javascript:void( window.open('%s','popup','width=%s,height=%s,scrollbars=yes,resizable=yes') );",$link,$width,$height);
  }
  
  function yes_draw_js_confirm_href($message,$link,$width=600,$height=400){
	return sprintf("javascript:void( show_yes_confirmbox('%s','%s',%s,%s) );",htmlentities($message),$link,$width,$height);
  }
  
  function yes_draw_quicknote($backlink = '',$popup_link = false){
	if( !strstr(yes_get_php_self(),FILENAME_NOTIZ) ){
		include(DIR_FS_CATALOG.'lang/'.$_SESSION['language'].'/admin/notiz.php'); 
		include(DIR_WS_CLASSES.'class.notiz.php');
	} 
	$hour_array = array();
	for($i=0;$i<24;$i++){
		$val = (strlen($i)==1)?'0'.$i:$i;
		$hour_array[] = array('id'=>$val,'text'=>$val);
	}
	$minute_array = array();
	for($i=0;$i<60;$i+=15){
		$val = (strlen($i)==1)?'0'.$i:$i;
		$minute_array[] = array('id'=>$val,'text'=>$val);
	}
	$notiz = new notiz(0);
	$note_smarty = new yesSmarty;
	$note_smarty->compile_dir = DIR_FS_CATALOG.'templates_c';
	if($backlink == '')
		$note_smarty->assign('BACKLINK',urlencode( yes_get_php_self().'?'.$_SERVER['QUERY_STRING']));
	else
		$note_smarty->assign('BACKLINK',urlencode($backlink));
		
	$note_smarty->assign('QUICKNOTE_TITLE',QUICKNOTE_TITLE);
	$note_smarty->assign('CLOSE_LINK',xtc_image('images/cal_close_small.gif',QUICKNOTEADD_CLOSE));
	$note_smarty->assign('NOTE_CAPT',NOTE_CAPT);
	$note_smarty->assign('NOTE_CAPT_INPUT',xtc_draw_input_field('capt',NOTE_QUICKNOTE_SUBJECT,'size="20" maxlength="100" id="capt"'));
	$note_smarty->assign('NOTE_NOTE',NOTE_NOTE);
	$note_smarty->assign('NOTE_NOTE_INPUT',xtc_draw_textarea_field('note','virtual','40','4', '','id="note"'));
	$note_smarty->assign('NOTE_EMPFAENGER',NOTE_EMPFAENGER);
	$note_smarty->assign('NOTE_EMPFAENGER_INPUT',$notiz->draw_recipients($_SESSION['customer_id']));
	$note_smarty->assign('NOTE_TERMIN',NOTE_TERMIN);
	$note_smarty->assign('NOTE_TERMIN_INPUT',xtc_draw_pull_down_menu('termin_hour',$hour_array,date('H'),'id="termin_hour"').'&nbsp;:&nbsp;'.xtc_draw_pull_down_menu('termin_minute',$minute_array,0,'id="termin_minute"'));
	$note_smarty->assign('BUTTON_NOTE_SAVE',BUTTON_NOTE_SAVE);
	$note_smarty->assign('NOTE_PFLICHTFELDER',NOTE_PFLICHTFELDER);
	
	return $note_smarty->fetch(DIR_FS_ADMIN.'templates/quicknote.tpl');
  }  
  