// mimicks li:hover for IE, which doesn't support it
var hoverDebug = Array();
sfHover = function() {
	try
	{

		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
	catch(err)
	{
		hoverDebug[0] = err;
	}
}
//if (window.attachEvent) window.attachEvent("onload", sfHover);

// for thumbnail view toggling
function vidExpand() {
	if(document.getElementById && document.createTextNode) {
		if(document.getElementById('vidclips')) {
			document.getElementById('vidclips').className='full';
			document.getElementById('linkExpand').className='hide';
			document.getElementById('linkCollapse').className='';
			document.getElementById('vid-toggle-bottom').className='vid-toggle';
			if (typeof tooltip.deactivate == "function") tooltip.deactivate(document.getElementById("vidclips"));
		}
	}
}

function vidCollapse() {
	if(document.getElementById && document.createTextNode) {
		if(document.getElementById('vidclips')) {
			document.getElementById('vidclips').className='simple';
			document.getElementById('linkCollapse').className='hide';
			document.getElementById('linkExpand').className='';
			document.getElementById('vid-toggle-bottom').className='hide';
			if (typeof tooltip.activate == "function") tooltip.activate(document.getElementById("vidclips"));
		}
	}
}


// for popups
function popupOpen(a){
	// close all popups first - so two cant be open at the same time
	var all = a.parentNode.parentNode.getElementsByTagName("div");
	for (var i=0; i<all.length; i++){
		if (all[i].className.indexOf(" open") > 0){
			all[i].className = all[i].className.substring(0, all[i].className.indexOf(" open"));
		}
	}
	
	var siblingDivs = a.parentNode.getElementsByTagName("div");
	for (var i=0; i<siblingDivs.length; i++){
		if (siblingDivs[i].className == "popup"){
			siblingDivs[i].className = siblingDivs[i].className + " open";
		}
	}
}


function popupClose(a){
	var popup = a.parentNode.parentNode;
	var popupClass = popup.className;
	popup.className = popupClass.substring(0, popupClass.indexOf(" open"));
}

function sort_project_results(sortby, sortdir) {
  document.psf.sort.value = sortby;
  document.psf.sortdir.value = sortdir;
  document.psf.submit();
}

function checkprojectform() {
  if (document.projectForm.project_name.value == "") {
    alert("Please enter this project's name.");
    document.projectForm.project_name.focus();
    return false;
  }
/*
  else if (document.projectForm.contractor.value == "") {
    alert("Please enter this project's contractor.");
    document.projectForm.contractor.focus();
    return false;
  }
  else if (document.projectForm.architect.value == "") {
    alert("Please enter this project's architect.");
    document.projectForm.architect.focus();
    return false;
  }
  else if (document.projectForm.developer.value == "") {
    alert("Please enter this project's developer.");
    document.projectForm.developer.focus();
    return false;
  }
  else if (document.projectForm.city.value == "") {
    alert("Please enter this project's city.");
    document.projectForm.city.focus();
    return false;
  }
*/
  else if (document.projectForm.state.value == "") {
    alert("Please select this project's state.");
    document.projectForm.state.focus();
    return false;
  }
  else if (document.projectForm.size.value == "") {
    alert("Please enter this project's size (in square feet).");
    document.projectForm.size.focus();
    return false;
  }
  else if (document.projectForm.turnaround_time.value == "") {
    alert("Please enter this project's turnaround time.");
    document.projectForm.turnaround_time.focus();
    return false;
  }
  else if (isNaN(document.projectForm.turnaround_time.value)) {
    alert("Please enter this project's turnaround time (in days) as an integer.");
    document.projectForm.turnaround_time.value = "";
    document.projectForm.turnaround_time.focus();
    return false;
  }
  else if ((document.projectForm.date_completed_m.value == "") || (document.projectForm.date_completed_y.value == "")) {
    alert("Please enter this project's full completion date.");
    if (document.projectForm.date_completed_m.value == "") {
      document.projectForm.date_completed_m.focus();
    }
    else {
      document.projectForm.date_completed_y.focus();
    }
    return false;
  }
  else if (isNaN(document.projectForm.date_completed_m.value)) {
    alert("Please enter this project's completion date (month) as an integer.");
    document.projectForm.date_completed_m.value = "";
    document.projectForm.date_completed_m.focus();
    return false;
  }
  else if (isNaN(document.projectForm.date_completed_y.value)) {
    alert("Please enter this project's completion date (year) as an integer.");
    document.projectForm.date_completed_y.value = "";
    document.projectForm.date_completed_y.focus();
    return false;
  }
  else {
    document.projectForm.fsubmit.value = "processing...please wait";
    document.projectForm.fsubmit.disabled = true;
    if ($('imgbtn')) {
      $('imgbtn').disabled = true;
    }
    return true;
  }
}

function delete_project(pid) {
  var kk = confirm("Are you sure you want to delete this project?");
  if (kk == true) {
    window.location = "?action=delete&pid="+pid;
  }
}

function edit_project(pid) {
  window.location = "project_form.php?pid="+pid;
}

function checkJobForm() {
  if (document.jobForm.name.value == "") {
    alert("Please enter a job name.");
    document.jobForm.name.focus();
    return false;
  }
  else if (document.jobForm.description.value == "") {
    alert("Please enter a job description.");
    document.jobForm.description.focus();
    return false;
  }
  else {
    document.jobForm.submit.value = "saving...";
    document.jobForm.submit.disabled = true;
    return true;
  }

}

function checkNameform(tt) {
  if (document.nameForm.name.value == "") {
    alert("Please enter a "+tt+" name.");
    document.nameForm.name.focus();
    return false;
  }
  else {
    document.nameForm.submit.value = "saving...";
    document.nameForm.submit.disabled = true;
    return true;
  }
}

function checkProductForm() {
  if (document.ProductForm.name.value == "") {
    alert("Please enter a product name.");
    document.ProductForm.name.focus();
    return false;
  }
  else {
    document.ProductForm.submit.value = "saving...";
    document.ProductForm.submit.disabled = true;
    if ($('imgbtn')) {
      $('imgbtn').disabled = true;
    }
    return true;
  }
}

function edit_product(id) {
  window.location = "?pid="+id;
}

function delete_product(id) {
  var kk = confirm("Are you sure you want to delete this product?");
  if (kk == true) {
    window.location = "?action=delete&pid="+id;
  }
}

function edit_name(id) {
  window.location = "?action=edit&id="+id;
}

function delete_name(id,tt) {
  var kk = confirm("Are you sure you want to delete this "+tt+"?");
  if (kk == true) {
    window.location = "?action=delete&id="+id;
  }
}

function edit_client(id) {
  window.location = "?action=edit&id="+id;
}

function delete_client(id) {
  var kk = confirm("Are you sure you want to delete this client?");
  if (kk == true) {
    window.location = "?action=delete&id="+id;
  }
}

function checkUploadForm() {
  if (document.imgupload.image.value == "") {
    alert("Please select a JPG to upload.");
    document.imgupload.image.focus();
    return false;
  }
  else {
    if ($('imgbtn')) {
      $('imgbtn').value = "uploading...please wait";
      $('imgbtn').disabled = true;
    }
  }
}

function toggleUpImg() {
  if ($('uploadimage').style.display == 'none') {
    $('uploadimage_toggle').innerHTML = "<img src=\"../images/icon_close.png\" border=\"0\" /> Cancel";
    $('uploadimage').style.display = '';
    $('uploadimage').effect('height', { duration: 500, transition: Fx.Transitions.sineIn }).start(0,100);
  }
  else {
    $('uploadimage_toggle').innerHTML = "<img src=\"../images/icon_add.png\" border=\"0\" /> Upload Image";
    $('uploadimage').effect('height', { duration: 500, transition: Fx.Transitions.sineIn }).start(100,0);
    setTimeout("$('uploadimage').style.display='none';",500);
  }
}

function img_save(id,pid) {
  window.location = "?action=save_caption&imgid="+id+"&pid="+pid+"&caption="+URLEncode($('caption'+id).value);
}

function img_delete(id,pid) {
  var kk = confirm("Are you sure you want to delete this image?");
  if (kk == true) {
    window.location = "?action=delimg&imgid="+id+"&pid="+pid;
  }
}

function URLEncode(str) {
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var plaintext = str;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";	// x-www-urlencoded, rather than 
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert("Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted.");
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;
}

function checkContactForm() {
  if (document.contactForm.name.value == "") {
    alert("Please enter your name.");
    document.contactForm.name.focus();
    return false;
  }
  else if (document.contactForm.email.value.indexOf('@') <= 0) {
    alert("Please enter a valid email address.");
    document.contactForm.email.focus();
    document.contactForm.email.select();
    return false;
  }
  else if (document.contactForm.subject.value == "") {
    alert("Please enter a subject.");
    document.contactForm.subject.focus();
    return false;
  }
  else if (document.contactForm.message.value == "") {
    alert("Please enter a message.");
    document.contactForm.message.focus();
    return false;
  }
  else if (document.contactForm.code.value == "") {
    alert("Please enter the validation code.");
    document.contactForm.code.focus();
    return false;
  }
  else {
    document.contactForm.submit.value = "Sending Mail...Please Wait";
    document.contactForm.submit.disabled = true;
    return true;
  }
}

function checkQuoteForm() {
  if (document.quoteForm.name.value == "") {
    alert("Please enter your name.");
    document.quoteForm.name.focus();
    return false;
  }
  else if (document.quoteForm.company.value == "") {
    alert("Please enter your company name.");
    document.quoteForm.company.focus();
    return false;
  }
  else if (document.quoteForm.phone.value == "") {
    alert("Please enter your phone number.");
    document.quoteForm.phone.focus();
    return false;
  }
  else if (document.quoteForm.email.value.indexOf('@') <= 0) {
    alert("Please enter a valid email address.");
    document.quoteForm.email.focus();
    document.quoteForm.email.select();
    return false;
  }
  else if (document.quoteForm.location.value == "") {
    alert("Please enter your project location.");
    document.quoteForm.location.focus();
    return false;
  }
  else if (document.quoteForm.details.value == "") {
    alert("Please enter a details.");
    document.quoteForm.details.focus();
    return false;
  }
  else {
    document.quoteForm.submit.value = "Sending Quote Request...Please Wait";
    document.quoteForm.submit.disabled = true;
    return true;
  }
}

function chk_featured_proudct(fid) {
  dim_screen();
  setTimeout("window.location='?mode=featured&fid="+fid+"'",1000);
}

function chk_featured_project(fid) {
  dim_screen();
  document.psf.mode.value = "featured";
  document.psf.fid.value = fid;
  setTimeout("document.psf.submit()",1000);
}

function sel_feat_projects(id) {
  window.location = "?expand="+id;
}
function chk_feat_cat_proj(expid,id) {
  dim_screen();
  setTimeout("window.location='?expand="+expid+"&featid="+id+"'",1000);
}

function dim_screen() {
  $('screendim').style.display = '';
  $('screendim').effect('opacity', { duration: 500, transition: Fx.Transitions.sineIn }).start(0,.8);
}