/*
 * 
 */
function videoCodeCheck()
{
	// get all video code digits 
	nCode1 = $("input[name='video_code_1']").val();
	nCode2 = $("input[name='video_code_2']").val();
	nCode3 = $("input[name='video_code_3']").val();
	nCode4 = $("input[name='video_code_4']").val();
	nCode5 = $("input[name='video_code_5']").val();
		
	// we entered all the digits?
	if (nCode1.length > 0 && nCode2.length > 0 && nCode3.length > 0 && nCode4.length > 0 && nCode5.length > 0)
	{
		// check to see if the video code is real
        $.ajax({
            url: '/ajax/videoexists/' + nCode1 + nCode2 + nCode3 + nCode4 + nCode5 + '/',
            type: 'GET',
            error: function()
            {
        		// hide payment options
        		$("#payment-options").slideUp(
        			"slow", 
        			function()
        			{
                		$("input[name='video_title']").val('Invalid video code please try again!');
                    	
        				// set dialog content
        				$("#dialogInfoBody").html('You have entered an invalid video code. Please check and try again!');
        			
        				// pop dialog notice box
        	        	$("#dialogInfo").dialog(
        			    {
        			    	title: 'Clipstar',
        		            resizable: false,
        		            close: function() { $(this).dialog('destroy'); },
        		            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
        			    });  
        			}
        		);	      	
            },
            success: function(xml)
            {               
            	// get video details
            	sVideoName      = $(xml).find("name:first").text();
            	sVideoId        = $(xml).find("video:first").attr("videoId");
            	sVideoIdEncoded = $(xml).find("video:first").attr("videoIdEncoded");
            	
            	// padd video code?
            	if (sVideoId.length == 4)
            	{
            		sVideoId = '0' + sVideoId;
            	}
            	
            	// set details
            	$("input[name='video_title']").val(sVideoName);
            	$("input[name='videocode']").val(sVideoId);
            	$("input[name='videotitle']").val(sVideoName);
            	$("input[name='videoidEncoded']").val(sVideoIdEncoded);
            	
            	// show payment options
            	$("#payment-options").slideDown("slow");

            }
        });		
		
	} else {
		// show payment options
    	$("#payment-options").slideUp("slow");
	}
	
}

/*
 *  Calculate unixtimestamp and return
 */
function unixTime()
{
	// get unix time
	var foo = new Date; // Generic JS date object
	var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch
	var unixtime = parseInt(unixtime_ms / 1000);
	return unixtime;
};

/*
 *  Refresh the captcha image on click
 */
function refreshCaptcha() 
{
	$(document).ready(function() 
	{
		$('#captcha').attr('src','/captcha.html?rand='+(Math.random()*11));
	});
};

function hasNumbers(t)
{
    var regex = new RegExp("[0-9]");           
    return regex.test(t);
};

function hasUpper(t)
{
    var regex = new RegExp("[A-Z]");           
    return regex.test(t);
};

function hasLower(t)
{
    var regex = new RegExp("[a-z]");           
    return regex.test(t);	
};

function hasPunct(t)
{
    var regex = new RegExp("[^a-zA-Z0-9]");
    //var c = t.match("[^a-zA-Z0-9]", "g");
    //var count = c.length;
    return regex.test(t);
};

function passwordStrength()
{
    // get value of form element 
    var myInput = $("input[name='Password']").val();
  
    // test for numbers, upper, punctuation 
    bNumber = hasNumbers(myInput);
    bUpper  = hasUpper(myInput);
    bPunct  = hasPunct(myInput);
    bLower  = hasLower(myInput);
   
    //alert(bPunct);
    
    if (myInput.length > 0)
    {
        if (myInput.length < 5)
        {
        	$("#passwordbar").attr("style", "width:45px; height:4px; background: #a7c6e2;");
        	$("#passwordtext").html('<span>Password is too short</span>');
        }
        else if (myInput.length > 5) 
        {
            if (bNumber == true && bUpper == true && bPunct == true && bLower == true)
            {
            	
            	$("#passwordbar").attr("style", "width:135px; height:4px; background: #4588c5;");
            	$("#passwordtext").html('<span>Password is good</span>');         
            }
            else
            {
            	$("#passwordbar").attr("style", "width:90px; height:4px; background: #77a8d4;");
            	$("#passwordtext").html('<span>Password is fair</span>');
            }
        }
   }
   else if (myInput.length == 0)
   {
   		$("#passwordbar").attr("style", "");
		$("#passwordtext").html('');
   }
};

function urlInputFilter(o)
{
	o.value=o.value.replace(/([^0-9A-Za-z-_])/g,"");
}

