
// Moved this function from js/help.js
function new_window(URL) {
    try {
        var left=screen.width-265;
        var top=screen.height-798;
        other='width=270px,height=500px,scrollbars=yes,status=no,left='+left+'px,top='+top+'px';
        window.open(URL,'_blank',other)
    } catch(E) {}
}
function handleSearchFields(){
	return true;
}


var oldId = -1;
function showHide(divId){

	divId = document.getElementById(divId);	
	
	if (oldId != -1)oldId.style.display = "none";	
	if (divId != oldId){
		divId.style.display = "block";
		oldId = divId;
	}else{
		oldId = -1;
	}
	
}

function toggleDiv(divId)
{
	var item = document.getElementById(divId);
	
	if (item.style.display == "none")
		item.style.display="";
	else
		item.style.display="none";
}


function getE( id )
{
	try { e = document.getElementById(id); } catch(E) { return null; }
	return e;
}
function isInt( str )
{
	var i = parseInt(str);
	if( isNaN(i) ) return false;
	i = i.toString();
	if( i != str ) return false;
	return true;
}

function checkRecruiten(ident){
	//check files
	cont = true;
	files = document.getElementById('total').value;
	erro = document.getElementById('total_err');
	if(files == 0){
		erro.innerHTML = '<br>At least one file must be uploaded';
		cont = false;
	}
	//dependency
	sel = document.getElementById('sel').selectedIndex;
	txt = document.getElementById('txt').value;
	if(sel == 0 && txt == ''){
		document.getElementById('sel_err').innerHTML = 'Please make a selection or enter text';
		cont = false;
	}else{
		document.getElementById('sel_err').innerHTML = '';
	}
	if(cont == true)self.location.href = 'confirm.php?id=' + ident + '&sel=' + sel + '&txt=' + txt;
}
function checkRecruites(ident){
	//check files
	cont = true;
	files = document.getElementById('total').value;
	erro = document.getElementById('total_err');
	if(files == 0){
		erro.innerHTML = '<br>Al menos un archivo debe ser cargado';
		cont = false;
	}
	//dependency
	sel = document.getElementById('sel').selectedIndex;
	txt = document.getElementById('txt').value;
	if(sel == 0 && txt == ''){
		document.getElementById('sel_err').innerHTML = 'Por favor, haga una selección o ingrese un texto';
		cont = false;
	}else{
		document.getElementById('sel_err').innerHTML = '';
	}
	if(cont == true)self.location.href = 'confirm.php?id=' + ident + '&sel=' + sel + '&txt=' + txt;
}
function showHidden(ident){
	show = document.getElementById(ident).style;
	if(show.display == '')show.display = 'none';
	else show.display = '';
	
}

function testFields(){
	txt = document.getElementById('txt').value;
	titl = document.getElementById('title').value;
	if(txt != '' && titl != ''){
		document.form1.submit();
		return true;
	}else{
		alert('Please fill out all fields');
		return false;
	}
	
	
}
function show(divId){
  div=document.getElementById(divId);
  if (div.style.display == "none"){
    div.style.display="block";
  }
  else{
    div.style.display="none";
  } 
}
function OpenPFIWindow( id )
{
    nw = window.open( '/ProformaInvoice.php?id='+id, 'PFI' );
    if( window.focus ) nw.focus()
}

function OpenIdeaLogWindow( id )
{
    nwl = window.open( '/admin/idea/ShowIdeaLog.php?id='+id, 'Idea Log' );
    if( window.focus ) nwl.focus()
}

function subscriptionPaymentHelper(txt)
{
    getE('iError').style.display = getE('payByCC').checked ? 'block' : 'none';
//    getE('payBTcode').style.display = rCC.checked ? 'none' : 'block';
    getE('generated_trans_code').innerHTML = txt;
}

