var dtCh= ".";
var minYear=1900;
var maxYear=2100;
function expander(a,b){
	var obj=document.getElementById(a).style;
	if(b){
		if(b==1){obj.display='';}
		else{obj.display='none';}
	} else{obj.display=(obj.display =='none')?'':'none';}
}
function enableField(a,b,c){
    var obj=document.getElementById(a);
    var obj1=document.getElementById(b);
    if(c==1){
        obj.disabled=true;
        obj1.disabled=true;
    } else {
        obj.disabled=false;
        obj1.disabled=true;
    }
}
function createRow(parentobj,childtype,childid,childclass){
    var parent = parentobj.parentNode;
    var nextrow=parentobj.nextSibling;
	//CREATE DETAILROW
	var newtag = document.createElement(childtype);
	newtag.setAttribute('id',childid);
	newtag.setAttribute('class',childclass);
	newtag.setAttribute('className',childclass);
	var text = document.createTextNode('');
	//
	newtag.appendChild(text);
	parent.appendChild(newtag);
	parent.insertBefore(newtag,nextrow);
	return newtag;
}
function createInnerElem(parentobj,childtype,childid,childclass){
    var nextrow=parentobj.lasthild;
	//CREATE DETAILROW
	var newtag = document.createElement(childtype);
	newtag.setAttribute('id',childid);
	newtag.setAttribute('class',childclass);
	newtag.setAttribute('className',childclass);
	var text = document.createTextNode('');
	//
	newtag.appendChild(text);
	parentobj.appendChild(newtag);
	//parentobj.insertBefore(newtag,nextrow);
	return newtag;
}
function checkRequired(obj){
	var toret=true;
	var val=trim(obj.value);
	
	if(obj.getAttribute('required')&&obj.getAttribute('required')=='1'){
		if(obj.getAttribute('requiredtype')){
			switch(obj.getAttribute('requiredtype')){
				case 'integer':
					toret=isInteger(val);
					break;
				case 'gsmnumber':
            		if(!(isInteger(val)&&val.length==8&&(val.charAt(0)==9||val.charAt(0)==4))) toret=false;
					break;
				case 'date':
            		toret=isDate(val);
					break;
				default:
					break;
			}
		} else if(val=='') toret=false;
	}
	if(!toret) updateWarning(obj,1);
	else updateWarning(obj);
	return toret;
}
function updateWarning(obj,set){
	var objclass=getCSSClass(obj);
	if(set){
		if(!objclass.match('warn')) setCSSClass(obj,objclass+' warn');
	} else {
		if(objclass.match('warn')){
			var classy=objclass.substring(0,objclass.indexOf('warn')-1);
			setCSSClass(obj,classy);
		}
	}
}
function recalcTotal(fld,total,ref){
	var total=document.getElementById(total);
	var ref=document.getElementById(ref);
	var newtotal=parseFloat(ref.value)-fld.value;   
	if(newtotal<0){ alert('Totalpris må ikke være negativ!');fld.value=""; }
	else {
		total.innerHTML=newtotal;
		var dibstotal=document.getElementById('amount');
		if (dibstotal!=null){
			//dibstotal.value=newtotal*100;
			var loc="/index.php/Dispatcher/action/recalculatedibs/newtotal/"+newtotal;
			asyncloadHTML(loc,'GET','','dibsblock');
		}
	}
}
function goDibs(){
		var ccform=document.getElementById('ccform');
		var accurl=ccform.accepturl;
		var baseaccurl=document.getElementById('baseaccepturl');
		var callbackurl=document.getElementById('callbackurl');

		var es=document.getElementById('extrasubs');
		if(es)
			exsubs=es.value;
		else exsubs=0;
		var data="/extrasubs/"+exsubs;

		accurl.value=baseaccurl.value+data;
		if(window.location.protocol=="https:")
			callbackurl.value='https://'+location.hostname+'/index.php/Portal/section/dibscallback/oid/'+document.getElementById('orderid').value;
		else 
			callbackurl.value='http://'+location.hostname+'/index.php/Portal/section/dibscallback/oid/'+document.getElementById('orderid').value;
		callbackurl.value=callbackurl.value+data;
	
		// Send data to Visa
		//alert("Dibs: "+accurl.value+"\n\nCallback: "+callbackurl.value);
		ccform.submit();
}

