﻿/* LANGUAGE */

function changeLanguage()
{
	var cCookieStr = document.cookie;
  if(cCookieStr.indexOf("SMLanguage=ENG")!=-1)
  {
  	changeLang('SWE');
  }else{
  	changeLang('ENG');
  }
  
  return false;
}

function changeLang(cLang){
	document.cookie="SMLanguage=" + cLang +";";
	var c_Loc = window.location.href;
	c_Loc = c_Loc.replace("smlanguage=", "smlanguage=" + cLang + "&");
	window.location.href = c_Loc;
	
	return false;
}

function getName(cDiv){
	var oDiv = document.getElementById(cDiv);
	if(!oDiv){
		return false;
	}
	var cCookieStr = document.cookie;
	var cRes = '';

  if(cCookieStr.indexOf("SMLanguage=ENG")!=-1)
  {
  	cRes = 'In Swedish';
  }else{
  	cRes = 'In English';
  }
  
  oDiv.innerHTML = cRes;
  
  return false;
}

function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
/* LANGUAGE */



function showPopup(page, height, width) {
	    var win = null;
            h = height;
            w = width;
            myname = "JSpopup";
            LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
            TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
            settings = 'height='+h+', width='+w+', top='+TopPosition+', left='+LeftPosition+', menubar=no, resizable=yes, scrollbars=yes, status=no'
            win = window.open(page, myname, settings)
}

function toggleFaq(id) {
	answer = document.getElementById('faqAnswer' + id);
	img = document.getElementById('faqImage' + id);
	
	if(answer != null && img != null) {
		if(answer.style.display == "none") {
			answer.style.display = "block";
			img.src = "images/layout/arrow_small_down_white_blue.jpg";
		}
		else {
			answer.style.display = "none";
			img.src = "images/layout/arrow_small_right_white_blue.jpg";
		}
	}
}



function print(cContentId){
  var oDiv = document.getElementById(cContentId);
  if(!oDiv){
  	return false;
  }

  var oFooter = document.getElementById("footerContact");
  var cFooter = '';
  if(!oFooter){
    cFooter = 'Carpark AB - Box 90240 - Besöksadress: Lindetorpsvägen 17 - 120 23  STOCKHOLM - Tel vxl 08-722 15 00 - <a href="mailto:info@carpark.se">info@carpark.se</a>';
  }else{
    cFooter = oFooter.innerHTML;
  }
  
	var cTpl = '';
	cTpl += '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
	cTpl += '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">';
	cTpl += '<head>';
	cTpl += '<title>Carpark</title>';
	cTpl += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	cTpl += '<link rel="stylesheet" type="text/css" href="style.css" />';
	cTpl += '</head>';
	
	cTpl += '<body style="background-color:#FFFFFF;margin:0;padding:0;">';
	cTpl += '<div style="width:788px;">';
	cTpl += '<div><img src="images/layout/header.jpg" alt="" /></div>';
	
	cTpl += '<div style="padding: 30px 10px 0px 10px;">';
	cTpl += '<div style="padding-bottom: 30px;">';
	cTpl += oDiv.innerHTML;
	cTpl += '</div>	';
	cTpl += '<div>';
	cTpl += cFooter;
	cTpl += '</div>';
	cTpl += '</div>';
	cTpl += '</div>';
	cTpl += '<sc' + 'ript>';
	cTpl += 'var oPrint = document.getElementById("printAndTip");';
	cTpl += 'if(oPrint){';
	cTpl += 'oPrint.style.display = "none";';
	cTpl += '}';
	cTpl += 'window.print();';
	cTpl += '</scr' + 'ipt>';
	cTpl += '</body>';
	cTpl += '</html>';
  
  prt=window.open('','','scrollbars=yes');
  prt.document.open();
  prt.document.writeln(cTpl);
  prt.document.writeln();
  prt.document.close();
  return false;

}

function openSubMenu(cDivId)
{
	var oSubMenuPH = document.getElementById("submenu");

	var oSub = document.getElementById(cDivId);
	if(!oSub){
		return false;
	}
	oSubMenuPH.innerHTML = oSub.innerHTML;	
	return false;
}

function goToLink(cLink){
    window.location.href = cLink;
}
//********************************************
//	RETURN FULL URL
//********************************************
function getTipsaUrl(){
var myUrl = document.location.href;

myUrl = myUrl.replace(new RegExp(/&/g), "XXX"); 
myUrl = myUrl.replace(new RegExp(/=/g), "YXY"); 
myUrl = myUrl.replace(new RegExp(/#/g), ""); 

return myUrl;
}

function tipFriend(){
	var cUrl = 'smpage.fwx?page=194&tipsaurl=' + getTipsaUrl();
	window.open(cUrl,'','width=350,height=300, scrollbars=no');
	return false;
}



/********** FORM VALIDATION **********/

function validateForm(cFormId){
	var oForm = document.getElementById(cFormId);
	var oElement, cType, cName, cValue, cMsg, cOutput;
	
	cOutput = "";
	
	if(!oForm)
	{
		return false;
	}
	
	
	
	for(var i=0; i<oForm.elements.length; i++){
		oElement = oForm.elements(i);
		
		cType = oElement.type;
		cType = cType.toLowerCase();
		cName = oElement.name;
		cId = oElement.id;
		
		if(cId=='toValidate'){
				cMsg = document.getElementById(cName + "Alert");
				switch(cType){
					case "textarea":
					{
						cValue = oElement.value;
						if(cValue==undefined || cValue==''){
							cOutput += getMessage(cName) + "\n";
						}
						break;
					}
					
					case "text":
					{
						cValue = oElement.value;
						if(cValue==undefined || cValue==''){
							cOutput += getMessage(cName) + "\n";
						}else{
							if(cName.toLowerCase()=="email" || cName.toLowerCase()=="epost")
							{
								// validate e-mail
								if(!validEmail(cValue))
								{
									cOutput += getMessage(cName) + "\n";
								}
							}
						}	
						break;
					}
					
					case "select-one":
					{
						cValue = oElement.options[oElement.selectedIndex].value;
						
						if(oElement.selectedIndex==0){
							cOutput += getMessage(cName) + "\n";
						}
						break;
					}
					
					case "radio":
					{
						var good = false;

						var oRButtons = document.getElementsByName(cName);
						for(var j = 0; j < oRButtons.length; j++)
						{
							if(oRButtons[j].checked)
							{
								good = true;
							}
						}
						
						if(!good){
							cOutput += getMessage(cName) + "\n";
						}
						break;
					}
				}
		}
	}
	
	if(cOutput!=''){
		if(oForm.fillin){
			cOutput = oForm.fillin.value + "\n" + cOutput;
		}
	}

	return cOutput;
}

function validEmail(cMail){
	var r = new RegExp("^.+\\@.+\\..{2,3}$");
	if (!r.test(cMail)){
		return false;
	}
	
	return true;
}

function getMessage(cName){
	var cResult = "";
	
	var oMsg = document.getElementById(cName + "Alert");
	if(oMsg)
	{
		cResult = oMsg.innerText;
	}
	return cResult;
}

/********** FORM VALIDATION END **********/