/*Opens and closes subnav lists*/
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		} );
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function toggle(list) {
	var listElement=document.getElementById(list);  
	if (getStyle(listElement, "display")=="block"){
		listElement.style.display="none";
	} else {
		listElement.style.display="block";
	}
} 
/*light up menu*/
function setPage(url) { 
for ( i = 0; i < document.links.length; i += 1) {
	var a = document.links[i]

	if (a.href == "http://206.67.52.230/" + url) {
		a.setAttribute("class","current")
	}
}
}

// work in jQuery 1.3.x

jQuery.noConflict()

var featuredcontentglider={
	csszindex: 100,
	ajaxloadingmsg: '<b>Fetching Content. Please wait...</b>',
	glide:function(config, showpage, isprev){
		var selected=parseInt(showpage)
		if (selected>=config.$contentdivs.length){ //if no content exists at this index position
			alert("No content exists at page "+(selected+1)+"! Loading 1st page instead.")
			selected=0
		}
		var $target=config.$contentdivs.eq(selected)
		//Test for toggler not being initialized yet, or user clicks on the currently selected page):
		if (config.$togglerdiv.attr('lastselected')==null || parseInt(config.$togglerdiv.attr('lastselected'))!=selected){
			var $selectedlink=config.$toc.eq(selected)
			config.$next.attr('loadpage', (selected<config.$contentdivs.length-1)? selected+1+'pg' : 0+'pg')
			config.$prev.attr('loadpage', (selected==0)? config.$contentdivs.length-1+'pg' : selected-1+'pg')
			var startpoint=(isprev=="previous")? -config.startpoint : config.startpoint
			$target.css(config.leftortop, startpoint).css("zIndex", this.csszindex++) //hide content so it's just out of view before animating it
			var endpoint=(config.leftortop=="left")? {left:0} : {top:0} //animate it into view
			$target.animate(endpoint, config.speed)
			config.$toc.removeClass('selected')
			$selectedlink.addClass('selected')
			config.$togglerdiv.attr('lastselected', selected+'pg')
		}
	},

	getremotecontent:function($, config){
		config.$glider.html(this.ajaxloadingmsg)
		$.ajax({
			url: config.remotecontent,
			error:function(ajaxrequest){
				config.$glider.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
			},
			success:function(content){
				config.$glider.html(content)
				featuredcontentglider.setuptoggler($, config)
			}
		})
	},

	aligncontents:function($, config){
		config.$contentdivs=$("#"+config.gliderid+" ."+config.contentclass)
		config.$contentdivs.css(config.leftortop, config.startpoint).css({height: config.$glider.height(), visibility: 'visible'}) //position content divs so they're out of view:
	},

	setuptoggler:function($, config){
		this.aligncontents($, config)
		config.$togglerdiv.hide()
		config.$toc.each(function(index){
				$(this).attr('pagenumber', index+'pg')
				if (index > (config.$contentdivs.length-1))
					$(this).css({display: 'none'}) //hide redundant "toc" links
		})
		var $nextandprev=$("#"+config.togglerid+" .next, #"+config.togglerid+" .prev")
		$nextandprev.click(function(event){ //Assign click behavior to 'next' and 'prev' links
			featuredcontentglider.glide(config, this.getAttribute('loadpage'), this.getAttribute('buttontype'))
			event.preventDefault() //cancel default link action
		})
		config.$toc.click(function(event){ //Assign click behavior to 'toc' links
			featuredcontentglider.glide(config, this.getAttribute('pagenumber'))
			event.preventDefault()
		})
		config.$togglerdiv.fadeIn(1000, function(){
			featuredcontentglider.glide(config, config.selected)
			if (config.autorotate==true){ //auto rotate contents?
				config.stepcount=0 //set steps taken
				config.totalsteps=config.$contentdivs.length*config.autorotateconfig[1] //Total steps limit: num of contents x num of user specified cycles)
				featuredcontentglider.autorotate(config)
			}
		})
		config.$togglerdiv.click(function(){
			featuredcontentglider.cancelautorotate(config.togglerid)
		})
	},

	autorotate:function(config){
		var rotatespeed=config.speed+config.autorotateconfig[0]
		window[config.togglerid+"timer"]=setInterval(function(){
			if (config.totalsteps>0 && config.stepcount>=config.totalsteps){
				clearInterval(window[config.togglerid+"timer"])
			}
			else{
				config.$next.triggerHandler('click')
				config.stepcount++
			}
		}, rotatespeed)
	},

	cancelautorotate:function(togglerid){
		if (window[togglerid+"timer"])
			clearInterval(window[togglerid+"timer"])
	},

	getCookie:function(Name){ 
		var re=new RegExp(Name+"=[^;]+", "i") //construct RE to search for target name/value pair
		if (document.cookie.match(re)) //if cookie found
			return document.cookie.match(re)[0].split("=")[1] //return its value
		return null
	},

	setCookie:function(name, value){
		document.cookie = name+"="+value
	},

	init:function(config){
		jQuery(document).ready(function($){
			config.$glider=$("#"+config.gliderid)
			config.$togglerdiv=$("#"+config.togglerid)
			config.$toc=config.$togglerdiv.find('.toc')
			config.$next=config.$togglerdiv.find('.next')
			config.$prev=config.$togglerdiv.find('.prev')
			config.$prev.attr('buttontype', 'previous')
			var selected=(config.persiststate)? featuredcontentglider.getCookie(config.gliderid) : config.selected
			config.selected=(isNaN(parseInt(selected))) ? config.selected : selected //test for cookie value containing null (1st page load) or "undefined" string	
			config.leftortop=(/up/i.test(config.direction))? "top" : "left" //set which CSS property to manipulate based on "direction"
			config.heightorwidth=(/up/i.test(config.direction))? config.$glider.height() : config.$glider.width() //Get glider height or width based on "direction"
			config.startpoint=(/^(left|up)/i.test(config.direction))? -config.heightorwidth : config.heightorwidth //set initial position of contents based on "direction"
			if (typeof config.remotecontent!="undefined" && config.remotecontent.length>0)
				featuredcontentglider.getremotecontent($, config)
			else
				featuredcontentglider.setuptoggler($, config)
			$(window).bind('unload', function(){ //clean up and persist
				config.$togglerdiv.unbind('click')
				config.$toc.unbind('click')
				config.$next.unbind('click')
				config.$prev.unbind('click')
				if (config.persiststate)
					featuredcontentglider.setCookie(config.gliderid, config.$togglerdiv.attr('lastselected'))
				config=null
				
			})
		})
	}
}

