// JavaScript Document

/******************************************/
/*             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(); 
}

/******************************************/
/*      CONFIRM REPLACE DOCUMENT          */
/******************************************/
function confirm_replace()
{
 var where_to= confirm("There is already a file with the same name on the server, do you want to replace this file with the one you are uploading");
 if (where_to== true)
 {
	//alert('Replace');
	//add_photo_form.doc_title.value="true";
   	window.location="http://www.aaawebsitedesign.co.uk/admin_new_document.php?op=replace";
	//document.getElementById(add_photo_form).submit();
 }
 	//else
 {
	//alert('Do not replace');
 // window.location="http://www.aaawebsitedesign.co.uk/admin_new_document.php?op=no_replace";
 }
}
//window.onload=confirm_replace; 
/******************************************/
/*              BODY FUN                  */
/******************************************/
function BodyFun()
{
	var fb = gup( 'fb' );
	switch(fb)
	{
	case 'notadmin' : alert('You are not logged in as administrator');  
		break;  
	case 'page_updated' : alert('Page saved okay');  
		break; 
	case 'CustomerAddedOK' : alert('New customer added okay');  
		break;
	case 'CustomerUpdatedOK' : alert('Customer details updated okay');  
		break; 
	case 'CustomerDeletedOK' : alert('Customer deleted okay');  
		break; 
	case 'PageDeletedOK' : alert('News page deleted okay');  
		break;
	case 'PageNotEditable' : alert('The page you tried to edit is locked');  
		break;
	case 'PageAddedOK' : alert('News page added okay');  
		break; 
	case 'DocumentDeletedOK' : alert('Document deleted okay');  
		break;
	case 'DocumentAddedOK' : alert('Document added okay'); 
		break;
	case 'DocumentUpdatedOK' : alert('Document updated okay'); 
		break;
	case 'ConfirmReplace' : confirm_replace(); 
		break;
	case 'loginFailed' : alert('Wrong username or password'); 
		break;
	case 'portfolio_job_deleted' : alert('Portfolio item deleted'); 
		break;
	case 'portfolio_updated' : alert('Portfolio item updated'); 
		break;
	case 'portfolio_pic_deleted' : alert('Portfolio pic deleted'); 
		break;
	case 'portfolio_pic_uploaded' : alert('Portfolio pic uploaded'); 
		break;
	case 'portfolio_job_added' : alert('Portfolio item added'); 
		break;
	case 'contact_deleted' : alert('Contact deleted okay'); 
		break;
	case 'contact_updated' : alert('Contact updated okay'); 
		break;
	case 'contact_added' : alert('Contact added okay'); 
		break;
	case 'renewable_deleted' : alert('Renewable deleted okay'); 
		break;
	case 'renewable_updated' : alert('Renewable updated okay'); 
		break;
	case 'renewable_added' : alert('Renewable added okay'); 
		break;
	} 	
	
	var focusme = gup( 'focusme' );
	if (focusme){
		focusIt(focusme); 	
	}
	
	var selectme = gup( 'selectme' );
	if (selectme){
		SelectIt(selectme); 	
	}
}

window.onload=BodyFun; 

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


function ValidDoc() {
	var file=document.getElementById('file').value;
	//var slashtest = /\\/;
	//Check for no string
    if ( file == "" )	
    {
		alert ( "Please browse to a document" );
		return false;
    }
	//Check file extensions
	else if (!/(\.doc|\.pdf|\.docx|\.xlsx|\.xls)$/i.test(file)) {	
		alert("Please only upload files that end in doc, docx, xls, xlsx or pdf");
		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);
}

function ValidPhoto() {
	var file=document.getElementById('file').value;
	//var slashtest = /\\/;
	//Check for no string
    if ( file == "" )	
    {
		alert ( "Please browse to a picture" );
		return false;
    }
	//Check file extensions
	else if (!/(\.gif|\.jpg|\.jpeg)$/i.test(file)) {	
		alert("Please only upload files that end in gif, 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;
	}
}



