// JavaScript Document

/******************************************/
/*             PARSE QUERYSTRING          */
/******************************************/
function ToggleMenuStatus()
{  
var search_toggle_status = document.getElementById("search_toggle_status");
var status = search_toggle_status.value;
 if (status== 'on')
 search_toggle_status.value='off';
 else
 search_toggle_status.value='on';
}

/******************************************/
/*          GET SELECTED GROUP            */
/******************************************/
function GetSelectedGroup(){
	var output='';
	var w = document.getElementById("select_groups").selectedIndex;
	if(w){
		var selected_text = document.getElementById("select_groups").options[w].text;
		var selected_value = document.getElementById("select_groups").value;
		if(selected_value.match("add")){
			output='Are you sure you want to add selected contacts to '+selected_text+'?';
		}
		
		if(selected_value.match("rem")){
			output='Are you sure you want to remove selected contacts from '+selected_text+'?';
		}
	}
	return output;
}

function resetSelectGroup(){
	document.getElementById("select_groups").selectedIndex=0;
}

/******************************************/
/*          CHECK / UNCHECK ALL           */
/******************************************/
function toggleall(formname,checkname,button_id){
	var thestate=document.getElementById(button_id).checked;
	var el_collection=eval("document.forms."+formname)
	for (c=0;c<el_collection.length;c++)
	{
		if(el_collection[c].type=='checkbox') el_collection[c].checked=thestate;
	}
}

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

/******************************************/
/*           VALIDATE PHOTO               */
/******************************************/
/*Reload the animated gif after post else it stops in IE*/
//function AlertOnce(message) {
	//document.getElementById("fbflag").value="";
	//document.getElementById("fb").value="";
	//Look for session variable feedback (stored in hidden field)
	//var fbflag=document.getElementById("fbflag").value;
	//if (fbflag=="1"){
	//	alert(message); 
	//	document.getElementById("fbflag").value="";
	//}	
//}

