// MCD Javascript API

/*##########################################
## VARIABLE MAP
##
## var a = customer id
## var b = export error
## var c = iframe height
## var d = iframe width
## var e = location domain name
## var f = user id
## func h = assemble iframe
## func i = iframe html
## func j = return faulty security
## global mcd_frame_shva = md5( user id )
## global mcd_frame_shvc = iframe height
## global mcd_frame_shvd = iframe width
## global mcd_frame_shvg = ajax/mysql paging
##########################################*/

(function(){
	var a = false;
	var b = false;
	var c = ( typeof mcd_frame_height !== 'undefined' ) ? mcd_frame_height : 1450;
	var d = ( typeof mcd_frame_width !== 'undefined' ) ? mcd_frame_width : 960;
	var e = window.location.href;
	var f = ( typeof mcd_frame_shva !== 'undefined' ) ? mcd_frame_shva : false;		
	var g = ( typeof mcd_frame_per_page !== 'undefined' ) ? mcd_frame_per_page : false;		
	function h(){ 
		if( f )
			document.write( i() );
		else
			document.write( j() );
		
		//alert( document.getElementsByName( 'mcd_iframe' )[0].innerHTML );
		//document.getElementsByName( 'mcd_iframe' )[0].getElementById( 'coupons-search' ).action = e;
	}
	
	function i(){ 
		var p = '';
		var q = '';
		var v = getUrlVars( 'search' );
		if( g )
			p = '&paginate=' + g;
		if( v )
			q = '&search=' + v;
		var iframe = '<iframe name="mcd_iframe" src="http://mycoupondatabase.com/api/coupons-iframe.php?secure=' + e + '&user=' + f + p + q + '&url=' + e + '" width="' + d + '" height="' + c + '" frameborder="0"></iframe>';
		
		return iframe;
	}
	
	function j(){
		alert( 'Your credentials did not match any we have on file. Please check your code and try again.' );
		var html = '<div style="font-weight:bold;font-size:small;">No coupons were imported. If you think this is our error please <a href="#">report it</a>.</div>';
		
		return html;	
	}
	
	h();
	
	function getUrlVars( v ) {
		var vars = {};
		vars[v] = false;
		var parts = window.location.href.replace( /[?&]+([^=&]+)=([^&]*)/gi, function( m, key, value ) {
			vars[key] = value;
		} );
		
		if( vars[v] ){
			return vars[v];
		} else {
			return false;	
		}
	}
})();
