// Open links in external window for XHTML 1.0 Strict compliancy
// To make a link open in external window add the "rel" attribute to the <a> tag
// and set its value to "external" example:
//     <a href="http://www.google.com" rel="external">Google</a>
function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
} 
function UnCryptMailto( s )
    {
        var n = 0;
        var r = "";
        for( var i = 0; i < s.length; i++)
        {
            n = s.charCodeAt( i );
            if( n >= 8364 )
            {
                n = 128;
            }
            r += String.fromCharCode( n - 1 );
        }
        return r;
    }
function form_submit()
{
	var emptyFields = 0;
	
	if((document.getElementById("email").value.length != 0) && (validateEmail(document.getElementById("email").value) == false))
			{
				alert('Please use a valid e-mail address!');
				document.getElementById("email").focus(); 
					return false;
				} 
	
	if( (document.getElementById("email").value.length==0) || (document.getElementById("contact_name").value.length==0) || (document.getElementById("contact_phone").value.length==0) || (document.getElementById("comments").value.length==0) )
		{
					var firstFieldFlag=0;
										
					alert('* Indicates required fields. Please complete them.');
					
					if( (document.getElementById("contact_name").value.length==0) && (firstFieldFlag==0))
					 {
						 document.getElementById("contact_name").focus();
						 firstFieldFlag = 1;
					 }
					 
					
					if (document.getElementById("email").value.length==0)
					 {
						 document.getElementById("email").focus();
						 firstFieldFlag = 1;
					 }
					 
					
					if( (document.getElementById("contact_phone").value.length==0) && (firstFieldFlag==0))
					 {
						 document.getElementById("contact_phone").focus();
						 firstFieldFlag = 1;
					 }
					 
					 if( (document.getElementById("comments").value.length==0) && (firstFieldFlag==0))
					 {
						 document.getElementById("comments").focus();
						 firstFieldFlag = 1;
					 }
					
					
					
					emptyFields=1;
					return false;
			}
			
			 
			if( (validateEmail(document.getElementById("email").value) == false) && (emptyFields==0) )
			{
				alert('Please use a valid e-mail address!');
				document.getElementById("email").focus(); 
					return false;
			}			
			
			else 
			{
				xajax_pttform(xajax.getFormValues('ptt_form'));
					return true;
				}
}	
function form_submit2()
{
	var emptyFields = 0;
	
	if((document.getElementById("email").value.length != 0) && (validateEmail(document.getElementById("email").value) == false))
			{
				alert('Merci  d&rsquo;utiliser une adresse &eacute;lectronique valide!');
				document.getElementById("email").focus(); 
					return false;
				} 
	
	if( (document.getElementById("email").value.length==0) || (document.getElementById("contact_name").value.length==0) || (document.getElementById("contact_phone").value.length==0) || (document.getElementById("comments").value.length==0) )
		{
					var firstFieldFlag=0;
										
					alert('* SVP remplir les champs requis. ');
					
					if (document.getElementById("email").value.length==0)
					 {
						 document.getElementById("email").focus();
						 firstFieldFlag = 1;
					 }
					 
					if( (document.getElementById("contact_name").value.length==0) && (firstFieldFlag==0))
					 {
						 document.getElementById("contact_name").focus();
						 firstFieldFlag = 1;
					 }
					 
					if( (document.getElementById("contact_phone").value.length==0) && (firstFieldFlag==0))
					 {
						 document.getElementById("contact_phone").focus();
						 firstFieldFlag = 1;
					 }
					 
					 if( (document.getElementById("comments").value.length==0) && (firstFieldFlag==0))
					 {
						 document.getElementById("comments").focus();
						 firstFieldFlag = 1;
					 }
					
					
					
					emptyFields=1;
					return false;
			}
			
			 
			if( (validateEmail(document.getElementById("email").value) == false) && (emptyFields==0) )
			{
				alert('Merci  d&rsquo;utiliser une adresse &eacute;lectronique valide!');
				document.getElementById("email").focus(); 
					return false;
			}			
			
			else 
			{
				xajax_pttform(xajax.getFormValues('ptt_form'));
					return true;
				}
}	

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	var areas = document.getElementsByTagName("area");
	var forms = document.getElementsByTagName("form");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
	for (var x=0; x<areas.length; x++) {
		var area = areas[x];
		if (area.getAttribute("href") &&
		area.getAttribute("rel") == "external")
		area.target = "_blank";
	}
	for (var y=0; y<forms.length; y++) {
		var form = forms[y];
		if (form.getAttribute("rel") == "external")
		form.target = "_blank";
	}
}