/******************************************/
/*              BODY FUN                  */
/******************************************/
function BodyFun()
{
	//Look for session variable feedback (stored in hidden field)
	var hash=document.getElementById("hash").value;
	//If no feedback in hidden field - look in GET array
	if (hash==""){
	var hash = gup( 'hash' ); 	
	}
	if(hash){
	window.location = window.location + "#"+hash;
	}
	
	//Look for session variable count(stored in hidden field)
	var count=document.getElementById("count").value;
	//If no count in hidden field - look in GET array
	if (count==""){
		var count = gup( 'count' ); 	
	}
	
	//Look for session variable feedback (stored in hidden field)
	var fb=document.getElementById("fb").value;
	//If no feedback in hidden field - look in GET array
	if (fb==""){
		var fb = gup( 'fb' ); 	
	}
	
	//Give approprate alerts and function calls
	switch(fb)
	{
	//Permissions
	case 'notadmin' : alert('You are not logged in as administrator');  
		break;
	//Pages
	case 'page_added_okay' : alert('Page added okay');  
		break; 
	case 'page_updated' : alert('Page saved okay');  
		break; 
	case 'page_not_editable' : alert('The page you tried to edit is locked');  
		break;
	case 'page_delete_locked' : alert('Page cannot be deleted'); 
		break;
	case 'page_deleted' : alert('Page deleted'); 
		break;
	case 'page_turned_on' : alert('Page has been turned on'); 
		break;	
	case 'page_turned_off' : alert('Page has been turned off'); 
		break;
	//Galleries
	case 'gallery_added_okay' : alert('Gallery added okay'); 
		break; 
	case 'gallery_deleted_okay' : alert('Gallery and photos deleted'); 
		break;
	case 'gallery_updated_okay' : alert('Gallery details updated'); 
		break;
	case 'photo_caption_updated' : alert('Photo caption updated'); 
		break;
	case 'confirm_replace' : ConfirmReplace(); 
		break;
	case 'photo_deleted' : alert('Photo deleted'); 
		break;
	case 'photos_deleted' : alert(count + ' photos deleted'); 
		break;
	case 'photo_uploaded' : alert('Photo uploaded okay'); 
		break;
	case 'photo_invalid' : alert('Invalid photo'); 
		break;
	//Regions
	case 'region_updated_okay' : alert('Region updated'); 
		break;
	//Newsletters
	case 'newsletter_delete_locked' : alert('Newsletter cannot be deleted'); 
		break;	
	case 'newsletter_deleted' : alert('Newsletter deleted'); 
		break;
	case 'newsletter_updated' : alert('Newsletter updated'); 
		break;	
	case 'newsletter_added' : alert('Newsletter added, you can now add articles'); 
		break;
	case 'newsletter_deleted' : alert('Newsletter deleted');
		break;
	case 'newsletter_turned_on' : alert('Newsletter has been turned ON and WILL be auto sent'); 
		break;
	case 'newsletter_turned_off' : alert('Newsletter has been turned OFF and WILL NOT be auto sent'); 
		break;
	//Newsletter articles
	case 'newsletter_article_deleted' : alert('Article deleted'); 
		break;
	case 'newsletter_article_added' : alert('Article added'); 
		break;
	case 'newsletter_article_updated' : alert('Article updated'); 
		break;
	//Mail Groups
	case 'mail_group_deleted' : alert('Mail group deleted'); 
		break;
	case 'mail_group_added' : alert('Mail group added'); 
		break;
	case 'mail_group_updated' : alert('Mail group updated'); 
		break;
	//Contacts
	case 'contact_unsubscribed' : alert('Contact turned off'); 
		break;
	case 'contact_subscribed' : alert('Contact turned on'); 
		break;
	case 'contact_deleted' : alert('Contact deleted'); 
		break;
	case 'contacts_deleted' : alert(count+' '+SingleOrPlural('contact','contacts',count)+' deleted'); 
		break;
	case 'contacts_subscribed' : alert(count+' '+SingleOrPlural('contact','contacts',count)+' turned on');
		break;
	case 'contacts_unsubscribed' : alert(count+' '+SingleOrPlural('contact','contacts',count)+' turned off'); 
		break;
	case 'contact_updated' : alert('Contact updated'); 
		break;
	case 'contact_not_editable' : alert('The contact you tried to edit is locked');  
		break;
	case 'contacts_added_to_group' : alert(count+' '+SingleOrPlural('contact','contacts',count)+' added to mail group'); 
		break;
	case 'contacts_removed_from_group' : alert(count+' '+SingleOrPlural('contact','contacts',count)+' removed from mail group'); 
	//case 'contact_added' : alert('Contact was added okay. You may now add another..');  
		break;	
	case 'contact_added' : ConfirmAnotherContact();
		break;
	}
	
	//look for focus instruction in querystring
	var focusme = gup( 'focusme' );
	if (focusme){
		focusIt(focusme); 	
	}
	//look for select instruction in querystring
	var selectme = gup( 'selectme' );
	if (selectme){
		SelectIt(selectme); 	
	}
	
	//Repost after feedback (minus Get feedback)
	//if (fb=="page_deleted"){
	//	var thisurl=document.location.href;
	//	var newurl=thisurl.replace("fb=page_deleted", "");
	//	window.location=newurl;
	//}

}

//Run on load
window.onload=BodyFun; 

function SingleOrPlural(str_single,str_plural,count){
	var output;
	if((count>1)||(count=0))
		output=str_plural;
	else
		output=str_single;
	return output;
}


/******************************************/
/*       CONFIRM ANOTHER CONTACT          */
/******************************************/
function ConfirmAnotherContact() {
	var answer = confirm('New contact was added okay.\nClick OK to add another contact.\nClick CANCEL to return to Contacts Manager')
	if (answer){
		//focusIt('contact_name');
		window.location = "index.php?page_id=110&focusme=contact_name";
	}
	else{
		window.location = "index.php?page_id=108";
	}
}

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

/******************************************/
/*          VALIDATE NEWSLETTER            */
/******************************************/
function validateNewsletter(){
	var output='';
	var w = document.getElementById("newsletter_status").selectedIndex;
		var selected_text = document.getElementById("newsletter_status").options[w].text;
		//var selected_value = document.getElementById("newsletter_status").value;
		if(selected_text.match("On")){
			var agree=confirm('ATTENTION! Status ON means that newsletter will send automatically on scheduled send date! Click OK if your newsletter is ready to send or CANCEL if you wish to postpone');
		if (agree)
			document.getElementById("newsletter_status").value='On';
		else
			document.getElementById("newsletter_status").value='Off';
		}
	}

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

/******************************************/
/*            CONFIRM DELETE              */
/******************************************/
/*Informs the user if no check boxes are checked and asks confirmation before posting the form*/
function ConfirmContactDelete(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);
	resetSelectGroup();
	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
	resetSelectGroup();
	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;
	}
}




