<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="xml/basic.css" />
<script>
	function stopUpload(){
		var ifr = document.getElementById('invisibleUpload');
		var innerDoc = ifr.contentDocument || ifr.contentWindow.document;
		document.getElementById('title').value = innerDoc.getElementById("titleResult").innerHTML;
		document.getElementById('artist').value = innerDoc.getElementById("artistResult").innerHTML;
		document.getElementById('codeval').value = innerDoc.getElementById("codeResult").innerHTML;
		document.getElementById('pInput2').style.display = 'none';
		document.getElementById('sub').disabled = false;		
		}
</script>
</head>
<body>

<?php

include 'globals.php';


function get_url_contents($url){
        $crl = curl_init();
        $timeout = 5;
        curl_setopt ($crl, CURLOPT_URL,$url);
        curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
        $ret = curl_exec($crl);
        curl_close($crl);
        return $ret;
}


$uid=$_COOKIE['TestCookie'];
if ($uid==''){
 	$usr='NoUser';
 	}
else{
	if ($_GET[override]<>'') {
		$uid=$_GET[override];
		}
	$page=get_url_contents("http://static.dthd.co.uk/songwars/ref.php?uid=$uid");

	$DOM = new DOMDocument;
	$DOM->loadHTML($page);
	$items = $DOM->getElementsByTagName('h1');
	foreach ($items as $item){
		$usr = $item->nodeValue;	
		}
	}

$usr = str_Replace(' ','_',$usr);

$weekdata = simplexml_load_file('xml/users.xml');	
//$findthemedata="//users/weeks/*";
//$themeresult=$weekdata->xpath($findweekdata);
$numberofweeks=$weekdata->weeks->numweeks;
$themes1 = array_pad(explode(',',$weekdata->weeks->themes1),intval($numberofweeks),0);
$themes2 = array_pad(explode(',',$weekdata->weeks->themes2),intval($numberofweeks),0);
$themes3 = array_pad(explode(',',$weekdata->weeks->themes3),intval($numberofweeks),0);
$themes4 = array_pad(explode(',',$weekdata->weeks->themes4),intval($numberofweeks),0);

//echo $themes1[0];

for ($iT = 1; $iT <= intval($numberofweeks); $iT++) {
	$themesArr[$iT] = $themes1[$iT-1] . ',' . $themes2[$iT-1] . ',' . $themes3[$iT-1] . ',' . $themes4[$iT-1];
	}

//$padded_themes=array_pad($themes,intval($numberofweeks),0);



echo("<div id=topbar><h1>SONG WARS III</h1></div>");
echo("<div id=links style='width:10%;float:left'>");



for ($i = 1; $i <= intval($numberofweeks); $i++) {
	$tmpval = $i-1;
	//$tmpval2 = "themes$i"	
	//$weekString = $weekdata->weeks->$tmpval2;	
	echo("<a href='input.php?week=$i&user=$usr&theme=$themesArr[$i]' id=test>Week $i</a><br/>");
	}




?>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
</div>