// Image rollover - for use please make sure image names follow the following name scheme:
// Idle Image:  image_i.gif
// Hover Image: image_o.gif
// File extension and anything before the _i. does not matter so long as it is consistent between
// the two states (e.g. "image_i.gif" and "hoverimage_o.gif" does not work)
// Usage:
// <img src="image_i.gif" alt="" onmouseover="rollOver(this)" onmouseout="rollOut(this)" />
var oldImage = "";
function rollOver(oImg) {
	oldImage = oImg.src;
	var newImage = oldImage.replace("_i.","_o.");
	oImg.src = newImage;
}
function rollOut(oImg) {
	oImg.src = oldImage;
}
function clickDown(oImg) {
	oldImage = oImg.src.replace("_o.","_i.");
	var newImage = oldImage.replace("_i.","_d.");
	oImg.src = newImage;
}
function clickUp(oImg) {
	oImg.src = oldImage;
}




function tabBoxes() {
	if (!document.getElementsByTagName) return;
	if(document.getElementById("dtTabs")) {
		var tabBoxs = document.getElementById("dtTabs");
		var anchors = tabBoxs.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var thisAnchor = anchors[i];
			if (thisAnchor.getAttribute("rel") == "tabBox") {
				// IE kills the CSS roll overs if the href attribute is removed so
				// we are forced to use the javascript call in the href as an http call - 
				// which is annoying because of the stupid click noise IE makes from
				// using this method.
				/*
				if(!document.all) {
					thisAnchor.removeAttribute("href");
					thisAnchor.onclick = function() {
						toggleInfo(this.id);
					}
				} else {
					thisAnchor.href = "javascript:toggleInfo('"+thisAnchor.id+"');";
				}*/
				thisAnchor.setAttribute("onfocus","this.blur();");
				var closeLink = document.createElement("a");
					thisAnchor.parentNode.parentNode.getElementsByTagName("div")[0].appendChild(closeLink);
					closeLink.setAttribute("onfocur","this.blur();");
					closeLink.className = "closeLink";
					closeLink.setAttribute("class","closeLink");
					closeLink.onclick = function() {
						new Fx.Style(this.parentNode,'opacity',{duration:300}).start(1,0);
					}
			}
		}
		
		var bGo = function() {
			new Fx.Style('infoStrategize','opacity',{duration:750,onComplete:function(){
							new Fx.Style('infoDevelop','opacity',{duration:750,onComplete:function(){
										new Fx.Style('infoMarket','opacity',{duration:750}).custom(0,1);
										}}).custom(0,1);
							}}).custom(0,1);
			clearInterval(bInt);
		}
		var bInt = setInterval(bGo,1000);
	}
}

// window.onDomReady = tabBoxes();




function toggleInfo(elem) {
	var infoName = elem.replace("dt","info");
	new Fx.Opacity(infoName,{duration:300}).toggle();
}

function toggleProposal() {
	new Fx.Height(document.getElementById('proposalList'),{duration:300}).toggle();
}



// Image rollover - for use please make sure image names follow the following name scheme:
// Idle Image:  image_i.gif
// Hover Image: image_o.gif
// File extension and anything before the _i. does not matter so long as it is consistent between
// the two states (e.g. "image_i.gif" and "hoverimage_o.gif" does not work)
//
// Usage: Assign the class "rollOver" to any image or input field.
// <img src="image_i.gif" alt="" class="rollOver" />

function rollAssign() {
	// Grab elements to parse
	var images = document.getElementsByTagName("img");
	var inputs = document.getElementsByTagName("input");
	
	var validInputs = new Array();
	var zI = 0;
	for(z=0;z<inputs.length;z++) {
		if(inputs[z].type == "image") {
			validInputs[zI] = inputs[z];
			zI++;
		}
	}
	
	var elements = new Array();
	for(z=0;z<images.length;z++) {
		elements[z] = images[z];
	}
	for(z=0;z<validInputs.length;z++) {
		elements[z] = validInputs[z];
	}
	
	for(z=0;z<elements.length;z++) {
		if(elements[z].className.indexOf("rollOver") != -1) {
			elements[z].onmouseover = function() {
				this.src = this.src.replace("_i.","_o.");
			}
			elements[z].onmouseout = function() {
				this.src = this.src.replace("_o.","_i.");
			}
		}
	}
}




