/*
	This note must stay intact.
	
	Author:
		Mattias Rundqvist, Webparts (www.webparts.se)
	License:
		Creative Commons Attribution-Share Alike 3.0 License
		http://creativecommons.org/licenses/by-sa/3.0/
*/

var wp_Prototypes = true;

Array.prototype.find = function( value ) {
    for(count = 0; count<this.length; count++) {
        if( this[count] == value ) return count;
    }
    return -1;
}

Array.prototype.intersection = function( value ) {

	var result = new Array();
	
	if( typeof value != "object" ) {
		value = new Array(value);
	}
	
    for( var tc = 0; tc<this.length; tc++) {
		for( var vc = 0; vc < value.length; vc++ ) {
	        if( this[tc] == value[vc] ) {
				result.push_back(value[vc]);
			}
		}
    }
    return result;
}

Array.prototype.push_back = function( value ) {
	if( typeof value == "object" && value.length ) {
		for( var count = 0; count < value.length; count++ ) {
			this[this.length] = value[count];
		}
	} else {
		this[this.length] = value;
	}
}

Array.prototype.remove = function( index ) {
	var tmp = new Array();
	for( count = 0; count < this.length; count++ ) {
		if( index != count ) {
			tmp[tmp.length]=this[count];
		}
	}
	return tmp;
}

String.prototype.isMatch = function( valid ) {
	for( count = 0; count < this.length; count++ ) {
		if( valid.indexOf(this.substring(count,count+1)) == -1 )
			return false;
	}
	return true;			
};

String.prototype.isPattern = function( pattern ) {
	
	if( typeof pattern == "string" ) {
		var expression = "/^";
		var sign = "";
		for( count = 0; count<pattern.length; count++ ) {
			sign=pattern.substring(count,count+1);
			if( sign == "?" ) {
				expression+=".{1}";
			} else if( sign=="." ) {
				expression+="\\.{1}";
			} else if( sign=="*" ) {
				expression+=".{0,}";
			} else if( sign=="(" || sign==")" || sign=="+") {
				expression+=("\\"+sign+"{1}");
			} else {
				expression+=(sign+"{1}");
			}
		}
		expression += "$/";
		pattern=eval(expression);
	}
	if(this.search(pattern)==0)
		return true;
	return false;
};

String.prototype.isNum = function() {
	return this.isMatch("1234567890");
};

String.prototype.isDec = function() {
	return this.isMatch("1234567890,.");			
};

String.prototype.isAlpha = function() {
	return this.isMatch("abcdefghijklmnopqrstuvwxyzåäöæøüABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖÆØÜ ");			
};

String.prototype.isEmail = function() {
	return this.match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i) ? true : false;
}

String.prototype.trim = function() {
	return this.replace(/^\s*/,'').replace(/\s*$/,'');
};

String.prototype.right = function( length ) {
	return this.substring(this.length-length,this.length);
}

Date.prototype.getWeek = function() {
    var determinedate = new Date();
    determinedate.setFullYear(this.getFullYear(), this.getMonth(), this.getDate());
    var D = determinedate.getDay();
    if(D == 0) D = 7;
    determinedate.setDate(determinedate.getDate() + (4 - D));
    var YN = determinedate.getFullYear();
    var ZBDoCY = Math.floor((determinedate.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
    var WN = 1 + Math.floor(ZBDoCY / 7);
    return WN;
}

Date.prototype.originalToString = Date.prototype.toString;
Date.prototype.toString = function( format ) {
    if( typeof format == "undefined" ) {
        return this.originalToString();
    } 
        
    var str_date = format;

    str_date = str_date.replace(/yyyy/g,fourDigit(this.getFullYear()));
    str_date = str_date.replace(/yy/g,twoDigit( this.getFullYear().toString().substring(2,4) ) );
    str_date = str_date.replace(/MM/g,twoDigit(this.getMonth()+1));
    str_date = str_date.replace(/dd/g,twoDigit( this.getDate() ));
    str_date = str_date.replace(/HH/g,twoDigit(this.getHours()));
    str_date = str_date.replace(/hh/g,twoDigit( (this.getHours()>11)?(this.getHours()-12):this.getHours() ));
    str_date = str_date.replace(/mm/g,twoDigit(this.getMinutes()));
    str_date = str_date.replace(/ss/g,twoDigit(this.getSeconds()));

    return str_date; 
   
   
    function fourDigit( value ) {
        value = value.toString();
        value = (value.length==0?"0000"+value:value);
        value = (value.length==1?"000"+value:value);
        value = (value.length==2?"00"+value:value);
        value = (value.length==3?"0"+value:value);
        return value;
    }  
   
    function twoDigit( value ) {
        value = value.toString();
        value = (value.length==0?"00"+value:value);
        value = (value.length==1?"0"+value:value);
        return value;
    }  
}

Date.prototype.parseDate = function( str_date ) {
    str_date = str_date.replace(/\s/g,'').replace(/-/g,'').replace(/\./g,'').replace(/:/g,'');
   
    // -- år
    if( str_date.length >= 4 ) {
        this.setFullYear( str_date.substring(0,4) );
    }  
    // -- månad
    if( str_date.length >= 6 ) {
        this.setMonth( new Number(str_date.substring(4,6)-1) );
    } else { this.setMonth( 0 ) };
    // -- dag
    if( str_date.length >= 8 ) {
        this.setDate( new Number(str_date.substring(6,8)) );
    } else { this.setDate( 1 ) };
    // -- timme
    if( str_date.length >= 10 ) {
        this.setHours( new Number(str_date.substring(8,10)) );
    } else { this.setHours( 0 ) };
    // -- minut
    if( str_date.length >= 12 ) {
        this.setMinutes( new Number(str_date.substring(10,12)) );
    } else { this.setMinutes( 0 ) };
    // -- sekund
    if( str_date.length >= 14 ) {
        this.setSeconds( new Number(str_date.substring(12,14)) );
    } else { this.setSeconds( 0 ) };
}

document.createNamedElement = function(type, name) {
   var element = null;
   // Try the IE way; this fails on standards-compliant browsers
   try {
      element = document.createElement('<'+type+' name="'+name+'">');
   } catch (e) {
   }
   if (!element || element.nodeName != type.toUpperCase()) {
      // Non-IE browser; use canonical method to create named element
      element = document.createElement(type);
      element.name = name;
   }
   return element;
};

document.cloneElement = function(myObj)
{
	if(typeof(myObj) != 'object') return myObj;
	if(myObj == null) return myObj;

	var myNewObj = new Object();

	for(var i in myObj)
		myNewObj[i] = clone(myObj[i]);

	return myNewObj;
}

