// JavaScript Document

// --------------------------------------------
// ---- CHECK IF MAC OR PC --------------------
// --------------------------------------------
function Mac_or_PC1() 
{
	detect = navigator.userAgent.toLowerCase();
	
	isMac = detect.indexOf('mac') + 1;
	
	if( isMac )
	{
		menu = document.getElementById('menu_items');
		menuLinks = menu.getElementsByTagName("a");
		for(i = 0; i < menuLinks.length; i++){	
			menuLinks[i].style.padding = "1px 20px 3px 20px";
		}
	}
}

// ------------------------------------------

function equal_height(el_one,el_two,extra) 
{
	if( document.getElementById(el_one) && document.getElementById(el_two) )
	{
		var first_height = document.getElementById(el_one).offsetHeight;
		var second_height = document.getElementById(el_two).offsetHeight;
		
		if (second_height < (first_height+extra)) {
			document.getElementById(el_two).style.height = (first_height+extra)+'px';
		}
	
	}
	
}

// -----------------------------------------------------------------------------------
// -------- VIEW PORTFOLIO IN NEW WINDOW ---------------------------------------------
// -----------------------------------------------------------------------------------
function viewportfolio(url,wname)
{
	portfolio_window = window.open(url,wname,'width=770,height=580,left=20,top=20,toolbar=yes,scrollbars=yes,location=yes,resizable=yes');
	portfolio_window.location.href = url;
	
	if (window.focus) 
	{
		portfolio_window.focus();
	}
}

// -----------------------------------------------------------------------------------
// -------- VIEW TEMPLATE IN NEW WINDOW ----------------------------------------------
// -----------------------------------------------------------------------------------
function viewtemplate(url,wname)
{
	template_window = window.open('',wname,'width=650,height=300,left=20,top=20,toolbar=no,scrollbars=no,location=no,resizable=no');
	//portfolio_window.location.href = url;
	
	var tmp = template_window.document;
	tmp.write('<html><head>');
	tmp.write('</head><body>');
	tmp.write('<table cellpadding="0" cellspacing="5"><tr><td rowspan="3"><img style="padding:0; margin:0; " src="/images/'+url+'_medium.jpg"></td><td><img style="padding:0; margin:0; " src="/images/'+url+'a.jpg"></td><td><img style="padding:0; margin:0; " src="/images/'+url+'c.jpg"></td>');
	tmp.write('</tr><tr><td><img style="padding:0; margin:0; " src="/images/'+url+'b.jpg"></td><td><img style="padding:0; margin:0; " src="/images/'+url+'d.jpg"></td>');
	tmp.write('</tr></table>');
	tmp.write('<p style="text-align:right;"><a href="javascript:self.close()">Close</a></p>');
	tmp.write('</body></html>');
	tmp.close();

if (window.focus) 
	{
		template_window.focus();
	}
}


// -----------------------------------------------------------------------------------
// -------- VIEW IMAGE IN NEW WINDOW -------------------------------------------------
// -----------------------------------------------------------------------------------

var image_view
function showImage(image_url,image_name,image_descrip)
{
	image_view=window.open('','image','width=520,height=490,left=50,top=50,resizable=yes,scrollbars=yes,toolbar=no,status=no,location=no');
	
	var tmp = image_view.document;
	tmp.write('<html><head><title>'+image_name+'</title>');
	tmp.write('<link rel="stylesheet" href="/css/view_image.css">');
	tmp.write('</head><body><div id="content"><img src="'+image_url+'" alt="'+image_name+'" />');
	tmp.write('<h4>'+image_name+'</h4>');
	tmp.write('<p>'+image_descrip+'</p>');
	tmp.write('<p style="text-align:right;"><br /><br /><a href="javascript:self.close()">Close</a><br /><br /></p>');
	tmp.write('</div></body></html>');
	tmp.close();
	
	if (window.focus) {image_view.focus()}
}

// -----------------------------------------------------------------------------------
// -------- VIEW HELP IN NEW WINDOW --------------------------------------------------
// -----------------------------------------------------------------------------------
function popup_help(url)
{
	help_window = window.open(url,'help','width=400,height=470,left=20,top=20,resizable=1,scrollbars=1,toolbar=0,status=0,location=0');
	help_window.location.href = url;
	
	if (window.focus) 
	{
		help_window.focus();
	}
}

function invisihelp(element)
{
	var theQ = element.getElementsByTagName('p');
	theQ[0].style.display = 'block';
}
function invisiclose(element)
{
	//var theQ = element;
	//theQ.style.display = 'none';
	var theQ = document.getElementById('invisihelper').getElementsByTagName('p');
	theQ[0].style.display = 'none';
	
}

// ----------------------------------------------------------------------------------
// -------- DELETION CONFIRMATIONS --------------------------------------------------
// ----------------------------------------------------------------------------------

