/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

////// index.jsp
// generate adspace id
function generateAdsIDshow(width,height,modal,divid) {
    CallDialog(width,height,modal,divid);
}

function setValueForAdultPopUp(publisher_id, adspace_id){
	if (document.getElementById("adultAdspace")) {
		document.getElementById("adultAdspace").innerHTML = adspace_id;
	}
	if (document.getElementById("requestUrlFrom")) {
		document.getElementById("requestUrlFrom").innerHTML = 'http://soma.smaato.net/oapi/reqAd.jsp?pub='+publisher_id+'&adsapces='+adspace_id+'&response=yourResponse';
	}
	if (document.getElementById("requestUrlTo")) {
		document.getElementById("requestUrlTo").innerHTML = 'http://go.reddadd.net/oapi/reqAd.jsp?pub='+publisher_id+'=&adspace='+adspace_id+'&response=yourResponse' ;
	}
}

function checkAdsTypeID (adsTypesSelId,errorTD) {
    var sel = document.getElementById(adsTypesSelId);
    if (sel.tagName == 'SELECT') {
	for(i=0; i<sel.options.length; i++) {
	    if (sel.options[i].selected && sel.options[i].value != '-1'){
		return true;
	    }
	}
    }
    var typeError = document.getElementById(errorTD);
    if (typeError) {
	typeError.innerHTML = '<img src=\"images/redstatus.gif\">&nbsp;&nbsp;&nbsp;&nbsp;  <span><b>please select one type</b></span></p>';
    }
    return false;
}

function generateAdsID (adsTypesSelId,publisher_id) {
    var sel = document.getElementById(adsTypesSelId);
    if (sel.tagName == 'SELECT') {
	for(i=0; i<sel.options.length; i++) {
	    if (sel.options[i].selected && sel.options[i].value != '-1'){
		adsType_id = sel.options[i].value;
		break;
	    }
	}
    }

    document.location.href='appsOperate.jsp?ac=geid&publisher_id='+publisher_id+'&adstype_id='+adsType_id;
}


function checkDont (checkboxID) {
    var checkbox =document.getElementById(checkboxID);
    if (checkbox.checked) {
	var today = new Date();
	today.setTime( today.getTime() );
	expires = 1000 * 60 * 60 * 24*30;
	expires_date = new Date( today.getTime() + (expires) );
	document.cookie= "award=smaato_award;expires="+expires_date.toGMTString()+"; domain=.smaato.com";//.smaato.com
    }
}


//notify dialog
function showNotifyDialog(adspace_id,status,adspace_name,adspacesidInputId,statusInputId,adspacenameInputId, width,height,modal,divid) {  
    var adspaceidInput= document.getElementById(adspacesidInputId);
    var statusInput = document.getElementById(statusInputId);
    var adspacenameInput = document.getElementById(adspacenameInputId);
    if (adspaceidInput.tagName=='INPUT') {
	adspaceidInput.value= adspace_id;
    }
    if (statusInput.tagName=='INPUT') {
	statusInput.value= status;
    }
    if (adspacenameInput.tagName=='INPUT') {
	adspacenameInput.value= adspace_name;
    }
    CallDialog(width,height,modal,divid);
}
// show sdk pop
function showSDKDialog(sdk_name,width,height,modal,divid) {
    var sdkTitleImage = document.getElementById(sdk_name+'_sdkDialogTitleImage');
    var sdkIconImage = document.getElementById(sdk_name+'_sdkIcon');
    var featureP = document.getElementById(sdk_name+'_sdkFeature');
    if (sdk_name=='apple') {
	sdkTitleImage.setAttribute('src', 'images/headers/iphoneipadsdk.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_iOS.png');
	featureP.innerHTML= 'SOMA for iPhone/iPad features:';
    }

    
    if (sdk_name=='android') {
	sdkTitleImage.setAttribute('src', 'images/headers/android_sdk.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_Android.png');
	featureP.innerHTML= 'SOMA for Android features:';
    }
    if (sdk_name=='windows') {
	sdkTitleImage.setAttribute('src', 'images/headers/SmaatoSDKforWP.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_WP.png');
	featureP.innerHTML= 'SOMA for Windows Phone features:';
    }
    if (sdk_name=='symbian') {
	sdkTitleImage.setAttribute('src', 'images/headers/s60_sdk.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_s60.png');
	featureP.innerHTML= 'SOMA for Symbian features:';
    }
    if (sdk_name=='wrt') {
	sdkTitleImage.setAttribute('src', 'images/headers/wrt_sdk.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_WRT.png');
	featureP.innerHTML= 'SOMA for WRT features:';
    }
    if (sdk_name=='qt') {
	sdkTitleImage.setAttribute('src', 'images/headers/qt_sdk.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_QT.png');
	featureP.innerHTML= 'SOMA for Qt features:';
    }
    if (sdk_name=='blackberry') {
	sdkTitleImage.setAttribute('src', 'images/headers/blackberry_sdk.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_blackberry.png');
	featureP.innerHTML= 'SOMA for Backberry features:';
    }

    if (sdk_name=='bada') {
	sdkTitleImage.setAttribute('src', 'images/headers/badasdk.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_bada.png');
	featureP.innerHTML= 'SOMA for Bada features:';
    }

    if (sdk_name=='jspcode') {
	sdkTitleImage.setAttribute('src', 'images/headers/jsp.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_JSP.png');
	featureP.innerHTML= 'SOMA for JSP code snippet features:';
    }
    if (sdk_name=='phpcode') {
	sdkTitleImage.setAttribute('src', 'images/headers/php.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_PHP.png');
	featureP.innerHTML= 'SOMA for PHP code snippet features:';
    }
    if (sdk_name=='somaapi') {
	sdkTitleImage.setAttribute('src', 'images/headers/api.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_smaato.png');
	featureP.innerHTML= 'SOMA for API features:';
    }
    
    if (sdk_name=='adtag') {
	sdkTitleImage.setAttribute('src', 'images/headers/adTagHeader.png');
	sdkIconImage.setAttribute('src', 'images/infoPopUp_AdTag.png');
	featureP.innerHTML= 'SOMA for Ad Tag features:';
    }

    CallDialog(width,height,modal,divid);
}

