/* ***************************************
Javascript developed by input 9 interactive
Link: http://www.input9.com

(c) 2008

Author: Bill Hanff

Last Modified: November 4, 2008
*************************************** */


// Global variables
var isFirefox;
var isIE;
var isSafari;
var isMobile;

var currentSentItem  = 0;
var cartArray = Array();


function init(){
	sniff();
	externalLinks();
	}	
	
function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("mobile") != -1) return 'mobile';
	if (agt.indexOf("palm") != -1) return 'mobile';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}

function sniff() {
		
			var ff = "Netscape";
			var ie = "Microsoft Internet Explorer";
			var tmpVend = navigator.vendor;

			isFirefox = ( navigator.appName == ff );
			isIE = (navigator.appName == ie ); 
			if(tmpVend){
				isSafari = (tmpVend.indexOf("Apple")==-1) ? false : true;
			} else {
				isSafari = false;
			}
			isMobile = (whichBrs() == 'mobile') ? true : false;
			
		}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.99:opacity; 
	obj.style.filter = "alpha(opacity:"+opacity+")"; // IE/Win
	obj.style.KHTMLOpacity = opacity/100; // Safari<1.2, Konqueror
	obj.style.MozOpacity = opacity/100; // Older Mozilla and Firefox
	obj.style.opacity = opacity/100;	// Safari 1.2, newer Firefox and Mozilla, CSS3
	}
	
	
function showhide(divID){
		var node = document.getElementById(divID);
		if(node.className == "hide"){
			node.className = "show";
			if(!isIE){ fadeIn(divID, 0); }
		} else {
			node.className = "hide";
		}
	}

	
function fadeIn(objID, opacity) {
	if (document.getElementById) {//&& !isIE
		obj = document.getElementById(objID);
		if (opacity < 100) {
			opacity += 10;
			setOpacity(obj, opacity);
			window.setTimeout("fadeIn('"+objID+"',"+opacity+")", 50);
			}
		}
	}

function externalLinks() {
			var links = document.getElementsByTagName('a');// Fetch all the anchor elements in the document.
			for (var i = links.length; i != 0; i--) { // Loop through the a elements
				var a = links[i-1]; // Pull out the element for this iteration.
				if (!a.href) continue; // If the element doesn't have an href, skip it.
				if (a.className && a.className.indexOf('external') != -1){
						a.target = '_blank';
				}
			}
		}

function validateEmail(email) {
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	for (var i=0; i < email.length; i++) {
		var letter = email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
		continue;
		parsed = false;
		break;
	}
	return parsed;
}

function setupNav() {
	for(i=0; i<NavElements.length; i++){
			var node = document.getElementById(NavElements[i][0]);
			/*
			if(NavElements[i][0]==curnavpage){
				NavElements[i][1] = -12;
				NavElements[i][2] = -12;
			}
			*/
			//node.curY = (NavElements[i]==curnavpage) ? -12 : -20;
			if(!isIE){
				node.addEventListener( "mouseover", navroll, false);
				node.addEventListener( "mouseout", navrollOut, false);
			} else {
				node.onmouseover = navroll;
				node.onmouseout = navrollOut;
			}
		}
	}


function previousImg() {
	if( document.getElementById('bck').className != "inactive"){
		document.getElementById('bck').className = "inactive";
		document.getElementById('fwd').className = "inactive";
		imgcurr--;
		document.getElementById('uturnImgImg').innerHTML = '<img src="assets_img/uturn/'+imgcurr+'.jpg" width="550" height="421" id="uturnImgImgImg" class="invisible" onload="setUturnFadeIn()" alt="" />';
		}
	}
	
function advanceImg() {
	if( document.getElementById('fwd').className != "inactive"){
		document.getElementById('bck').className = "inactive";
		document.getElementById('fwd').className = "inactive";
		imgcurr++;
		document.getElementById('uturnImgImg').innerHTML = '<img src="assets_img/uturn/'+imgcurr+'.jpg" width="550" height="421" id="uturnImgImgImg" class="invisible" onload="setUturnFadeIn()" alt="" />';
		}
	}