function urlcheck()
{
    $(document).ready(function()
    {    	
       // get value of form element 
       myInput = $("input[name='Username']").val();
       
       // check if user has entered something
       if (myInput.length > 0)
       {
           $.ajax({
                url: '/ajax/urlcheck/' + myInput + '/',
                type: 'GET',
                error: function(){
                    $("input[name='register']").attr("disabled", false);
                    $("input[name='register']").css({ opacity: '1' });
                    
                    //$("#otherurls").html('');
                    
                },
                success: function(sResponse){
                	if (sResponse == 'exists') {
	                    $("input[name='register']").attr("disabled", true);
	                    $("input[name='register']").css({ opacity: '0.2' });
	                    
	                    //$("#otherurls").html('<p style="color: #E85318;"><em style="color: #E85318;">Sorry that username is not available</em><br/>You could try something like: ' + myInput + '123</p>');
	                    
	        			// set dialog content
	        			$("#dialogInfoBody").html('<p class="redalert">Sorry that username is not available. You could try something like ' + myInput + '123</p>');
	        		
	        			// pop dialog notice box
	                	$("#dialogInfo").dialog(
	        		    {
	        		    	title: 'Clipstar',
	        	            resizable: false,
	        	            close: function() { $(this).dialog('destroy'); },
	        	            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
	        		    });	                    
	                    
                	} else {
                        $("input[name='register']").attr("disabled", false);
                        $("input[name='register']").css({ opacity: '1' });
                        
                        //$("#otherurls").html('');                 		
                	}
                }
            });
        }
    });
};

/*
 * Check user has entered a valid video id
 */
function videoidcheck()
{
	$(document).ready(function()
	{
		// get value of form element 
	    myInput = $("input[name='VideoId']").val();
	    
		if (myInput.length > 0)
		{
	        $.ajax({
	            url: '/ajax/videoexists/'+ myInput + '/',
	            type: 'GET',
	            error: function()
	            {
					// set dialog content
					$("#videoidcheck").html('<span style="color:red;">Invalid video ID. A default Clipstar group video will be used.</span>');
	            },
	            success: function()
	            {
	            	$("#videoidcheck").html('This video ID is valid.');
	            }
	        });
		}
	});
};

function setvideoid()
{
	nVideoId = $('select[name="Video"] :selected').val();
	$('input[name="VideoId"]').val(nVideoId);
	
	// check id
	videoidcheck();
};
 
function popUp(url, nWidth, nHeight) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+nWidth+",height="+nHeight+"');");
};

/*
 * bookmark link
 */
function bookmark_us(url, title)
{
		url = 'http://www.clipstar.com';
		title = 'Clipstar - be somebody';
		 
		if (window.sidebar) // firefox
		{
			window.sidebar.addPanel(title, url, "");
		}
		else if(window.opera && window.print) // opera
		{ 
		    var elem = document.createElement('a');
		    elem.setAttribute('href',url);
		    elem.setAttribute('title',title);
		    elem.setAttribute('rel','sidebar');
		    elem.click();
		}
		else if(document.all) // ie
		{
		    window.external.AddFavorite(url, title);
		}
		else
		{
			// set dialog content
			$("#dialogInfoBody").html('To add Clipstar as a bookmark press CTRL + D');
		
			// pop dialog notice box
        	$("#dialogInfo").dialog(
		    {
		    	title: 'Clipstar',
	            resizable: false,
	            close: function() { $(this).dialog('destroy'); },
	            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
		    });
		}
};
 
 /*
  * get data from flash
  */
 function getSignature(nFonts, user_manu, user_timeoffset, user_cam, user_mic, user_lang, user_os, user_screenX, user_screenY)
 {
	// get javascript computer information
	js_availheight = screen.availHeight;
	js_availwidth  = screen.availWidth;
	js_colour = screen.colorDepth;
	js_dpi	  = screen.pixelDepth;
	js_width  = screen.width;
	js_height = screen.height;
	js_os     = version=navigator.appVersion;
	  
 	// set hidden form elements
	$("input[name='js_availheight']").val(js_availheight);
	$("input[name='js_availwidth']").val(js_availwidth);
	$("input[name='js_colour']").val(js_colour);
	$("input[name='js_dpi']").val(js_dpi);
	$("input[name='js_width']").val(js_width);
	$("input[name='js_height']").val(js_height);
	$("input[name='js_os']").val(js_os);
	$("input[name='fontsno']").val(nFonts);
 	$("input[name='user_manu']").val(user_manu);
 	$("input[name='user_timeoffset']").val(user_timeoffset);
 	$("input[name='user_cam']").val(user_cam);
 	$("input[name='user_mic']").val(user_mic);
 	$("input[name='user_lang']").val(user_lang);
 	$("input[name='user_os']").val(user_os);
 	$("input[name='user_screenX']").val(user_screenX);
 	$("input[name='user_screenY']").val(user_screenY);
 	$("input[name='user_dpi']").val(document.getElementById("inch").offsetWidth + "x" + document.getElementById("inch").offsetWidth);
};