// Comment: Aco je car!
function backButtonDetector( InputName )
{
    try {
        i = document.getElementById( InputName );
    	var x="1";
    	var isBack;
    	isBack = (x != i.value);
    	if( isBack == true )    // User used 'back' button in browser - Page should be reloaded
    	{
//    		alert( window.location + "\nBad user! Don't use back button in browser!" );
    		i.value = 1;
    		self.location.href=window.location;
    	} else {                // Page is loaded normally, set flag
    		i.value = 2;
    	}
    } catch(E) {}
}
String.prototype.wordWrap = function(m, b, c){ 
	var i, j, s, r = this.split("\n");
	if(m > 0) for(i in r){
		for(s = r[i], r[i] = ""; s.length > m;
			j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
			|| j.input.length + (j = s.substr(m).match(/^\S*/)).input.length + j[0].length,
			r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
		);
		r[i] += s;
	}
	return r.join("\n");
};
function SubmitAndJumpTo( formID, btnID, where )
{
    try {
        form = document.getElementById( formID );
        form.action += '#' + where;
        sub = document.getElementById( btnID );
        sub.name = "DontSaveToDB_" + sub.name;
        sub.click();
    } catch(E) {}
}
function manipulateParent(locate){
	window.parent.location.href=locate;
}
function PrintIt(whicElement, OptionPR){
    try {
    	var content_vlue = document.getElementById(whicElement).innerHTML; 
    	var printWin = window.open("","printSpecial", 'width=500px,height=500px,scrollbars=yes,status=no,left=150px,top=150px');
    	html_st = '<html><head><title>OpenAd</title><link rel="stylesheet" type="text/css" href="/css/dyna_layout.css" /><style>.dashed_line{width:100%; height: 1px; border-bottom: 1px dashed #000000;}.gray_line2{width:100%; height: 1px; border-bottom: 1px dashed #c6c6c6; padding: 0px;}.gray_line{width:100%; height: 1px; border-bottom: 1px solid #c6c6c6;}.gray_dashed{width:100%; height: 1px; border-bottom: 1px solid #c6c6c6; padding: 0px;margin:0px;}</style></head><body><div style="padding:5px;height:6em;font-size:9px;">';
    	html_en = "</div></body></html>";
    	printWin.document.open();
    	printWin.document.write(html_st + content_vlue + html_en);
    	if(OptionPR == true)printWin.print();
    	printWin.document.close();
    } catch(E) {} 
}
function PrintIt2(whicElement, OptionPR,Addon){
    try {
    	var content_vlue = document.getElementById(whicElement).innerHTML; 
    	var dodatek = document.getElementById(Addon).innerHTML; 
    	var printWin = window.open("","printSpecial", 'width=500px,height=500px,scrollbars=yes,status=no,left=150px,top=150px');
    	html_st = '<html><head><title>OpenAd</title><link rel="stylesheet" type="text/css" href="/css/dyna_layout.css" /><style>.dashed_line{width:100%; height: 1px; border-bottom: 1px dashed #000000;}.gray_line2{width:100%; height: 1px; border-bottom: 1px dashed #c6c6c6; padding: 0px;}.gray_line{width:100%; height: 1px; border-bottom: 1px solid #c6c6c6;}.gray_dashed{width:100%; height: 1px; border-bottom: 1px solid #c6c6c6; padding: 0px;margin:0px;}</style></head><body><div style="padding:5px;height:6em;font-size:9px;">';
    	html_en = "</div></body></html>";
    	printWin.document.open();
    	printWin.document.write(html_st + content_vlue + html_en);
    	if(OptionPR == true)printWin.print();
    	printWin.document.close();
    } catch(E) {}
}
function disableMe(whom){
    try {
    	butt = document.getElementById('cancelMeAfterSubmit');
    	butt.disabled = true;
    	return true;
    } catch(E) { return true; }
}
function EnableIfAnySet( cbName, elem )
{
	try {
		d = document.getElementsByTagName('input');
		for( i=0; i<d.length; i++ ){
			if( (d[i].id).substring( 0, cbName.length ) == cbName && d[i].checked ){
        			getE(elem).disabled = false;
				return;
	    		}
		} 
    	getE(elem).disabled = "true";
	} catch(E) {}		
}


function ShowHideByCheckbox( cbID, ID, tag )
{
    try {
    	cb = document.getElementById(cbID);
    	disp = cb.checked ? false : true;
    	d = document.getElementsByTagName(tag);
    	for( i=0; i<d.length; i++ ) {
    		if( (d[i].id).substring( 0, ID.length ) == ID ) {
    			d[i].disabled = disp;
    	   }
    	}
    } catch(E) {}
}
function JSShowHideByCB_Idea()
{
    try {
    	r = getE("priceRadio");
    	a1 = getE("hideError");
    	a2 = getE("hideErrorArrow");
    	t = getE("textPrice");
    	if( r.checked )
    	{
    		t.disabled = "";
    		if( !isInt(t.value) || parseInt(t.value) <= 0 )
    		{
    			if(a1) a1.style.display = "";
    			if(a2) a2.style.display = "";
    		}
    	} else {
    		t.disabled = "disabled";
    		if(a1) a1.style.display = "none";
    		if(a2) a2.style.display = "none";
    	}
    } catch(E) {}
}

function ConMe(nameAp,Loc){
    try {
	    check = confirm(nameAp);
	    if(check == true)self.location.href = Loc;
	} catch(E) {}
}

function confimMessage(nameAp){
    try {
	    return confirm(nameAp);
	} catch(E) {}
	return false;
}

