﻿function fixDotNetInit() {
    document.getElementById('__VIEWSTATE').value = ' ';
    document.getElementById('aspnetForm').encoding = 'application/x-www-form-urlencoded';
    document.getElementById('aspnetForm').action = 'http://www.fsb.org.uk/forms/cmsresponder_initforyou.asp';
}

function fixDotNet() {
    document.getElementById('__VIEWSTATE').value = ' ';
    document.getElementById('aspnetForm').encoding = 'application/x-www-form-urlencoded';
    document.getElementById('aspnetForm').action = 'http://www.fsb.org.uk/forms/cmsresponder.asp';
}

function fixDotNetGoogle() {
    document.getElementById('__VIEWSTATE').value = ' ';
    document.getElementById('aspnetForm').encoding = 'application/x-www-form-urlencoded';
    document.getElementById('aspnetForm').action = 'http:/maps.google.co.uk/maps';
}


// clear out aspnet form - change the action to my (ps) action.... this is for web forms
// so that they perform as normal forms
function formControl(theaction) {
    document.getElementById('__VIEWSTATE').value = '';
    document.getElementById('aspnetForm').encoding = 'application/x-www-form-urlencoded';
    document.getElementById('aspnetForm').action = theaction;
    document.getElementById('aspnetForm').onsubmit == '';
    return true;
}


// put this on the onClick for forms  will need the name of the ASP.NET form - and the ID of a span for error message
// will  go through elements and colour if not what you asked for - options include required, postcode, email

function ps_form_validate(ferrors, theaction) {
    var ers = document.getElementById(ferrors);
    var myform = document.getElementsByName('aspnetForm');
    var length = myform[0].length; // first form with this name - and the number of elements in it.
    var el;
    var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
    var postcodeReg = "^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$";
    var regex = new RegExp(emailReg);
    var pregex = new RegExp(postcodeReg, "i");
    for (var i = 0; i < (length - 1); i++)  // iterate through elements - split them up by colon, look for options and process
    {
        el = myform[0].elements[i];
        el.style.backgroundColor = "";
        els = el.name.split("^");
        m = els.length; // if more than 1, there are options
        var b = 0;
        while (++b < m) {
            if (els[b] == "required") { if (el.value == "") { el.style.backgroundColor = '#ffddaa'; ers.innerHTML = "* Input required"; return false; } }
            if (els[b] == "email") { if ((!regex.test(el.value)) && (el.value != "")) { el.style.backgroundColor = "yellow"; ers.innerHTML = "* Email required"; return (false); } }
            if (els[b] == "postcode") { if ((!pregex.test(el.value)) && (el.value != "")) { el.style.backgroundColor = "yellow"; ers.innerHTML = "* Postcode required"; return (false); } }
            // more checks here if you like
        }
    }

    for (var i = 0; i < (length - 1); i++)  // iterate through form elements - remove first colon and anything after that - ie restore to original name
    {
        el = myform[0].elements[i];
        els = el.name.split("^");
        el.name = els[0];
    }
    formControl(theaction);
}

function checkCapsLock( e ) {
 var myKeyCode=0;
 var myShiftKey=false;
 var evtobj = window.event ? event : e
 if (evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey) {
     myShiftKey = true
}

 // Internet Explorer 4+
 if ( document.all ) {
  myKeyCode=e.keyCode;
  //myShiftKey=e.shiftKey;

 // Netscape 4
 } else if ( document.layers ) {
  myKeyCode=e.which;
  //myShiftKey=( myKeyCode == 16 ) ? true : false;

 // Netscape 6
 } else if ( document.getElementById ) {
  myKeyCode=e.which;
  //myShiftKey=( myKeyCode == 16 ) ? true : false;

 }

 // Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
 if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {
 showdiv('capsLockWarning');

 // Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
 } else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
 showdiv('capsLockWarning');
 
 } else {
 hidediv('capsLockWarning');

 }
}

function showdiv(id) {
    //safe function to show an element with a specified id

    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'block';
    }
    else {
        if (document.layers) { // Netscape 4
            document.id.display = 'block';
        }
        else { // IE 4
            document.all.id.style.display = 'block';
        }
    }
}

function hidediv(id) {
    //safe function to show an element with a specified id

    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'none';
    }
    else {
        if (document.layers) { // Netscape 4
            document.id.display = 'none';
        }
        else { // IE 4
            document.all.id.style.display = 'none';
        }
    }
}

function youTubeUpdate(vref,sid) {
	 // for use with YouTube objects - added Dec 2011 P.Scargill. YouTube ref number -  and ID of the iFrame
	 $('#'+sid).attr('src',"http://www.youtube-nocookie.com/embed/"+vref+"?rel=0");
	 document.getElementById(sid).src = document.getElementById(sid).src;
}

