

// global variables
// for ajax
var xmlHttp;
var target;
//for function add new text fields  = addInput(divName)
var counter = 1;
var limit = 5;
//PUBLIC variable for Language
var LanguageSET = "en";
var quantity=0;
var neededNumber;

//######################################################################################
// function to change the content
function showContent(url, div){ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ('Browser does not support HTTP Request');
		return;
	} 
	if (url != "-") {
		// take the div ID where needs to input requested page
		target = div;
		//####
		xmlHttp.onreadystatechange=stateChanged;
		//request to server to get page	
		xmlHttp.open('GET',url,true);
		//####
		xmlHttp.send(null);
	} else {
		hideLanguageBar();
		document.getElementById(div).innerHTML = "Please choose the site from list";
	}
}//end function

// ajax function
function stateChanged(){ 
// if state is not ready yet
	if (xmlHttp.readyState==1 || xmlHttp.readyState=='loading'){ 
// insert ..... while it is loading	
		document.getElementById(target).innerHTML = "Loading...";
	}
// if state is complete	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete'){ 
// insert the loaded site	
		document.getElementById(target).innerHTML=xmlHttp.responseText;
 	} 
}//end function

//ajax function
function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
// differents between browsers
 		// Firefox, Opera 8.0+, Safari
	 	xmlHttp=new XMLHttpRequest();
   		}catch (e){
			//Internet Explorer
			try{
// making activexobject
  				xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
  			}catch (e){
  			xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
  		}
 	}
	return xmlHttp;
}//end function

function digitGen() {
	for (i=1; i<=5; i++) {
		if (!document.getElementById("adName_"+i))	{
			neededNumber=i;
			return neededNumber;
		} 
	}
}

//function to add new fields
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding inputs");
     }
     else {
	    var inn_material='';
		inn_material+='  <select id="adName_'+digitGen()+'" name="adName_'+digitGen()+'" style="width:126px" onchange="customAdFunction(this)">';
		inn_material+='    <option value="Large rectangle"> Large rectangle</option>';
		inn_material+='    <option value="Medium rectangle"> Medium rectangle</option>';
		inn_material+='    <option value="Half banner"> Half banner</option>';
		inn_material+='    <option value="Full banner"> Full banner</option>';
		inn_material+='    <option value="Leaderboard"> Leaderboard</option>';
		inn_material+='    <option value="Skyscraper"> Skyscraper</option>';
		inn_material+='    <option value="Wide-skyscraper"> Wide-skyscraper</option>';
		inn_material+='    <option value="customAd">Custom...</option>';
		inn_material+='  </select>';
		inn_material+='  <input id="adDimension_'+digitGen()+'" name="adDimension_'+digitGen()+'" type="text" style="width:77px">';   
		inn_material+='  <input id="adMaxKb_'+digitGen()+'" name="adMaxKb_'+digitGen()+'" type="text" style="width:77px">';   
		inn_material+='  <input id="adMaxKbFlash_'+digitGen()+'" name="adMaxKbFlash_'+digitGen()+'" type="text" style="width:87px">';     
		inn_material+='  <select id="adExpand_'+digitGen()+'" name="adExpand_'+digitGen()+'" style="width:77px">';     
		inn_material+='    <option value="yes">Yes</option>';
		inn_material+='    <option value="no">No</option>';
		inn_material+='  </select>';     
		inn_material+='  <input id="adExpandMaxKb_'+digitGen()+'" name="adExpandMaxKb_'+digitGen()+'" type="text" style="width:82px">  ';      
		inn_material+='  <input id="adExpandMaxKbFlash_'+digitGen()+'" name="adExpandMaxKbFlash_'+digitGen()+'" type="text" style="width:86px">';
		inn_material+='  <img src="styles/img/delete.png" border="0" alt="delete current row" style="cursor:pointer;width:10px;height:10px" onclick="removeField(this);" />';
		var newdiv = document.createElement('div');
		newdiv.style.marginTop = 5 + 'px';
		newdiv.innerHTML = inn_material;
		document.getElementById(divName).appendChild(newdiv);
		counter++;
		quantity++;
    }
}//end function
function removeField(but) {
	but.parentNode.parentNode.removeChild(but.parentNode);
	quantity--;
	limit++;
}

