<?php
function getCurrentCityWeather($webservices_url, $request, $log=false) {
	$logfile = "";
	$ws = "";
	// Create the client instance
	$client = new nusoap_client($webservices_url, true);
	$client->setEndpoint($webservices_url);
	// Check for an error
	$err = $client->getError();
	if ($err) {
	    // Display the error
	    if($log) {
	    	$logfile .= 'Constructor error. ' . "\n";
	    	$logfile .= implode(" | ", $err) . "\n";
	    }
	    // At this point, you know the call that follows will fail
	}
	// Call the SOAP method
	$result = $client->call(
			'getRequestedServer', 
			array(
				'SERVER_NAME' 		=> $request['server_name'],
	    		'FUNCTION_NAME' 	=> $request['function_name'],
		    	'FUNCTION_PARAMS' 	=> $request['function_params']
			)
		);
	// Check for a fault
	if ($client->fault) {
	     if($log) {
	    	$logfile .= "Faulty result" . "\n";
	    	$logfile .= implode(" | ", $result) . "\n";
	     }
	} 
	else {
	    // Check for errors
	    $err = $client->getError();
	    if ($err) {
	        // Display the error
	        if($log) {
	        	$logfile .= is_array($err) ? implode(" | ", $err) . "\n" : $err . "\n";
	        }
	    } else {
	        $ws = unserialize(base64_decode($result));
	    }
	}
	
	if($log) {
        echo '<pre>$result = ' . (var_export($result, true)) . '</pre>';
		// Display the request and response
		echo '<h2>Request</h2>';
		echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
		echo '<h2>Response</h2>';
		echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
		// Display the debug messages
		echo '<h2>Debug</h2>';
		echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
		echo '<pre>' . (var_export(explode("|", $logfile), true)) . '</pre>';
		echo '<pre>' . (var_export($request, true)) . '</pre>';
	}
	
	if($logfile == "") return $ws;
	else return false;
}

function getCurrentLocations($webservices_url, $request, $log=false) {
	$logfile = "";
	$ws = "";
	// Create the client instance
	$client = new nusoap_client($webservices_url, true);
	// Check for an error
	$err = $client->getError();
	if ($err) {
	    // Display the error
	    if($log) {
	    	$logfile .= 'Constructor error. ' . "\n";
	    	$logfile .= implode(" | ", $err) . "\n";
	    }
	    // At this point, you know the call that follows will fail
	}
	// Call the SOAP method
	$result = $client->call(
			'getRequestedServer', 
			array(
				'SERVER_NAME' 		=> $request['server_name'],
	    		'FUNCTION_NAME' 	=> $request['function_name'],
		    	'FUNCTION_PARAMS' 	=> $request['function_params']
			)
		);
	// Check for a fault
	if ($client->fault) {
	     if($log) {
	    	$logfile .= "Faulty result" . "\n";
	    	$logfile .= implode(" | ", $result) . "\n";
	     }
	} 
	else {
	    // Check for errors
	    $err = $client->getError();
	    if ($err) {
	        // Display the error
	        if($log) {
	        	$logfile .= implode(" | ", $err) . "\n";
	        }
	    } else {
	        $ws = unserialize(base64_decode($result));
	    }
	}
	
	if($log) {
		// Display the request and response
		echo '<h2>Request</h2>';
		echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
		echo '<h2>Response</h2>';
		echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
		// Display the debug messages
		echo '<h2>Debug</h2>';
		echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
	}
	
	if($logfile == "") return $ws;
	else return false;
}

// artcateg_content_td artcateg_content_td_even

function convert_temp($temp, $to_scale="f", $from_scale="c"){
	if($from_scale == "c" && $to_scale == "f") {
		$conv = $temp * 1.8 + 32;
		if(is_float($conv)) {
			$ret = number_format($conv, 1, '.','');
		}
		else {
			$ret = number_format($conv, 0, '.','');
		}
	}
	
	if($from_scale == "f" && $to_scale == "c") {
		$conv = ($temp - 32) * 0.18;
		if(is_float($conv)) {
			$ret = number_format($conv, 1, '.','');
		}
		else {
			$ret = number_format($conv, 0, '.','');
		}
	}
	
	if($from_scale == $to_scale) $ret = $temp;
	
	return $ret;
}

function show_scale($current, $new="c") {
	if($new == "c") return "C";
	else return "F";
}

if(!function_exists('tx')) {
    function tx($str, $params = array()) {
        if(count($params) > 0) {
            array_unshift($params, $str);
            $str = call_user_func_array('sprintf', $params);
        }

        return $str;
    }
}