﻿function TemplateParser(outdivid,tabcontrolsdivid,state1,state2,wrappedresult){
    this._outdivid = outdivid;
    this._tabcontrolsdivid = tabcontrolsdivid;
    this.requiredcontrols = function(){return jQuery( this._outdivid +" .required");};
    this.checknumbercontrols = function(){ return jQuery( this._outdivid +" .checknumber");};
    this.inputcontrols =  function(){return jQuery(this._outdivid).find("input, select, textarea");};
    this.hastemplate = false;
    this._thetemplate = jQuery(this._outdivid);
    this._thecontrolbar = jQuery(this._tabcontrolsdivid);
    this._state2 = state2;
    this._state1 = state1;
    this._resultdiv = wrappedresult;
     this.hastemplate = function(){
        if(this._thetemplate.length > 0){
            if (this._thetemplate.html().length > 50){
                return true;
            }
        }
        return false;
    }
    this.init = function(){
        if(!this.hastemplate()){
           this._thecontrolbar.remove();
        }
        else{
            this._thecontrolbar.find("a").html(this._state1).unbind("click.once").bind("click.once",{obj:this},function(event){
                var thetempate = event.data.obj;
                thetempate.showorhidetemplate(false);
            })
        }
    }
    this.init();
    this.showorhidetemplate = function(forceshow){
           var thebutton = this._thecontrolbar.find("a");
          if (!this._thetemplate.is(":visible") || forceshow ){
            this._thetemplate.show();
            thebutton.attr("class","tab_grey")
            thebutton.html(this._state2)
           }
           else{
            this._thetemplate.hide()
           thebutton.attr("class","tab_silver")
            thebutton.html(this._state1)
           
           }
    }
    this.GetTemplatevalue = function(){
        var output = "";
        this.inputcontrols().each(function(i)
        {
            theobject = jQuery(this);
            if (theobject.is("input:checkbox") && (!theobject.is(":checked"))) return;
            var name = theobject.attr("name");
            if (theobject.val() == '' || theobject.val() == null) return;
            var group = 'normal';
            if (theobject.attr("group"))
            {
                group = theobject.attr("group");
            }
            var continueaddvalue = true;
			var thevalue = theobject.val()
			//if droplist loop through the number of times selected
			if (theobject.is("select")){
				continueaddvalue = false;
				//get value from the form elelemt: should look like 'Tank|1'
				
				//continue add the value into returnValue if the value of the select does not match the rule "Tank|1"
				if (thevalue.indexOf("|") == -1)continueaddvalue=true;
				//split value into "name" and "quantity" 
				var stringArray = thevalue.split("|")
				var objname = stringArray[0];
				var objQuantity = stringArray[1];
				 for(i=0;i<objQuantity;i++){
							output += name + ":" + objname + "|group:" + group + ";";
				 } // end of FOR
			} // if (theobject.is("select")){ END
			if(continueaddvalue){
				output += name + ":" + thevalue + "|group:" + group + ";";
			}
        });
        //alert(output);
        return output;
    }
    this.isValid =  function()
    {
        var errormsg = "";
        
        this.requiredcontrols().each(function(i)
        {
            theobject = jQuery(this);
            if (theobject.val() == '' || theobject.val() == null)
            {
            
               errormsg +="<li>"+ theobject.attr("name") +" is required </li>"
            }
        });
     
        this.checknumbercontrols().each(function(){
            var theoject = jQuery(this);
            var checkboxs = theoject.find("input");
            var number = theoject.attr("optionsToSelect");
            //get all check box which is checked
            var n = theoject.find("input:checked").length;
			//get all dropdownlist 
			var alldropdownlists = theoject.find("select");
			if (alldropdownlists.length > 0){
				for(i=0;i<alldropdownlists.length;i++){
//--------- lee 29/6/10 - deal with droplist that have muliplte qunatities for WI free gifts
// looking for droplist list that follow the rule tank|1

				var thevalue = alldropdownlists.eq(i).val();
				if (thevalue.indexOf("|") != -1){
					//split value into "name" and "quantity" 
					var stringArray = thevalue.split("|")
					var objQuantity = stringArray[1];
					n = n + objQuantity * 1
				}else{
					n = n + thevalue * 1
				} // if
//--------------------------------------------------------------------------------------------------	
				}
			}
            if (n != number)
            {
                var errMsg = theoject.attr("errormsg");
		var itemstring = " items";
		if (number == 1){ itemstring= " item"}
                if (errMsg == '' || errMsg == 'undefined') errMsg = "Please choose " + number + itemstring;
                errormsg +="<li>"+ errMsg +"</li>";
                
            } 
        });
       
        if (errormsg != ""){
           errormsg = "<ul>"+errormsg+"</ul>";
            if ( !jQuery(this._resultdiv +" .templateErrMsg").length > 0){
                 jQuery(this._resultdiv).prepend("<div class='templateErrMsg'></div>");
            }
            jQuery(this._resultdiv + " .templateErrMsg").html(errormsg);
        }
        else
        {
            jQuery(this._resultdiv + " .templateErrMsg").html("");
        }
	return (errormsg == "");
     }
}
String.prototype.endsWith = function(str) {
    return (this.match(str + 'jQuery') == str)
}
String.prototype.trimEnd = function(c) {
    if (c)        
        return this.replace(new RegExp(c.escapeRegExp() + "*jQuery"), '');
    return this.replace(/\s+jQuery/, '');
}
String.prototype.trimStart = function(c) {
    if (c)
        return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
    return this.replace(/^\s+/, '');
}
String.prototype.escapeRegExp = function() {
    return this.replace(/[.*+?^jQuery{}()|[\]\/\\]/g, "\\jQuery0");
};