//FOR CONTACT FORM ON EVERY PAGE
function slideContact() {
	//GET THE CLASS NAME OF THE IMAGE TO DETERMINE IF WE SHOULD SLIDE OPEN OR CLOSE
	var action=document.getElementById('btnContact').getElementsByTagName('img')[0].getAttribute('class');
	var actionIE=document.getElementById('btnContact').getElementsByTagName('img')[0].getAttribute('classname');
	
	if(action=="showForm" || actionIE=="showForm") {
		showContact();
		document.getElementById('btnContact').innerHTML='<img src="/images/blank.gif" width="82" height="142" alt="Contact Us" title="Contact Us" class="closeForm" />';
	}
	
	else if(action=="closeForm" || actionIE=="closeForm") {
		hideContact();
		document.getElementById('btnContact').innerHTML='<img src="/images/blank.gif" width="82" height="142" alt="Contact Us" title="Contact Us" class="showForm" />';
	}
}
function showContact() {
	var position=-400;
	var timer = setInterval(function(){document.getElementById('contactForm').style.marginRight=(position+=20) + 'px'; if(position == 0){clearTimeout(timer);}}, 1);
}
function hideContact() {
	var position=0;
	var timer = setInterval(function(){document.getElementById('contactForm').style.marginRight=(position-=20) + 'px'; if(position == -400){clearTimeout(timer);}}, 1);
}
//CONTACT FORM ONFOCUS FOR TEXT INPUTS

//ONFOCUS CLEAR TEXT FOR FORM VALUES
function clearText (thefield) {
	if (thefield.defaultValue==thefield.value)
		thefield.value = ""
}
//ONBLUR TEXT FOR FORM VALUES
function addText (thefield,thetext) {
	if (""==thefield.value)
		thefield.value = thetext
}
function clearTextarea(thefield) {
	var message = document.getElementById(thefield).innerHTML;
	if(message=="Message"){
		document.getElementById(thefield).innerHTML="";
	}
}
function resetTextarea(thefield) {
	var message = document.getElementById(thefield).innerHTML;
	if(message==""){
		document.getElementById(thefield).innerHTML="Message";
	}
}

//FOR CONTACT FORM
function checkForm(theform) {
	var returnval=true //by default, allow form submission
	var checkit = document.getElementById('required_fields').value;
	var requireds=theform.required_fields.value.split(";") //split using blank space as delimiter
	for (i=0; i<requireds.length; i++){
		var fieldnamez = requireds[i];
		theform[fieldnamez].style.border = ''
		if(fieldnamez.length > 1){
			if (theform[fieldnamez].type=="text" || theform[fieldnamez].type=="textarea" || theform[fieldnamez].type=="file"){
				if (theform[fieldnamez].value=="" || theform[fieldnamez].value=="Your Name*"){ //if empty field
					alert("Please make sure all required fields are filled out.") //alert error message
					theform[fieldnamez].style.border = '1px solid #F15A22'
					theform[fieldnamez].focus();
					returnval=false //disallow form submission
					break //end loop. No need to continue.
				}
			}
			if (theform[fieldnamez].type=="select-one"){
				if (theform[fieldnamez].value==""){
					alert("Please make sure all required fields are filled out.") //alert error message
					theform[fieldnamez].style.border = '1px solid #F15A22'
					theform[fieldnamez].focus();
					returnval=false //disallow form submission
					break //end loop. No need to continue.
				}
			}
			if(theform[fieldnamez].name=="email"){
				var str = theform.email.value
				var at="@"
				var dot="."
				var comma=","
				var lat=str.indexOf(at)
				var lstr=str.length
				var ldot=str.indexOf(dot)
				if (str.indexOf(at)==-1){
					alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
				}
				if (str.indexOf(comma)>0){
					alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
				}
				if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
   			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(at,(lat+1))!=-1){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(dot,(lat+2))==-1){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
  			}
  			if (str.indexOf(" ")!=-1){
    			alert("Invalid E-mail Address")
					theform.email.style.border = '1px solid #F15A22'
					theform.email.focus();
					returnval=false //disallow form submission
					return false; 
					break //end loop. No need to continue.
				}
			}
		}
		theform[fieldnamez].style.border = ''
	}
	return returnval; 
}

