////////////////////////////////////////////////////////////////////////////////
// Namespace: TJG
////////////////////////////////////////////////////////////////////////////////
TJG = [];

////////////////////////////////////////////////////////////////////////////////
// Class: TJG.QueryString
////////////////////////////////////////////////////////////////////////////////
TJG.QueryString = function() {
}
// Static members
TJG.QueryString.parse = function(queryString) {
	if (queryString.indexOf("?") != -1) {
		queryString = queryString.substr(1);
	}
	var nvc = [];
	var parameters = queryString.split("&");
	for (var i=0; i<parameters.length; i++) {
		var parts = parameters[i].split("=");
		var key = "";
		var value = "";
		if (parts.length > 0) {
			key = parts[0];
		}
		if (parts.length > 1) {
			value = decodeURIComponent(parts[1]).replace(/\+/ig, " ");
		}
		// don't pass in the DCMP tag from the querystring, should be set via set_dcmp() only.
		if (key != "DCMP") nvc[key] = value;
	}
	return nvc;
}
TJG.QueryString.toString = function(parameters) {
	var queryString = "?";
	var separator = "";
	for (var key in parameters) {
		queryString += separator + key + "=" + encodeURIComponent(parameters[key]);
		if (separator.length == 0) {
			separator = "&";
		}
	}
	return queryString;
}

////////////////////////////////////////////////////////////////////////////////
// Namespace: TJG.JobLink
////////////////////////////////////////////////////////////////////////////////
TJG.JobLink = [];

TJG.JobLink.validateLink = function(element) {
	var links = element.getElementsByTagName("a");
	if (links != null) {
		// Check for required back link.
		var re = new RegExp("^"+TJG.JobLink.RootUrl+"[/]*$", "i");
		for (var i=0; i<links.length; i++) {
			if (re.test(links[i].href)) {
				return true;
			}
		}
		
	}
	return false;
}
TJG.JobLink.createFrame = function(control, pageUrl) {
	if (TJG.JobLink.validateLink(control._element) && control._parameters["DCMP"] != null && control._parameters["DCMP"] != "") {
		// Create iframe
		control._iframe = document.createElement("iframe");
		control._iframe.id = control._element.id + "Frame";
		control._iframe.frameBorder = "0";
		control._iframe.src = pageUrl + TJG.QueryString.toString(control._parameters);
		if(navigator.userAgent.indexOf('MSIE') > -1){
    		control._iframe.scrolling = "yes";
		}
		if (control._autoSize) {
			control._iframe.width = "100%";
			control._iframe.scrolling = "no";
		}
		else {
			control._iframe.width = control._width;
			control._iframe.height = control._height;
		}
		control._element.innerHTML = "";
		control._element.appendChild(control._iframe);
	}
	else
	{
	    var tjnlDomainName = "www.totaljobs.nl";
	    var tjnlPort = "8051";
	    
        if (pageUrl.indexOf(tjnlDomainName) > -1 || pageUrl.indexOf(tjnlPort) > -1)
        {
            alert("Om uw Job link site goed te laten functioneren, dient het " + TJG.JobLink.RootUrl + " logo en uw unieke referentie aanwezig te zijn. Controleer alstublieft uw Job Link code snippet features: de div id 'divJoblinkSearch' en de 'set_dcmp' method.");
	    }
	    else 
	    {
    	    alert("In order for your Job Link site to work, the " + TJG.JobLink.RootUrl + " logo and your unique reference must be present. Please check your Job Link code snippet features the div id 'divJoblinkSearch', and the 'set_dcmp' method. ");
	    }			   
	}
}
TJG.JobLink.resizeFrame = function(elementId) {
	if (window.location.hash && window.location.hash != "#") {
		var h = window.location.hash.substring(1) + "px";
		var control = document.getElementById(elementId)._control;
		if (control._iframe.height != h && h != "0px") {
			control._iframe.height = h;
			window.clearInterval(control._resizeTimer);
		}
	}
}

