// JavaScript Document
function goto( where )
{ 
location.href=where;
}

/******************************************/
/*             PARSE QUERYSTRING          */
/******************************************/
function gup( name )
{  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );  var results = regex.exec( window.location.href );
	if( results == null )    return "";
	else    
	return results[1];
}

/******************************************/
/*              FOCUS                     */
/******************************************/
function focusIt(field)
{
  var mytext = document.getElementById(field); 
  mytext.focus(); 
}

/******************************************/
/*              SELECT                    */
/******************************************/
function SelectIt(field)
{
  var mytext = document.getElementById(field); 
  mytext.select(); 
}

/******************************************/
/*              BODY FUN                  */
/******************************************/
function BodyFun()
{
	var fb = gup( 'fb' );
	switch(fb)
	{
	case 'CaptureDeleted' : alert('Capture deleted');  
	break;  
		case 'limit_reached' : alert('Upload limit reached - your photo was not uploaded');  
	break; 
	
	case 'done' : alert('Recipient has been added');  
	break; 
	//case 'CapturesDeleted' : alert(gup( 'count' )+' Captures deleted');  
	//break;  
	//case 'FriendGroupDeleted' : alert('Friend Group deleted');  
	//break;  
	//case 'FriendGroupsDeleted' : alert(gup( 'count' )+' Friend Groups deleted');  
	//break;  
	//case 'PhotoDeleted' : alert('Photo deleted');  
	//break;  
	//case 'PhotosDeleted' : alert(gup( 'count' )+' Photos deleted');  
	//break;
	//case 'uploadOK' : alert('Photo uploaded');  
	//break;
	//case 'CaptureUpdatedOK' : alert('Capture updated');  
	//break
	//	case 'LandscapeTooSmall' : alert('The lanscape photo you tried to upload was an invalid size, try a larger picture with a standard aspect ratio');  
	//break
	//	case 'PortraitTooSmall' : alert('The portrait photo you tried to upload was an invalid size, try a larger picture with a standard aspect ratio');  
	//break
	//	case 'SquareTooSmall' : alert('The square photo you tried to upload was too small, try a larger picture - preferably with a standard aspect ratio');  
	//break
	//	case 'RatioError' : alert('The lanscape photo you tried to upload was an invalid size, try a picture with a more standard aspect ratio');  
	//break
	} 	
	
	var focusme = gup( 'focusme' );
	if (focusme){
		focusIt(focusme); 	
	}
	
	var selectme = gup( 'selectme' );
	if (selectme){
		SelectIt(selectme); 	
	}
}

/******************************************/
/*           VALIDATE PHOTO               */
/******************************************/
/*Reload the animated gif after post else it stops in IE*/
function showUploading() {
	document.getElementById('photo_submit').disabled = true;
	document.getElementById('uploading').style.display='block';
}

function ValidPhoto() {
	var file=document.getElementById('file').value;
	//var slashtest = /\\/;
	//Check for no string
    if ( file == "" )	
    {
		alert ( "Please browse to a photo" );
		return false;
    }
	//Check file extensions
	else if (!/(\.jpg|\.jpeg)$/i.test(file)) {	
		alert("Please only upload files that end in jpeg or jpg");
		return false;
	}
	//Note: turned this off as it was failing in new Firefox
	// Check string contains a backslash
	//else if (!slashtest.test(file)) {				
	//	alert("Invalid file path");
	//	return false;
	//}
	else
	window.setTimeout("showUploading()", 50);
}

/******************************************/
/*            CONFIRM DELETE              */
/******************************************/
/*Informs the user if no check boxes are checked and asks confirmation before posting the form*/
function confirmdelete(formname,nothingmessage,confirmmessage)
{
	//Count selected checkboxes
	var formControls = document.getElementById(formname).elements;
	var c = 0;
	for (var i = 0; i < formControls.length; i++)
	if (formControls[i].type.toLowerCase() == 'checkbox'
	&& formControls[i].checked)
	c++;
	//If no checkbox is selected
	if (c==0)
	{
	alert(nothingmessage);
	return false;
	} else {
	// entry is marked so show confirm option	
	var agree=confirm(confirmmessage);
	if (agree)
	{
	//Submit the form
	//return true;
	document.forms[formname].submit(); 
	}
	else
	return false;
	} 
}

/******************************************/
/*         SHOW / HIDE LAYERS 1           */
/******************************************/
function toggleLayer( whichLayer ){
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work    
	elem = document.getElementById( whichLayer );  else if( document.all ) // this is the way old msie versions work      
	elem = document.all[whichLayer];  else if( document.layers ) // this is the way nn4 works   
	elem = document.layers[whichLayer];  vis = elem.style;  // if the style.display value is blank we try to figure it out here  
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)    
	vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
	vis.display = (vis.display==''||vis.display=='block')?'none':'block';
	}

/******************************************/
/*         SHOW / HIDE LAYERS  2          */
/******************************************/
//Shows an upload animation on image upload
function showhide(id, formname){ 
	if (document.getElementById){ 
	obj = document.getElementById(id); 
	if (obj.style.display == "none"){ 
	obj.style.display = ""; 
	} else { 
	obj.style.display = "none"; 
	} 
	} 
	//Hide feedback
	document.getElementById('uploadfb').style.display = "none";
	document.getElementById('galleryupdatefb').style.display = "none";
	//Submit the uploadform
	document.forms[formname].submit();
} 

/******************************************/
/*         DISABLE SUBMIT BUTTON          */
/******************************************/
function disable(button_id,form_id){
	document.getElementById(button_id).disabled = true;
	document.getElementById(form_id).submit();
}

/******************************************/
/*          CLEAR FORM FIELDS             */
/******************************************/
//Called on click  - only clear the field if it is equal to the default value
function ClearControl(objName, DefaultTxt)
	{
	if (document.getElementById(objName).value == DefaultTxt)
	{document.getElementById(objName).value="";}
}

/******************************************/
/*          RESET FORM FIELD              */
/******************************************/
//Called on blur - only reset the fields if they are empty
function ResetControl(objName, DefaultTxt)
	{
	if (document.getElementById(objName).value=="")
	{document.getElementById(objName).value=DefaultTxt;}
}

/******************************************/
/*                TRIM                    */
/******************************************/
function Trim(str) { 
	var trimmed = str.replace(/^\s+|\s+$/g, '') ;
	return trimmed; 
}

/******************************************/
/*             CONFIRMATION               */
/******************************************/
// JavaScript Document
//onclick="confirmation();
function confirmation() {
	var answer = confirm("Are you sure you want to log out?")
	if (answer){
		/*alert("Bye bye!") */
		window.location = "scripts/logout.php";
	}
	else{
		alert("Thanks for sticking around!")
	}
}

/******************************************/
/*              SHOW IMAGE                */
/******************************************/
function SwapPic(imageSrc) {
    if (document.images) document.images['large_pic'].src = imageSrc;
}

/******************************************/
/*             LIMIT TEXT                 */
/******************************************/
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}



