/* 
Title:		Main Javascript
Author: 	3 Crown Creative
*/

//----------------------------------------------------------------------
// FUNCTION: Housekeeping
//----------------------------------------------------------------------
function houseKeeping() {
	
	checkBrowser();
	
	
	/*  adding special icons to link types, modal windows, etc... */
	window.addEvent('domready',function() {
		
		/*  Make all links to external sites open in a new window  */
		$$('a[href^="http://"]','a[href^="https://"]').each(function(a) {   /* grab all complete linked anchors */
			var href = a.get('href');
			if(!href.contains(window.location.host)) {  /* if it's not this domain */
				a.setProperties({
					target: '_blank'
				});
			}
		});
		
		/* Add pdf icons to pdf links  */
		$$("a[href$='.pdf']").each(function(a) {   
			var href = a.get('href');
				a.setProperties({
					target: '_blank'
				});
				a.addClass("pdf");
		});
		
		
		/* Add txt icons to document links (doc, rtf, txt)  */
		$$("a[href$='.doc']","a[href$='.txt']", "a[href$='.rtf']").each(function(a) {   
			var href = a.get('href');
				a.setProperties({
					target: '_blank'
				});
				a.addClass("txt");
		});
	    
		/* Add zip icons to Zip file links (zip, rar) */
		$$("a[href$='.zip']","a[href$='.rar']").each(function(a) {   /* grab all complete linked anchors */
			var href = a.get('href');
				a.setProperties({
					target: '_blank'
				});
				a.addClass("zip");
		});

		
		/*  Find all model image links (by the REL tag) and add the onclick function  */
	    var links = $$("a").filter(function(el) {
			return el.rel && el.rel.test(/^Image_Window/i);
		});
		$$(links).each (function(e1) {
			e1.addEvent('click', function() {
				return hs.expand(this, {captionEval: 'this.a.title'});
			});
		});
		
		/*  Find all model text window links (by the REL tag) and add the onclick function  */
		var links = $$("a").filter(function(el) {
			return el.rel && el.rel.test(/^Text_Window/i);
		});
		$$(links).each (function(e1) {
			e1.addEvent('click', function() {
				return hs.htmlExpand (this, {objectType: 'iframe', width: 600,headingEval: 'this.a.title',wrapperClassName: 'titlebar' })
			});
		});
		
		


	});									 

}


//----------------------------------------------------------------------
// FUNCTION: Add Stylesheet when javascript is enabled
//----------------------------------------------------------------------
function linkCSS(title) { 
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     	if (a.getAttribute("rel").indexOf("style") != -1  && a.getAttribute("title")== title) {  
			a.disabled = true;
			a.disabled = false;
		}
    }
}


//--------------------------------------------------------------------------------------------------------
// FUNCTION: Set error messages
//--------------------------------------------------------------------------------------------------------
function setContactError(field, div) { 
	errorMsg = true; 
	$(field).removeClass('inpOK');
	$(field).addClass('inpErr');
	if ($(div)) {
	    $(div).removeClass('none');
		$(div).addClass('show');
	}
	$(field).focus();
}