function delete_confirm (type,link_to_delete)
{
	if(type == 'section') { // DELETE SECTION
		var confirm_msg = confirm("This will DELETE ALL IMAGES contained in selected section. \n Are you sure you want to continue?");
	} else if (type == 'news') { // DELETE NEWS
		var confirm_msg = confirm("This will PERMANENTLY DELETE this news item. \n Are you sure you want to continue?");
	} else if (type == 'image') { // DELETE IMAGE
		var confirm_msg = confirm("This will PERMANENTLY DELETE the image. \n Are you sure you want to continue?");
	}
	
	if(confirm_msg){
		window.location = link_to_delete;
	}
}

// ----------------------------------------------------------------------------------
// ------- OPEN OR CLOSE SECTIONS OF PORTFOLIO WITH ARROWS --------------------------
// ----------------------------------------------------------------------------------

function open_section(id) 
{
	var link_id = 'link_'+id;
	var sect_id = 'images_'+id;
	
	var visible = document.getElementById(sect_id).style.display;
	
	if(visible == undefined || visible == "none") {
		document.getElementById(sect_id).style.display = "block";
		document.getElementById(link_id).innerHTML = '<img height="11" width="11" alt="Expand this section" src="/images/arrow_down.gif" />';
	} else {
		document.getElementById(sect_id).style.display = "none";
		document.getElementById(link_id).innerHTML = '<img height="11" width="11" alt="Minimize this section" src="/images/arrow_right.gif" />';
	}
	
	
}


// ----------------------------------------------------------------------------------
// ------- SHOW / HIDE COLOR SELECTION  ---------------------------------------------
// ----------------------------------------------------------------------------------

function show_hide_color(color_area) 
{
	if( document.getElementById(color_area) )
	{
		hidden_status = document.getElementById(color_area).style.display;
		
		if( hidden_status == 'block' ) {
			document.getElementById(color_area).style.display = 'none';
		} else {
			document.getElementById(color_area).style.display = 'block';
		}
	}

}

// ----------------------------------------------------------------------------------
// ---- UPLOAD IMAGE VALIDATION FORM ------------------------------------------------
// ----------------------------------------------------------------------------------

var upload_popup;
function upload_validate(form,theSelectedAction,number_of_entries) 
{
	//alert('hello world from function: '+document.getElementById(form).selected_folder.item(0).value);  // WORKS FOR SAFARI!!!!!!!!!!!
	
	// Validate the form
	if( formValidation(form,number_of_entries) ) 
	{
		//open_upload_popup(); // If validated, show popup window explaining that it is downloading.
		
		var theAction = document.forms[0].action;
		document.forms[0].action = theAction+'&next='+theSelectedAction;
		
		document.forms[0].submit();
		//document.getElementById(form).submit();
		// HOW TO INTEGRATE TO SEND TO VIEW MORE???
		
		show_hide_color('flash_message');
		
		show_upload_screen();  
		
		//return true;
		
	} else {
		//return false;
	}
	
	
} // END Function "upload_validate"



// ----- // FORMVALIDATION code ----

var alertMessage = '';
var one_is_checked = '';
var bad_section_title = '';

function formValidation(form,number_of_entries) 
{
	var sections = document.getElementById(form).selected_folder;
	var section = document.getElementById(form);
	
	// ------------------------------------------------------
	// -- CHECK IF IMAGE FILES ARE GOING TO BE DOWNLOADED ---
	// ------------------------------------------------------
	
	
	// Some sections already exist (radio buttons exist)
	if(sections.value !== '')
	{
		// Check if the IMAGE TITLES are correct
		alertMessage += check_image_titles(number_of_entries); 
		
		// Check all "section" imputs
		var inputs = section.getElementsByTagName('input');
		
		// If text imput is EMPTY	
		//if(inputs[0].value == '' && inputs[0].type == 'selected_folder'){ 
		if( (inputs[0].value == '' && inputs[0].type == 'text') || inputs[0].type == 'radio' ){ 
		
			// Check all radio buttons
			for (var i = 0; i < inputs.length; ++i) {
				if(inputs[i].name == 'selected_folder')
				{
						// Check if radio buttons checked
						if( inputs[i].type == 'radio' && inputs[i].checked )	
						{
							one_is_checked += 'yes';
						}//FI
					
				} // If input is 'selected folder'
			} // FOR (inputs)

		}
		else // If new section added...
		{
			// Check if SECTION TITLE has BAD CHARACTERS
			//if( !isAlphaNumeric(inputs[0].value,'no_spaces') )
			//if( !isAlphaNumeric(inputs[0].value,'yes_spaces') )
			if( !isAlphaNumeric(inputs[0].value,'yes_spaces') )
			{
				bad_section_title += 'yes';
			} else {
				one_is_checked = 'yes';
			}
		}// FI (text field is empty)
	}
	else // No sections exist and the text field IS EMPTY (there are no radio buttons)
	{
		alertMessage += "You must name the SECTION you want to upload your image(s) to. \n";
	}// FI Sections already exist
	
	// Check variables
	if(bad_section_title == 'yes')
	{
		alertMessage += "Your SECTION NAME must only contain letters, numbers or spaces. \n (-no special characters-) \n";
	}
	else if(one_is_checked !== 'yes' || one_is_checked == '')
	{
		alertMessage += "You must create or select the SECTION you want to upload your image(s) to. \n";
	}
	
	
	// VALIDATE THIS FUNCTION
	if( alertMessage == '' )
	{
		return true;
	} else {
		alert(alertMessage);
		alertMessage = '';
		one_is_checked = '';
		bad_section_title = '';
		return false;
	}
	
}