function showYouThumbs(area,thediv,theplayer)
{
	var sometext="";
	$.ajax(
	{
		type: "GET",
		url: "/"+area+"/youvideos.xml",
		dataType: "xml",
		success: function(xml) 
		{
			$(xml).find('videos').each(function()
			{
				$(this).find('play').each(function()
				{
					var title = $(this).find('title').text();
					var subtitle=$(this).find('subtitle').text();	
					var time=$(this).find('time').text();	
					var url = $(this).find('id').text();	

					sometext+='<div class="mousetoHand"><img src="http://i1.ytimg.com/vi/'+url+'/default.jpg" alt="'+subtitle+'" title="'+subtitle+'" onclick="youTubeUpdate(';					
					sometext+="'"+url+"','" + theplayer + "');";
					sometext+='"/><br/><span class="small_text">'+title+'</span></div><br/>';
				});
			});
		$('#'+thediv).append(sometext);
		}
	});				
}

    
function youplay(pla,lis) {
        var search_input = encodeURIComponent($("#"+lis).attr("youList"));
 		if (args['vid']==undefined) play_input=encodeURIComponent($("#"+pla).attr("youPlay")); else play_input=encodeURIComponent(args['vid']);
        $("#theYouList").html("");
		var yt_url='http://gdata.youtube.com/feeds/api/videos?q='+play_input+'&format=5&max-results=1&v=2&alt=jsonc';

		$.ajax
		({
			type: "GET",
			url: yt_url,
			dataType:"jsonp",
			success: function(response)
			{
				if(response.data.items)
				{
					$.each(response.data.items, function(i,data)
					{
						var video_frame="<iframe id='thepl' class='youtube-player' style='width:"+$("#"+pla).attr("youWidth")+"px; height:"+$("#"+pla).attr("youHeight")+"px' src='http://www.youtube.com/embed/"+data.id+"' frameborder='0' type='text/html'></iframe>";
						$("#"+pla).html(video_frame); // Result
					});
				}
				else
				{
					$("#"+pla).html("Sorry - badly specified Video");
				}
			}
		});

		yt_url='http://gdata.youtube.com/feeds/api/videos?q='+search_input+'&format=5&max-results='+$("#"+lis).attr("maxthumbs")+'&v=2&alt=jsonc';

		$.ajax
		({
			type: "GET",
			url: yt_url,
			dataType:"jsonp",
			success: function(response)
			{
				if(response.data.items)
				{
                   
					var final="<div style='width:"+$("#"+lis).attr("youWidth")+"; height:"+$("#"+lis).attr("youHeight")+"px; overflow: auto;'>";
					$.each(response.data.items, function(i,data)
					{
						

                        var video_id=data.id;
						var video_title=data.title;
						var video_viewCount=data.viewCount;
      					var video_desc=data.description;
						var video_duration=data.duration;
						var video_default=data.thumbnail.sqDefault;
                        final=final+"<div title='"+ video_desc +"' alt='"+ video_desc +"'   class='mousetohand' ";
                        final=final+'onclick="$(\'#thepl\').attr(\'src\',\'http://www.youtube-nocookie.com/embed/'+video_id+'?rel=0\');document.getElementById(\'thepl\').src = document.getElementById(\'thepl\').src;"';
                        final=final+"style='padding: 2px;'><div style='float:left; '><img src='" + video_default + "' ";
                        final=final+'></div><div class="youtube_normal item_highlight" style="width:160px; height: 86px; float:right; padding:2px;">';
                        final=final+video_title+"<br/><span class='youtube_small'>"+video_duration+" Seconds</span><br/></span></div><div style='clear:both;'></div></div>";


					});
                     final=final+"</div>";
                        $("#"+lis).append(final); // Result					
				}
				else
				{
					$("#"+lis).html("Sorry - badly specified List");
				}
                
    
				// AFTER page load then AFTER all the divs have been created, add a mouseover situation....
				$(".item_highlight").mouseover(function() {
					$(this).css({'background-color' : '#ddeedd'});
				});
				
				$(".item_highlight").mouseout(function(){
				
						$(this).css({'background-color' : '#ffffff'});
				});
							
							
                
                
			}
		});		
		
}

/*******************************************************************************

'args.js', by Charlton Rose

Permission is granted to use and modify this script for any purpose,
provided that this credit header is retained, unmodified, in the script.

*******************************************************************************/


// This function is included to overcome a bug in Netscape's implementation
// of the escape () function:

function myunescape (str)
{
	str = '' + str;
	while (true)
	{
		var i = str . indexOf ('+');
		if (i < 0)
			break;
		str = str . substring (0, i) + ' ' + str . substring (i + 1, str . length);
	}
	return unescape (str);
}



// This function creates the args [] array and populates it with data
// found in the URL's search string:

function args_init ()
{
	args = new Array ();
	var argstring = window . location . search;
	if (argstring . charAt (0) != '?')
		return;
	argstring = argstring . substring (1, argstring . length);
	var argarray = argstring . split ('&');
	var i;
	var singlearg;
	for (i = 0; i < argarray . length; ++ i)
	{
		singlearg = argarray [i] . split ('=');
		if (singlearg . length != 2)
			continue;
		var key = myunescape (singlearg [0]);
		var value = myunescape (singlearg [1]);
		args [key] = value;
	}
}