function customAdFunction(id) {
	if (id.value == "customAd") {
		var newName = id.name;
		var newElement = document.createElement('input');
		newElement.type="text";
		newElement.value="";
		newElement.style.width = 124+'px';

		id.style.display="none";
		id.parentNode.insertBefore(newElement,id.nextSibling);
		id.parentNode.removeChild(id);
		newElement.name=newName;
		newElement.id=newName;	
	} else {}
}


//function to load the sites for changed Publisher name
function list_changer (publisherName) {
	var selectID = document.getElementById("left_part_sites");
	var list_p='';
	if (publisherName != "-") {
		list_p += '<select id="sites" name="sites" class="leSel" onchange="showLanguageBar();showContent(document.getElementById(\'sites\').value, \'content\');">';
		list_p += '<option selected="selected" value="-">Find Sites</option>';
		for(var i=0;i<eval(publisherName).length;i++) {
			var new_str = eval(publisherName)[i].replace(/\s/g, "_");
			var new_str2 = new_str.replace(/\.nl/g, "");
			if(LanguageSET == "en") {
				list_p += '<option id="'+eval(publisherName)[i]+'" value="styles/site_specs_en/en_'+new_str2.toLowerCase()+'.html">'+eval(publisherName)[i]+'</option>';
			} else {
				list_p += '<option id="'+eval(publisherName)[i]+'" value="styles/site_specs/'+new_str2.toLowerCase()+'.html">'+eval(publisherName)[i]+'</option>';
			}
		}
		list_p += '</select>';
		selectID.innerHTML = list_p;
	} else {
		list_p += '<select id="sites" name="sites" class="leSel" onchange="showContent(document.getElementById(\'sites\').value, \'content\');">';
		list_p += '<option selected="selected" value="-">Find site</option>';
		list_p += '</select>';
		selectID.innerHTML = list_p;
		hideLanguageBar();
		showContent('styles/pages/aboutSpecs.html', 'content');
	}
}//end function

//function to change language
function langChange(l) {
var savedIndex = document.getElementById('sites').selectedIndex;
//alert(document.getElementById('sites').options[document.getElementById('sites').selectedIndex].value);
	if((l=='en') && (LanguageSET != 'en') ) {
		document.getElementById("lang_en").style.color="#FF0000";
		document.getElementById("lang_nl").style.color="#09B4E2";
		LanguageSET = "en";		
		if((document.getElementById('publishers')) && (document.getElementById('publishers').value != "-") && (document.getElementById('publishers').selectedIndex != 0) && (savedIndex != 0)) {
		
			list_changer (document.getElementById('publishers').value);
			document.getElementById('sites').selectedIndex = savedIndex;
			showContent(document.getElementById('sites').options[document.getElementById('sites').selectedIndex].value, 'content');
		} else {
			document.getElementById('publishers').selectedIndex = 0;
			document.getElementById('sites').selectedIndex = 0;
			showContent('styles/pages/aboutSpecs.html', 'content');
			list_changer ("-");
		}
	} else if((l=='nl') && (LanguageSET != 'nl')) {
		document.getElementById("lang_nl").style.color="#FF0000";
		document.getElementById("lang_en").style.color="#09B4E2";	
		LanguageSET = "nl";
		if((document.getElementById('publishers')) && (document.getElementById('publishers').value != "-") && (document.getElementById('publishers').selectedIndex != 0) && (savedIndex != 0)) {
		
			list_changer (document.getElementById('publishers').value);
			document.getElementById('sites').selectedIndex = savedIndex;
			showContent(document.getElementById('sites').options[document.getElementById('sites').selectedIndex].value, 'content');
		} else {
			document.getElementById('publishers').selectedIndex = 0;
			document.getElementById('sites').selectedIndex = 0;
			showContent('styles/pages/aboutSpecs.html', 'content');
			list_changer ("-");
		}
		
	} else {
	//do nothing
	}
}//end function

// Ajax function to send the data from form to PHP
function createInstance() {
	var req = null;
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) 
	{
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)
		{
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) 
			{
				alert("XHR not created");
			}
		}
		}
	return req;
}//end function

function storing(data)
{
    //var element = document.getElementById('storage');
    //element.innerHTML = data;
}

