<?php

class Select_Apank_Form
{
	protected $ext = 'select';
	
	public function createForm($data, $name)
	{
		$input = "<select id='$name'";
		foreach($data as $key => $row){
			$input.= " $key='$row'";
		}
		
		$input.= " class='".$this->ext."' ></select> <div id='errorApank$name' class='errorApankform'></div>";
	
		return $input;
	}
	
	public function addOption($form, $param, $val)
	{
		$ex = explode(' <div ', $form);
		
		$form = str_ireplace('</select>', '', $ex[0]);
		$form.= "<option value='$param'>$val";
		$form.= '</select> <div '.$ex[1];
		return $form;
	}
	
	public function addSelected($form, $param)
	{
		$ex = explode(' <div ', $form);
	
		$i = explode('><', $ex[0]);
	
	
		if($i[1] == "option value=''"){
			$option = $i[2];
			$ops = "<option value=''>";
		}else{
			$option = $i[1];
			$ops = '';
		}
	
		$option = str_ireplace('</select>', '', $option);
		$option = str_ireplace("option value='", '', $option);
		$option = str_ireplace('<', ';', $option);
		$option = explode(";", $option);
		
		$form = $i[0].'>'.$ops;

		foreach($option as $row){
			$val = explode("'>", $row);
			$sel = '';
			
			if(trim($param) == trim($val[0])){
				$sel = 'selected';
			}
			
			$form.= "<option value='".$val[0]."' $sel>".$val[1];
		}
		
		$form.= '</select><div '.$ex[1];

		return $form;
	}
}