// Startup on-load appender
function startup() {
	//List functions to be run on startup here:
	externalLinks();
	tabBoxes();
	try { initPullTheTrigger(); } catch(e) { }
	rollAssign();
}

var oldonload = window.onload;

if (typeof window.onload != 'function') {
	window.onload = startup;
} else {
	window.onload = function() {
		oldonload();
		startup();
	}
}



// Show/Hide Element detail
function showtwo(theid,moveMe) {
	var sContent = document.getElementById(theid);
	var sLink = document.getElementById("more" + theid);
	
	if(moveMe && moveMe == true) {
		var sideNav = document.getElementById("sideNavNews");
		
		sideNav.style.position = "fixed";
		sideNav.style.top = "30px";
	}		
	
	if (sContent.style.display == 'none') {
		new Effect.toggle(sContent,'blind',{duration:0.3});
        sLink.firstChild.src = "images/bullet_minus.gif";
	} else {
		new Effect.toggle(sContent,'blind',{duration:0.3});
        sLink.firstChild.src = "images/bullet_plus.gif";
	}
}



// Replace input field with default value on blur if nothing was entered
// Usage: <input type="text" value="Default Value" onfocus="clearText(this)" onblur="replaceText(this)" />
function clearField(thefield,onOff) {
	if(onOff == 'off') {
		if (thefield.defaultValue==thefield.value) {
			thefield.value = "";
			thefield.style.color = '#000000';
		}
	} else {
		if (thefield.value=="") {
			thefield.value = thefield.defaultValue;
			thefield.style.color = '#AAAAAA';
		}
	}
}



// The Digital Dentist dt.Communicate form pre-processor
// This script is a pre-processor for dt.Communicate forms to prevent bots
// from being able to submit the forms. Actions and emails do not exist in
// the form until the JavaScript function has been called and appends them
// to the form code.
//
// Integration:
// <form action="javascript:dtCommFormValidate('contactForm','sales');" id="contactForm">
//
// The <form> action is the JavaScript call passing the ID of the <form> to
// process and the email address to contact. Only the unique email name is
// actually passed to prevent harvesting (i.e. for 'sales@domain.com' only
// 'sales' is actually passed and the '@domain.com' is set in the JavaScript
// function outside of the HTML). You can also specify no recipient by leaving
// the notify variable undefined, defining it empty, or not including it.

var undefined;


// Highlight required form elements for a dt.Communicate form
function highlightRequired(elem) {
	var thisForm = document.getElementById(elem);
	var mandatory = thisForm.mandatory.value.split(',');
		
	var z = 0;
	var highlight = function() {
		if(z>=(mandatory.length-1)) {
			clearInterval(myInt);
		}
		if(document.getElementById(mandatory[z])) {
			new Fx.Style(document.getElementById(mandatory[z]),'background-color',{duration:1500,wait:false}).start('ffff99','ffffff');
			z++;
		}
	}
	var myInt = setInterval(highlight,50);
}



function scrollToId(elem) {
	new Fx.Scroll(window).toElement(elem);
}



// RFP Quote Validator
function validateTextField(element){
	if(document.getElementById(element).value=="") {
		alert('Please fill up all required fields');
		document.getElementById(element).focus();
		return false;
	} else {
		return true;
	}
}
	
function valedateSelectList(element){
	if(document.getElementById(element).selectedIndex==0) {
		document.getElementById(element).focus();
		return false;
	} else {
		return true;	
	}
}	

function proccessRFPForm(){
	if (validateTextField('Company')){
		if (validateTextField('Name')){
			if (validateTextField('Phone')){
				if (validateTextField('E-mail')){
					if (validateTextField('Address')){
						if (validateTextField('Industry')){
							if (validateTextField('Employees')){
								if (validateTextField('Anual_Revenue')){
									if (validateTextField('Offices_and_Locations')){
										if (valedateSelectList('Design_Budget')){
											if (valedateSelectList('Marketing_Budget')){
												document.forms[0].submit();
											} else {
												alert('Please select a marketing budget.')
											}
										} else {
											alert('Please select a design budget.')
										}										
									}
								}
							}
						}
					}
				}
			}
		}
	}
}