// Check Image titles if they are correct
function check_image_titles(number_of_entries)
{
	file_to_upload = '';
	TitleMessage = '';
	
	for (var j = 1; j <= number_of_entries; j++) // For 3 entries
	{
		// IF FILE TITLE IS NONEXISTENT OR HAS BAD CHARACTERS, TELL USER TO CHANGE THAT
		filename = document.getElementById('filename_'+j).value;
		imageTitle = document.getElementById('title_'+j).value;
		
		// FILEname is not empty
		if(filename !== '')
		{
			file_to_upload = 'yes';
			
			// Make sure image has a title
			if(imageTitle == '')
			{
				TitleMessage += "Please give a TITLE to image "+j+".\n";
			}
			else // Make sure existing title doesn't have illegal characters
			{
				// Validate Alphanumeric
				if( !isAlphaNumeric(imageTitle,'yes_spaces_and_apostrophe') )
				{
					TitleMessage += "Image "+j+"'s title must only contain letters and spaces. \n (no special characters) \n";
				}
				
			} // FI imageTitle check
			
		}// FI Filename is not empty
		
	} // END FOR
	
	// If "file_to_upload" is not set (to "yes"), then there are no images to upload!!
	if(file_to_upload == '') {TitleMessage += "You must select IMAGE FILES to upload to your account!\n"; }
	
	// If there is a message because of an error
	if( TitleMessage )
	{
		return TitleMessage;
		
	} else {
		
		return '';
		
	}
}

// check to see if input is alphanumeric
function isAlphaNumeric(val,spaces)
{
	if(spaces == 'yes_spaces')
	{
		if (val.match(/^[a-zA-Z0-9 ]+$/))
		{
			return true;
		} else {
			return false;
		}
	} else if(spaces == 'yes_spaces_and_apostrophe')
	{
		if (val.match(/^[a-zA-Z0-9 '"]+$/))
		{
			return true;
		} else {
			return false;
		}
	} else {
		if (val.match(/^[a-zA-Z0-9]+$/))
		{
			return true;
		} else {
			return false;
		}
	}
}

// ----- // SHOW THE UPLOAD SCREEN -----------

function show_upload_screen() {
	
	document.getElementById('hide_upload_form').style.display = 'none';
	//document.getElementById('image_upload_form').style.display = 'none';
	document.getElementById('uploading_message').style.display = 'block';
	window.scroll(0,0);
	
}
function hide_upload_screen() {
	
	document.getElementById('hide_upload_form').style.display = 'block';
	//document.getElementById('image_upload_form').style.display = 'block';
	document.getElementById('uploading_message').style.display = 'none';
	
}

// ------------------------------------------------------
// -------- // VALIDATE CHANGE FOLDER NAME --------------
// ------------------------------------------------------

function validate_folder_name_change(form)
{
	
	var newname = document.getElementById(form).new_folder_name;
	var alertMessage = '';
	
	if(newname.value !== '')
	{
			// Check if SECTION TITLE has BAD CHARACTERS 
			if( !isAlphaNumeric(newname.value,'yes_spaces') )
			{
				alertMessage += "Your SECTION NAME must only contain letters, numbers or spaces. \n (- no special characters -) \n";
			}
	}
	else // Text field IS EMPTY 
	{
		alertMessage += "You must name the SECTION you want to upload your image(s) to. \n";
	}// FI Empty? 
	
	
	// VALIDATE THIS FUNCTION 
	if( alertMessage == '' )
	{
		theforms = (document.forms.length)-1;
		document.forms[theforms].submit();
	}
	else
	{
		alert(alertMessage);
		alertMessage = '';
		document.getElementById(form).new_folder_name.value = '';
	}

}


// --------------------------------------------------
// --------------------------------------------------
// --------------------------------------------------

function uncheck_radios (form,form_element) 
{
		//var section = form.selected_folder; 
		//var section = eval(form+"."+form_element); 
		
		// Uncheck radio buttons 
		//for (var i = 1; i < section.length; i++) { 
		for (var i = 1; i < form.elements.length; i++) { 
			form[i].checked = false;
			//section.item(i).checked = false;
		} // FOR
}

function clear_text_input (form,form_element) 
{
		var section = form.selected_folder; 

		// Set text input to NULL
		if(section.item(0).type == 'text')
		{
			section.item(0).value = '';
		}
}

// -----------------------------------------------------------------------------------
// -------- VIEW PRODUCT INFO POPUPS -----------------------------------------------
// -----------------------------------------------------------------------------------
function popup_product_info(info_id)
{
	url = '/product_info_popup.php?prod_info_id=' + info_id;
	popup_window = window.open(url,'Product_Info','width=300,height=250,left=40,top=40,resizable=1,scrollbars=1,toolbar=0,status=0,location=0');
	
	if (window.focus) 
	{
		popup_window.focus();
	}
}