// delete adspaces
function showDeleteDialog (adspace_id,adspacesidInputId,width,height,modal,divid,adspaceTypeId) {
    var adspaceidInput = document.getElementById(adspacesidInputId);
    if (adspaceidInput.tagName=='INPUT') {
	adspaceidInput.value= adspace_id;
    }
    var titleImage = document.getElementById('deleteDialogTitleImage');
    var textP = document.getElementById('deleteAppOrSite');
    if (titleImage.tagName == 'IMG') {
	if (adspaceTypeId=='1') {
	    titleImage.setAttribute('src', 'images/headers/deletesite.jpg');
	    textP.innerHTML = 'Are you sure you want to delete this Site?';
	} else {
	    titleImage.setAttribute('src','images/headers/deleteapp.jpg');
	    textP.innerHTML = 'Are you sure you want to delete this App?';
	}
    }
    CallDialog(width,height,modal,divid);
}

function notifyToSmaato (publisher_id,adspacesidInputId,statusInputId,adspacenameInputId) {
    var adspaceidInput= document.getElementById(adspacesidInputId);
    var statusInput = document.getElementById(statusInputId);
    var adspacenameInput = document.getElementById(adspacenameInputId);
    if (adspaceidInput.tagName=='INPUT') {
	var adspace_id = adspaceidInput.value;
    }
    if (statusInput.tagName=='INPUT') {
	var status = statusInput.value;
    }
    if (adspacenameInput.tagName=='INPUT') {
	var adspace_name = adspacenameInput.value;
    }

    if (adspace_id && status && adspace_name ) {
	//alert('all notify params ok');
	document.location.href='appsOperate.jsp?ac=changStatus&publisher_id='+publisher_id+'&adspace_id='+adspace_id+'&status='+status+'&adspace_name='+adspace_name;
    }
}

function deleteAdspaces (publisher_id, adspacesidInputId) {
    var adspaceidInput = document.getElementById(adspacesidInputId);
    if (adspaceidInput.tagName=='INPUT') {
	var adsID = adspaceidInput.value;
    }
    if (adsID) {
        document.location.href='appsOperate.jsp?ac=deid&publisher_id='+publisher_id+'&adspace_id='+adsID;
    }
}


// add adspaces

function addAdspaces (publisher_id, adspace_id,adspacetype_id,pageNow) {
    document.location.href='appsOperate.jsp?ac=newapp&publisher_id='+publisher_id+'&adspace_id='+adspace_id+'&adspacetype_id='+adspacetype_id+'&page_now='+pageNow;
}


function editAdspaces (publisher_id, adspace_id,adspacetype_id,pageNow) {
    document.location.href='appsOperate.jsp?ac=editapp&publisher_id='+publisher_id+'&adspace_id='+adspace_id+'&adspacetype_id='+adspacetype_id+'&page_now='+pageNow;
}

// hugo award
function smaatoaward () {
    var award_checkbox = document.getElementById("me");
    if (award_checkbox.checked) {
            var today = new Date();
            today.setTime( today.getTime() );
            expires = 1000 * 60 * 60 * 24*30;
            expires_date = new Date( today.getTime() + (expires) );
            document.cookie= "award=smaato_award;expires="+expires_date.toGMTString()+"; domain=.smaato.com";//.smaato.com
            //alert (document.cookie.toString());
    }
    document.getElementById("awardDialog").style.display="none";
}

////////// codeSnippet.jsp

