// 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 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("bigTabs")) {
		var tabBoxs = document.getElementById("bigTabs");
		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';
		}
	}
}

// This script is a pre-processor for 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:FormValidate('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 domain = 'ivasar.com'
var undefined;

function dtCommFormValidate(formID,notify) {
	var tForm = document.getElementById(formID);
	
		tForm.action = "http://emailer.dtcommunicate.com/multisubscribe.ice";

	var notification = document.createElement('input');
		notification.type = 'hidden';
		notification.name = 'notification';
		if(notify == null || notify == undefined || notify == "") {
			notification.value = "";
		} else {
			notification.value = notify+'@'+domain;
		}
		tForm.appendChild(notification);
	var contactEmail = document.createElement('input');
		contactEmail.type = 'hidden';
		contactEmail.name = 'contactemail';
		if(notify == null || notify == undefined || notify == "") {
			contactEmail.value = "";
		} else {
			contactEmail.value = notify+'@'+domain;
		}
		tForm.appendChild(contactEmail);
	
	tForm.submit();
}

// Highlight required form elements for a 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);
	}
}
