/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistmenu="yes" //"yes" or "no". Make sure each SPAN content contains an incrementing ID starting at 1 (id="sub1", id="sub2", etc)
var persisttype="local" //enter "sitewide" for menu to persist across site, "local" for this page only

if (document.getElementById){ //DynamicDrive.com change
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}

function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var il = document.getElementById(obj+"sign");	
	var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
		if(el.style.display != "block"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu") //DynamicDrive.com change
				{	ar[i].style.display = "none";
					il2=document.getElementById(ar[i].id+"sign");	
					il2.className="plusdesc";		
				}
			}
			el.style.display = "block";
			//alert(obj+"sign");
			if(il.className=="minusdesc")
			{
				il.className="plusdesc";
			}
			else{
				il.className="minusdesc";
			};
			
		}else{
			el.style.display = "none";
			il.className="plusdesc";
		}
	}
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function onloadfunction(){
if (persistmenu=="yes"){
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=get_cookie(cookiename)
if (cookievalue!="")
document.getElementById(cookievalue).style.display="block"
}
}

function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}


if (window.addEventListener)
window.addEventListener("load", onloadfunction, false)
else if (window.attachEvent)
window.attachEvent("onload", onloadfunction)
else if (document.getElementById)
window.onload=onloadfunction

if (persistmenu=="yes" && document.getElementById)
window.onunload=savemenustate

function colexpWork()
{
	var tbox = document.getElementById('howwork');
	var arrow1 = document.getElementById('ttarrow1');

	if(tbox.style.display == "none") 
	{
			// tbox.style.display = 'block';
			arrow1.src = 'images/arrowup.gif';
	}
	else
	{
		// tbox.style.display = 'none';
		arrow1.src = 'images/arrowdown.gif';
	}
}

var ua = navigator.userAgent.toLowerCase();
var os,browser,version,data;

function detect(text)
 {
   stringposition = ua.indexOf(text) + 1;
   data = text;
   return stringposition;
}

function advance(elmnt,nxt){
    
    var elmntlngth = elmnt.value.length;
    if(elmntlngth == elmnt.maxLength)
    {
    	document.getElementById(nxt).focus();
    }
}

function launchBudgetEstimator(category)
{
window.open('index.php?section=budgetestimator&popup=true&category='+category, 'BudgetEstimator', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=350');
}

function launch(url,width,height)
{
window.open(url, 'Window', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width='+width+',height='+height);
}


function loading()
{
 document.getElementById('realcontent').style.display='none';
 document.getElementById('loading').style.display='block';
}

var rrFlds = new Array();	
var errors = 0;
rrFlds[0] = 'name';
rrFlds[1] = 'name';
rrFlds[2] = 'address_line1';
rrFlds[3] = 'city';
rrFlds[4] = 'postalcode';
// rrFlds[5] = 'country';
rrFlds[5] = 'telephone';
rrFlds[6] = 'email';
rrFlds[7] = 'secreta';
rrFlds[8] = 'secretq';

function  checkAdCreateFields()
{
	
	rrFlds[6] = 'description';
	rrFlds[7] = 'buywhen';
	rrFlds[8] = 'budget';
	rrFlds[9] = 'contactwhen';

	return checkFields(true);
	
}

function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.forms[0].email.value;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
      return false;
    } 
    return true; 
}

function checkFields(AdCreate)
{
	
	// woorden laten beginnen met een hoofdletter
	stripAlphaChars(document.getElementById('telephone'));
	stripAlphaChars(document.getElementById('fax'));
	
	checkValues();
	document.getElementById(rrFlds[0]).value = document.getElementById(rrFlds[0]).value.capitalize();

	// verify email
	if(isValidEmail(document.getElementById('email')) == false) 
	{
		showError('email1');
	}
	hideError('email2');
	
	if(AdCreate == false) 
	{
		verifyPasswords();
		verifyBirth();
		verifyTerms();
	}

	// indien er errors zijjn gaan we de error box weergeven
	if(errors > 0) 
	{
		scroll(0,0);
		document.getElementById('errorbox').className='errormsg';
		errors = 0;
		return false;
	}
	else
	{
		document.getElementById('errorbox').className='hidden';
		errors = 0;
		return true;
	}
		
}

function verifyTerms()
{
	hideError('terms');
	if(document.getElementById('terms').checked == false)
	{
		showError('terms');
	}
}

function verifyBirth()
{
	hideError('birth_day');
	hideError('birth_month');
	hideError('birth_year');
	
	stripAlphaChars(document.getElementById('birth_day'));
	stripAlphaChars(document.getElementById('birth_month'));
	stripAlphaChars(document.getElementById('birth_year'));
	
	if(document.getElementById('birth_day').value > 31 || document.getElementById('birth_day').value < 1)
	{
		showError('birth_day');
	}
	if(document.getElementById('birth_month').value > 31 || document.getElementById('birth_month').value < 1)
	{
		showError('birth_month');
	}
	if(document.getElementById('birth_year').value > 2000 || document.getElementById('birth_year').value < 1900)
	{
		showError('birth_year');
	}
}

function checkValues()
{
	
	for(i=0;i<rrFlds.length;i++)
	{
		hideError(rrFlds[i]);
		if(document.getElementById(rrFlds[i]).value == '' || document.getElementById(rrFlds[i]).value.length < 2)
		{
			showError(rrFlds[i]);
		}
	}
	
}

function verifyPasswords()
{
	hideError('password');
	hideError('password_ver');

	if(document.getElementById('password').value.length < 5 || document.getElementById('password').value.gotNum() == false)
	{
		showError('password');
	}
	if(document.getElementById('password_ver').value != document.getElementById('password').value)
	{
		showError('password_ver');
	}
}

function showError(name)
{
	if(document.getElementById(name+'_error'))
	{
		document.getElementById(name+'_error').className='errormsg';
		errors++;
	}
}

function hideError(name)
{
	if(document.getElementById(name+'_error'))
	{
		document.getElementById(name+'_error').className='hidden';
	}
}


function stripAlphaChars(where) 
{ 
 where.value = where.value.replace(/[^\d]/g, '');
}

String.prototype.capitalize = function(){ //v1.0
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};

String.prototype.gotNum=function(){
   return /\d/.test(this)
} 

function quickInvoice(invoiceid)
{
	if(window.open('index.php?section=invoices&action=quickView&id='+invoiceid, 'Quick invoice viewer', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=300'))
	{
		return false;
	}
	else
	{
		return true;
	}
	
}

function editCreditCard()
{
	
	document.getElementById('availableCreditCard').style.display='none';
	document.getElementById('insertCreditCard').className='';
	
	document.getElementById('useAvailableCreditCard').value='false';
	
}

function cancelEditCreditCard()
{
	
	document.getElementById('availableCreditCard').style.display='';
	document.getElementById('insertCreditCard').className='hidden';
	
	document.getElementById('useAvailableCreditCard').value='true';
	
}

function switchButton(where,staat)
{
	
	if(staat == 'off')
	{
		where.className='button';
	}
	else
	{
		where.className='buttonO';
	}
	
}

function applyButtonStyles()
{
	
	var buttons = document.getElementsByTagName('input');
	for(var i=0;i<buttons.length;i++)
	{
		
		if(buttons[i].type=='submit' || buttons[i].type=='button' || buttons[i].type=='reset')
		{
			switchButton(buttons[i],'off');
			buttons[i].onmouseover=function(){switchButton(this,'on');};
			buttons[i].onmouseout=function(){switchButton(this,'off');};
			
		}
		
	}

}