$(document).ready(function() {
	if($('#fmCODA_SE_UC_Registration').length>0) {
		$('#btnSubmit').click(function() {
			var err = '';
			if($('#SrcName1').val()=='') { err += '- Namn\n'; }
			if($('#E-post').val()=='') { err += '- E-post\n'; }
			if(err!='') {
				err+='\n\n';
				alert('Kontrollera att följande fält är fullständiga...\n\n'+err);
				return false;
			}
		});
	}
});

function drawFlash(paramFile, paramWidth, paramHeight)
{
	document.write("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"+ paramWidth +"' height='"+ paramHeight +"' id='flash' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='wmode' value='transparent' /><param name='movie' value='"+ paramFile +"' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' /><embed src='"+ paramFile +"' quality='high'  wmode='transparent' bgcolor='#ffffff' width='"+ paramWidth +"' height='"+ paramHeight +"' name='hedgefund' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object>"); 
}


function drawFlashBanner(paramFile, paramWidth, paramHeight)
{
    
	document.write("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='"+ paramWidth +"' height='"+ paramHeight +"' id='CODANL' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='allowFullScreen' value='false' /><param name='movie' value='"+ paramFile +"' /><param name='quality' value='high' /><param name='bgcolor' value='#0000000' /><embed src='"+ paramFile +"' quality='high'  bgcolor='#000000' width='"+ paramWidth +"' height='"+ paramHeight +"' name='CODANL' align='middle' allowScriptAccess='sameDomain' allowFullScreen='false' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object>"); 
}

function clearDefault(elID, defaultContent)
{
	defaultContent = defaultContent.toString();
	currentContent = (document.getElementById(elID).value).toString();
	if (defaultContent.toLowerCase() == currentContent.toLowerCase())
	{
		document.getElementById(elID).value = "";
	}
}

function quickSignup_checkContent()
{
	var intMinChecks = 0;
	var intCheckCount = 0;
	var boolEmailValid = false;
	var boolDoSubmit = true;
	var strErrors = "";
	
	if (document.getElementById('fm_Updates').checked)
		intCheckCount++;
		
	if (document.getElementById('fm_Beta').checked)
		intCheckCount++;
		
	if (document.getElementById('fm_Further').checked)
		intCheckCount++;

	boolEmailValid = checkMail('fm_Email');

	if (intCheckCount == intMinChecks)
	{
		strErrors = "* You've not chosen anything of interest!\n";
		boolDoSubmit = false;
	}
	
	if (!boolEmailValid)
	{
		strErrors += "* You've not given a valid email address.";
		boolDoSubmit = false;		
	}
	
	if (!boolDoSubmit)
	{
		alert("Oops! You've forgotten something on the form.\n"+ strErrors);
	} else {
		document.getElementById('fm_QuickSignup').submit();
	}
	
}

function checkMail(fmElement)
{
	var varEmail = document.getElementById(fmElement).value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(varEmail));
}


function drawSalesforce_Links() 
{ 
	strURLGo = "http://www.coda.com/products-services/Salesforce/CODA2go";
	strURLInt = "http://www.coda.com/products-services/Salesforce/Integrator";	
	strFile = "/System/SalesForce_Links.swf?";
	strURLs	= "URL1="+ strURLInt +"&URL2="+ strURLGo +"&URL3="+ strURLInt +"&URL4="+ strURLGo;
	document.write("</div><div class='content'><hr /><br /><center><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='500' height='250' id='map' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='"+ strFile + strURLs +"' /><param name='quality' value='best' /><param name='wmode' value='transparent' /><embed src='"+ strFile + strURLs +"' quality='best' width='500' height='250' name='map' align='middle' wmode='transparent' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></object></center>"); 
} 



function callwin(url,w,h,scroll,res,type)
{
	width=w; 
	height=h;

	if(type=="video")
	{
		width=width+20;
		height=height+20
	}

	if(scroll){}else
	{
		scroll=0;
	}

	if(res){}else
	{
		res=0;
	}

	LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
	win = window.open(url, "codapopup", "height="+height+", width="+width+", left="+LeftPosition+",top="+TopPosition+",toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars="+scroll+",resizable=" + res);
	win.focus();
}