function setUturnFadeIn(){
	document.getElementById('uturnImgImgImg').className = "visible";
	fadeIn('uturnImgImgImg', 0);
	document.getElementById('imgcaption').innerHTML = "Image "+imgcurr+" of "+imgcount;
	
	if( imgcurr>1 ){ document.getElementById('bck').className = "active"; }
	if( imgcurr<imgcount ){ document.getElementById('fwd').className = "active"; }
	}

function emailSub(){
		document.getElementById('emailsub').className = "hide";
		document.getElementById('emailSendResponse').className = "hide";
		document.getElementById('emailSending').className = "show";
		fadeIn('emailSending', 0);
		
		var emailName = document.getElementById('emailName').value;
		var emailMail = document.getElementById('emailMail').value;
	
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = processSubscriptionSubmit;
			req.open("GET", 'emailsubscribe.php?emailName='+emailName+'&emailMail='+emailMail);
			req.send(null);	
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				req.onreadystatechange = processSubscriptionSubmit;
				req.open("GET", 'emailsubscribe.php?emailName='+emailName+'&emailMail='+emailMail);
				req.send();
			}
		}
	}

function processSubscriptionSubmit(){
		if (req.readyState == 4) {
			if (req.status == 200) {
				document.getElementById('emailSending').className = "hide";
				document.getElementById('emailSendResponse').className = "show";
				if(req.responseText=="go"){
					document.getElementById('emailSendResponse').innerHTML = "<br /><br />Your email address has been added.<br /><br />Thank You!";
					fadeIn('emailSendResponse', 0);
					document.getElementById('subscriptionEmail').className = "hide";
				} else {
					document.getElementById('emailsub').className = "button";
					fadeIn('emailsub', 0);
					document.getElementById('emailSendResponse').innerHTML = req.responseText;
					fadeIn('emailSendResponse', 0);
				}
			}
		}
	}



function emailTafSub(){
		//emailtaf.php?from=<?=$pageid?>" onsubmit="(); return false;" method="post" id="tafEmail">
		document.getElementById('tafsub').className = "hide";
		document.getElementById('tafSendSendResponse').className = "hide";
		document.getElementById('tafSendSending').className = "show";
		fadeIn('tafSendSending', 0);
		
		var emailYName = document.getElementById('emailYName').value;
		var emailYMail = document.getElementById('emailYMail').value;
		
		var emailFName = document.getElementById('emailFName').value;
		var emailFMail = document.getElementById('emailFMail').value;
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = processTafSubmit;
			req.open("GET", 'emailtaf.php?yn='+emailYName+'&ym='+emailYMail+'&fn='+emailFName+'&fm='+emailFMail);
			req.send(null);	
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				req.onreadystatechange = processTafSubmit;
				req.open("GET", 'emailtaf.php?yn='+emailYName+'&ym='+emailYMail+'&fn='+emailFName+'&fm='+emailFMail);
			req.send();
			}
		}
	}

				
	
function processTafSubmit(){
		if (req.readyState == 4) {
			if (req.status == 200) {
				document.getElementById('tafSendSending').className = "hide";
				document.getElementById('tafSendSendResponse').className = "show";
				if(req.responseText=="go"){
					document.getElementById('tafEmail').className = "hide";
					document.getElementById('tafSendSendResponse').innerHTML = "<br /><br />Your friend has been sent an email.<br /><br />Thank You!";
					fadeIn('tafSendSendResponse', 0);
				} else {
					document.getElementById('tafsub').className = "button";
					fadeIn('tafsub', 0);
					document.getElementById('tafSendSendResponse').innerHTML = req.responseText;
					fadeIn('tafSendSendResponse', 0);
				}
			}
		}
	}


function resetEmail(){
	document.getElementById('emailSending').className = "hide";
	document.getElementById('emailSendResponse').className = "hide";
	document.getElementById('subscriptionEmail').className = "show";
	fadeIn('subscriptionEmail', 0);
}

function navroll(){
	for(i=0; i<NavElements.length; i++){
		if(this.id == NavElements[i][0]){
			NavElements[i][2] = -12;
		} else {
			NavElements[i][2] = -20;
		}
	}
}
	
function navrollOut(){
	for(i=0; i<NavElements.length; i++){ NavElements[i][2] = -20; }
}