// JS function to call AJAX to send data to PHP 
function submitForm() { 
    var req =  createInstance();
	
    var siteUrl = escape(document.addForm.siteUrl.value);
    var publisherName = escape(document.addForm.publisherName.value);
    var emailTraffic = escape(document.addForm.emailTraffic.value);
    var emailSales = escape(document.addForm.emailSales.value);
	
	var adName_ = escape(document.addForm.adName_.value);
	var adDimension_ = escape(document.addForm.adDimension_.value);
	var adMaxKb_ = escape(document.addForm.adMaxKb_.value);
	var adMaxKbFlash_ = escape(document.addForm.adMaxKbFlash_.value);
	var adExpand_ = escape(document.addForm.adExpand_.value);
	var adExpandMaxKb_ = escape(document.addForm.adExpandMaxKb_.value);
	var adExpandMaxKbFlash_ = escape(document.addForm.adExpandMaxKbFlash_.value);

	var landPage = escape(document.addForm.landPage.value);	
	var altText = escape(document.addForm.altText.value);	
	var addInfo = escape(document.addForm.addInfo.value);
    var flashVer = escape(document.addForm.flashVer.value);

	var arrNames;
	var arrDims;
	var adMaximumKb;
	var adMaximumKbFlash;
	var adExpandables;
	var adExpandMaximumKb;
	var adExpMaximumKbFlash;
	
	
	for (var i=1; i<=5; i++) {
		if (document.getElementById("adName_"+i)) {
			if (eval("document.addForm.adName_"+i).value == "") { eval("document.addForm.adName_"+i).value=" "; } if (i == 1) { arrNames = escape(eval("document.addForm.adName_"+i).value); } else { arrNames = arrNames + "|" + escape(eval("document.addForm.adName_"+i).value); }
			
			if (eval("document.addForm.adDimension_"+i).value == "") { eval("document.addForm.adDimension_"+i).value=" "; } if (i == 1) { arrDims = escape(eval("document.addForm.adDimension_"+i).value); } else { arrDims = arrDims + "|" + escape(eval("document.addForm.adDimension_"+i).value); }		
			
			if (eval("document.addForm.adMaxKb_"+i).value == "") { eval("document.addForm.adMaxKb_"+i).value=" "; } if (i == 1) { adMaximumKb = escape(eval("document.addForm.adMaxKb_"+i).value); } else { adMaximumKb = adMaximumKb + "|" + escape(eval("document.addForm.adMaxKb_"+i).value); }
			
			if (eval("document.addForm.adMaxKbFlash_"+i).value == "") { eval("document.addForm.adMaxKbFlash_"+i).value=" "; } if (i == 1) { adMaximumKbFlash = escape(eval("document.addForm.adMaxKbFlash_"+i).value); } else { adMaximumKbFlash = adMaximumKbFlash + "|" + escape(eval("document.addForm.adMaxKbFlash_"+i).value); }		
			
			if (eval("document.addForm.adExpand_"+i).value == "") { eval("document.addForm.adExpand_"+i).value=" "; } if (i == 1) { adExpandables = escape(eval("document.addForm.adExpand_"+i).value); } else { adExpandables = adExpandables + "|" + escape(eval("document.addForm.adExpand_"+i).value); }		
			
			if (eval("document.addForm.adExpandMaxKb_"+i).value == "") { eval("document.addForm.adExpandMaxKb_"+i).value=" "; } if (i == 1) { adExpandMaximumKb = escape(eval("document.addForm.adExpandMaxKb_"+i).value); } else { adExpandMaximumKb = adExpandMaximumKb + "|" + escape(eval("document.addForm.adExpandMaxKb_"+i).value); }	
			
			if (eval("document.addForm.adExpandMaxKbFlash_"+i).value == "") { eval("document.addForm.adExpandMaxKbFlash_"+i).value=" "; } if (i == 1) { adExpMaximumKbFlash = escape(eval("document.addForm.adExpandMaxKbFlash_"+i).value); } else { adExpMaximumKbFlash = adExpMaximumKbFlash + "|" + escape(eval("document.addForm.adExpandMaxKbFlash_"+i).value); }			
		}
	}
    
	var data = "siteUrl="+siteUrl+"&publisherName="+publisherName+"&emailTraffic="+emailTraffic+"&emailSales="+emailSales+"&adName_="+adName_+"&adDimension_="+adDimension_+"&adMaxKb_="+adMaxKb_+"&adMaxKbFlash_="+adMaxKbFlash_+"&adExpand_="+adExpand_+"&adExpandMaxKb_="+adExpandMaxKb_+"&adExpandMaxKbFlash_="+adExpandMaxKbFlash_+"&addInfo="+addInfo+"&flashVer="+flashVer+"&altText="+altText+"&landPage="+landPage+"&quantity="+quantity+"&arrNames="+arrNames+"&arrDims="+arrDims+"&adMaximumKb="+adMaximumKb+"&adMaximumKbFlash="+adMaximumKbFlash+"&adExpandables="+adExpandables+"&adExpandMaximumKb="+adExpandMaximumKb+"&adExpMaximumKbFlash="+adExpMaximumKbFlash;
	req.onreadystatechange = function()
    { 
        if(req.readyState == 4)
        {
			if(req.status == 200)
			{
				//alert(req.responseText);
				alert("Thank You! \nThe information was sent.");
				showContent('styles/pages/aboutSpecs.html', 'content');
			}	
			else	
			{
				// alert the data that is being sent from PHP
				alert("Error: returned status code " + req.status + " " + req.statusText);
			}	
        } 
	}
	req.open("POST", "styles/getForm.php", true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(data); 
}//end function


function hideLanguageBar() {
	if((document.getElementById('LangDiv'))) {
	document.getElementById('LangDiv').style.visibility='hidden';
	}
}
function showLanguageBar() {
	if(document.getElementById('LangDiv') && (document.getElementById('sites')) && (document.getElementById('sites').value != '-')) {
	document.getElementById('LangDiv').style.visibility='visible';
	}
}



//######################################################################################
// arrays with publisher names and sites data
var PubBase = new Array();
PubBase=
[
"ANWB",
"De_Telefoongids",
"HACHETTE",
"MEDIAPLUS",
"FD_Mediagroep",
"IDG",
"IMNetworks",
"VNU_MEDIA",
"ADFAB",
"Kluwer",
"RTL"
];

//################
var ANWB = [
	'anwb.nl'
	];
var De_Telefoongids = [
	'detelefoongids.nl'
	];
var HACHETTE = [
	'charlieschoice.nl',
	'elle.nl',
	'ellegirl.nl',
	'quotenet.nl',
	'red.nl'
	];
var MEDIAPLUS = [
	'plusonline.nl'
];
var FD_Mediagroep = [
	'fd.nl',
	'bnr.nl',
	'z24.nl'
	]
var IDG = [
	'tipsentrucs.nl',
	'infoworld',
	'idg.nl',
	'techworld.nl',
	'webwereld.nl',
	'zoom.nl',
	'gamez.nl',
	'channelworld'
	];
var IMNetworks = [
	'Autovandaag',
	'BusinessCompleet',
	'Management Scope',
	'Mkb Net',
	'Mkb Ondernemers'
	];
var VNU_MEDIA = [
	'Channelweb',
	'Computable',
	'Emerce',
	'In Ict',
	'In Financieel',
	'In Interim',
	'In Overheid',
	'Intermediair',
	'Intermediair PW',
	'Intermediair Starters',
	'IT Directory',
	'Management Team',
	'Marketing Directory',
	'Sprout',
	'Tweakers'
	];
var ADFAB = [
	'ajax',
	'Az',
	'Feyenoord',
	'PSV',
	'sport1',
	'Goal.com',
	'BBC',
	'Flametree',
	'925',
	'Jort Kelder',
	'Seesle',
	'frackers.com',
	'Catherine',
	'Consumed',
	'Dokter.nl',
	'Gezondheid',
	'Medischforum',
	'CU2',
	'DJScene',
	'Funkybabes',
	'Funkyhunks',
	'Kaboem',
	'moviescene',
	'partypeeps',
	'partyscene',
	'pozerscene',
	'celebscene',
	'fashionscene',
	'girlscene',
	'modeblog',
	'ze'
	];
var Kluwer=[
	'Management support',
	'OR Informatie',
	'securitymanagement',
	'servicemanagement',
	'zorgmarkt'
	];
var RTL=[
	'rtl.nl'
	];