////////////////////////////////////////////////////////////////////////////////
// Class: TJG.JobLink.Search
////////////////////////////////////////////////////////////////////////////////
TJG.JobLink.Search = function(element) {
	this._element = element;
	this._element._control = this;
	this._iframe = null;
    this._parameters = TJG.QueryString.parse(document.location.search);
    this._width = null;
    this._height = null;
    this._autoSize = false;
    this._resizeTimer = null;
}
// Instance members
TJG.JobLink.Search.prototype = {
    // Width in pixels
    set_width: function(value) {
        this._width = value;
    },
    // Height in pixels
    set_height: function(value) {
        this._height = value;
    },
    // Path to CSS file
    set_css: function(value) {
        this._parameters["Css"] = value;
    },
    // URL of job results page
    set_resultsUrl: function(value) {
        this._parameters["ResultsUrl"] = value;
    },
	// Company ID
	set_companyId: function(value) {
        this._parameters["CompanyId"] = value;
	},
	// Industry ID
	set_industryId: function(value) {
        this._parameters["Industry"] = value;
	},
	// Search keywords
	set_keywords: function(value) {
        this._parameters["Keywords"] = value;
	},
	// Search location text
	set_location: function(value) {
        this._parameters["LTxt"] = value;
	},
	// Number of results per page
	set_pageSize: function(value) {
		// Page size must be between 5 and 20
		value = Math.min(value, 20);
		value = Math.max(value, 5);
        this._parameters["PageSize"] = value;
	},
	// Proximity radius
	set_radius: function(value) {
        this._parameters["Radius"] = value;
	},
	// Starting salary rate
	set_rate: function(value) {
        this._parameters["Rate"] = value;
	},
	// Salary rate type
	set_rateType: function(value) {
        this._parameters["RateType"] = value;
	},
	// Posted Days
	set_postedDays: function(value) {
        this._parameters["PostedDays"] = value;
	},	
	// Results sort order
	set_sort: function(value) {
        this._parameters["Sort"] = value;
	},
	// Results AndOr
	set_andOr: function(value) {
        this._parameters["AndOr"] = value;
	},
    // DCMP tag
    set_dcmp: function(value) {
        this._parameters["DCMP"] = value;
    },
	clear: function() {
        if (this._parameters) {
			delete this._parameters;
        }
		this._parameters = [];
	},
    render: function() {
		// This control does not support auto resizing
		TJG.JobLink.createFrame(this, TJG.JobLink.RootUrl + TJG.JobLink.SearchPage);
    }
}

////////////////////////////////////////////////////////////////////////////////
// Class: TJG.JobLink.Results
////////////////////////////////////////////////////////////////////////////////
TJG.JobLink.Results = function(element) {
	this._element = element;
	this._element._control = this;
	this._iframe = null;
    this._parameters = TJG.QueryString.parse(document.location.search);
    this._width = null;
    this._height = null;
    this._autoSize = false;
    this._resizeTimer = null;
}
// Instance members
TJG.JobLink.Results.prototype = {
    // Width in pixels
    set_width: function(value) {
        this._width = value;
    },
    // Height in pixels
    set_height: function(value) {
        this._height = value;
    },
    // Path to CSS file
    set_css: function(value) {
        this._parameters["Css"] = value;
    },
    // URL of job results page
    set_resultsUrl: function(value) {
        this._parameters["ResultsUrl"] = value;
    },
    // URL of job details page
    set_detailsUrl: function(value) {
        this._parameters["DetailsUrl"] = value;
    },
	// Company ID
	set_companyId: function(value) {
        this._parameters["CompanyId"] = value;
	},
	// Industry ID
	set_industryId: function(value) {
        this._parameters["Industry"] = value;
	},
	// Search keywords
	set_keywords: function(value) {
        this._parameters["Keywords"] = value;
	},
	// Search location text
	set_location: function(value) {
        this._parameters["LTxt"] = value;
	},
	// Number of results per page
	set_pageSize: function(value) {
		// Page size must be between 5 and 20
		value = Math.min(value, 20);
		value = Math.max(value, 5);
        this._parameters["PageSize"] = value;
	},
	// Proximity radius
	set_radius: function(value) {
        this._parameters["Radius"] = value;
	},
	// Starting salary rate
	set_rate: function(value) {
        this._parameters["Rate"] = value;
	},
	// Salary rate type
	set_rateType: function(value) {
        this._parameters["RateType"] = value;
	},
	// Results sort order
	set_sort: function(value) {
        this._parameters["Sort"] = value;
	},
    // DCMP tag
    set_dcmp: function(value) {
        this._parameters["DCMP"] = value;
    },
	clear: function() {
        if (this._parameters) {
			delete this._parameters;
        }
		this._parameters = [];
	},
    render: function() {
		// NB: Cannot have multiple instances of Results control on one page, due to the use of document.location.search.
		if (document.location.search.indexOf("LTxt") > -1 && document.location.search.indexOf("LIds") == -1) {
			this._parameters["LSel"] = 1;
			// If no dimensions specified, assume auto sizing.
//			if (this._height == null && this._width == null) {
//				this._autoSize = true;
//			}
			// If auto height required, sets ParentUrl
//			if (this._autoSize) {
//			    this._parameters["ParentUrl"] = document.URL;
//			}
			TJG.JobLink.createFrame(this, TJG.JobLink.RootUrl + TJG.JobLink.LocationSelectPage);
			// Start timer for resizing iframe
//			if (this._autoSize) {
//				this._resizeTimer = window.setInterval(new Function("TJG.JobLink.resizeFrame('"+this._element.id+"');"));
//			}
		}
		else {
			if (this._parameters["JobId"] != null && this._parameters["JobId"].length > 0) {
				// If JobId parameter exists, then hide results control.
				this._element.innerHTML = "";
			}
			else {
				if (this._parameters["Keywords"] != null) {
					// If no dimensions specified, assume auto sizing.
//					if (this._height == null && this._width == null) {
//						this._autoSize = true;
//					}
					// If auto height required, sets ParentUrl
//					if (this._autoSize) {
//					    this._parameters["ParentUrl"] = document.URL;
//					}
					// If Keywords parameter exists, then display results control.
					TJG.JobLink.createFrame(this, TJG.JobLink.RootUrl + TJG.JobLink.ResultsPage);
					// Start timer for resizing iframe
//					if (this._autoSize) {
//						this._resizeTimer = window.setInterval(new Function("TJG.JobLink.resizeFrame('"+this._element.id+"');"));
//					}
				}
				else {
					// If not, hide results control.
					this._element.innerHTML = "";
				}
			}
		}
    }
}

