var songs;
var str;
var params;
var mode;
var full;
var auto_play, autoplay_str;
var player_window;
var player_params;

auto_play = 0;
full = "true";

function getSongStr(song_id){
	var url = "http://www.kimberleyrecords.co.uk/ajax/returnSongUrl.php?"+song_id;
	req = false;
    	// branch for native XMLHttpRequest object
    	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    		try {
			req = new XMLHttpRequest();
        	} catch(e) {
			req = false;
        	}
	// branch for IE/Windows ActiveX version
    	} else if(window.ActiveXObject) {
       	try {
        		req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        		try {
          			req = new ActiveXObject("Microsoft.XMLHTTP");
        		} catch(e) {
          			req = false;
        		}
		}
    	}
	if(req) {
		req.onreadystatechange = processSongReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function openNewPlayer(){
	player_window = window.open ("http://www.kimberleyrecords.co.uk/player_object.php?"+player_params,
"mywindow","menubar=0,resizable=0,width=350,height=250"); 
	document.getElementById('song_player').style.display="none";


}

function processSongReqChange(){
	if(req.status==200){
		if(req.responseText!=''){
			songs = req.responseText;
			mode="basic";
			if(songs.indexOf('|')>0){
				mode="advanced";	
			}

			str= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="260" height="50" id="play_v2" align="middle"><param name="allowScriptAccess" value="sameDomain" />';
			if(auto_play=='1'){
				autoplay_str = 'true';
				str+='<param name="autoplay" value="'+autoplay_str+'" />';
			} else {
				autoplay_str = 'false';
			}
			str+='<param name="full" value="'+full+'" /><param name="songs" value="'+songs+'" /><param name="movie" id="ie_player_param" value="/player_final.swf?songs='+songs+'&mode='+mode+'&autoplay='+autoplay_str+'&full='+full+'" /><param name="quality" value="high" /><embed src="/player_final.swf" quality="high" width="260" height="50" name="play_v2" align="middle" allowScriptAccess="sameDomain"  flashvars="songs='+songs+'&mode='+mode+'&autoplay='+autoplay_str+'&full='+full+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object><br/>';
			if(!player_window){
				str+='<!--a href="#" onClick="openNewPlayer()">Open Player in New Window</a-->';	
			}
			if(player_window){
				document.getElementById('song_player').style.display="none";
				player_window.document.getElementById('p_o').innerHTML = str;
				player_window.focus();
			} else {
				//str+='<a href="#" onClick="openNewPlayer();">+</a>';
				document.getElementById('song_player').style.display="block";
				document.getElementById('song_player').innerHTML = str;
			}
		}
	}
}
	
function reloadPlayer(song_id, ap){
	auto_play = ap;
	player_params = song_id;
	getSongStr(song_id);
}

function submitLoginForm(){
	document.forms.login_form.submit();
}

function changeLanguage(){
	var action = document.getElementById('action').value;
	var lang = document.getElementById('language_chooser').options[document.getElementById('language_chooser').selectedIndex].value;
	window.location= "?action="+action+"&lang="+lang;
}

function submitContactUsForm(){
	var email = document.email_us.email_address.value;
	var retype_email = document.email_us.retype_email_address.value;
	var subject = document.email_us.email_subject.value;
	var code = document.email_us.txtNumber.value;
	var message = document.email_us.email_message.value;
	
	
	
	if(email==''){
		alert('You must enter an email address.');
		return false;
	}

	if(retype_email==''){
		alert('You must retype your email address.');
		return false;
	}	
	if(email!=retype_email){
		alert('Your email addresses do not match.');
		return false;
	}
	if(code==''){
		alert('Your must enter the verification code.');
		return false;
	}
	if(subject==''){
		alert('You must enter a subject.');
		return false;
	}	

	if(message==''){
		alert('You must enter a message.');
		return false;
	}			
	document.email_us.submit();
	//return true;
}

function togglebyid(id){
	menu=document.getElementById(id);
	if (menu==null){
		alert('Cannot find menu '+id);
	}
	arrow=document.getElementById('t_'+id);
	if (arrow==null){
		alert('Cannot find arrow t_'+id);
	}
	if (menu.style.display=='block'){
		menu.style.display='none';
		arrow.src='/images/plus.gif';
	} else {
		menu.style.display='block';
		arrow.src='/images/minus.gif';
	}
}

var xmlDoc;

function addItemToBasket(demo_id, basket_id){
	if(basket_id==''){

	}
	var url = 'http://www.kimberleyrecords.co.uk/ajax/addPackage2Basket.php?package_id='+demo_id+'&basket_id='+basket_id;
	if (window.XMLHttpRequest){
		req = new XMLHttpRequest();
		req.onreadystatechange = processShoppingBasketXml;
		req.open("GET", url, true);
	      req.send(null);
 			try {
				xmlDoc=document.implementation.createDocument("ns","root",null);
				xmlDoc.onload.init;
	        	} catch(e) {
				xmlDoc= false;
        		}
                   // branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		try{
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 
		} catch(e){
			xmlDoc = false;
		}
            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
			req.onreadystatechange = processShoppingBasketXml;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processShoppingBasketXml(){
	if (req.readyState == 4) {
		if(req.status==200){
if(req.responseText!=''){
//alert(req.responseText);
var items = req.responseText.split("\n");
var no_of_items = items.length;
var html;
html = '';
for(i=0; i<no_of_items; i++){
  var item = items[i];
if((item!='')&&(item!='undefined')){
var item_split = item.split(',');
var artist = item_split[0];
var package = item_split[1];
var amount = item_split[2];

//alert(item);
html+="<tr><td>"+artist+" - "+package+"</td><td>"+amount+"</td></tr>";
}
}
alert(html);
document.getElementById('shopping_basket_body').InnerHTML=html;
}
/**			if(req.responseXML!=''){
				//xmlDoc.load(req.responseXML.xml);  //or 
				xmlDoc = req.responseXML;
				var doc = xmlDoc.documentElement;
				var total = doc.attributes.total_amount.value;
				var str;
				var item_nodes = doc.childNodes;
				var no_of_items = item_nodes.length;
				var item_node;
				var i;
				var artist_name;
				var package_name;
				var amount;
				for(i=0;i<no_of_items;i++){
					item_node = item_nodes.item(i);
					alert(item_node);
					if(item_node!='undefined'){
					artist_name = item_node.childNodes.item(0).firstChild.data;
					package_name = item_node.childNodes.item(1).firstChild.data;
					amount = item_node.childNodes.item(2).firstChild.data;

					str += "<tr><td>"+artist_name+" - "+package_name+"</td><td>£"+amount+"</td><td><button type=\"button\">x</button></td></tr>";
					}
				}
				str += "<tr><th colspan=\"2\" align=\"right\">£"+total+"</th></tr>";
				document.getElementById('shopping_basket').innerHTML = str;
			} else {
				alert('no xml');
			}*/
		}
	}
}