// JavaScript Document

// set the png fix to load with document
$(document).ready(function(){ 
	//$(document).pngFix(); 
	fadeImages();
}); 




// function to fade buttons
function fadeImages() {
	$('.fadeimg')
		.css({ opacity: 1.0 })
		.mouseover(function() {				
			$(this).stop().animate(
				{ opacity: 0.6 }
			, 200); // this is the duration of the fade on mouseover in milliseconds
		})
		.mouseout(function() {
			$(this).stop().animate(
				{ opacity: 1.0 }
			, 1000); // this is the duration of the fade on mouseout in milliseconds
		});
}





// function to fade any element by it's class name, id etc
function showHideElement($element) {
 			   
     $($element).toggle();  
	 return false();

}


// function to fade any element by it's class name, id etc
function hideElement($element) {
 			   
     $($element).hide();  
	 return false();
}

// function to fade any element by it's class name, id etc
function showElement($element) {
 			   
     $($element).show();  
	 return false();
}




// function to fade any element -- REQUIRES JQUERY 1.6.2
function fadeElement($element) {
 			   
     $($element).fadeToggle();  

}



// function to make page for object to print in
function makepage(src)
{
  // We break the closing script tag in half to prevent
  // the HTML parser from seeing it as a part of
  // the *main* page.

  return "<html>\n" +
    "<head>\n" +
    "<title>Print Object</title>\n" +
    "<script>\n" +
    "function step1() {\n" +
    "  setTimeout('step2()', 10);\n" +
    "}\n" +
    "function step2() {\n" +
    "  window.print();\n" +
    "  window.close();\n" +
    "}\n" +
    "</scr" + "ipt>\n" +
    "</head>\n" +
    "<body onLoad='step1()'>\n" +
    "<img src='" + src + "'/>\n" +
    "</body>\n" +
    "</html>\n";
}


function printme(src)
{

  link = "about:blank";
  var pw = window.open(link, "_new");
  pw.document.open();
  pw.document.write(makepage(src));
  pw.document.close();
}



// Custom Javascript Using jQuery





/* ************************************************************
/* FUNCTION TO CLEAR TEXT FIELD ON FIRST CLICK 
/* add this to the input:   onfocus="firstClear(this)"
/* ************************************************************ */

function firstClear(obj)
{
  if(obj.clearThis == undefined)
  {
    obj.value = '';
    obj.clearThis = true;
  }
}



/* ************************************************************
/* VALIDATION CODE
/* ************************************************************ */

function validateForm(contactfrm) {

// validate mandatory fields for blank entries

// Get required fields from hidden form field in contact form then convert to array for validation

var reqfields = contactfrm.required.value;
var reqarray = reqfields.split(','); 


var i = 0;
var fieldvalue = "";
var fieldname = "";
var totalfields = reqarray.length;



for(i=0;i<=totalfields;i++) {


	// get the current field name
	fieldname = reqarray[i];

	fieldvalue = document.getElementById(fieldname).value; // get field value
	
	if (fieldvalue == "") {

		//alert(fieldname + " = " + fieldvalue);

		// now let's show the red border for fields not filled in
		document.getElementById(fieldname).style.borderColor='red';
	
		
		
		if (i == (totalfields- 1)) {
				
					alert("Please ensure all mandatory fields marked with an asterisk(*) are complete and then send again!"); 
					return false; 
	
			
					}
		

		}


	}


}



// script for hidden form field  
function showOther() {

	var selectedItem  		= document.contactfrm.leadsource.selectedIndex;
	var selectedItemValue   = document.contactfrm.leadsource.options[selectedItem].value;

	if (selectedItemValue == "Other") {

		//document.getElementById("othersource").style.visibility = "visible";
		//document.getElementById("hiddenspan").style.visibility = "visible";	
		document.getElementById("othersource").style.display = "block";	
		document.getElementById("hiddenspan").style.display = "block";	
		}
		
	else {

		//document.getElementById("othersource").style.visibility = "hidden";
		//document.getElementById("hiddenspan").style.visibility = "hidden";	
		document.getElementById("hiddenspan").style.display = "none";		
		document.getElementById("othersource").style.display = "none";
		 }	
	
	}
	
	
	
	
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }		



