// Affiliate Ad loader

var load_top = 0;

if(typeof idevaff_form == 'undefined') {
	var idevaff_form = {};											// wrapper object
}

idevaff_form.server = 'http://aff.paravion.ro/';						// paravion server

idevaff_form.script_id = 'aff_script_form';								// loader script script ID
idevaff_form.affURL = 'http://aff.zitec.ro/idevaffiliate.php?id=';	// affiliate tracker URL
idevaff_form.remoteURL = idevaff_form.server + 'static/js/affiliate';		// remote scripts folder
idevaff_form.adID = 'idevaff_ad_form';									// id of ad div container
idevaff_form.iframeID = 'idevaff_iframe_form';							// container iframe ID
idevaff_form.formName = 'cautare';									// name of the form inside the iframe
idevaff_form.iframeLocation = idevaff_form.server + 'affiliate-form17'; 	// form iframe location / src
idevaff_form.iframeLoaded = 0;
idevaff_form.iframeWidth = 614;
idevaff_form.iframeHeight = 118;

idevaff_form.showAd = function () {
	this.loader_script = document.getElementById(this.script_id);
	if( this.loader_script ) {
		this.afflink = this.loader_script.getAttribute('afflink');
		//~ alert(this.afflink);
		if( this.afflink ) {
		
			//~ alert(afflink);
			//~ alert(this.affURL);
			var aff_id_parts_c = this.afflink.replace(this.affURL,'').split('_');
			this.aff_id = aff_id_parts_c[0];
			
			this.createAd();
			this.loadAd(this.remoteURL+'/aff'+this.aff_id+'.js');
		}
	}

}

idevaff_form.createAd = function() {
	// creates iframe container DIV, e.g.: <div id="idev_aff" style="width:300px; height: 300px; border:#000 1px solid; padding:2px;"></div>
	this.ad = document.createElement('DIV');
	this.ad.id = this.adID;
	//this.ad.style.width = '946px';
	this.ad.style.width = '615px';
	this.ad.style.height = '118px';
	//this.ad.style.border = '#000 1px solid';
	this.ad.style.padding = '1px';
       this.ad.style.margin = '0 0 0 0';
	   
	this.loader_script.parentNode.insertBefore(this.ad, this.loader_script);
}

idevaff_form.loadAd = function(url) {
    var s_form_c = document.createElement('script');
    s_form_c.src = url;
	
    s_form_c.type = 'text/javascript';
    // s.owner = this; // closure
    var loaded = false;
    //*
    s_form_c.onload = s_form_c.onreadystatechange = function() {
    	load_top = 0;
		try{
    		var rs = this.readyState;
    	} catch(e) {
    		alert('rs error');
    		var rs = null;
    	}
	    if (rs && rs!='complete' && rs!='loaded') return;
	    if (loaded) return;
	    loaded = true;
	    //document.body.removeChild(s);
	    //alert('affiliate script loaded');
    	//idevaff_form.addAffFields();
    }
    //*/
    this.loader_script.parentNode.insertBefore(s_form_c, this.loader_script);
}

idevaff_form.addAffFields = function() {

	//alert('add fields called - iframe loaded: ' + idevaff_form.iframeLoaded);

	// populate idev_id and idev_afflink in iframe's location to fill in the search form inside the container iframe at submit time
	var ifr_c = null;
	try {
		ifr_c = document.getElementById(idevaff_form.iframeID);
	} catch(e) {
		//alert('Error - cannot get iframe');
	}


	if( ifr_c && (idevaff_form.iframeLoaded == 1) ) {

		//alert('iframe loaded');

		var iwin_c;
		try {
			if(navigator.userAgent.indexOf("Safari") != -1){
				iwin_c = frames[idevaff_form.iframeID];
			}else{
				iwin_c = document.getElementById(idevaff_form.iframeID).contentWindow;
			}
		} catch(e) {
			//alert('Error on getting iwin');
		}
		//iwin.location = iwin.location + '#' + this.aff_id + '|' + this.afflink;

		try {
			iwin_c.location = idevaff_form.iframeLocation + '#' + idevaff_form.aff_id + '|' + idevaff_form.afflink + '|' + idevaff_form.server;
			//iwin.location = idevaff_form.iframeLocation + '#' + idevaff_form.aff_id ;
		} catch(e) {
			//alert('Location error');
		}
		// alert(iwin.location);

	} else {
		try{
			setTimeout(function(){idevaff_form.addAffFields();}, 2000);
		} catch(e) {
			//alert('error on recursive call');
		}
	}

	/*
	var doc = window.frames[idevaff_form.iframeID].document;
	*/
}

idevaff_form.setIframeLoaded = function() {
	//alert('iframe onload handler here - first line');
	if(idevaff_form.iframeLoaded == 1) {
		//document.getElementById(idevaff_form.iframeID).onload = function() {};
		return;
	}
	idevaff_form.iframeLoaded = 1;
	idevaff_form.addAffFields();
	//alert('iframe onload handler here - last line');
}

idevaff_form.showAd();