////////////////////////////////////////////////////////////////////////////////
// Class: TJG.JobLink.Details
////////////////////////////////////////////////////////////////////////////////
TJG.JobLink.Details = function(element) {
	this._element = element;
	this._element._control = this;
	this._iframe = null;
    this._parameters = TJG.QueryString.parse(document.location.search);
    this._width = null;
    this._height = null;
    this._autoSize = false;
    this._resizeTimer = null;
}
// Instance members
TJG.JobLink.Details.prototype = {
    // Width in pixels
    set_width: function(value) {
        this._width = value;
    },
    // Height in pixels
    set_height: function(value) {
        this._height = value;
    },
    // Path to CSS file
    set_css: function(value) {
        this._parameters["Css"] = value;
    },
	// Company ID
	set_companyId: function(value) {
        this._parameters["CompanyId"] = value;
	},
	// Industry ID
	set_industryId: function(value) {
        this._parameters["Industry"] = value;
	},
	// Search keywords
	set_keywords: function(value) {
        this._parameters["Keywords"] = value;
	},
	// Search location text
	set_location: function(value) {
        this._parameters["LTxt"] = value;
	},
	// Number of results per page
	set_pageSize: function(value) {
		// Page size must be between 5 and 20
		value = Math.min(value, 20);
		value = Math.max(value, 5);
        this._parameters["PageSize"] = value;
	},
	// Proximity radius
	set_radius: function(value) {
        this._parameters["Radius"] = value;
	},
	// Starting salary rate
	set_rate: function(value) {
        this._parameters["Rate"] = value;
	},
	// Salary rate type
	set_rateType: function(value) {
        this._parameters["RateType"] = value;
	},
	// Results sort order
	set_sort: function(value) {
        this._parameters["Sort"] = value;
	},
    // DCMP tag
    set_dcmp: function(value) {
        this._parameters["DCMP"] = value;
    },
	clear: function() {
        if (this._parameters) {
			delete this._parameters;
        }
		this._parameters = [];
	},
    render: function() {
		if (this._parameters["JobId"] != null && this._parameters["JobId"].length > 0) {
			// If no dimensions specified, assume auto sizing.
//			if (this._height == null && this._width == null) {
//				this._autoSize = true;
//			}
			// If auto height required, sets ParentUrl
//			if (this._autoSize) {
//			    this._parameters["ParentUrl"] = document.URL;
//			}
			// If JobId parameter exists, then display details control.
			TJG.JobLink.createFrame(this, TJG.JobLink.RootUrl + TJG.JobLink.DetailsPage);
			// Start timer for resizing iframe
//			if (this._autoSize) {
//				this._resizeTimer = window.setInterval(new Function("TJG.JobLink.resizeFrame('"+this._element.id+"');"));
//			}
		}
		else {
			// If not, hide details control.
			this._element.innerHTML = "";
		}
    }
}
TJG.JobLink.RootUrl = 'http://www.retailchoice.com';
TJG.JobLink.SearchPage= '/JobLink/JobSearch.aspx';
TJG.JobLink.ResultsPage = '/JobLink/Results.aspx';
TJG.JobLink.DetailsPage = '/JobLink/JobDetails.aspx';
TJG.JobLink.LocationSelectPage = '/JobLink/LocationSelect.aspx';