function callmovie(url,w,h)
{
	width=w; 
	height=h;

	LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
	win = window.open(url, "codamovie", "height="+height+", width="+width+", left="+LeftPosition+",top="+TopPosition+",toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=0,resizable=0");
	win.focus();
}


function toggleDivDiet(el, show, valClear)
{
	if (show) 
	{
		document.getElementById(el).style.display = 'block';				
	} else {
		document.getElementById(el).style.display = 'none';		
		document.getElementById(valClear).value = '';
	}
}


function togglediv(el)
{
	if(document.getElementById(el).style.display == 'none')
	{
		document.getElementById(el).style.display = 'block';
		document.getElementById(el+'_img').src = '/img/lookfeel/cross_minus.gif';
		
	}else
	{
		document.getElementById(el).style.display = 'none';
		document.getElementById(el+'_img').src = '/img/lookfeel/cross_plus.gif';
	}
}

function toggleCaseStudyOptions(el)
{
	//ARRAY OF SEARCH DROP-DOWN LISTS
	var arrLists = Array('optproducts','optbus_sect','optgeog_loc');
	
		document.getElementById('txtsearchobj').value = el.name; //retreive search element name
		document.getElementById('txtsearchval').value = el.value; //retreive search element value

	if(el.name != 'txtsearch') //Only reset if called by a dropdown list
	{
		document.getElementById('txtsearch').value = ''; //reset search text box
		document.getElementById('txtdropdowntext').value = el[el.selectedIndex].text; //retreive search element text value
//alert(el[el.selectedIndex].text);	
	}
	
	//Loop thru search dropdown lists and reset all except the one we just selected
	for (var icount = 0; icount < arrLists.length; icount++)
	{
		if(el.name != arrLists[icount]) //check if this is the selected one or not
		{
			document.getElementById(arrLists[icount]).selectedIndex = 0; //reset each list
		}
	}
}