/* validation */
// JavaScript Document

function SymError()
{return true;}
window.onerror = SymError;

function SymError()
{
  return true;
}

//window.onerror = SymError;

if(window.location.protocol == "https:")
	{
	if(window.location.pathname != "/onecart2.php" && window.location.pathname != "/onecart1.php" && window.location.pathname != "/onecart.php" && window.location.pathname != "/onecommit.php" && window.location.pathname != "/oneprocess.php" && window.location.pathname != "/onereceipt2.php")
		{window.location = "http://www.precisioncamera.com" + window.location.pathname + window.location.search;}
	}
if(window.location.hostname == "precisioncamera.com")
	{window.location = window.location.protocol + "//www.precisioncamera.com" + window.location.pathname + window.location.search;}

function chex()
{

//for(i=0; i<document.form1.elements.length; i++)
	if (document.form1.ref.value==''||document.form1.ref.value=='enter your ref#')
		
		
	
	{
	alert('You must enter your ref# to proceed');
	document.form1.ref.focus();
	document.form1.ref.style.backgroundColor="#FF9900";
	return false;
	
	}
}
function chex2()
{
if (document.form2.q.value==''||document.form2.q.value=='enter keyword')
		
		
	
	{
	alert('You must enter a search keyword');
	document.form2.q.focus();
	document.form2.q.style.backgroundColor="#FF9900";
	return false;
	
	}
}
function chex3()
{
if (document.form3.newsletter.value=='' ||document.form3.newsletter.value=='email address')
		
		
	
	{
	alert('You must enter your email address to proceed');
	document.form3.newsletter.focus();
	document.form3.newsletter.style.backgroundColor="#FF9900";
	return false;
	
	}

		
}	

function chex4()
{
if (document.form4.makesel.value=='0')
	{
	alert('You must select a make to proceed');
	document.form4.makesel.style.backgroundColor="#FF9900";
	return false;
	}
whelement = document.getElementById("modelseldd"+document.form4.makesel.value);
if (whelement.value=='0')
	{
	alert('You must select a model number to proceed');
	whelement.style.backgroundColor="#FF9900";
	return false;
	}
document.form4.makesel.selectedIndex = 0;
document.form4.cameraID.value = whelement.value;
//return false;
}	

function chex5()
{
if(document.form5.msgComments.value == "")	
	{
	document.form5.msgComments.focus();
	alert("Please enter your message.");
	return false;
	}
if(document.form5.msgName.value == "")	
	{
	document.form5.msgName.focus();
	alert("Please enter your name.");
	return false;
	}
if(document.form5.msgTelephone.value == "")	
	{
	document.form5.msgTelephone.focus();
	alert("Please enter your telephone number.");
	return false;
	}
if(document.form5.msgEmail.value == "")	
	{
	document.form5.msgEmail.focus();
	alert("Please enter your email address.");
	return false;
	}

return true;
}
function chexemail()
{
if (document.formemail.email.value=='')
	{
	alert('You must enter a valid email address to proceed');
	document.formemail.email.focus();
	document.formemail.email.style.backgroundColor="#FF9900";
	return false;
	}
	
	if (echeck(document.formemail.email.value)==false){
		document.formemail.email.value=""
		document.formemail.email.focus()
  		document.formemail.email.style.backgroundColor="#FF9900";
		return false
	}

}	
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address \r Please enter a valid email address \re.g. you@domain.com")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address \r Please enter a valid email address \re.g. you@domain.com")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		     alert("Invalid E-mail Address \r Please enter a valid email address \re.g. you@domain.com")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		     alert("Invalid E-mail Address \r Please enter a valid email address \re.g. you@domain.com")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			 alert("Invalid E-mail Address \r Please enter a valid email address \re.g. you@domain.com")		
			 return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
 			alert("Invalid E-mail Address \r Please enter a valid email address \re.g. you@domain.com")
			return false
		 }
		 
		
		 if (str.indexOf(" ")!=-1){
		 alert("Invalid E-mail Address \r Please enter a valid email address \re.g. you@domain.com")		
		 return false
		 }

 		 return true					
	}