<div id=cuntent>
<?php
if ($_POST['title']<>''){
	if ($_POST['type']=='1'){
		$data = simplexml_load_file('xml/songs.xml');	
		$finddata="//data/week$_POST[week]/*";
		$dresult=$data->xpath($finddata);
		if (count($dresult)==0) {
			$newWeek = $data->addChild("week$_POST[week]");
			$newSong = $newWeek->addChild("$_POST[user]");
			$newSong->addChild("songtype","1");
			$newSong->addChild("string","$_POST[code]");
			$newSong->addChild("title","$_POST[title]");
			$newSong->addChild("artist","$_POST[artist]");
			$newSong->addChild("group","$_POST[group]");
			}
		else {
			$finddata = "//data/week$_POST[week]/$_POST[user]/*";
			$dresult = $data->xpath($finddata);
			$sweek = "week" . $_POST[week];
			$suser = $_POST[user];
			if (count($dresult)==0) {
				
				$newSong = $data->$sweek->addChild("$_POST[user]");
				$newSong->addChild("songtype","1");
				$newSong->addChild("string","$_POST[code]");
				$newSong->addChild("title","$_POST[title]");
				$newSong->addChild("artist","$_POST[artist]");
				$newSong->addChild("group","$_POST[group]");
				//echo "1";
				//echo $data;
				}
			else {
				//echo $sweek;
				//echo $suser;
				//echo $data->$week->$user->songtype;
				$findsongtoupdate = "//data/$sweek/$suser/*";
				
				$gotSong = $data->xpath($findsongtoupdate);
				//echo count($gotSong);
				//echo $gotSong[3];
				//echo "$_POST[artist]";
				$gotSong[0][0] = "$_POST[type]";
				$gotSong[1][0] = "$_POST[code]";
				$gotSong[2][0] = "$_POST[title]";
				$gotSong[3][0] = "$_POST[artist]";
				$gotSong[4][0] = "$_POST[group]";
				//echo $gotSong[3];
				//echo "2";
				//echo $gotsong;
			}
				
				
		}
		//Format XML to save indented tree rather than one line
		$dom = new DOMDocument('1.0');
		$dom->preserveWhiteSpace = false;
		$dom->formatOutput = true;
		$dom->loadXML($data->asXML());

		//Save XML to file - remove this and following line if save not desired
		$dom->save('xml/songs.xml');
		
	
		echo ("<div id=pInput>");
		echo ("Thanks for submitting your entry for week $_POST[week]<br/>Your choice is $_POST[title] by $_POST[artist]. If your song doesn't appear below, it is either not a valid youtube video or embedding is disabled.<br/><br/>");
			
			
		echo ("<objectwidth='425' height='350'>");
		echo ("<param name='movie' value=http://www.youtube.com/v/$_POST[code] />");
		echo ("<param name='wmode' value='transparent' />");
		echo ("<embed src=http://www.youtube.com/v/$_POST[code] type='application/x-shockwave-flash' wmode='transparent' width='425' height='350' /></object>");
		echo ("</div>");
		}
	else {
	
		$data = simplexml_load_file('xml/songs.xml');	
		$finddata="//data/week$_POST[week]/*";
		$dresult=$data->xpath($finddata);
		if (count($dresult)==0) {
			$newWeek = $data->addChild("week$_POST[week]");
			$newSong = $newWeek->addChild("$_POST[user]");
			$newSong->addChild("songtype","2");
			$newSong->addChild("string","$_POST[code]");
			$newSong->addChild("title","$_POST[title]");
			$newSong->addChild("artist","$_POST[artist]");
			$newSong->addChild("group","$_POST[group]");
			}
		else {
			$finddata = "//data/week$_POST[week]/$_POST[user]/*";
			$dresult = $data->xpath($finddata);
			$sweek = "week" . $_POST[week];
			$suser = $_POST[user];
			if (count($dresult)==0) {
				
				$newSong = $data->$sweek->addChild("$_POST[user]");
				$newSong->addChild("songtype","2");
				$newSong->addChild("string","$_POST[code]");
				$newSong->addChild("title","$_POST[title]");
				$newSong->addChild("artist","$_POST[artist]");
				$newSong->addChild("group","$_POST[group]");
				//echo "1";
				echo $data;
				}
			else {
				//echo $sweek;
				//echo $suser;
				//echo $data->$week->$user->songtype;
				$findsongtoupdate = "//data/$sweek/$suser/*";
				
				$gotSong = $data->xpath($findsongtoupdate);
				//echo count($gotSong);
				//echo $gotSong[3];
				//echo "$_POST[artist]";
				$gotSong[0][0] = "$_POST[type]";
				$gotSong[1][0] = "$_POST[code]";
				$gotSong[2][0] = "$_POST[title]";
				$gotSong[3][0] = "$_POST[artist]";
				$gotSong[4][0] = "$_POST[group]";
				//echo $gotSong[3];
				//echo "2";
				//echo $gotsong;
			}
				
				
		}
		//Format XML to save indented tree rather than one line
		$dom = new DOMDocument('1.0');
		$dom->preserveWhiteSpace = false;
		$dom->formatOutput = true;
		$dom->loadXML($data->asXML());

		//Save XML to file - remove this and following line if save not desired
		$dom->save('xml/songs.xml');
	
	
	
	
	
	
		echo ("<div id=pInput>");
		echo ("Thanks for submitting your entry for week $_POST[week]<br/>Your choice is $_POST[title] by $_POST[artist]. If your song doesn't appear below, something broke. Hard.<br/><br/>");
			echo("<div id='container'><a href='http://www.macromedia.com/go/getflashplayer'>Get the Flash Player</a> to see this player.</div>");
			echo("<script type='text/javascript' src='playa/swfobject.js'></script>");
			echo("<script type='text/javascript'>");
    			echo("var s1 = new SWFObject('player.swf','ply','328','200','9','#FFFFFF');");
    			echo("s1.addParam('allowfullscreen','true');");
    			echo("s1.addParam('allowscriptaccess','always');");
    			echo("s1.addParam('flashvars','file=http://www.beexcellenttoeachother.com/songwars/playa/mp3/$_POST[code]&image=http://beexcellenttoeachother.com/songwars/playa/preview.jpg');");
    			echo("s1.write('container');");
			echo("</script>");
			echo("</div>");
		}
	}