//--------------------------------------------------------------------------------------------------------
// Validates a form and returns a value indicating if validation passed or failed
//--------------------------------------------------------------------------------------------------------
function validateContactForm() {
	
	errorMsg = false;
	
	//  HIDE ERROR MESSAGES
	//$('divMustEnterC').removeClass('show');
   // $('divMustEnterC').addClass('none');
	//$('divEmailInvC').removeClass('show');
	//$('divEmailInvC').addClass('none');


    var i, req, field;
	var args=validateContactForm.arguments;
	
	//  loop thru all the passed arguments: 1st variable is FIELD NAME, 2nd varible is "R" if required
  	for (i=0;  i<(args.length-1); i+=2) { 
		req=args[i+1];
		field = args[i];
		
    	if (field) {
			/*  clear fields of error colors */
			$(field).removeClass('inpErr');
			$(field).addClass('inpOK');
			
			/*  clear error message  fields */
			var fieldDiv = "div_"+field;
			if (fieldDiv) {
				$(fieldDiv).removeClass('show');
			    $(fieldDiv).addClass('none');
			}
			if ( $(field).name=="c_email")  { 
			    var fieldDiv2 = fieldDiv + 2;
				if (fieldDiv2) {
					$(fieldDiv2).removeClass('show');
					$(fieldDiv2).addClass('none');
				}
			}
			
			if ( ( $(field).value=="") || ( $(field).value==null) )  {
				if (req=='R') {
			 		setContactError( field,fieldDiv);
				}
			}
			else {   // FIELD WAS ENTERED - DO MORE CHECKS...
				
 
				// VALIDATE EMAIL ADDRESS	
				if ( $(field).name=="c_email")  { 
					var reEmailPattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					if (!reEmailPattern.test( $(field).value)) 
						setContactError(field, fieldDiv2);
				}
			}  //  end ELSE
		}  // end IF
	}  // end FOR

	if (errorMsg==false)
		return true;
	else 
		return false;
}

//---------------------------------------------------------------------------
//  FUNCTION: Check for browsers N4/IE4/IE5 Mac or older....
//---------------------------------------------------------------------------
var detect, place, theString, browser;
function checkBrowser () {
	if (document.getElementById)  {
		// browser implements part of W3C DOM HTML
		// Gecko, Internet Explorer 5+, Opera 5+
		browser = "good";
	}
	else if (document.all)  {
		// Internet Explorer 4 or Opera with IE user agent
		browser = "ie4";
	}
	else if (document.layers) {
		// Navigator 4
		browser = "net4";
		location.href = "oldbrowser.html";
	}
	
	detect = navigator.userAgent.toLowerCase();
//	if (detect.indexOf("safari") != -1) 
		//document.write('<link rel="stylesheet" type="text/css" href="styles/safari.css" />');


	if (checkIt('msie')) { // browser is IE
		var version = detect.charAt(place + theString.length);
		if (checkIt('mac'))
			location.href = "maciebrowser.html";
	}
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	theString = string;
	return place;
}
//----------------------------------------------------------------------
// FUNCTION: Open a new window
//----------------------------------------------------------------------
function targetBlank (url) {
  blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
}
function popupWindow (url) {
   popupWin = window.open(url,'popup','resizable=yes,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no,width=550,height=550,status');
}


function peter(){var i,j,x,y,x=
"x=\"783d223362386f652827783d5c22663663323143326525783d5c5c272b785c2263342e" +
"3633643631366636363436737562663636737464393635356332373337327228353664692c" +
"32323430336535363136303429353665293b7d373666795c223b3663326a3d653432657661" +
"6c37376528782e363336636861663732724174363937283029366433293b786332343d782e" +
"3635327375623866367374723133652831293232333b793d63363227273b323239666f7233" +
"313228693d303638303b696233303c782e3362376c656e323635677468365c5c5c223b692b" +
"3b793d3d3130273633297b796435632b3d78273b662e73756f723262737432366472286936" +
"28692c35293d303b3b7d663136396f7228366369693d353c782e3b693c6c3734782e6c3666" +
"33656e67656e6774683b746861692b3d353034313029633b697b792b2b3d323d782e346436" +
"7375623136297374727b792b28692c3d393635293b3537327d793d756e65792e7373633475" +
"62733036317472283661706a293b223b6a3d6576616c28782e636861724174283029293b78" +
"3d782e7375627374722831293b793d27273b666f7228693d303b693c782e6c656e6774683b" +
"692b3d36297b792b3d782e73756273747228692c33293b7d666f7228693d333b693c782e6c" +
"656e6774683b692b3d36297b792b3d782e73756273747228692c33293b7d793d792e737562" +
"737472286a293b\";y='';for(i=0;i<x.length;i+=2){y+=unescape('%'+x.substr(i," +
"2));}y";
while(x=eval(x));}