function absoluteClicker(loc){
	self.location.href = loc;
}
function submitForm(someValue, hiddenField, whichForm){
	bool = false;
	
	if(document.getElementById('subforward')){
	  document.getElementById('subforward').disabled = true;
	}
	if(document.getElementById('subbackward')){
	  document.getElementById('subbackward').disabled = true;
	}
	
	try {
		//get permission
		te = document.getElementById('rtePerm').value;
		bool = (te == 1) ? true : false;
	}catch(E){bool = false;}
    try {
    	if(bool == true)submitFormRTE();
    	document.getElementById(hiddenField).value = someValue;
    	var thisForm = document.getElementById? document.getElementById(whichForm) : document.all;
    	thisForm.submit();
    } catch(E) {}
}
function submitFormRTE(){
	try{
	updateRTE('additional_comments');

	}catch(E){
		//alert(E);
	}
	return true;

}	
function uploadIdeaDestination(){
    try {
		//pitch del
		Pitch = document.getElementById('pitch');
		PitchBtn = document.getElementById('pitch_btn');
		Gallery = document.getElementById('gallery');
		GalleryBtn = document.getElementById('gallery_btn');
		if(Pitch.checked){
			PitchBtn.disabled = Pitch.checked ? "" : "disabled";
			if(PitchBtn.value > 0)GalleryBtn.disabled = "";
			else GalleryBtn.disabled = "disabled";	
		}else if(Gallery.checked){
			PitchBtn.disabled = Pitch.checked ? "" : "disabled";
			GalleryBtn.disabled = Gallery.checked ? "" : "disabled";
		}
	} catch(E) {}
}


function Step1_BUYER_Load(){
    try {
    	el = document.getElementById("agree");
    	el.checked = false;
    	el.disabled = true;
    } catch(E) {}
}
function submitAgree(checkBox, sbtButton) {
    try {
	    iagree = document.getElementById(checkBox);
	    btn = document.getElementById(sbtButton);
	    btn.disabled = iagree.checked ? "" : "disabled";
	} catch(E) {}
}

function appScroll(precent, divId, enableTarget) {
    try {
    	target = document.getElementById(enableTarget);
    	scr = document.getElementById(divId);
    	if( !scr || !target ) return;
    	locationScroll = scr.scrollTop;
    	offHeight = scr.offsetHeight;
    	realHeight = scr.scrollHeight;
    	finalHeight = realHeight - offHeight;
    	offSets = (locationScroll/finalHeight) * 100;
    	if(offSets >= precent) target.disabled = "";
    	else { target.checked = ""; target.disabled = "disabled"; }
    } catch(E) {}
}

function removeAllOptions( dropdownName ) {
    try {
    	var elSel = document.getElementById(dropdownName);
    	var i;
    	if( elSel!=null) {
    		for (i = elSel.length - 1; i>=0; i--) {
    			elSel.remove(i);
    		}
    	}
    } catch(E) {}
}

function updateConfirmButton( select1, select2, confirmButton ) {
    try {
    	if( document.getElementById(select1).selectedIndex!=0 &&
    		document.getElementById(select2).selectedIndex!=0 ) 
    	{
    		 document.getElementById(confirmButton).disabled=false;
    	} else {
    		 document.getElementById(confirmButton).disabled=true;
    	}
    } catch(E) {}
}
function updateAdjectiveDropdown( tones, adjectives, confirmButton, toneArray, adjArray, defTone, defAdj, selectText ) {
  	toneSelect = document.getElementById(tones);
  	adjSelect = document.getElementById(adjectives);
	if(defTone>0) {
	  	removeAllOptions( toneSelect );
		var elOptNew = new Option(selectText, 0);
		toneSelect.options[0]=elOptNew;
  		for( i=0;i<4; i++) {
			var elOptNew = new Option(toneArray[i], i+1);
			toneSelect.options[i+1]=elOptNew; // IE only
			if(defTone==(i+1)) {
				toneSelect.options[i+1].selected=true;
			}
		}
	}

  	removeAllOptions( adjectives );
  	if( toneSelect.selectedIndex == 0 ) {
		var elOptNew = new Option(selectText, 0);
		adjSelect.options[0]=elOptNew;
  	} else {  		
  		var startVal = (toneSelect.selectedIndex-1)*10;
  		var i = 0;
		var elOptNew = new Option(selectText, 0);
		adjSelect.options[0]=elOptNew;
  		for( i=0;i<20; i++) {
  			var arIndex = ((startVal+i)%40);
			var elOptNew = new Option(adjArray[arIndex], i+1);
			adjSelect.options[i+1]=elOptNew; // IE only
			if(defAdj==(i+1)) {
				adjSelect.options[i+1].selected=true;
			}
		}
  	}
  	updateConfirmButton( tones, adjectives, confirmButton );
//	nullOptions( document.getElementById(adjectives) );
//	adjSelect.remove(0);
}