function deleteRow(obj){var parent = obj.parentNode;parent.removeChild(obj);}
function openDialog(url,params){params=(params)?params:"location=0,status=0,scrollbars=0,height=300,width=600,menubar=0,resizable=1";var newWindow=window.open(url,"",params);}
function execHint(hint,obj){if(hint){if(obj){obj.value=hint;}else{var hintlen=hint.length;if(hint.substring(hintlen-1,hintlen)==')'){eval(hint);}else{ eval(hint+ '()');}}}}
function showAlert(content,target,hint){alert(content);var obj=document.getElementById(target);execHint(hint,obj);return false;}
function showContent(response,target,hint){var contentDiv = document.getElementById(target);if(contentDiv){if(trim(response)!='')contentDiv.innerHTML = response;else{var sobj=document.getElementById(target).style;sobj.display='none';}}execHint(hint);return true;
}
function dummyContent(response,target,hint){execHint(hint);return true;}
function dummy(){}
function updateVar(variable,value){var temp=""+variable+"='"+value+"'";eval(temp);}

function setCSSClass(obj,classy){if(classy){obj.setAttribute('class',classy);obj.setAttribute('className',classy);}else{obj.removeAttribute('class');obj.removeAttribute('className');}
}function getCSSClass(obj){var classname=obj.getAttribute('class');if(classname){ return classname;}return obj.getAttribute('className');}
function delay(milliseconds){var then, now;then = new Date().getTime();now = then;while ((now - then) < milliseconds){ now = new Date().getTime();}}
function redirect(url){self.location=url;}
function reload(){self.location.reload();}
function trim(str){return str.replace(/^(\s+)?(\S*)(\s+)?$/, '$2');}
function ltrim(str){return str.replace(/^\s*/, '');}
function rtrim(str){return str.replace(/\s*$/, '');}
function isInteger(s){var i;for (i = 0; i < s.length; i++){var c = s.charAt(i);if (((c < "0") || (c > "9"))){ return false;}}return true;}
function stripCharsInBag(s, bag){var i;var returnString = "";for (i = 0; i < s.length; i++){var c = s.charAt(i);if (bag.indexOf(c) == -1){returnString += c;}}return returnString;}
function daysInFebruary (year){return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );}
function DaysArray(n){for (var i = 1; i <= n; i++) {this[i] = 31;if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}if (i==2) {this[i] = 29;}}return this;}
function isDate(dtStr){var daysInMonth = new DaysArray(12);var pos1=dtStr.indexOf(dtCh);var pos2=dtStr.indexOf(dtCh,pos1+1);var strDay=dtStr.substring(0,pos1);var strMonth=dtStr.substring(pos1+1,pos2);var strYear=dtStr.substring(pos2+1);strYr=strYear;if (strDay.charAt(0)=="0" && strDay.length>1){ strDay=strDay.substring(1);}if (strMonth.charAt(0)=="0" && strMonth.length>1){ strMonth=strMonth.substring(1);}for (var i = 1; i <= 3; i++) {if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);}month=parseInt(strMonth);day=parseInt(strDay);year=parseInt(strYr);if (pos1==-1 || pos2==-1){alert("Dato format er : dd.mm.åååå");return false;}if (strMonth.length<1 || month<1 || month>12){alert("Vannligst skriv riktig måned");return false;}if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){alert("Vannligst skriv riktig dag");return false;}if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){alert("Vannligst skriv årstal mellom "+minYear+" og "+maxYear);	return false;}if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){alert("Vannligst skriv riktig adto");return false;}return true;}
function openUrl(url,params){params=(params)?params:"location=0,status=0,scrollbars=0,height=300,width=600,menubar=0,resizable=1";var newWindow=window.open(url,"",params);}
function updateDIBS(content,target){target=document.getElementById(target);target.innerHTML=content;}