/** 
* @projectDescription 	BindCheck's polyvalent URLParser class
*
* @author	Stefano Di Paola stefano.dipaola@mindedsecurity.com
* @version	0.1 
* @namespace	BindCheck
*
* Usage: var p = new BindCheck.URLParser('http://user:password@BindCheck.com/pathname?arguments=1#fragment');
* p.getHost() == 'BindCheck.com';
* p.getProtocol() == 'http';
* p.getPathname() == '/pathname';
* p.getQuerystring() == 'arguments=1';
* p.getFragment() == 'fragment';
* p.getUsername() == 'user';
* p.getPassword() == 'password';
* p.isGoodProto(); 
* p.isBadProto() ;
*
* See the unit test file for more examples.
* URLParser is freely distributable under the terms of an MIT-style license.
*/

if (typeof BindCheck == 'undefined')
 var BindCheck = {};

/**
 * Creates an URLParser instance
 *
 * @classDescription	Creates an URLParser instance
 * @return {Object}	return an URLParser object
 * @param {String} url	The url to parse
 * @constructor
 * @exception {String}  Throws an exception if the specified url is invalid
 */
BindCheck.URLParser = function(url) {

 if(typeof this._a == 'undefined' )
  this._a = document.createElement('A');

 this._a.setAttribute("href",url);
 this._fields = {'Username' : 'username', 'Password' : 'password', 'Port' : 'port', 'Protocol' : 'protocol', 'Host' : 'host', 'Pathname' : 'pathname', 'URL' : 'href', 'Querystring' : 'search', 'Fragment' : 'hash'};
 
 // Create the getters for every parsed field 
 for(var f in this._fields)
   this['get' + f] = this._makeGetter(f);

}
 
/**
 * @method 
 * @param {String} url	The url to parse
 * @exception {String} 	Throws an exception if the specified url is invalid
 */
BindCheck.URLParser.prototype.setURL = function(url) {
this._a.setAttribute("href",unsecape(url));
}

BindCheck.URLParser.prototype._makeGetter = function(field) {
 return function() {
  return this._a[this._fields[field]];
 }
}

// Blacklist Approach
BindCheck.URLParser.prototype.isBadProto = function(proto) {
 if(proto==null){
  proto= this.getProtocol();

  if(isIE)
   if(proto == null || proto == '' || proto==':' ) {
    // if protocol is empty then the link is forced to be a relative link.	 
      uri=(document.location.href.substring(0,document.location.href.indexOf('?') ))
      return  uri.substring(0,uri.lastIndexOf('/') + 1)+this._a.href;
    }
    else if(proto == null ) { 
      // if protocol is null then return is bad protocol (non IE browsers)
 
     return true;
   }
 }
  displayObject("debug", document.location ); 
 if(proto.match(/script/) || proto.match(/data/)|| proto.match(/chrome/) || proto.match(/wysiwyg/) )
   return  true;
 else return false;
 
}


// Whitelist Approach
BindCheck.URLParser.prototype.isGoodProto = function(proto) {
 if(proto==null){

  proto= this.getProtocol();
  
  if(proto == null)
   {
     return false;
   }
 }
 
 if(proto == 'http' || proto == 'ftp')
 return  true;
 else return false;
 
}

// Facilities for document parsing
function displayObject(id,obj){
var elid=document.getElementById(id);
if(elid.style.display!='none')
 for (var i in obj) if(!i.match(/inner|outer/))
  elid.innerHTML+=i+" "+obj[i]+" "+typeof obj[i]+"<br>";
}
BindCheck.documentParser =  function( ) {  }

BindCheck.documentParser.prototype.getCharset = function(doc ) { return doc.characterSet }

BindCheck.documentParser.prototype.styleParser =  function( cssText ){ 
 if(isIE){
 if(typeof this._bstyle == 'undefined' )
  this._bstyle = document.createElement('A');

  this._bstyle.style.cssText= cssText;

 if(typeof this._astyle == 'undefined' )
  this._astyle = document.createElement('A'); 
  
 this._astyle.style.cssText = this._bstyle.style.cssText.toString() ;
 delete this._bstyle;
 
  alert(this._astyle.style.cssText)
 displayObject("debug",this._astyle.style);
// try{
if(isIE && this._astyle.style["cssText"].toString().match(/url|expression/i)){
   alert("Sorry, the text contained expression or Url");
     this._astyle.style["cssText"]='';
}
}else{ 
 if(typeof this._astyle == 'undefined' )
  this._astyle = document.createElement('A');

  this._astyle.style.cssText= cssText;

for (var i in this._astyle.style){
 //alert(typeof this._astyle.style[i]+" "+ i );
 
//   try{
//   alert(i);
//   if(i=='clear')  
//    alert( this._astyle.style.getExpression( "clear" ) );
//     }catch(w){alert("c "+i);}
     
     
   if((typeof this._astyle.style[i])==="string" && this._astyle.style[i].toString().match(/url|expression/i) ){
    if(!isIE)
     this._astyle.style.removeProperty(this._astyle.style[i]);
    else
    this._astyle.style[i]='';
// displayObject("debug",this._astyle.style);
     alert("Removed "+i+" " );
     return false;
    }
   }
   
}
//   alert("finito");
//    }catch(e){alert(e+" "+typeof this._astyle.style[i]);}
// displayObject("debug",this._astyle.style);
//  alert(this._astyle.style.cssText)
if(isIE)
 return this._astyle.style.cssText;
 else{ 
 return true;
 }
}

BindCheck.documentParser.prototype.sanitizeTag =  function() { 


}