//SLIDESHOW FUNCTIONS
var time;
function changeSelectClass(index) {
	clearTimeout(time);
	var clear=document.getElementById('toggler').getElementsByTagName('a');
	for(i=0;i<clear.length;i++){
		clear[i].setAttribute("className","select"); //FOR IE
		clear[i].setAttribute("class","select");
	}
	clear[index].setAttribute("className","selected"); //FOR IE
	clear[index].setAttribute("class","selected");
}
function switchImg(name) {
	var image=name;
	document.getElementById('custSlideshowInner').getElementsByTagName('img')[0].setAttribute("src",image);
}
function slideSelect(index,name) {
	changeSelectClass(index);
	switchImg(name);
}
function autoSlide(index,last,name) {
	changeSelectClass(index);
	switchImg(name);
	if(index==last){
		index=0;
	}
	else index++;
	var att=document.getElementById('toggler').getElementsByTagName('a')[index].getAttribute("onclick").toString();
	var next=att.split("'");
	var callFunction="autoSlide("+index+","+last+",'"+next[1]+"')";
	time=setTimeout(callFunction, 5000);
}

//FOR FOOTER HOVER ICONS
function hideIt(theID) {
	document.getElementById(theID).style.display="none";
}
function showIt(theID) {
	hideIt('wordpress-icon');
	hideIt('twitter-icon');
	hideIt('facebook-icon');
	hideIt('map-icon');
	document.getElementById(theID).style.display="block";
}
function showItAbout(theID) {
	hideIt('aboutJason-icon');
	hideIt('aboutMatt-icon');
	hideIt('aboutTegan-icon');
	document.getElementById(theID).style.display="block";
}

//PRELOADER FOR CSS HOVER ITEMS
function mm_preloadimages() { //v3.0
	var d=document; 
	if(d.images)
	{ 
		if(!d.mm_p) 
		{
			d.mm_p=new Array();
		}
		var i,j=d.mm_p.length,a=mm_preloadimages.arguments;
		for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0)
		{
			d.mm_p[j]=new Image;
			d.mm_p[j++].src=a[i];
		}
	}
}
function loadGlobalImages() {
	mm_preloadimages('/images/menu-home-hover.png','/images/menu-about-us-hover.png','/images/menu-portfolio-hover.png','/images/menu-services-hover.png','/images/menu-printing-hover.png','/images/popup-explanation.png','/images/jsMenuContactHover.gif','/images/btnContactSubmitHover.png','/images/close-window.png','/images/icon-wordpress-hover.png','/images/icon-twitter-hover.png','/images/icon-map-hover.png','/images/icon-fluxl-hover.png','/images/icon-facebook-hover.png');
}
function loadPortfolioBtn() {
	mm_preloadimages('/images/btnViewPortfolio-hover.png');
}

//FUNCTIONS FOR FLOATING PRINT MENU
function showMenu(ID){
	ID="sub-"+ID;
	document.getElementById(ID).style.display="block";
}
function hideMenu(){
	var divs=document.getElementById('printMenu').getElementsByTagName('div');
	for(i=0;i<divs.length;i++){
		divs[i].style.display="none";
	}
}

//AJAX FOR PRINTING TRANSACTION HISTORY
function showInvoice(str){
	if (str==""){
		document.getElementById("txtHint").innerHTML="";
		return;
	}
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","getuser.php?q="+str,true);
	xmlhttp.send();
}

//FINDS PAGE NAME OF CURRENT PAGE
function pageName() {
	var URL = window.location.toString();
	var breakdown = URL.split("/");
	return (breakdown[(breakdown.length-1)]);
}

//CUSTOM WINDOW LOADING FOR INDIVIDUAL PAGES
function init() {
	var current = pageName();
	/*if (current == "index.php" || current == "") {
		loadHomeLogoImages();
	}*/
	if (current == "portfolio.php") {
		loadPortfolioBtn();
	}
	loadGlobalImages();
}
window.onload=init;