// Start JQuery
$(document).ready(function() {

    $(window).on('click', function(e) {
		if(e.target == $(".blackpop2")[0]) {
			$(".blackpop2").stop().fadeOut(400);
		} else if(e.target == $(".blackpop")[0]) {
            $(".blackpop").stop().fadeOut(400);
        }
    });




    // load more
	$("body").on("click", ".loadmoreshii-xq", function(e) {
		thiz = $(this);
		thepost = thiz.attr("thepost");
		theloader = thiz.attr("theloader");
		totcount = thiz.attr("totcount");
		theurl = thiz.attr("theurl");
		if($(`.${thepost}`).length < $(`#${totcount}`).val()) {
			var lastId = $(`.${thepost}:last`).attr("id");
			var contType = $(`#${totcount}`).attr("conttype");
			getMoreData2(totcount, lastId, contType, thepost, theloader, theurl);
			$(`.${theloader} i`).addClass("fa-spin").addClass("fa-spinner").addClass("fa-pulse");
		} else {
			$(`.${theloader}`).fadeOut();
		}

	});

	function getMoreData2(totcount, lastId, contType, thepost, theloader, theurl) {
		$.ajax({
		url: `${theurl}&lastid=${lastId}`,
		type: "get",
		beforeSend: function ()
		{
			$(`.${theloader}`).show();
			// console.log(lastId, contType + " is cooool");
		},
		success: function (data) {
			setTimeout(function() {
			$(`.${theloader} i`).removeClass("fa-spin").removeClass("fa-spinner").removeClass("fa-pulse");
			$(`#${contType}-list`).append(data);
	
			if($(`.${thepost}`).length < $(`#${totcount}`).val()) {
					
			} else {
				$(`.${theloader}`).fadeOut();
			}
	
			}, 1000);
		},
		error: function(data) {
			$(".allerror").fadeIn(200).html("An error occurred. Refresh page and try again").delay(4000).fadeOut(500);
			$(".popstuff").fadeOut(100);
		}

		});
	}



    //guest upload profile 
    $("body").on("submit", ".pushtheformblank", function(e) {
        e.preventDefault();
            //Loading button text
    		var allofthis = $(this);
    		var link = $(this).attr("link");
    		var buttoni = $("button i", this);
    		var button = $("button", this);
            var thisStuff = $(this).attr("id");
            var form_data = new FormData(this);
            $(this).addClass("opachim");
            button.html("Uploading...").append("<i class='fa fa-spinner fa-spin"); //Loading button text 
            $(".form-info").show().html("<i class='fa fa-info-circle'></i> This might take a moment. Please be patient.");
            $.ajax({
                type: "POST",
                url: `${link}`,
                dataType:"json", //expect json value from server
                contentType: false,
                cache: false,
                processData: false,
                data: form_data,
            beforeSend: function() {
                $(this).addClass("opachim");
                button.html("Uploading..."); //Loading button text 
            },
            success: function(data) { //on Ajax success
                var errormsg = data.errormsg;
                var errormsg2 = data.errormsg2;
                if(data.items) {
                    // updateprice
                    $(".finalbody").slideDown(500);
                    $(".oldbody").slideUp(500);
                    $(".thelink").html(errormsg2);
                    $(".copylink").attr("link", `${errormsg2}`);
                    $(".copylink").attr("value", `${errormsg2}`);
                } else {
                    $(".thaerror").addClass("thaerror-neg").html(errormsg);
                    $(".thaerror").fadeIn(400).removeClass("positive");
                    $(".form-info").hide();
                    setTimeout(function() {
                        allofthis.removeClass("opachim");
                        button.html("Upload");
                    }, 1500);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $(".thaerror").addClass("thaerror-neg").html("An error occured. Refresh Page. If error persist, simply send all the requested information above to 09033594031 on WhatsApp. Our content team will help you upload them.");
                $(".thaerror").fadeIn(400).removeClass("positive");
                $(".form-info").hide();
                setTimeout(function() {
                    allofthis.removeClass("opachim");
                    button.html("Upload");
                }, 1500);
            //   alert(textStatus+" - "+errorThrown);
            //   console.log("error for log in");
            }

        });
        e.preventDefault();
    });



    $("body").on("click", ".lim", function(e) {
        e.preventDefault();
        $(".header-pop").stop().fadeIn(500);
    });

    $(".lni-close").on("click", function(e) {
        e.preventDefault();
        $(".header-pop").stop().fadeOut(500);
    });
    
    $("body").on("click", ".choosethis", function() {
        theid = $(this).attr("theid");

        // first
        $(".choosethis i").removeClass("fa-chevron-up").addClass("fa-chevron-down");
        $(`.thecontes`).slideUp();

        $(`.thecontes${theid}`).slideDown();

        if($("i", this).hasClass("fa-chevron-up")) {
            $("i", this).removeClass("fa-chevron-up").addClass("fa-chevron-down");
        } else {
            $("i", this).removeClass("fa-chevron-down").addClass("fa-chevron-up");
        }
    });


    $("body").on("input", ".compsearch", function() {
        thiz = $(this);
        thizlink = thiz.attr("link");
        thizbodyclass = thiz.attr("bodyclass");
        var theurlinput = encodeURIComponent(thiz.val());
        $(`.${thizbodyclass}`).html("<div class='margin-bottom__small primarycolor-text' style='font-size:2rem'><i class='fa fa-spinner fa-spin'></i> Loading...</div>")
        setTimeout(function() {
            $(`.${thizbodyclass}`).load(`${thizlink}&value=${theurlinput}`);
        }, 1000);
    });

	$("body").on("click", ".refreshpagecomp", function() {
        $(this).find("i.fa-refresh").addClass("fa-spin fa-pulse");
        setTimeout(function() {
            reloadPage();
        }, 500);
	});

    function reloadPage() {
        location.reload(true);
    }

    $("body").on("click", ".haslink", function() {
        var eleyi = $(this);
        var thistitle = eleyi.attr("thetitle");
        var thislink = eleyi.attr("thelink");
        // $(".openshit").attr("href", `${thislink}`);
        $(".openshit").addClass("justrefresh");
        $(".openshit").attr("fire", `${thislink}`);
        
        $(".opentitle").text(thistitle);
        $(".blackpop").fadeIn(500);
    });

    $("body").on("click", ".justrefresh", function() {
        var thislink = $(this).attr("fire");
        $.getJSON(`${thislink}`, function(data) {
        });

        $(this).removeClass("justrefresh");
        $(this).removeAttr("fire");
        location.reload(true);

    });

    $("body").on("click", ".hasSubtleLink", function() {
        var eleyi = $(this);
        var thistitle = eleyi.attr("thetitle");
        var thislink = eleyi.attr("thelink");
        // $(".openshit").attr("href", `${thislink}`);

		var nav = $(this).attr("id");
		$(".openshit").addClass("pumpitout");
		$(".openshit").attr("fire", `${thislink}`);
		$(".openshit").attr("fireid", `${nav}`);


        $(".opentitle").text(thistitle);
        $(".blackpop").fadeIn(500);
    });

    $("body").on("click", ".pumpitout", function() {
        var thislink = $(this).attr("fire");
        $.getJSON(`${thislink}`, function(data) {
        });
        var id = $(this).attr("fireid");
        console.log($(`#${id}.admin-content`).fadeOut(500));
        $(".blackpop").fadeOut(500);

        $(this).removeClass("pumpitout");
        $(this).removeAttr("fireid");
        $(this).removeAttr("fire");
        
    });

    $("body").on("click", ".closeshit", function() {
        $(".blackpop").fadeOut(500);
    });

    $("body").on("click", ".menushow", function() {
        thiz = $(this);
        type = thiz.attr("type");
        bringin = thiz.attr("bringin");
        whattodo = thiz.attr("whattodo");
        thiz.hide();
        bringin = $(`.${bringin}`).show();


        if(type==="show") {
            $(`.${whattodo}`).slideDown(500);
        } else if(type==="hide") {
            $(`.${whattodo}`).slideUp(500);
        }
    });


    // get slug url
    $("body").on("input", "#theurl", function() {
        var theurlinput = $("#theurl").val();
        $.getJSON(`/subpages/adminaction.php?type=slugtheurl&value=${theurlinput}`, function(data) {
            var returned = data.items;
            var returnedcontent = data.content;
            if(returned) {
                $("#slugurl").val(returnedcontent);
            }
        });
    });

    // check if slug url exist
    $("body").on("blur", "#theurl, #slugurl", function() {
        var theurlinput = $("#slugurl").val();
        $.getJSON(`/subpages/adminaction.php?type=checkslugurl&value=${theurlinput}`, function(data) {
            var returned = data.items;
            if(returned) {
                $("#slugurl").addClass("errorinput");
                $(".slugerr").text("Slug url exist").show(200);
                $(".form-submit").addClass("opachim");
            } else {
                $("#slugurl").removeClass("errorinput");
                $(".slugerr").text("").hide(200);
                $(".form-submit").removeClass("opachim");
            }
        });
    });



    // get slug url
    $("body").on("input", "#theurlname", function() {
        var theurlinput = $("#theurlname").val();
        $.getJSON(`/subpages/adminaction.php?type=slugtheurl&value=${theurlinput}`, function(data) {
            var returned = data.items;
            var returnedcontent = data.content;
            if(returned) {
                $("#slugurlname").val(returnedcontent);
            }
        });
    });

    // check if slug url exist
    $("body").on("blur", "#theurlname, #slugurlname", function() {
        var theurlinput = $("#slugurlname").val();
        $.getJSON(`/subpages/adminaction.php?type=checkslugname&value=${theurlinput}`, function(data) {
            var returned = data.items;
            if(returned) {
                $("#slugurlname").addClass("errorinput");
                $(".slugerr").text("Slug url exist").show(200);
                $(".form-submit").addClass("opachim");
            } else {
                $("#slugurlname").removeClass("errorinput");
                $(".slugerr").text("").hide(200);
                $(".form-submit").removeClass("opachim");
            }
        });
    });




    $("body").on("input", ".form-searchcontestant", function() {
        var theurlinput = encodeURIComponent($(".opages-search").val());
        var competeid = $(".opages-search").attr("competeid");
        $(".formshowcontestant").html("<div class='margin-bottom__small primarycolor-text' style='font-size:2rem'><i class='fa fa-spinner fa-spin'></i> Loading...</div>")
        setTimeout(function() {
            $(".formshowcontestant").load(`/subpages/adminaction.php?type=showcontestant&value=${theurlinput}&competeid=${competeid}`);
        }, 1000);
    });





    // format text code

    $("body").on("click", ".dostyle", function() {
        var first = $(this).attr("first");
        var second = $(this).attr("second");
        wrapText("txt1", first, second);
        // console.log(ouch + " is tobi guy");
    });

    function wrapText(elementID, openTag, closeTag) {
        var textArea = document.getElementById(elementID);
        var len = textArea.value.length;
        var start = textArea.selectionStart;
        var end = textArea.selectionEnd;
        var selectedText = textArea.value.substring(start, end);
        var replacement = openTag + selectedText + closeTag;
        textArea.value = textArea.value.substring(0, start) + replacement + textArea.value.substring(end, len);
        textArea.focus();
        textArea.selectionStart = textArea.selectionEnd = end + openTag.length + closeTag.length;
    }
    
    function addText(elementID, tag) {
        var textArea = document.getElementById(elementID);
        var len = textArea.value.length;
        var insertposition = textArea.selectionEnd;
        textArea.value = textArea.value.substring(0, insertposition) + tag + textArea.value.substring(insertposition, len);
        textArea.focus();
        textArea.selectionStart = textArea.selectionEnd = insertposition + tag.length;
    }
    
    


    // prevent number and 9

    // var nlumber = document.querySelector('.nlumber');

    // // Listen for input event on numInput.
    // nlumber.onkeydown = function(e) {
    // if(!((e.keyCode > 95 && e.keyCode < 106)
    //   || (e.keyCode > 47 && e.keyCode < 58) 
    //   || e.keyCode == 8)) {
    //     return false;
    // }

    $("body").on("input", ".nlumber", function() {
        var value = parseInt($(".nlumber").val());
        if(value=="0"){
            $(".submitpay").addClass("opachim");
        } else {
            $(".submitpay").removeClass("opachim");
        }

        $(".nlumber2").val(value);
        var votefee = parseInt($(".nlumber").attr("fee"));
        if(votefee==0) {
            votefee = 1;
        }
        $(".subval").text(value);
        var price = value * votefee;
        $(".sum").html(price);
        sumprice = $(".sum").text();
        if((sumprice=="") || (value=="0")) {
            $(".submitpay").addClass("opachim");
        } else {
            $(".submitpay").removeClass("opachim");
        }
    });



    $("body").on("input", ".nlumber3x", function() {
        var value = parseInt($(".nlumber3x").val());
        
        if(value=="0"){
            $(".submitpay").addClass("opachim");
        } else {
            if($(`.nlumber3x`).hasClass("maxkickin-xq")) {
                if(value>400) {
                    $(".submitpay").addClass("opachim");
                    $(".criticalinfoxp").show();
                } else {
                    $(".submitpay").removeClass("opachim");
                    $(".criticalinfoxp").hide();
                }
            } else {
                $(".submitpay").removeClass("opachim");
            }
        }

        $(".nlumber2").val(value);
        var votefee = parseInt($(".nlumber3x").attr("fee"));
        if(votefee==0) {
            votefee = 1;
        }
        $(".subval").text(value);
        var price = value * votefee;
        $(".sum").html(price);
        sumprice = $(".sum").text();
        if((sumprice=="") || (value=="0")) {
            $(".submitpay").addClass("opachim");
        } else {
            if($(`.nlumber3x`).hasClass("maxkickin-xq")) {
                if(value>400) {
                    $(".submitpay").addClass("opachim");
                    $(".criticalinfoxp").show();
                }
            } else {
                $(".submitpay").removeClass("opachim");
            }
        }
    });




    $("body").on("click", ".plusval", function() {
        var value = parseInt($(".nlumber").val());
        var valueadd = value + 1;
        $(".nlumber").val(valueadd);
        $(".nlumber").trigger("input");
    });

    $("body").on("click", ".plusval2", function() {
        var value = parseInt($(".nlumber3x").val());
        var valueadd = value + 1;
        if($(`.nlumber3x`).hasClass("maxkickin-xq")) {
            if(value<400) {
                $(".nlumber3x").val(valueadd);
            }            
        } else {
            $(".nlumber3x").val(valueadd);
        }
        $(".nlumber3x").trigger("input");
    });


    $("body").on("click", ".minusval", function() {
        var value = parseInt($(".nlumber").val());
        var valueadd = value - 1;
        if(value>1) {
            $(".nlumber").val(valueadd);
        }
        $(".nlumber").trigger("input");
    });


    $("body").on("click", ".minusval2", function() {
        var value = parseInt($(".nlumber3x").val());
        var valueadd = value - 1;
        if(value>1) {
            $(".nlumber3x").val(valueadd);
        }
        $(".nlumber3x").trigger("input");
    });



    // copy stuff

    $("body").on("click", ".copylink", function() {
        copy(".copylink");
        $(".copiedlink").html("<i class='fa fa-check'></i> Copied Link").css("background", "rgb(7, 180, 7)").css("color", "#fff");
        setTimeout(function() {
            $(".copiedlink").html("Copy Link").css("background", "").css("color", "")
        }, 5000);
    })
    
    // input email
    $("body").on("input", ".theemail", function() {
        var theemailval = $(".theemail").val();
        var theemailmainval = $(".theemailmain").val();
        $(".theemailmain").val(theemailval);
        if(theemailval) {
            $(".pushsubmit").removeClass("opachim");
        } else {
            $(".pushsubmit").addClass("opachim");
        }
    });

    $("body").on("input", ".thephone", function() {
        var thephoneval = $(".thephone").val();
        var thephonemainval = $(".thephonemain").val();
        $(".thephonemain").val(thephoneval);
        if(thephoneval) {
            $(".pushsubmit").removeClass("opachim");
        } else {
            $(".pushsubmit").addClass("opachim");
        }
    });


	$("body").on("click", ".closechart", function() {
		$(".chartbox").fadeOut(500);
	});

    $("body").on("click", ".openchart", function() {
		var link = $(this).attr("link");
		var title = $(this).attr("title");
		$(".chartbox").fadeIn(500);
		$(".charttitle").html(title);
		$(".chartcontent").html(`<div class='padding-general__2rem center' style='font-size:2.5rem'><i class='fa fa-spinner fa-spin fa-pulse'></i></div>`);
		setTimeout(function() {
		  $(".chartcontent").load(`${link}`);
		}, 2000);
	});

    $("body").on("click", ".openlinkintab", function() {
        // setTimeout(function() {
           var url = $(this).attr('url'); 
           window.open(url, '_blank');
        // }, 1000);
    });
    
    $("body").on("click", ".openthelink", function() {
       var url = $(this).attr('hrefl');
       $.getJSON(`${url}`, function(data) {
      });

// 		window.location.href = url;
    });


    //ajax form component
	$("body").on("submit", ".pushtheform", function(e) {
		// var form_data = $(this).serialize();
		var form_data = new FormData(this);
		var nav = $(".others", this);
		var buttontext = $("button", this).attr("normal");
		if(buttontext) {
			$("button", this).html(buttontext + "&nbsp; <i class='fa fa-pulse fa-spinner fa-spin'></i>");
		} else {
			$("button", this).append("&nbsp; <i class='fa fa-spinner fa-pulse fa-spin'></i>");
		}
			
		$(this).addClass("opachim");
		var allofthis = $(this);
		var link = $(this).attr("link");
		var buttoni = $("button i", this);
		var button = $("button", this);
		// console.log(buttontext, button, buttoni);
		// false();
		var msg = $("div.msg", this);
		setTimeout(function() {
			$.ajax({
				xhr: function() {
					var xhr = new window.XMLHttpRequest();
				
					xhr.upload.addEventListener("progress", function(evt) {
					  if (evt.lengthComputable) {
						var percentComplete = evt.loaded / evt.total;
						percentComplete = parseInt(percentComplete * 100);

						msg.removeClass("error-beep").removeClass("success-beep");
						if (percentComplete === 100) {
							msg.html(`<div class='percent-beep'>Submitting... almost complete</div>`);
						} else {
							msg.html(`<div class='percent-beep'>Submitting... ${percentComplete}%</div>`);
						}
						msg.fadeIn(100);
						// }
				
					  }
					}, false);
				
					return xhr;
				},
				type: "POST",
				url: `${link}`,
				dataType:"json", //expect json value from server
				contentType: false,
				cache: false,
				processData: false,
				data: form_data,
				beforeSend: function() {
				//   console.log(form_data, " this is the the first");
				},
				success: function(data) {
					var conf = data.items;
					var confval = data.msg;
					var confnext = data.next;
					var confnextaction = data.nextaction;
					var confnextaction_val = data.nextaction_value;
					var confnextaction_title = data.nextaction_title;
					var confvalnorm1 = data.valnorm1;
					var confvalnorm2 = data.valnorm2;
					if(conf) {
						//show msg
					  // $("input, textarea").val("");
                        msg.removeClass("error-beep").addClass("success-beep");
                        allofthis.removeClass("opachim");
                        if(confnext==="stayforever") {
                        allofthis.find("input, textarea").val("");
                        msg.fadeIn(500).html(`${confval}`).delay(30000).fadeOut(500);
                        } else if(confnext==="unusual") {
                        msg.fadeIn(500).html(`${confval}`).delay(2000).fadeOut(500);
                        } else if(confnext==="unusual2") {
                        allofthis.find("input, textarea").val("");
                        msg.fadeIn(500).html(`${confval}`).delay(1000).fadeOut(500);
                        } else if(confnext==="attachspecific") {
                        allofthis.find("input").val("");
                        msg.fadeIn(500).html(`${confval}`).delay(3050).fadeOut(500);
                        } else if(confnext==="attachspecificnew") {
                        allofthis.find("input").val("");
                        msg.fadeIn(500).html(`${confval}`).delay(3050).fadeOut(500);
                        } else {
                        allofthis.find("input").val("");
                        msg.fadeIn(500).html(`${confval}`).delay(15000).fadeOut(500);
                        }
                        if(buttontext) {
                        button.html(buttontext);
                        } else {
                        buttoni.removeClass("fa-spin").removeClass("fa-spinner").removeClass("fa-pulse");
                        }
                        if(confnext==="reloadpage") {
                            setTimeout(function() {
                            reloadPage();
                            }, 3000);
						} else if(confnextaction=="votetopup") {
							$(`.${confvalnorm1}`).html(`<span style='color:purple;font-weight:600'>[${confvalnorm2}]</span>`);
                            // setTimeout(function() {
                                $(".chartbox").fadeOut(500);
                            // }, 1000);
                        } else if(confnextaction==="soft_redirectlist") {
							setTimeout(function() {
							  $(".clistitle").html(confnextaction_title);
							  $(".clistcontent").html(`
							  <div class='padding-general center'>
								<div class='conceptloading-text'>Loading...</div>
								<img class='conceptloading-icon' src='/thenextstar/loadinggif.gif'>
							</div>
			  				`);
							setTimeout(function() {
								$(".clistcontent").load(`${confnextaction_val}`);
							  }, 3000);
							}, 3000);
						} else if(confnextaction==="soft_redirect") {
						setTimeout(function() {
						  $(".charttitle").html(confnextaction_title);
						  $(".chartcontent").html(`<div class='padding-general center' style='font-size:2.5rem'><i class='fa fa-spinner fa-pulse fa-spin'></i></div>`);
						  console.log(confnextaction, confnextaction_title, confnextaction_val);
						  setTimeout(function() {
							$(".chartcontent").load(`${confnextaction_val}`);
						  }, 3000);
						}, 3000);
					  } else if(confnextaction==="hard_redirect") {
						setTimeout(function() {
						  window.location.href = confnextaction_val;
						}, 3000);
					  }
	
	
					} else {
					  msg.removeClass("success-beep").addClass("error-beep");
					  msg.fadeIn(500).html(`${confval}`);
					  allofthis.removeClass("opachim");
					  if(buttontext) {
						button.html(buttontext);
					  } else {
						buttoni.removeClass("fa-spin").removeClass("fa-spinner").removeClass("fa-pulse");
					  }
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					if(buttontext) {
						button.html(buttontext);
					} else {
						buttoni.removeClass("fa-spin").removeClass("fa-spinner").removeClass("fa-pulse");
					}

					allofthis.removeClass("opachim");
					msg.removeClass("success-beep").addClass("error-beep");
					msg.fadeIn(500).html(`<i class='fa fa-info-circle'></i> An error occurred. Please refresh the page and try again`);
				}
			});
		}, 1000);
		e.preventDefault();
	});


    $("body").on("click", ".pingcirc", function() {
        themail = $(this).attr("email");
        thenumber = $(this).attr("number");
        theval = $(this).attr("val");
        console.log("I was pressed");

        $(`.pingemail`).val(themail);
        $(`.pingnumber`).val(thenumber);
        $(`.pingval`).val(theval);

        setTimeout(function() {
            $(".pingbutton").trigger("click");
        }, 20);
    });

    // submit form
    $("body").on("click", ".pushsubmit", function() {
        // $(".powersubmit").trigger("click");
        themail = $(".themail-nq").val();
        thephone = $(".thephone-nq").val();
        if((thephone) && (themail)) {
            $(this).addClass("opachim").append("&nbsp; <i class='fa fa-spinner fa-pulse fa-spin'></i>");
        }
    });

    // close bigguy
    $("body").on("click", ".bigguy-close", function() {
        $(".bigguy").fadeOut();
    });

    // open bigguy
    $("body").on("click", ".triggerthesubmit", function() {
        $(".bigguy").fadeIn();
    });


    function copy(classname) {
        /* Get the text field */
        var classname = classname;
        var copyText = document.querySelector(classname);
      
        /* Select the text field */
        copyText.select();
        copyText.setSelectionRange(0, 99999); /*For mobile devices*/
      
        /* Copy the text inside the text field */
        document.execCommand("copy");
      
    }
    
        $("body").on("click", ".copylink2", function() {
        whattocopy = $(this).attr("whattocopy");
        copy2(`.${whattocopy}`);
        $(".copiedlink").html("<i class='fa fa-check'></i> Copied Link").css("background", "rgb(7, 180, 7)").css("color", "#fff");
        setTimeout(function() {
            $(".copiedlink").html("Copy Link").css("background", "").css("color", "")
        }, 5000);
    })

    
    function copy2(selector) {
        // /* Get the text field */
        console.log("I got here" + selector);
        // var classname = classname;
        // var copyText = $(`.${classname}`).text().select();
        // // var copyText = document.querySelector(classname);
      
        // /* Select the text field */
        // // copyText.select();
        // copyText.setSelectionRange(0, 99999); /*For mobile devices*/
      
        // /* Copy the text inside the text field */
        // document.execCommand("copy");
		var $temp = $("<div>");
		$("body").append($temp);
		$temp.attr("contenteditable", true)
				.html($(selector).html()).select()
				.on("focus", function() { document.execCommand('selectAll',false,null); })
				.focus();
		document.execCommand("copy");
		$temp.remove();
		window.scrollTo(stop(true));
		$("body").stop();
      
    }

    
        // add vote in edit
    $("body").on("input", ".typevote", function() {
        var oldnum = parseInt($(".newvote").attr("oldvote"));
        var toadd = parseInt($(".typevote").val());
        if(toadd) {
            toadd = toadd;
        } else {
            toadd = 0;
        }
        var totalvotenow = oldnum + toadd;
        $(".newvote").text(totalvotenow);
    });



    $("body").on("submit", "#reportguestqvr", function(e) {
        e.preventDefault();
            //Loading button text
            var thisStuff = $(this).attr("id");
            var form_data = new FormData(this);
            $(this).addClass("opachim");
            $(`.form-submit`).html("Submitting...").append("<i class='fa fa-spinner fa-spin"); //Loading button text 
            $(".form-info").show().html("<i class='fa fa-info-circle'></i> This might take a moment. Please be patient.");
            $.ajax({
                type: "POST",
                url: `/pages/reportcontestant.php?type=contbackend`,
                dataType:"json", //expect json value from server
                contentType: false,
                cache: false,
                processData: false,
                data: form_data,
            beforeSend: function() {
                $(this).addClass("opachim");
                $(`.form-submit`).html("Submitting..."); //Loading button text 
            },
            success: function(data) { //on Ajax success
                var errormsg = data.errormsg;
                var errormsg2 = data.errormsg2;
                if(data.items) {
                    // updateprice
                    $(".finalbody").slideDown(500);
                    $(".oldbody").slideUp(500);
                    $(".thelink").html(errormsg2);
                    $(".copylink").attr("link", `${errormsg2}`);
                    $(".copylink").attr("value", `${errormsg2}`);
                } else {
                    $(".thaerror").addClass("thaerror-neg").html(errormsg);
                    $(".thaerror").fadeIn(400).removeClass("positive");
                    $(".form-info").hide();
                    setTimeout(function() {
                        $("#reportguestqvr").removeClass("opachim");
                        $(`.form-submit`).html("Submit");
                    }, 1500);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $(".thaerror").addClass("thaerror-neg").html("An error occured. Refresh Page. If error persist, simply send all of the above to 09033594031 on WhatsApp.");
                $(".thaerror").fadeIn(400).removeClass("positive");
                $(".form-info").hide();
                setTimeout(function() {
                    $("#reportguestqvr").removeClass("opachim");
                    $(`.form-submit`).html("Submit");
                }, 1500);
            //   alert(textStatus+" - "+errorThrown);
            //   console.log("error for log in");
            }

        });
        e.preventDefault();
    });



    //guest upload profile 
    $("body").on("submit", "#guestupload", function(e) {
        e.preventDefault();
            //Loading button text
            var thisStuff = $(this).attr("id");
            var form_data = new FormData(this);
            $(this).addClass("opachim");
            $(`.form-submit`).html("Uploading...").append("<i class='fa fa-spinner fa-spin"); //Loading button text 
            $(".form-info").show().html("<i class='fa fa-info-circle'></i> This might take a moment. Please be patient.");
            $.ajax({
                type: "POST",
                url: `/pages/guestupload.php?type=contbackend`,
                dataType:"json", //expect json value from server
                contentType: false,
                cache: false,
                processData: false,
                data: form_data,
            beforeSend: function() {
                $(this).addClass("opachim");
                $(`.form-submit`).html("Uploading..."); //Loading button text 
            },
            success: function(data) { //on Ajax success
                var errormsg = data.errormsg;
                var errormsg2 = data.errormsg2;
                if(data.items) {
                    // updateprice
                    $(".finalbody").slideDown(500);
                    $(".oldbody").slideUp(500);
                    $(".thelink").html(errormsg2);
                    $(".copylink").attr("link", `${errormsg2}`);
                    $(".copylink").attr("value", `${errormsg2}`);
                } else {
                    $(".thaerror").addClass("thaerror-neg").html(errormsg);
                    $(".thaerror").fadeIn(400).removeClass("positive");
                    $(".form-info").hide();
                    setTimeout(function() {
                        $("#guestupload").removeClass("opachim");
                        $(`.form-submit`).html("Upload");
                    }, 1500);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $(".thaerror").addClass("thaerror-neg").html("An error occured. Refresh Page. If error persist, simply email info@magicalhands.org. Our content team will help you upload them. Send us an email.");
                $(".thaerror").fadeIn(400).removeClass("positive");
                $(".form-info").hide();
                setTimeout(function() {
                    $("#guestupload").removeClass("opachim");
                    $(`.form-submit`).html("Upload");
                }, 1500);
            //   alert(textStatus+" - "+errorThrown);
            //   console.log("error for log in");
            }

        });
        e.preventDefault();
    });



    //submit nomination
    $("body").on("submit", "#nominatetheform", function(e) {
        e.preventDefault();
        $(this).addClass("opachim");
        $(".nominate-button").html(`Submitting...`);
        var form_data = $(this).serialize();
    
        setTimeout(function() {

            $.ajax({
                type: "POST",
                url: `/subpages/adminaction.php?type=uploadnominate`,
                dataType:"json", //expect json value from server
                data: form_data,
                beforeSend: function() {
                },
                success: function(data) { 
                    //on Ajax success
                    if(data.items) {
                        // updateprice
                        $(".sendmailerror").hide();
                        setTimeout(function() {
                            $("body").addClass("opachim");
                            $("#nominatetheform").removeClass("opachim");
                            $(".nominate-button").html(`Submit`);
                        }, 1000);
                        setTimeout(function() {
                            $("body").removeClass("opachim");
                            $("input, textarea").val("");
                            $(".sendmailpos").addClass("posmsg").show(500).html("<i class='fa fa-check'></i> Thank you for submitting a nomination. Please follow <b>@bestofedoaward</b> on Instagram for updates as regards this nomination").delay(10000).hide(500);
                        }, 2000);
                    } else {
                        $(".sendmailpos").hide();
                        $(".sendmailerror").show();
                        $(".sendmailerror").addClass("errormsg").html(`<i class='fa fa-info-circle'></i> ${data.msg}`);
                        $("#nominatetheform").removeClass("opachim");
                        $(".nominate-button").html(`Submit`);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $(".sendmailpos").hide();
                    $(".sendmailerror").show();
                    $(".sendmailerror").addClass("errormsg").html(`<i class='fa fa-info-circle'></i> ${data.msg}`);
                        $("#nominatetheform").removeClass("opachim");
                        $(".nominate-button").html(`Submit`);
            //   alert(textStatus+" - "+errorThrown);
                //   console.log("error for log in");
                }
            });
        e.preventDefault();
    
        }, 2000);

    });

   
   
   
   
   
       //submit nomination
    $("body").on("submit", "#sponsortheform", function(e) {
        e.preventDefault();
        $(this).addClass("opachim");
        $(".nominate-button").html(`Submitting...`);
        var form_data = $(this).serialize();
    
        setTimeout(function() {

            $.ajax({
                type: "POST",
                url: `/subpages/adminaction.php?type=uploadsponsor`,
                dataType:"json", //expect json value from server
                data: form_data,
                beforeSend: function() {
                },
                success: function(data) { 
                    //on Ajax success
                    if(data.items) {
                        // updateprice
                        $(".sendmailerror").hide();
                        setTimeout(function() {
                            $("body").addClass("opachim");
                            $("#sponsortheform").removeClass("opachim");
                            $(".nominate-button").html(`Submit`);
                        }, 1000);
                        setTimeout(function() {
                            $("body").removeClass("opachim");
                            $("input, textarea").val("");
                            $(".sendmailpos").addClass("posmsg").show(500).html("<i class='fa fa-check'></i> Thank you for your interest in sponsoring BOEA 2021. Your request has been received successfully. We will reach out to you soon enough").delay(10000).hide(500);
                        }, 2000);
                    } else {
                        $(".sendmailpos").hide();
                        $(".sendmailerror").show();
                        $(".sendmailerror").addClass("errormsg").html(`<i class='fa fa-info-circle'></i> ${data.msg}`);
                        $("#sponsortheform").removeClass("opachim");
                        $(".nominate-button").html(`Submit`);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    $(".sendmailpos").hide();
                    $(".sendmailerror").show();
                    $(".sendmailerror").addClass("errormsg").html(`<i class='fa fa-info-circle'></i> ${data.msg}`);
                        $("#nominatetheform").removeClass("opachim");
                        $(".nominate-button").html(`Submit`);
            //   alert(textStatus+" - "+errorThrown);
                //   console.log("error for log in");
                }
            });
        e.preventDefault();
    
        }, 2000);

    });
 
});