<?php

function getElementForSimpleNumber($target) {
	return  '<input type="text" name="simple_number" value="'.$target->getCallForwardNumber().'">';
}

function getElementForSimpleVoicemail($target) {
	return  '<select name="simple_voicemail" ><option>Ich bin gerade nicht erreichbar</option><option>Ausserhalb der Gesch&auml;ftszeiten</option></select>';
}

function printCallForwardListItemFromForward($callforward) {

		
	echo '<li id="listItem_'.$callforward->getId().'">';
	
	echo '<table><tr>';
	echo '<td width="30px"><img src="img/arrow.png" alt="move" width="16" height="16" class="handle" /></td>';	
	echo '<td width="100%" class="lrBorder">' . $callforward->getCriteriaDescription() . '<br />' . $callforward->getTargetDescription() . '</td>';
	echo '<td width="30px" style="text-align:center;">
			<button href="#"><img alt="Edit" src="img/action_edit.png"></button>
			<button href="#"><img alt="Remove" src="img/list-remove.png"></button>
		</td>';
	echo '</tr></table></li>';

}

function printDateTimeCriteriaHtml() {
	
	$wdays = array('mo'=>'Mon', 'tu'=>'Tue', 'we'=>'Wed', 'th'=>'Thu', 'fr'=>'Fri', 'sa'=>'Sat', 'su'=>'Sun');
	$wdaysl = array();
	foreach ($wdays as $col => $wdca)
        	$wdaysl[$col] = mb_subStr(strFTime('%a', strToTime('last '.$wdca)),0,1);
	unset($wdays);


        echo '<table cellspacing="0" class="tinytbl">',"\n";
        echo '', "\n";
        echo '<tr>', "\n";
        echo '<td>';
	  echo __("Wochentag und Zeit ist: ");
	echo '</td>';

	foreach ($wdaysl as $col => $v) {
                echo '<td>
			<label for="ipt-r_',$id,'_d_',$col,'">', $v, '</label>
			<input type="checkbox" name="r_',$id,'_d_',$col,'" id="ipt-r_',$id,'_d_',$col,'" value="1" checked="checked" /></td>';
        }
        echo '', "\n";
        echo '</td>', "\n";

	 echo '<td>&nbsp;&nbsp;';
	echo '</td>', "\n";
        
        echo '<td>';
        echo '<span class="nobr">';
        echo '<input type="text" name="r_',$id,'_h_from_h" value="00" size="2" maxlength="2" class="r" />:';
        echo '<input type="text" name="r_',$id,'_h_from_m" value="00" size="2" maxlength="2" class="r" /> -';
        echo '</span> ', "\n";
        echo '<span class="nobr">';
        echo '<input type="text" name="r_',$id,'_h_to_h" value="24" size="2" maxlength="2" class="r" />:';
        echo '<input type="text" name="r_',$id,'_h_to_m" value="00" size="2" maxlength="2" class="r" />';
        echo '</span>';
        echo '</td>', "\n";
	echo '</tr>', "\n";

	echo '</table>';
	
	
}

function printPhoneStatusCriteriaHtml() {
	echo __('Telefonstatus ist ');
	echo '<select name="criteria_phoneStatus" id="criteria_phoneStatus">"';
	echo '<option value="busy">Besetzt</option>';
	echo '<option value="noanswer">Keine Antwort</option>';
	echo '<option value="offline">Abgemeldet</option>';
	echo '</select>';
}

function printJabberStatusCriteriaHtml() {

	echo __('Pr&auml;senzstatus ist ');
        echo '<select name="criteria_jabberStatus" id="criteria_jabberStatus">"';
        echo '<option value="busy">Anwesend</option>';
        echo '<option value="noanswer">Abwesend</option>';
        echo '<option value="offline">Besch&auml;ftigt</option>';
        echo '<option value="offline">Abgemeldet</option>';
	echo '</select>';

}

function printAddressbookCriteriaHtml() {

	echo '<select name="is_or_is_not" id="criteria_Addrbook_is_or_not">"';
        echo '<option value="is">Anrufer ist im Adressbuch</option>';
        echo '<option value="is_not">Anrufer ist nicht im Adressbuch</option>';
        echo '</select>';


        echo '<select name="criteria_jabberStatus" id="criteria_jabberStatus">"';
        echo '<option value="busy">Pers&ouml;nliches Adressbuch</option>';
        echo '<option value="noanswer">Blacklist Adressbuch</option>';
        echo '<option value="offline">Kunden Adressbuch</option>';
	echo '<option value="offline">VIP Adressbuch</option>';
	echo '</select>';

}