?>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>


<script>
jQuery.ajaxSetup({ 
            // Disable caching of AJAX responses  
            cache: false 
        }); 

$(function() {

		
		

	jQuery.support.cors = true;
	$("#test").live("click", function(){                       
  		$("#cuntent").load($(this).attr("href")); 
		$('a').each(function(){
		 	$(this).attr("style","color:inherit");  	
		 	});
  		$(this).attr("style","color:#FF9900");  
  		return false;     
  		});
  		
	$("#opt").live("change",function() { 
 		 if ($("#opt").val() == '1') {  		  	
  		 	$("#pInput2").hide(); 
			$("#urlInput1").attr("style", "display:inline;font-size:14px");
			$("#urlInput2").attr("style", "display:inline;font-size:14px");
			$("#urlInput3").attr("style", "display:inline");		 	
			$("#showPlayer").hide();
		 	$("#title").val('');
		 	$("#artist").val('');
		 	$("#codeval").val('');
			$("#sub").prop("disabled",false);		 		 	
   			} 
  		else  { 
   			$("#pInput2").show(); 
			$("#urlInput1").attr("style", "display:none");
			$("#urlInput2").attr("style", "display:none");
			$("#urlInput3").attr("style", "display:none");
   			$("#sub").prop("disabled", true);
   			$("#uPlayer").hide(); 
   			$("#title").val('');
		 	$("#artist").val('');
		 	$("#codeval").val('');					
   			}  
   		});    	

	//$('#invisibleUpload').live("ready",function() { 
  	//
   	//			$("#title").val(document.frames['invisibleUpload'].document.getElementById("artistResult").innerHTML);
   	//			$("#artist").val(document.frames['invisibleUpload'].document.getElementById("artistResult").innerHTML);	
	//			$("#codeval").val(document.frames['invisibleUpload'].document.getElementById("artistResult").innerHTML);
	//
	//			});

	$("#sub").live("click",function() {
			var $validator = 'valid'	;
			var $temp=$("#artist").val();
			$("#artist").val($temp.replace(/'/g, ''));
			$temp=$("#title").val();
			$("#title").val($temp.replace(/'/g, ''));
			var $temp=$("#artist").val();
			$("#artist").val($temp.replace(/"/g, ''));
			$temp=$("#title").val();
			$("#title").val($temp.replace(/"/g, ''));		
			if ($("#artist").val() == '') {
				$("#artist").attr("style","background-color: #FFCCCC");
				$validator = 'invalid';				
				}
			else {
				$("#artist").attr("style","background-color: #FFFFFF");
				}	
			if ($("#title").val() == '') {
				$("#title").attr("style","background-color: #FFCCCC");
				$validator = 'invalid';				
				}
			else {
				$("#title").attr("style","background-color: #FFFFFF");
				}
			if ($("#codeval").val() == '') {
				$("#codeval").attr("style","background-color: #FFCCCC");
				$validator = 'invalid';				
				}
			else {
				$("#codeval").attr("style","background-color: #FFFFFF");
				}	
			if ($validator == 'invalid') {
				return false;
				}
			});
		
			
		
	});
	

</script>
	
</body>
</html>