function checkMail(){
	var errorMSG = "";
	if(document.mailform.name.value==""){
		errorMSG += "- No name was entered\n";
	} 
	
	if(document.mailform.company.value==""){
		errorMSG += "- No company was entered\n";
	} 
	
	if(document.mailform.phone.value==""){
		errorMSG += "- No phone was entered\n";
	} 
	
	if(document.mailform.email.value==""){
		errorMSG += "- No email was submitted\n";
	} else if( validateEmail(document.mailform.email.value)==false ){
		errorMSG += "- Email address was invalid\n";
	} 
	
	if(errorMSG!=''){
		alert("The following needs to be  remedied in order to submit: \n\n" + errorMSG);
	} else {
		setupPost();
	}
}

function setupPost(){
	document.getElementById('mailform').className = 'hide';
	document.getElementById('loading').className = 'show';
	fadeIn('loading', 0);
	
	var params = "name=" + document.mailform.name.value;
	params += "&company=" + document.mailform.company.value;
	params += "&phone=" + document.mailform.phone.value;
	params += "&email=" + document.mailform.email.value;
	params += "&subject=" + document.mailform.subject.value;
	params += "&fakemessage=" + document.mailform.fakemessage.value;
	params += "&message=" + document.mailform.message.value;
	params += "&newsletter=" + document.mailform.newsletter.value;
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processEmailSubmit;
		req.open("POST", 'contact_proc.php', true);
		req.setRequestHeader("Content-length", params.length);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.send(params);	
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processEmailSubmit;
			req.open("POST", 'contact_proc.php', true);
			req.setRequestHeader("Content-length", params.length);
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.send(params);
		}
	}
}

function processEmailSubmit(){
		if (req.readyState == 4) {
			if (req.status == 200) {
				document.getElementById('loading').className = "hide";
				document.getElementById('returnMsg').className = "show";
				document.getElementById('returnMsg').innerHTML = req.responseText;
			}
		}
	}

function checkformBGs(){
	if(document.getElementById('name')){
		document.getElementById('name').style.background = "#dddddd";
		document.getElementById('company').style.background = "#dddddd";
		document.getElementById('phone').style.background = "#dddddd";
		document.getElementById('email').style.background = "#dddddd";
		}
	}
	
function slideDownNav(objID, yval) {
	// && !isIE
	if (document.getElementById) {
		obj = document.getElementById(objID);
		if (yval < -153) {
			yval += Math.round((-150-yval)/3);
			obj.style.top = yval+'px';
			window.setTimeout("slideDownNav('"+objID+"',"+yval+")", 50);
		} else {
			obj.style.top = '-150px';
		}
	} else {
		obj = document.getElementById(objID);
		obj.style.top = '-150px';
	}
}

function slideDownSS(yval) {
	// && !isIE
	if (document.getElementById) {
		obj = document.getElementById('subscription');
		if (yval < -3) {
			yval += Math.round((0-yval)/3);
			obj.style.top = yval+'px';
			window.setTimeout("slideDownSS("+yval+")", 50);
		} else {
			obj.style.top = '0px';
		}
	} else {
		obj = document.getElementById('subscription');
		obj.style.top = '0px';
	}
}

function paypalStart(arrVal) {
	currentSentItem  = 0;
	cartArray = arrVal.split(',');
	paypalProcess();
}
function paypalProcess() {
		if(currentSentItem < (cartArray.length-1)){
			var params = 'cmd=_s-xclick&hosted_button_id='+cartArray[currentSentItem];
			var url = 'https://www.paypal.com/cgi-bin/webscr?'+params;
			parent.paypaliframe.location=url;
			paypaliframe.document.body.onload = processPaypalSubmit;
		} else {
			window.location='https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id='+cartArray[currentSentItem];
		}
	}

				
	
function processPaypalSubmit(){
		//alert('there..?');
		currentSentItem++;
	}

function slideUpSS(yval) {
	// && !isIE
	if (document.getElementById) {
		obj = document.getElementById('subscription');
		if (yval > -147) {
			yval += Math.round((-150-yval)/3);
			obj.style.top = yval+'px';
			window.setTimeout("slideUpSS("+yval+")", 50);
		} else {
			/* if(document.getElementById('collectionsListHeadLink')){
				document.getElementById('collectionsListHeadLink').style.zIndex = 3000;
			} */
			obj.style.top = '-150px';
		}
	} else {
		obj = document.getElementById('subscription');
		obj.style.top = '-150px';
	}
}