function switchVisiblity( show, hide, field, value ) {
    try {
    	getE(show).style.display='';
    	getE(hide).style.display='none';
    	getE(field).value=value;
    	return false;
    } catch(E) {}
}

function checkChecks(count,tag){
    try {
    	chk = false;
    	master = document.getElementById(tag + '_master' );
    	for(i = 1; count >= i; i++){
    		slave = document.getElementById(tag + '_' + i);
    		if(slave.checked == true){
    			master.disabled = false;
    			break;
    		}else{
    			master.disabled = true;
    		}
    	}
	} catch(E) {}
}
function enterTour(setLang,setFile){	
	setWidth = 800;
	setHeight = 600;

     	setLeft = (screen.width - setWidth) / 2;
     	setTop = (screen.height - setHeight) / 2;

	setURL = "guided-tour.php?lang=" + setLang + "&file=" + setFile;
	setWindow = "top="+setTop+", left="+setLeft+", toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+setWidth+",height="+setHeight; 

	OpenAdTour = window.open(setURL, "OpenAdTour", setWindow);

	self.location.href = "/";	//OLD SOLUTION
	//window.location.reload();	//BOLJE

}
function checkTour(){

	if (window.name != "OpenAdTour"){
		self.location.href = "/";
	}else{
		window.focus();
	}

}
function closeTour(setLocation){
		window.opener.location.href = "/" + setLocation;	//OLD SOLUTION
		window.close();
}
function hideOpenadIntro(){
	document.getElementById('openad-intro').style.visibility='hidden';
}

function submitAndGoToLink( linkHref, submitButton, question ) {
	if( document.getElementById('formIsDirty').value!="" ) {
		var answer = confirm(question);
		if( answer ) {
			document.getElementById('ignoreFormData').value='0';//save
		} else {
			document.getElementById('ignoreFormData').value='1';//dont save
		}	
		document.getElementById('forwardHref').value=linkHref;
		document.getElementById(submitButton).click();
		return false;
	}
	return true;
}

function isEmptyString( sVal ) {
	for (i = 0; i < sVal.length; i++)
	{
		if (sVal.charAt(i) != " "
			&& sVal.charAt(i) != "\f"
			&& sVal.charAt(i) != "\n"
			&& sVal.charAt(i) != "\r"
			&& sVal.charAt(i) != "\t")
		{
			return false;
		}
	}
	return true;
}

function setFormDirtyCarry( input ) {
    var dirtyEl = document.getElementById('formIsDirty');
	if( dirtyEl ) dirtyEl.value="carry";
}

function setFormDirty( input ) {
/*
//removed blank check... how to delete fields if blanks are denied?
	if( input ) {
//		alert( "!" + input + "!" );
		if( isEmptyString( input ) ) return;
	}
*/	
//	alert("form is now dirty");
    var dirtyEl = document.getElementById('formIsDirty');
	if( dirtyEl ) dirtyEl.value="1";
}





function moveOption( from, to )
{
	var fromBox = document.getElementById( from );
	var toBox = document.getElementById( to );
	if( fromBox == null || toBox == null ) return;
	if( fromBox.selectedIndex >=0 ) {
		var newOption = new Option( fromBox.options[fromBox.selectedIndex].text, fromBox.options[fromBox.selectedIndex].value );
		fromBox.remove(fromBox.selectedIndex);
		toBox.options[toBox.length] = newOption;
	}
}

function updateMiniGalleryValues( sel_id , input_id )
{
	var selectBox = document.getElementById( sel_id );
	var inputValue = document.getElementById( input_id );
	inputValue.value = '';
	for(i=0; i<selectBox.length; i++) {
		if(i>0) inputValue.value += ',';
		inputValue.value += selectBox.options[i].value;
	}
}

function sendAsyncRequest( strURL, postString, resultdiv_id )
{
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}
	var status;
    xmlhttp.open('POST', strURL, true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            document.getElementById( resultdiv_id ).innerHTML = xmlhttp.responseText;
//            alert(resultdiv_id);
        }
    }
    xmlhttp.send(postString);
}

function showMarkedIdeas( resultdiv_id, destination, url ) {
	var sPostString = 'destination='+destination;
	sPostString += '&url='+escape(url);
//	alert(sPostString);
	sendAsyncRequest( 'ShowMarkedIdeas.php', sPostString, resultdiv_id );
}