function printCalendarCriteriaHtml() {

	echo '<select name="is_or_is_not" id="criteria_Addrbook_is_or_not">"';
        echo '<option value="is">Besch&auml;ftigt laut Kalender: </option>';
        echo '<option value="is_not">Nicht besch&auml;ftigt laut Kalender: </option>';
        echo '</select>';

        echo '<select name="criteria_jabberStatus" id="criteria_jabberStatus">"';
        echo '<option value="busy">Feiertage</option>';
        echo '<option value="noanswer">Privat</option>';
        echo '<option value="offline">Team-Urlaub</option>';
        echo '</select>';

}

function printEditForCallForwardListItemFromForward($callforward) {

	echo '<li id="edit_listItem_'.$callforward->getId().'">';
	echo '<table><tr>';
	echo '<td width="30px"><img src="img/arrow.png" alt="move" width="16" height="16" class="handle" /></td>';
	echo '<td width="100%" class="lrBorder"><form id="TEST" class="newForwardRule">';
	//Print criteria Selection
	//add button
	?>
	<script>
	$('#saveRule').click(function () {
		var order =  $("#TEST").serialize(); 
		alert(order);
		$.get('<?php echo ''.GS_URL_PATH.'forwardsforwards_forwards.php';?>' + '?saveRule=save&' + order, {}, function(data){
				alert(data);
				//$("#criteria-list").prepend(data).slideDown('slow');
			});
			return false; 			     
		 }
	    );
	
	$('#addCriteria').click(function () {
		//alert("Click");
		var selectedValue = $("#edit_criteriaTypes option:selected").val();
		$.get(<?php echo '"'.GS_URL_PATH.'forwardsforwards_forwards.php"';?>, { addCriteria: selectedValue }, function(data){
				//alert("Data Loaded: " + data);
				$("#criteria-list").prepend(data).slideDown('slow');

			});
			return false; 			     
		 }
	    );
    </script>
    
    <select id="edit_criteriaTypes">
    <option value="phoneStatus">Telefonstatus</option>
    <option value="jabberStatus">Pr&auml;senz</option>
    <option value="timeRule">Wochentag und Uhrzeit</option>
    <option value="calendar">Kalendereintrag</option>
    <option value="addressbook">Addressbucheintrag</option>
    </select>
    
    <a href="#" id="addCriteria"><img src="img/list-add.png" alt="Add"></a>
	<?php 

	
	//Criteria list:
	$criterias = $callforward->getForwardCriterias();
	echo '<ul id="criteria-list">';
	foreach($criterias as $criteria) {
		echo '<li>';
		echo $criteria->getCriteriaDescription();
		echo "</li>";
	}
	
	echo '</ul>';
	
	
	///Print Target Selection
	$target = $callforward->getCallForwardTarget();
	?>
	<script>
	$("#edit_redirectOptions").change(function () {
		var selectedValue = $("#edit_redirectOptions option:selected").val();
		$.get(<?php echo '"'.GS_URL_PATH.'forwardsforwards_forwards.php"';?>, { element_type: selectedValue }, function(data){
					//alert("Data Loaded: " + data);
					$("#edit_additionalRedirectOptions").html(data).fadeIn('fast');
			});
		});
	</script>
	<?php 


	echo '<select name="edit_redirectOptions" id="edit_redirectOptions">';

	if ($target->getCallForwardType() == CallForwardTargetType::Number) {
		echo '<option value="simple_number" selected="selected" >'. __("Rufnummer:").'</option>';
	} else {
		echo '<option value="simple_number" >'. __("Rufnummer:").'</option>';
	}

	if ($target->getCallForwardType() == CallForwardTargetType::Voicemail) {
		echo '<option value="simple_voicemail" selected="selected">'.__("AB mit Ansage:").'</option>';
	} else {
		echo '<option value="simple_voicemail" >'.__("AB mit Ansage:").'</option>';
	}
	echo "</select>";
	
	echo '<div id="edit_additionalRedirectOptions" style="display:inline">';
	if ($target->getCallForwardType() == CallForwardTargetType::Number) {
		echo getElementForSimpleNumber($target);
	} else if ($target->getCallForwardType() == CallForwardTargetType::Voicemail) {
		echo getElementForSimpleVoicemail($target);
	}
	echo '</div>';
	echo '<a href="#" id="saveRule"><img src="img/action_save.png" alt="Safe"></a>';
	echo '</form></td>';
	echo '<td width="30px"><button href="#"><img alt="Remove" src="img/list-remove.png"></button></td>';
	echo '</tr></table></li>';

}