function activeSnippetLink (el,code,publisher_id) {
    var sel = document.getElementById(el.id);

    var textarea = document.getElementById('adcode');
    if (textarea.tagName == 'TEXTAREA') {
	textarea.style.display='none' ;
    }
    if (sel.tagName == 'SELECT' ) {
	var codeA = document.getElementById('cs'+code);
	if (codeA.tagName == 'A' && sel.value != '0') {
	    codeA.onclick =function(){generateCode(sel.value,code,publisher_id);};
	    codeA.href ='javascript:void(0)';
	    codeA.style.display = 'block';
	    
	} else {
	    if (codeA.tagName == 'A' && sel.value == '0') {
		    //codeA.style.textDecoration='line-through';
		    //codeA.removeAttribute('href');
		    //codeA.onclick='';
		    codeA.style.display = 'none';


/*
		    var textarea = document.getElementById('adcode');
		    if (textarea.tagName == 'TEXTAREA') {
			textarea.style.display='none' ;
		    }
*/
	    }
	}
    }
}

function generateCode(adspace_id,code,publisher_id) {
    var textarea = document.getElementById('adcode');
    if (textarea.tagName == 'TEXTAREA') {
	textarea.style.display='block' ;
    }
    translate (code,publisher_id,adspace_id);
}


var req = null;
var counter = 0;
function handleTranslation() {
	switch(req.readyState) {
		case 4:
			if(req.status!=200 ) {
				return false;
			}else{
				//felder des formulars
                            ad_code_field = document.getElementById("adcode");
			    //antwort des servers
			    codetext=req.responseText;
			    ad_code_field.value=codetext;
			    return true;
			}
			break;

		default:
			return false;
			break;
	}
}


function translate(lang, pub, adspace){
	try{
		req = new XMLHttpRequest();
	}
	catch (e){
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed){
				req = null;
			}
		}
	}

	if (req == null)
		alert("Error creating request object!");
	var url = window.location.protocol+'//'+window.location.host+'/selfservice/codeGenerate.jsp?lang=' + lang +'&pub=' + pub + '&adspace=' + adspace;
	//var url = window.location.protocol+'//'+window.location.host+'/2310PublisherSelfService/codeGenerate.jsp?lang=' + lang +'&pub=' + pub + '&adspace=' + adspace;
	req.open("GET", url, true);
	req.onreadystatechange = handleTranslation;
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(null);
}

function handleUpload() {
    switch(req.readyState){
	case 4:
	    if (req.status!=200) {
		return false;
	    }else{
		uploadImage = document.getElementById("thumb");
		uploadImage.src = req.responseText;
	    }
    }
}

function uploadScreenshot (adspace_id) {
    try{
		req = new XMLHttpRequest();
	}
	catch (e){
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed){
				req = null;
			}
		}
	}
    var url = window.location.protocol+'//'+window.location.host+'/selfservice/ajaxImageUpload.jsp?adspace_id='+adspace_id;
    req.open("GET",url, true);
    req.onreadystatechange = handleUpload;
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send(null);
}

function profileDelete(deleteButtonId,uploadButtonId,ssUploadId,ssUploadImgId,ssUploadDesId, isuploadId, colorboxAid) {
    if (colorboxAid) {
	//alert ('color box is not null : '+colorboxAid);
	var colorboxA = document.getElementById(colorboxAid)
	if (colorboxA != null && colorboxA.tagName=='A') {
	    colorboxA.href='';
	    //alert ('remove color box href ');
	}
    }
    var deleteButton=document.getElementById(deleteButtonId);
    deleteButton.style.display = 'none';
    var uploadButton = document.getElementById(uploadButtonId);
    uploadButton.style.display ='block';
    var uploadImgDiv= document.getElementById(ssUploadImgId);
    uploadImgDiv.style.display = 'none';
    var uploadTD = document.getElementById(ssUploadId);
    //uploadTD.style.attr('padding-bottom','0px');
    uploadTD.style.paddingBottom = '0px';
    var uploadDesTD =document.getElementById(ssUploadDesId);
    //uploadDesTD.style.attr('padding-bottom','0px');
    uploadDesTD.style.paddingBottom = '0px';
    if (isuploadId) {
	var hiddenInput = document.getElementById(isuploadId);
	hiddenInput.value = '0';
    }
}

function openWin7downloadPop(width,height,modal,divid) {
    CallDialog(width,height,modal,divid);
}

function showGenerateAdspaceInfo(el) {
	var adsTypeSelection = el.getElementsByTagName('OPTION');
	if (adsTypeSelection && adsTypeSelection.length>0) {
		for (i =0 ; i< adsTypeSelection.length; i++) {
			if (adsTypeSelection[i].selected) {
				if (adsTypeSelection[i].value != '-1' && adsTypeSelection[i].value != '1') {
					var appinfo =  document.getElementById('adstypenote');
					appinfo.style.visibility  = 'visible';
				} else {
					var appinfo =  document.getElementById('adstypenote');
					appinfo.style.visibility  = 'hidden';
				}
				break;
			}
		}
	}

}