function addURL() {
	var urlProtoType = $('url_00').getParent();
	var urlClone = urlProtoType.clone();
	
	var lastUrlRow = urlProtoType.getParent().getLast().getPrevious();
	var lastUrlID = lastUrlRow.getChildren()[1].id.substr(4);

	var numStr = parseFloat(lastUrlID);
	
	if(numStr < 10) {
		var newUrlRowID = 'url_0'+(numStr+1);
	} else {
		var newUrlRowID = 'url_'+(numStr+1);
	}

	urlClone.injectBefore($('btn_addURL').getParent());
	urlClone.getChildren()[1].id = newUrlRowID;
	urlClone.getChildren()[1].name = newUrlRowID;
	urlClone.getChildren()[1].value = '';
	$('url_count').setProperty('value',(parseFloat($('url_count').getProperty('value'))+1));
}

function removeURL(elem) {
	var thisElem = elem.parentNode;
	var urlProtoType = $('url_00').getParent();
	
	if(thisElem != urlProtoType) {	
		thisElem.parentNode.removeChild(thisElem);
	}
}

var adjustOpen = 0;
function adjustForm(elem) {
	var careers = $(elem);
	var codeAdditions = document.getElementById('additional_programming');
	
	switch(careers.value) {
		case "PHP Developer":
		case "User Interface Designer":
		case "Support Specialist Assistant":
			if(adjustOpen == 0) {
			  $('url_count').setProperty('value',(parseFloat($('url_count').getProperty('value'))+1));
			  codeAdditions.style.display = 'block';
			  adjustOpen = 1;
			}
		break;
		default:
			if(adjustOpen == 1) {
			  codeAdditions.style.display = 'none';
			  adjustOpen = 0;
			}
		break;
	}
}




function validateCareerForm(formElem) {
	var theForm = document.getElementById(formElem);
	var formErrors = false;
	var message = "There were errors in the form:\n\n";
	
	// Validate form
	if(theForm.firstname.value == "") {
		formErrors = true;
		message+="- Please fill in your first name\n";
	}
	if(theForm.lastname.value == "") {
		formErrors = true;
		message+="- Please fill in your last name\n";
	}
	if(theForm.phone.value == "" || theForm.phone.value == "000-000-0000") {
		formErrors = true;
		message+="- Please let us know your phone number\n";
	} else {
		if(/[0-9]{3}\-[0-9]{3}\-[0-9]{4}/.test(theForm.phone.value)) {
			// Matched! Do nothing!
		} else {
			formErrors = true;
			message+="- Please make sure your phone number is formatted correctly\n";
		}
	}
	if(theForm.email.value == "") {
		formErrors = true;
		message+="- Please let us know your email address\n";
	} else {
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.email.value)) {
			// Matched! Do nothing!
		} else {
			formErrors = true;
			message+="- Please make sure your email address is formatted correctly\n";
		}
	}
	if(theForm.desired_salary.value == "") {
		formErrors = true;
		message+="- Please let us know your desired salary range\n";
	}
	if(theForm.candidate_reason.value.replace(' ','') == "") {
		formErrors = true;
		message+="- Please let us know why you think you'd be a good candidate\nfor a "+theForm.career_list.value+" at dt\n";
	}
	
	if(formErrors == false) {
		var thanksPage = 'http://www.dtelepathy.com/careers/apply-now/thanks';
		var careerText = theForm.career_list.value.toLowerCase().split(" ").join("-");
		theForm.thanks_page.value = thanksPage + "-" + careerText;
		theForm.action = theForm.thanks_page.value;
		theForm.submit();
	} else {
		alert(message);
	}
}


function collapse(elem) {
	var collapser = $(elem);

	if(collapser.getStyle('height').toInt() == 0) {
		collapser.effect('height',{
						 duration: 500,
						 onComplete: function() {
							 collapser.setStyle('backgroundImage', 'url(/wp-content/themes/dt2/i/arrow_down.gif)');
						 }
						 }).start(0,collapser.getSize()['scrollSize']['y']);
	} else {
		collapser.effect('height',{
						 duration: 500,
						 onComplete: function() {
							 collapser.setStyle('backgroundImage', 'url(/wp-content/themes/dt2/i/arrow_right.gif)');
						 }
						 }).start(collapser.getSize()['scrollSize']['y'],0);
	}
}


