<?php
function parseTemplate($soapclient,$fcontents) 
{
	global $pathToScript,$sefriendly;
	# If Products are included using a tag pair, parse them and don't parse any other product tags
	if(preg_match("/<aw:products>/",$fcontents)) 
	{
		$productRows ="";
		$html="";
		$producthtml = split("<aw:products>",$fcontents);
		$realhtml = split("</aw:products>",$producthtml[1]);
		if(preg_match("/<aw:products:columns>/",$fcontents) and preg_match("/<\/aw:products:columns>/",$fcontents))
		{
			$colhtml = split("<aw:products:columns>",$fcontents);
			$columnhtml = split("</aw:products:columns>",$colhtml[1]);
			if(is_numeric($columnhtml[0])) $numCols = $columnhtml[0];
			else $numCols =1;
		}
		for($i=0; $i<sizeof($soapclient->return['soapVal']['products'][$i]); $i++) 
		{		
			if(sizeof($soapclient->return['soapVal']['products'][0][0])==0)
			{
				$fcontents = preg_replace("/<aw:products>.*<\/aw:products>/s","No products in this category",$fcontents);
			}
			for($ii=0; $ii<sizeof($soapclient->return['soapVal']['products'][$i][$ii]); $ii++) 
			{
				$html = $realhtml[0];
				if(isset($numCols)) 
				{
					if($ii==0 or is_int($ii/$numCols)) $html ="<tr>".$html;
				}
				$html = preg_replace("/<aw:products:columns>.*<\/aw:products:columns>/s",'',$html);
				$html = str_replace("<aw:brand />",$soapclient->return['soapVal']['products'][$i][$ii]['brand'],$html);
				$html = str_replace("<aw:image />","$pathToScript?action=image&image=".$soapclient->return['soapVal']['products'][$i][$ii]['thnImage'],$html);
				$html = str_replace("<aw:deeplink />",$soapclient->return['soapVal']['products'][$i][$ii]['deepLink'],$html);
				$html = preg_replace("/<aw:deeplink>(.*)<\/aw:deeplink>/","<a href=\"".$soapclient->return['soapVal']['products'][$i][$ii]['deepLink']."\" onMouseOver=\"window.status='<aw:deeplink:status />';return true\" onMouseOut=\"window.status='';return false\" target=\"_blank\">$1</a>",$html);
				$statusParts = explode("&p=",$soapclient->return['soapVal']['products'][$i][$ii]['deepLink']);
				$status = $statusParts[1];
				$html = str_replace("<aw:deeplink:status />",$status,$html);
				$html = str_replace("<aw:promotionaltext />",$soapclient->return['soapVal']['products'][$i][$ii]['promotionalText'],$html);
				$html = str_replace("<aw:merchantprogram />",$soapclient->return['soapVal']['products'][$i][$ii]['programName'],$html);
				$html = str_replace("<aw:merchantid />",$soapclient->return['soapVal']['products'][$i][$ii]['merchantID'],$html);
				$html = str_replace("<aw:category />",$soapclient->return['soapVal']['products'][$i][$ii]['masterCategory'],$html);
				$html = str_replace("<aw:language />",$soapclient->return['soapVal']['products'][$i][$ii]['language'],$html);
				$html = str_replace("<aw:delivery />",$soapclient->return['soapVal']['products'][$i][$ii]['delivery'],$html);
				$html = str_replace("<aw:description />",$soapclient->return['soapVal']['products'][$i][$ii]['description'],$html);				$html = str_replace("<aw:description />",$soapclient->return['soapVal']['products'][$i][$ii]['description '],$html);
				$html = str_replace("<aw:price />",$soapclient->return['soapVal']['products'][$i][$ii]['displayPrice'],$html);
				$html = str_replace("<aw:name />",$soapclient->return['soapVal']['products'][$i][$ii]['name'],$html);
				if(isset($numCols)) 
				{
					$x = $ii+1;
					if($ii==sizeof($soapclient->return['soapVal']['products'][0])-1 or is_int($x/$numCols)) $html.="</tr>\n";
				}
				$productRows .= $html;
			}
		}
		$fcontents = preg_replace("/<aw:products>.*<\/aw:products>/s",$productRows,$fcontents);
		unset($numCols);
	}
	elseif(preg_match("/<aw:products \/>/",$fcontents)) 
	{
		$productRows ="";
		$html="";
		for($i=0; $i<sizeof($soapclient->return['soapVal']['products'][$i]); $i++) 
		{
			if(sizeof($soapclient->return['soapVal']['products'][0][0])==0)
			{
				$fcontents = str_replace("<aw:products />","No products in this category",$fcontents);
			}
			else 
			{
				for($ii=0; $ii<sizeof($soapclient->return['soapVal']['products'][$i]); $ii++) 
				{
					$productRows .= "<tr>\n";
					$productRows .= "<td valign=\"top\"><img src=\"$pathToScript?action=image&image=".$soapclient->return['soapVal']['products'][$i][$ii]['thnImage']."\"></td>";
					$productRows .= "\t<td valign=\"top\"><a href=\"".$soapclient->return['soapVal']['products'][$i][$ii]['deepLink']."\">".$soapclient->return['soapVal']['products'][$i][$ii]['name']."</a><p>".$soapclient->return['soapVal']['products'][$i][$ii]['promotionalText']."</td>";
					$productRows .= "\t<td>";
					$productRows .= "\n</tr>\n";
					$productRows .= "<tr>\n";
					$productRows .= "\t<td>&nbsp;</td>";
					$productRows .= "\t<td><b>".$soapclient->return['soapVal']['products'][$i][$ii]['displayPrice']."</b><p></td>";
				}	
			}
		}
		$fcontents = str_replace("<aw:products />",$productRows,$fcontents);
	}
	if(preg_match("/<aw:matchingcats>/",$fcontents) and preg_match("/<\/aw:matchingcats>/",$fcontents)) 
	{
		
		$html="";
		$producthtml = split("<aw:matchingcats>",$fcontents);
		$realhtml = split("</aw:matchingcats>",$producthtml[1]);
		$rows = "";
		$i=0;
		if(preg_match("/<aw:matchingcats:columns>/",$fcontents) and preg_match("/<\/aw:matchingcats:columns>/",$fcontents))
		{
			$colhtml = split("<aw:matchingcats:columns>",$fcontents);
			$columnhtml = split("</aw:matchingcats:columns>",$colhtml[1]);
			if(is_numeric($columnhtml[0])) $numCols = $columnhtml[0];
			else $numCols =1;
		}
		for($ii=0; $ii<sizeof($soapclient->return['soapVal']['matchingCats'][0]); $ii++) 
		{	
			$html = $realhtml[0];
			if(isset($numCols)) 
			{
				if($ii==0 or is_int($ii/$numCols)) $html ="<tr>".$html;
			}
			$html = preg_replace("/<aw:matchingcats:columns>.*<\/aw:matchingcats:columns>/s",'',$html);
			if($sefriendly == 1) 
			{
				$html = str_replace("<aw:matchingcats:link />","$pathToScript/action/awBrowseProducts/catid/".$soapclient->return['soapVal']['matchingCats'][0][$ii]['id'],$html);			
			}
			else 
			{
				$html = str_replace("<aw:matchingcats:link />","$pathToScript?action=awBrowseProducts&catid=".$soapclient->return['soapVal']['matchingCats'][0][$ii]['id'],$html);
			}

			$html = str_replace("<aw:matchingcats:name />",$soapclient->return['soapVal']['matchingCats'][0][$ii]['name'],$html);
			$html = str_replace("<aw:matchingcats:count />",$soapclient->return['soapVal']['matchingCats'][0][$ii]['count'],$html);
			if(isset($numCols)) 
			{
				$x = $ii+1;
				if($ii==sizeof($soapclient->return['soapVal']['matchingCats'][0])-1 or is_int($x/$numCols)) $html.="</tr>\n";
			}
			$rows .= $html;	
		}
		$fcontents = preg_replace("/<aw:matchingcats>.*<\/aw:matchingcats>/s",$rows,$fcontents);
		unset($numCols);
	}
	if(preg_match("/<aw:breadcrumb \/>/",$fcontents)) 
	{
		for($i=0; $i<sizeof($soapclient->return['soapVal']['breadCrumb']); $i++) 
		{
			$levels = count($soapclient->return['soapVal']['breadCrumb'][0]);
			for($ii=0; $ii<sizeof($soapclient->return['soapVal']['breadCrumb'][$i]); $ii++) 
			{
				if($sefriendly == 1) 
				{
					$breadCrumb .= "<a href=\"$pathToScript/action/awBrowseProducts/catid/".$soapclient->return['soapVal']['breadCrumb'][$i][$ii]['catID']."\">".$soapclient->return['soapVal']['breadCrumb'][$i][$ii]['catName']."</a>";
				}
				else 
				{
					$breadCrumb .= "<a href=\"$pathToScript?action=awBrowseProducts&catid=".$soapclient->return['soapVal']['breadCrumb'][$i][$ii]['catID']."\">".$soapclient->return['soapVal']['breadCrumb'][$i][$ii]['catName']."</a>";
				}
				if($levels-1 > $ii) $breadCrumb .=$GLOBALS["breadCrumbSeparator"];		
			}
			$fcontents = str_replace("<aw:breadcrumb />",$breadCrumb,$fcontents);
		}
	}
	if(preg_match("/<aw:category \/>/",$fcontents))
	{
			if($sefriendly == 1) 
			{
				$fcontents = str_replace("<aw:category />","<a href=\"$pathToScript/action/awBrowseProducts/catid/".$GLOBALS['catid']."\">".$soapclient->return['soapVal']['catName']."</a>",$fcontents);
			}
			else 
			{
				$fcontents = str_replace("<aw:category />","<a href=\"$pathToScript?action=awBrowseProducts&catid=".$GLOBALS['catid']."\">".$soapclient->return['soapVal']['catName']."</a>",$fcontents);
			}
	}
	if(preg_match("/<aw:category:name \/>/",$fcontents)) $fcontents = str_replace("<aw:category:name />",$soapclient->return['soapVal']['catName'],$fcontents);
	if(preg_match("/<aw:category:link \/>/",$fcontents)) 
	{
		if($sefriendly == 1) $fcontents = str_replace("<aw:category:link />","$pathToScript/action/awBrowseProducts/catid/".$GLOBALS['catid'],$fcontents);
		else $fcontents = str_replace("<aw:category:link />","$pathToScript?action=awBrowseProducts&catid=".$GLOBALS['catid'],$fcontents);
	}

	if(preg_match("/<aw:categorynavigator \/>/",$fcontents))
	{
		if($sefriendly == 1) 
		{
			$fcontents = str_replace("<aw:category />","<a href=\"$pathToScript/action/awBrowseProducts/catid/".$GLOBALS['catid']."\">".$soapclient->return['soapVal']['catName']."</a>",$fcontents);
		}
		else 
		{
			$fcontents = str_replace("<aw:category />","<a href=\"$pathToScript?action=awBrowseProducts&catid=".$GLOBALS['catid']."\">".$soapclient->return['soapVal']['catName']."</a>",$fcontents);
		}
	}

	if(preg_match("/<aw:searchform:keywordsearch \/>/",$fcontents)) $fcontents = str_replace("<aw:searchform:keywordsearch />",$GLOBALS['keywords'],$fcontents);
	if(preg_match("/<aw:searchform:minprice \/>/",$fcontents)) $fcontents = str_replace("<aw:searchform:minprice />",$GLOBALS['minprice'],$fcontents);
	if(preg_match("/<aw:searchform:maxprice \/>/",$fcontents)) $fcontents = str_replace("<aw:searchform:maxprice />",$GLOBALS['maxprice'],$fcontents);
	if(preg_match("/<aw:searchform:orderbylist \/>/",$fcontents))
	{
		$allowedOrderByArray = array("Price"=>"searchPrice","Product Name"=>"name","Category"=>"masterCategoryID","Brand"=>"brand","Merchant"=>"merchant_id");	
		while(list($val,$key)=each($allowedOrderByArray)) 
		{
			$selectList .="<option value=\"$key\"";
			if($key==$GLOBALS['orderby'])$selectList .=" selected";
			$selectList .=">$val</option>";
		}
		$fcontents = str_replace("<aw:searchform:orderbylist />",$selectList,$fcontents);
	}
	if(preg_match("/<aw:searchform:limitlist \/>/",$fcontents))
	{
		# NB. SOAP server will return max 200 records
		$limitArray = array(10,20,30,50,100);
		for($ii=0; $ii<count($limitArray); $ii++) 
		{
			$limitList .="<option value='$limitArray[$ii]'";
			if($limitArray[$ii]==$GLOBALS['limit']) $limitList .=" selected";
			$limitList .=">$limitArray[$ii]</option>";
		}
		$fcontents = str_replace("<aw:searchform:limitlist />",$limitList,$fcontents);
	}
	if(preg_match("/<aw:searchform:catlist \/>/",$fcontents)) 
	{
		$selectList = "";
		for($i=0; $i<sizeof($soapclient->return['soapVal']['topLevelCats']); $i++) 
		{
			$levels = count($soapclient->return['soapVal']['topLevelCats'][0]);
			for($ii=0; $ii<sizeof($soapclient->return['soapVal']['topLevelCats'][$i]); $ii++) 
			{
				$selectList .= "<option value='".$soapclient->return['soapVal']['topLevelCats'][$i][$ii]['catID']."'";
				if($GLOBALS['catid']==$soapclient->return['soapVal']['topLevelCats'][$i][$ii]['catID']) 
				{
					$selectList .= " selected";
				}
				
				$selectList .= ">".$soapclient->return['soapVal']['topLevelCats'][$i][$ii]['catName']."</option>";		
			}
			$fcontents = str_replace("<aw:searchform:catlist />",$selectList,$fcontents);
		}
	}
	if(preg_match("<aw:toplevelcats \/>",$fcontents)) 
	{
		for($i=0; $i<sizeof($soapclient->return['soapVal']['topLevelCats']); $i++) 
		{
			$levels = count($soapclient->return['soapVal']['topLevelCats'][0]);
			for($ii=0; $ii<sizeof($soapclient->return['soapVal']['topLevelCats'][$i]); $ii++) 
			{
				if($sefriendly == 1) 
				{			
					$selectList.= "<a href=\"$pathToScript/action/awBrowseProducts/catid/".$soapclient->return['soapVal']['topLevelCats'][$i][$ii]['catID']."\">".$soapclient->return['soapVal']['topLevelCats'][$i][$ii]['catName']."</a><br>";
				}
				else 
				{
					$selectList.= "<a href=\"$pathToScript?action=awBrowseProducts&catid=".$soapclient->return['soapVal']['topLevelCats'][$i][$ii]['catID']."\">".$soapclient->return['soapVal']['topLevelCats'][$i][$ii]['catName']."</a><br>";	
				}
			}
			$fcontents = str_replace("<aw:toplevelcats />",$selectList,$fcontents);
		}
	}
	if(preg_match("/<aw:toplevelcats>/",$fcontents) and preg_match("/<\/aw:toplevelcats>/",$fcontents)) 
	{
		$html="";
		$producthtml = split("<aw:toplevelcats>",$fcontents);
		$realhtml = split("</aw:toplevelcats>",$producthtml[1]);
		$rows = "";
		$i=0;
		if(preg_match("/<aw:toplevelcats:columns>/",$fcontents) and preg_match("/<\/aw:toplevelcats:columns>/",$fcontents))
		{
			$colhtml = split("<aw:toplevelcats:columns>",$fcontents);
			$columnhtml = split("</aw:toplevelcats:columns>",$colhtml[1]);
			if(is_numeric($columnhtml[0])) $numCols = $columnhtml[0];
			else $numCols =1;
		}

		for($ii=0; $ii<sizeof($soapclient->return['soapVal']['topLevelCats'][0]); $ii++) 
		{
			$html = $realhtml[0];	
			if(isset($numCols)) 
			{
				if($ii==0 or is_int($ii/$numCols)) $html ="<tr>".$html;
			}
			$html = preg_replace("/<aw:toplevelcats:columns>.*<\/aw:toplevelcats:columns>/s",'',$html);
			if($sefriendly == 1) 
			{
				$html = str_replace("<aw:toplevelcats:link />","$pathToScript/action/awBrowseProducts/catid/".$soapclient->return['soapVal']['topLevelCats'][0][$ii]['catID'],$html);
			}
			else 
			{
				$html = str_replace("<aw:toplevelcats:link />","$pathToScript?action=awBrowseProducts&catid=".$soapclient->return['soapVal']['topLevelCats'][0][$ii]['catID'],$html);				
			}
			
			$html = str_replace("<aw:toplevelcats:name />",$soapclient->return['soapVal']['topLevelCats'][0][$ii]['catName'],$html);
			$html = str_replace("<aw:toplevelcats:id />",$soapclient->return['soapVal']['topLevelCats'][0][$ii]['catID'],$html);
			$html = str_replace("<aw:toplevelcats:count />",$soapclient->return['soapVal']['topLevelCats'][0][$ii]['count'],$html);
			if(isset($numCols)) 
			{
				$x = $ii+1;
				if($ii==sizeof($soapclient->return['soapVal']['topLevelCats'][0])-1 or is_int($x/$numCols)) $html.="</tr>\n";
			}
			$rows .= $html;
		}
		$fcontents = preg_replace("/<aw:toplevelcats>.*<\/aw:toplevelcats>/s",$rows,$fcontents);
		unset($numCols);
	}

	if(preg_match("/<aw:searchform:merchantlist \/>/",$fcontents)) 
	{
		$selectList = "";
		for($i=0; $i<sizeof($soapclient->return['soapVal']['merchants']); $i++) 
		{
			$levels = count($soapclient->return['soapVal']['merchants'][0]);
			for($ii=0; $ii<sizeof($soapclient->return['soapVal']['merchants'][$i]); $ii++) 
			{
				$selectList .= "<option value='".$soapclient->return['soapVal']['merchants'][$i][$ii]['merchantID']."'";
				if($GLOBALS['mid']==$soapclient->return['soapVal']['merchants'][$i][$ii]['merchantID']) 
				{
					$selectList .= " selected";
				}
				
				$selectList .= ">".$soapclient->return['soapVal']['merchants'][$i][$ii]['programName']."</option>";		
			}
			$fcontents = str_replace("<aw:searchform:merchantlist />",$selectList,$fcontents);
		}
	}

	if(preg_match("/<aw:subcats \/>/",$fcontents)) 
	{
		$productRows ="";
		$html="";
		$productrow ="";

		for($i=0; $i<sizeof($soapclient->return['soapVal']['subCats']); $i++) 
		{
			for($ii=0; $ii<sizeof($soapclient->return['soapVal']['subCats'][$i]); $ii++) 
			{
				if($soapclient->return['soapVal']['subCats'][$i][$ii]['count'] >0)
				{
					$numincat = "(".$soapclient->return['soapVal']['subCats'][$i][$ii]['count'].")";
				}
				else 
				{
					$numincat = "";
				}
				if($sefriendly == 1) 
				{
					$productrow = "<br><a href=\"$pathToScript/action/awBrowseProducts/catid/".$soapclient->return['soapVal']['subCats'][$i][$ii]['catID']."\">".$soapclient->return['soapVal']['subCats'][$i][$ii]['catName']."</a> ".$numincat;
				}
				else 
				{
					$productrow = "<br><a href=\"$pathToScript?action=awBrowseProducts&catid=".$soapclient->return['soapVal']['subCats'][$i][$ii]['catID']."\">".$soapclient->return['soapVal']['subCats'][$i][$ii]['catName']."</a> ".$numincat;					
				}
				$productRows .= $productrow;
			}
		}
		$fcontents = str_replace("<aw:subcats />",$productRows,$fcontents);
	}

	if(preg_match("/<aw:subcats>/",$fcontents) and preg_match("/<\/aw:subcats>/",$fcontents)) 
	{
		$html="";
		$producthtml = split("<aw:subcats>",$fcontents);
		$realhtml = split("</aw:subcats>",$producthtml[1]);
		$rows = "";
		$i=0;
		if(preg_match("/<aw:subcats:columns>/",$fcontents) and preg_match("/<\/aw:subcats:columns>/",$fcontents))
		{
			$colhtml = split("<aw:subcats:columns>",$fcontents);
			$columnhtml = split("</aw:subcats:columns>",$colhtml[1]);
			if(is_numeric($columnhtml[0])) $numCols = $columnhtml[0];
			else $numCols =1;
		}
		for($ii=0; $ii<sizeof($soapclient->return['soapVal']['subCats'][0]); $ii++) 
		{	
			$html = $realhtml[0];
			if(isset($numCols)) 
			{
				if($ii==0 or is_int($ii/$numCols)) $html ="<tr>".$html;
			}
			$html = preg_replace("/<aw:subcats:columns>.*<\/aw:subcats:columns>/s",'',$html);
			if($sefriendly == 1) 
			{
				$html = str_replace("<aw:subcats:link />","$pathToScript/action/awBrowseProducts/catid/".$soapclient->return['soapVal']['subCats'][0][$ii]['catID'],$html);
			}
			else 
			{
				$html = str_replace("<aw:subcats:link />","$pathToScript?action=awBrowseProducts&catid=".$soapclient->return['soapVal']['subCats'][0][$ii]['catID'],$html);	
			}
			$html = str_replace("<aw:subcats:name />",$soapclient->return['soapVal']['subCats'][0][$ii]['catName'],$html);
			$html = str_replace("<aw:subcats:id />",$soapclient->return['soapVal']['subCats'][0][$ii]['catID'],$html);
			$html = str_replace("<aw:subcats:count />",$soapclient->return['soapVal']['subCats'][0][$ii]['count'],$html);
			if(isset($numCols)) 
			{
				$x = $ii+1;
				if($ii==sizeof($soapclient->return['soapVal']['subCats'][0])-1 or is_int($x/$numCols)) $html.="</tr>\n";
			}
			$rows .= $html;
		}
		$fcontents = preg_replace("/<aw:subcats>.*<\/aw:subcats>/s",$rows,$fcontents);
		unset($numCols);
	}

	if(preg_match("/<aw:resultstitle \/>/",$fcontents)) $fcontents = str_replace("<aw:resultstitle />",$soapclient->return['soapVal']['paginater']['displaytitle'],$fcontents);
	if(preg_match("/<aw:scriptname \/>/",$fcontents))$fcontents = str_replace("<aw:scriptname />",$pathToScript,$fcontents);
	if(preg_match("/<aw:previouslink \/>/",$fcontents)) $fcontents = str_replace("<aw:previouslink />",$soapclient->return['soapVal']['paginater']['previouslink'],$fcontents);
	if(preg_match("/<aw:nextlink \/>/",$fcontents)) $fcontents = str_replace("<aw:nextlink />",$soapclient->return['soapVal']['paginater']['nextlink'],$fcontents);
	if(preg_match("/<aw:previoushref \/>/",$fcontents)) $fcontents = str_replace("<aw:previoushref />",$soapclient->return['soapVal']['paginater']['previoushref'],$fcontents);
	if(preg_match("/<aw:nexthref \/>/",$fcontents)) $fcontents = str_replace("<aw:nexthref />",$soapclient->return['soapVal']['paginater']['nexthref'],$fcontents);
	if(preg_match("/<aw:numrecords \/>/",$fcontents)) $fcontents = str_replace("<aw:numrecords />",$soapclient->return['soapVal']['paginater']['reccount'],$fcontents);

	Return $fcontents;
}



function readOutputFile($outputfile) 
{
	global $templateDir;
	$outputfile = $templateDir."/$outputfile";
	$fd = @fopen($outputfile,'r');
	if(!$fd) 
	{
		echo("Unable to open $outputfile for parsing. Please check location");
		exit;
	}
	$fcontents = fread($fd,filesize($outputfile));
	fclose($fd);
	Return $fcontents;
}

function outputContents($fcontents,$outputType)
{
	if($outputType=="javascript") 
	{
		enjavanate($fcontents);
	}
	else 
	{
		echo($fcontents);
	}
	
}

function enjavanate($str)
{
	$lines = explode("\n", $str);
	
	reset ($lines);
	$i = 0;


	while (list(,$line) = each($lines))
	{
        $line = str_replace("\r", "", $line);
        $line = str_replace("'", "\\'", $line);
        
    	if (!empty($line))
    	{
            if ($i++)
                print "document.writeln('')\n";
            print "document.write('$line')\n";
        }
	}
}