function getElementsByClassName(oElm, strTagName, strClassName) {
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

SelectQA = function(myid) {
	var Elements = getElementsByClassName(document, "div", "hiddendiv");
	for (i=0; i<Elements.length; i++) {
		if (Elements[i].id == myid) {
			Elements[i].style.display = "block";
		}
		else {
			Elements[i].style.display = "none";
		}
	}
}

AnchorLinks = function() {		
	if (document.getElementById("AnchorList")) {
		var charOffset = 2;
		var currentURL = document.location
		var thisURL = currentURL.toString();
		var the_char=thisURL.charAt(thisURL.length -2);
		var the_char2=thisURL.charAt(thisURL.length -3);
		
		if (the_char == "#") 
			charOffset = 2;
		if (the_char2 == "#") 
			charOffset = 3;
		
		if (the_char!="#" || the_char2!="#")
		{
			AnchorID="1"
		}
		else
		{
			AnchorID=stripAlphaChars(thisURL.substring(thisURL.length - charOffset, thisURL.length));
		}
		
		SelectQA('QA'+AnchorID);
		
		ListElements = document.getElementById("AnchorList").getElementsByTagName("A");
		for (i=0; i<ListElements.length; i++) {
			node = ListElements[i];
			if (node.nodeName=="A") {
				node.onclick = function(i) {
					SelectQA("QA" + stripAlphaChars(this.href.substring(this.href.length - charOffset, this.href.length)));
					return false;
				}
			}
		}
	}
}


drawDebugWindow = function()
{
	var Elements = getElementsByClassName(document, "div", "debug");

	if (Elements!="")
	{
		var debugwindow = document.createElement('div');
		debugwindow.setAttribute('className', 'debug-window');
		debugwindow.innerHTML="<div class='debug-title' style='cursor:default'>DEBUG WINDOW</div>"
		debugwindow.onmouseover = function()
		{
			document.getElementById("debugitemwindow").style.display="block";
		}

		debugwindow.onmouseout = function()
		{
			document.getElementById("debugitemwindow").style.display="none";
		}

		var debugitemwindow = document.createElement('div');
		debugitemwindow.setAttribute('id', 'debugitemwindow');
		debugitemwindow.style.display='none';

	
		for (i=0; i<Elements.length; i++)
		{
			var currentDebug = Elements[i]
			currentDebug.style.display='block';
			debugitemwindow.appendChild(currentDebug)
		}

		debugwindow.appendChild(debugitemwindow)		
		document.body.appendChild(debugwindow)
	}

}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

CheckLanguage = function() {
	if (getCookie("lang")) {
		HideLanguageDivs();
	}
}

ShowLanguageDivs = function() {
	var c = new Date()
	var Elements = getElementsByClassName(document, "div", "lang_off");
	for (i=0; i<Elements.length; i++) {
			Elements[i].className = "lang";
	}
	var Elements = getElementsByClassName(document, "div", "lang_id_off");
	for (i=0; i<Elements.length; i++) {
			Elements[i].className = "lang_id";
	}
	if (getCookie("lang")) {
		document.cookie = "lang = show; expires=" + c.toGMTString();
	}
}

HideLanguageDivs = function() {
	
	var Elements = getElementsByClassName(document, "div", "lang");
	for (i=0; i<Elements.length; i++) {
		Elements[i].className = "lang_off";
	}
	var Elements = getElementsByClassName(document, "div", "lang_id");
	for (i=0; i<Elements.length; i++) {
		Elements[i].className = "lang_id_off";
	}

	document.cookie = "lang = show";
}

if(window.addEventListener)
{
	window.addEventListener('load', AnchorLinks, false); // gecko, safari, konqueror and standard
	window.addEventListener('load', drawDebugWindow, false); // gecko, safari, konqueror and standard
	window.addEventListener('load', CheckLanguage, false); // gecko, safari, konqueror and standard
	window.addEventListener('load', setinitialSearchText, false); // gecko, safari, konqueror and standard
}

if (window.attachEvent)
{
	window.attachEvent('onload', AnchorLinks);
	window.attachEvent('onload', drawDebugWindow);
	window.attachEvent('onload', CheckLanguage);
	window.attachEvent('onload', setinitialSearchText);
}

var initialSearchText=""

function setinitialSearchText()
{
	if (document.getElementById("Search Text"))
	{
		initialSearchText = document.getElementById("Search Text").value
	}
}

function clearsearchinput(el)
{
	if (document.getElementById(el))
	{
		if (document.getElementById(el).value ==initialSearchText)
		{
			document.getElementById(el).value=""
		}
	}
}

function unclearsearchinput(el)
{
	if (document.getElementById(el))
	{
		if (document.getElementById(el).value=="")
		{
			document.getElementById(el).value = initialSearchText;
		}
	}
}

function inArray(arrList, intValue)
{
	for (var i = 0; i < (arrList.length); i++) 
	{ 
		if (arrList[i] == intValue) 
			return true; 
	}
	return false;
}

function setSelectListBlank(elementID)
{
	if (document.getElementById(elementID))
	{
		document.getElementById(elementID).selectedIndex = 0;
	}
}

function showHideReferralOther(elementID, showIndex, elementShow)
{
	var showIndexList = showIndex.split(',');
	if (document.getElementById(elementID) && document.getElementById(elementShow))
	{
		if (inArray(showIndexList, document.getElementById(elementID)[document.getElementById(elementID).selectedIndex].value) == true)
		{
			document.getElementById(elementShow).style.display = 'inline';
		}	else {
			document.getElementById(elementShow).style.display = 'none';
		}
	}
}

function showEventDetails(object,eventContent) { 
	document.getElementById(object).style.visibility = 'visible'; 
	document.getElementById(object).innerHTML = eventContent; 
}

function stripAlphaChars(pstrSource) 
{ 
	var m_strOut = new String(pstrSource); 
		m_strOut = m_strOut.replace(/[^0-9]/g, ''); 
	
	return m_strOut; 
}
