//if (typeof (km_scripts) == 'undefined') var km_scripts = new Object();
//km_myclass_import("http://code.jquery.com/jquery-latest.js");

//function km_myclass_import(jsFile) {
//    if (km_scripts[jsFile] != null) return; 
//    var scriptElt = document.createElement('script');
//    scriptElt.type = 'text/javascript';
//    scriptElt.src = jsFile;
//    var body = document.getElementsByTagName('head')[0];
//    body.appendChild(scriptElt);
//    km_scripts[jsFile] = jsFile;
//}
//  

function addJavascript(jsname,pos) {
document.write('<scr' + 'ipt type="text/javascript" src="http://code.jquery.com/jquery-latest.js" ></scr' + 'ipt>');

/*var th = document.getElementsByTagName(pos)[0];
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src',jsname);
th.appendChild(s); //alert(""+s);  
*/
}

addJavascript('http://code.jquery.com/jquery-latest.js','head');

function getContentValue()
{

    var xmlhttp=false;
    var affiliateID = 4;
    var qStringName = 'couponcode';
    var qStringValue = displayItem(qStringName);
  
    var currentTime = new Date();
    var month = currentTime.getMonth();
    var day = currentTime.getDate()+14;
    var year = currentTime.getFullYear();

    if (qStringValue != null)
    {
        delete_cookie (qStringName);
	    set_cookie (qStringName,qStringValue,year,month,day);
	}
    else
	    qStringValue = get_cookie(qStringName);

    //alert(affiliateID + ' ' + qStringName + ' ' + qStringValue); 

    jQuery(document).ready(function() {

        var dataString = "affiliateID=" + affiliateID + "&qStringName=" + qStringName + "&qStringValue=" + qStringValue;
        var url = "http://client.dynamicdial.com/xmlhttp.aspx?" + dataString; 

        $.getJSON(url + "&jsoncallback=?", function(data) {
            if (data.retval.length > 1){

		var ctrl = document.getElementById('skinedp');

		if(ctrl != null) ctrl.innerHTML = data.retval; //alert(data.retval);
	    }

        });

        return false;
    });
    
	
} 
 
/*
To be called as below
set_cookie ( "username", "John Smith", 2003, 01, 15, "",
             "elated.com", "secure" );

*/

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

/*
To be called as delete_cookie ( "username" );
*/

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

/*
To be called as below 
var x = get_cookie ( "username" );
*/

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


        function PageQuery(q) 
        {
	        if(q.length > 1) 
		        this.q = q.substring(1, q.length);
	        else 
		        this.q = null;
	        this.keyValuePairs = new Array();
	        if(q) 
	        {
		        for(var i=0; i < this.q.split("&").length; i++) 
		        {
			        this.keyValuePairs[i] = this.q.split("&")[i];
		        }
	        }

	        this.getKeyValuePairs = function() { return this.keyValuePairs; }
	        this.getValue = function(s) 
	        {for(var j=0; j < this.keyValuePairs.length; j++) 
	        {
		        if(this.keyValuePairs[j].split("=")[0] == s)
		        return this.keyValuePairs[j].split("=")[1];
	        }
        	
	        return false;
	        }

	        this.getParameters = function() 
	        {
		        var a = new Array(this.getLength());
		        for(var j=0; j < this.keyValuePairs.length; j++) 
		        {
			        a[j] = this.keyValuePairs[j].split("=")[0];
		        }
		        return a;
	        }

	        this.getLength = function() { return this.keyValuePairs.length; } 
        }

        function queryString(key)
        {
	        var page = new PageQuery(window.location.search); 
	        return unescape(page.getValue(key)); 
        }

        function displayItem(key)
        {
	        if(queryString(key)=='false') 
	        {
		        return null;
	        }
	        else
	        {
		        return queryString(key);
	        }
        }   

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

window.onload=function() {getContentValue();}

