function showCode()
{
    document.getElementById('card_code2').style.display="block";
}
function go_page(a,b,c){
  window.location.href=a+b+c;
}

function checkFields(field_id1,field_id2,email)
{
    if (!checkPassword(field_id1,field_id2)) return false;
    if (!checkValidEmail(document.getElementById(email).value)) return false;
    return true;
}
function countryChange()
{
    switch (document.getElementById("country_id").value)
    {
        case "227":
        {
            document.getElementById("usa_states_text").style.display="block";
            document.getElementById("usa_states_list").style.display="block";
            break;
        }
        default:
        {
            document.getElementById("usa_states_text").style.display="none";
            document.getElementById("usa_states_list").style.display="none";
        }
    }
}

function checkPassword(field_id1,field_id2)
{
    var password=document.getElementById(field_id1).value;
    var password2=document.getElementById(field_id2).value;
    if (password!=password2)
    {
        alert("Please type the same password in both fields!");
        return false;
    }
    return true;
}

function checkValidEmail(str) {
        if (str.match("'") != null)
        {
        alert("The given e-mail address is not valid!");
        return 0;
        }

        if (str.match('"') != null)
        {
        alert("The given e-mail address is not valid!");
        return 0;
        }

        if (str.match("@") == null)
        {
        alert("The given e-mail address is not valid!");
        return 0;
        }

        return 1;
}

function checkValidFloat(str, fieldname) {
        if (!isFloatNumber(str)) {
        alert("Please provide a valid floating point number for " + fieldname + ".");
        return false;
        }
        return true;
}

function checkValidInt(str, fieldname) {
        if (!isNumber(str)) {
                alert("Please provide a valid integer number for " + fieldname + ".");
                return false;
        }
        return true;
}

function isDigit(aChar) {
        if (aChar.length != 1)
                return false;
        var num = parseInt(aChar,10);
        if (isNaN(num))
                return false;
        return true;
}

function isNumber(numString) {
        if (numString == '')
                return false;
        for (var i = 0; i < numString.length;i++)
                if (!isDigit(numString.charAt(i)))
                        return false;
        return true;
}

function isFloatNumber(numString) {
        if (numString == '') {
                return false;
        }
        var pc = 0;
        var i = 0;
        if (numString.charAt(0)=='-') {
            i++;
        }
        for ( ; i < numString.length;i++)
                if (!isDigit(numString.charAt(i))) {
                  if ( ( numString.charAt(i) == ".") && (pc==0) ){
                        pc++;
                  } else {
                                return false;
                        }
                }
        return true;
}


function showAlert(fieldName, withPattern, pattern) {
   if (withPattern)
                alert("The entry for " + fieldName + " is incorrect.\rPlease check your entry and try again.\rThe allowed special characters are '" + pattern + "'.");
   else
                alert("The entry for " + fieldName + " is incorrect.");
}

function checkFieldWithPattern(fieldValue,pattern) {
        if (fieldValue == "")
                return false;
        var entryLength = fieldValue.length;
        var verifyChar = 0;
        var aChar = '';
        for (var i = 0;i < entryLength;i++) {
                aChar = fieldValue.charAt(i);
                verifyChar = pattern.indexOf(aChar);
                if ((pattern.indexOf(aChar) == -1) && (!isDigit(aChar))) {
                        return false;
           }
        }
        return true;
}

function trim(str) {
         while (str.substring(0,1) == '  ')
                 str = str.substring(1, str.length);

         while (str.substring(str.length - 1,str.length)  ==  '  ')
                 str = str.substring(0, str.length - 1);

        return str;
 }

function removeSpaces(str) {
   var newStr = '';
   for (i = 0;i < str.length;i++)
          if (str.charAt(i) != ' ')
                newStr += str.charAt(i);
   return newStr;
}

function checkTimeFormat(timeString) {
  var fieldArray = timeString.split(":");

  if (fieldArray.length < 2)
         return false;

  fieldArray[0] = parseInt(fieldArray[0]);
  fieldArray[1] = parseInt(fieldArray[1]);

  if (!isNumber(fieldArray[0])) {
        return false;
  }
  if (!isNumber(fieldArray[1])) {
        return false;
  }
  if (fieldArray.length > 2) {
        fieldArray[2] = parseInt(fieldArray[2]);
        if (!isNumber(fieldArray[2])) {
          return false;
        }
  }

  if ( (fieldArray[0] < 0) || (fieldArray[0] > 23)        ) {
   return false;
  }
  if ( (fieldArray[1] < 0) || (fieldArray[1] > 59) ) {
   return false;
  }
  if (fieldArray.length > 2) {
   if ( (fieldArray[2] < 0) || (fieldArray[2] > 59) ) {
         return false;
   }
  }
  return true;
}

function checkDateFormat(dateString) {
  var fieldArray = dateString.split("/");
  if (fieldArray.length != 3)
         return false;

   fieldArray[0] = parseInt(fieldArray[0]);
   fieldArray[1] = parseInt(fieldArray[1]);
   fieldArray[2] = parseInt(fieldArray[2]);

   if (!isNumber(fieldArray[0])) {
          return false;
   }
   if (!isNumber(fieldArray[1])) {
          return false;
   }
   if (!isNumber(fieldArray[2])) {
          return false;
   }
   if ( (fieldArray[0] < 1) || (fieldArray[0] > 12)  ) {
         return false;
   }
   if ( (fieldArray[1] < 1) || (fieldArray[1] > 31) ) {
         return false;
   }
   if ( (fieldArray[2] < 1900) || (fieldArray[2] > 2500) ) {
         return false;
   }
   return true;
}

function checkDateTimeFormat(datetimeString) {
  datetimeString = trim(datetimeString);
  var index = datetimeString.indexOf(' ');
  if (index>0) {
        dateString = trim(datetimeString.slice(0,index))
        timeString = trim(datetimeString.slice(index))
        return checkDateFormat(dateString) && checkTimeFormat(timeString);
  } else {
        return checkDateFormat(datetimeString);
  }
}

function padleft(s,wdt,filler) {
  s = "" + s;
  while (s.length < wdt) {
        s = filler + s;
  }
  return s;
}

function normalizeDate(dateString) {
  var fieldArray = dateString.split("/");
   var mm = parseInt(fieldArray[0],10);
   var dd = parseInt(fieldArray[1],10);
   var yyyy = parseInt(fieldArray[2],10);

   dd = padleft(dd,2,"0");
   mm = padleft(dd,2,"0");

   return "" + dd + "/" + mm + "/" + yyyy;
}

function createDateObject(dateString) {
   var fieldArray = dateString.split("/");
   var mm = parseInt(fieldArray[0],10);
   var dd = parseInt(fieldArray[1],10);
   var yyyy = parseInt(fieldArray[2],10);
   var tmpDate = new Date(yyyy,(mm - 1),dd);
   return tmpDate;
}

function isBefore(date1,date2) {
  var time1 = date1.getTime();
  var time2 = date2.getTime();
  if (time1 < time2)
        return true;
  return false;
}

function isAfter(date1,date2) {
  var time1 = date1.getTime();
  var time2 = date2.getTime();
  if (time1 > time2)
        return true;
  return false;
}

function isEqual(date1,date2) {
   var d1 = date1.getDate();
   var m1 = date1.getMonth();
   var y1 = date1.getFullYear();
   var d2 = date2.getDate();
   var m2 = date2.getMonth();
   var y2 = date2.getFullYear();
   return ((d1 == d2) && (m1 == m2) && (y1 == y2));
}

function checkExpirationDate(dateString) {
  if (dateString == '') {
         alert("The expiration date is invalid.");
         return;
  }
  dateString = removeSpaces(trim(dateString));
  if (!checkDateFormat(dateString))  {
         alert("The expiration date is invalid.\rEnter date in mm/dd/YYYY format.");
         return false;
  }
  var date1 = createDateObject(dateString);
  var date2 = new Date();
  if (isEqual(date1,date2))
        return true;
  if (isBefore(date1,date2)) {
         alert("Expiration date should be in the future.");
         return false;
  }
  return true;
}

function checkBirthDate(dateString) {
  if (dateString == '') {
         alert("The birth date is invalid.");
         return;
  }
  dateString = removeSpaces(trim(dateString));
  if (!checkDateFormat(dateString))  {
         alert("The birth date is invalid.\rEnter date in mm/dd/YYYY format.");
         return false;
  }
  var date1 = createDateObject(dateString);
  var date2 = new Date();
  if (isAfter(date1,date2)) {
         alert("The birth date is invalid.");
         return false;
  }
  return true;
}

// Checks the specified field for correct entry
/*
function isNumC(idx,msg) {
        var boxValue = idx.value;
        var boxLength = boxValue.length;
        var SpecChar = "-() ";
        var VerifyChar;
        for (var i = 0;i < boxLength;i++) {
                aChar = boxValue.charAt(i);
                VerifyChar = SpecChar.indexOf(aChar);
                if ((VerifyChar == -1) && (aChar < '0') || (aChar > '9')) {
                        idx.value=""
                        alert("The entry for '" + msg + "' was incorrect.\nPlease check your entry and try again.\nThe allowed special characters are '-()'.");
                        return false
                }
        }
        alert("The entry for '" + msg + "' was correct!");
        return true
}*/

function show_hide(divname){
       if (document.getElementById(divname).style.display=='none') {
         document.getElementById(divname).style.display='block';
       } else {
         document.getElementById(divname).style.display='none';
       }
}

function show_hide_pm(divname){
       if (document.getElementById(divname).style.display=='none') {
         document.getElementById(divname).style.display='block';
         document.getElementById(divname+'_plus').style.display='none';
         document.getElementById(divname+'_minus').style.display='block';
       } else {
         document.getElementById(divname).style.display='none';
         document.getElementById(divname+'_plus').style.display='block';
         document.getElementById(divname+'_minus').style.display='none';
       }
}

function show_hide_i(divname,imagesrc,showimage,hideimage){
       if (document.getElementById(divname).style.display=='none') {
         document.getElementById(divname).style.display='block';
         document.getElementById(imagesrc).src=hideimage;
       } else {
         document.getElementById(divname).style.display='none';
         document.getElementById(imagesrc).src=showimage;
       }
}


timeOutToCheck=300;//in Miliseconds
timeOutToDownload=1500;//in Miliseconds

function newReq(){
  if(window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch(e) {
      req = false;
    }
  } else if(window.ActiveXObject) {
         try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
        try {
          req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
          req = false;
        }
    }
  }
  return req;
}
lastId=-1;//The last message ID

function Connector(){
  this.message="";
  this.countDown=0;
  this.name="";

  this.send=function() {
    if (this.message!="") return;//If we have not sent the last message, return.
    var msg=document.getElementById("msg");
    if (msg.value=="") return;//No message to send
    msg.disabled=false;
    document.getElementById("btn").disabled=false;
    this.message=msg.value;
    msg.value="";
    msg.focus();
  }

  this.runner=function(){
    this.countDown--;
//    window.status=this.countDown;
    if (this.message || this.countDown<=0) {
      readFromServer("/"+"chata"+"sync"+"."+"php"+"?"+"location="+escape(window.location.href)+"&last_id="+lastId+"&name="+escape(this.name)+"&message="+escape(this.message));
      if (lastId==-1) lastId=0;
      this.name="";
      this.message="";
      this.countDown=timeOutToDownload/timeOutToCheck;
    } else setTimeout('connector.runner()',timeOutToCheck);
  }

  readFromServer=function(url){
    req=newReq();
    req.onreadystatechange=ready;
    req.open("GET", url, true);
    req.send("");
  }

  ready=function(){
    if (req.readyState==4 && req.getResponseHeader("response_ok")){
      if (req.responseText){
        addNewLine(req.responseText);
      }
      try{
        if (req.getResponseHeader("last_id")) {
          lastId=req.getResponseHeader("last_id");
        }
      } catch(e){

      }
     setTimeout('connector.runner()',timeOutToCheck);
    }
  }

  addNewLine=function(text){
    iframe=parent.innerFrame.window;
    iframeDocument=iframe.document;

    iframeDocument.body.innerHTML+=text;
    iframe.scrollBy(0,10000);
  }

}

function bookmark(){
  if(window.external) {
    window.external.AddFavorite('http://designasign.biz','Design A Sign');
  } else {
    //alert('Please bookmark us');
  }
}
/* Use this to create hints on shopzeus.com admin interface. */
function ihint(id,title,value) {
    addHintById(id,'<img src="/images/info.png"><b>'+title+"</b><br><small>"+value+"</small>");
}
function ierr(id,title,value) {
    addHintById(id,'<img src="/images/warning.png"><b class="color:red;">'+title+"</b><br><small>"+value+"</small>");
}

//SELECTS
function changeSelects()
{
    //return;
    var selects=document.getElementsByTagName("select");
    //for (i=0; i<selects.length; i++)
    var i=0;
    while (selects.length>i)
    {
        /*if (document.location.href.match('task_search.php'))
        {
            i++;
            continue;
        }*/
        if (((selects[i].name=='status_id[]') && document.location.href.match('task_search.php')) ||
         ((selects[i].name=='producer_id') && document.location.href.match('amazon_order.php')) ||
         (document.location.href.match('am_categorize.php')))
        {
            i++;
            continue;
        }
        var newTb=document.createElement('input');
        newTb.id=selects[i].name+"_value";
        newTb.setAttribute("type","text");
        newTb.setAttribute("onclick","showOptions('"+selects[i].name+"')");
        newTb.setAttribute("onblur","tbLostFocus('"+selects[i].name+"')");
        newTb.setAttribute("onkeypress","return showHints(event,'"+selects[i].name+"')");
        var selectedText=selects[i].options[selects[i].selectedIndex].innerHTML.replace(/&nbsp;/g,"");
        newTb.setAttribute("value",selectedText);
        var tbWidth;
        if (selects[i].offsetWidth) tbWidth=selects[i].offsetWidth;
        else tbWidth="120";
        selectStyle=selects[i].getAttribute('style');
        var bcolor="#ffffff";
        if (selectStyle)
        {
            styles=selectStyle.split(";");
            for (k=0; k<styles.length; k++)
            {
                x=styles[k].split(":");
                if (x[0].match("background-color")) bcolor=x[1];
            }
        }
        newTb.setAttribute("style",'cursor:default; background:url("/images/select_arrow.jpg") no-repeat right;cursor:default; width:'+tbWidth+'px; background-color:'+bcolor+';');
        newTb.setAttribute("autocomplete","off");
        selects[i].parentNode.insertBefore(newTb,selects[i]);
        var newH2=document.createElement('input');
        newH2.id=selects[i].name;
        newH2.name=selects[i].name;
        newH2.setAttribute("type","hidden");
        selects[i].parentNode.insertBefore(newH2,selects[i]);
        var newDiv=document.createElement('div');
        newDiv.id=selects[i].name+"_options";
        options=selects[i].getElementsByTagName('option');
        var divWidth=0;
        //var selectedIndex=0;
        for (var j=0; j<options.length; j++)
        {
            var isSelected=false;
            var newOpt=document.createElement('p');
            var textLength=options[j].innerHTML.replace(/&nbsp;/g,"").length;
            if (textLength>divWidth) divWidth=textLength;
            if (options[j].innerHTML.replace(/ /g,"").length>0)
            {
                newOpt.innerHTML=options[j].innerHTML;
            }
            else
                newOpt.innerHTML="&nbsp;";
            newOpt.id=selects[i].name+'__'+j;
            newOpt.setAttribute("class",options[j].value);

            if (options[j].innerHTML.replace(/&nbsp;/g,"")==selectedText)
            {
                newOpt.setAttribute("style","margin:0; color:#ffffff; background-color:#7099cc; white-space:nowrap;");
                document.getElementById(selects[i].name).setAttribute("value",options[j].value);
                isSelected=true;
                //selectedIndex=j;
            }
            else
                newOpt.setAttribute("style","margin:0; cursor:default; white-space:nowrap;");
            newOpt.setAttribute("onmouseover","highlight(this,'"+selects[i].name+"',1)");
            newOpt.setAttribute("onmouseout","highlight(this,'"+selects[i].name+"',0)");
            newOpt.setAttribute("onclick",'select_this("'+options[j].value+'","'+newOpt.innerHTML+'","'+selects[i].name+'")');
            if (isSelected)
            {
                selected[selects[i].name]=newOpt;
                //alert(selected[selects[i].name].innerHTML+' '+selects[i].name);
            }
            newDiv.appendChild(newOpt);
        }
        //alert(selects[i].name+' '+divWidth);
        if (divWidth<4)
            divWidth=25;
        else
            divWidth=divWidth*7;
        newDiv.setAttribute("style","width:"+(divWidth)+"px; border:1px solid #000000; position:absolute; max-height:175px; overflow:auto; display:none; font-family:Sans; font-size:80%; background-color:"+bcolor+";");
        selects[i].parentNode.insertBefore(newDiv,selects[i]);
        //newDiv.scrollTop=selected[select_id].offsetTop;

        //if (selects[i].name=="order_by") alert(selects[i].parentNode);

        selects[i].parentNode.removeChild(selects[i]);
    }
    if (document.location.href.match('task_search.php')) document.getElementById('more').style.display="none";
}
function select_this(id,svalue,select_id)
{
    if (hideTimer) clearTimeout(hideTimer);
    document.getElementById(select_id+'_value').value=svalue.replace(/&nbsp;/g,"");
    document.getElementById(select_id).value=id;
    //alert(document.getElementById(select_id)+' '+id);
    document.getElementById(select_id+'_options').style.display="none";
    document.getElementById(select_id+"_value").select();
}
var direction;
function positioningOptions(select_id,changeDirection)
{
    var element=document.getElementById(select_id+'_value');
    var toppos=0;
    var leftpos=0;
    while ((element!=null) && (element.tagName!="BODY"))
    {
        toppos=toppos+element.offsetTop;
        leftpos=leftpos+element.offsetLeft;
        element=element.offsetParent;
    }
    document.getElementById(select_id+"_options").style.display="block";
    optionsHeight=document.getElementById(select_id+"_options").offsetHeight;
    if (changeDirection)
    {
        if ((toppos-window.scrollY+10+optionsHeight*1)>window.innerHeight)
        {
            toppos=toppos-optionsHeight;
            direction="up";
        }
        else
        {
            toppos=toppos+document.getElementById(select_id+'_value').offsetHeight;
            direction="down";
        }
    }
    else
    {
        if (direction=="up")
        {
            toppos=toppos-optionsHeight;
        }
        else
        {
            toppos=toppos+document.getElementById(select_id+'_value').offsetHeight;
        }
    }
    document.getElementById(select_id+"_options").style.top=toppos+"px";
    document.getElementById(select_id+"_options").style.left=leftpos+"px";
    document.getElementById(select_id+"_options").style.display="block";
    document.getElementById(select_id+"_options").scrollTop=document.getElementById(selected[select_id].id).offsetTop;
}
function showOptions(select_id)
{
    if (document.getElementById(select_id+"_options").style.display=="none")
    {
        positioningOptions(select_id,true);
    }
    else document.getElementById(select_id+"_options").style.display="none";
}
var hideTimer;
function tbLostFocus(select_id)
{
    hideTimer=setTimeout("hideOptions('"+select_id+"')",280);
}
function hideOptions(select_id)
{
    document.getElementById(select_id+'_options').style.display="none";
}
var hintsTimer=null;
function changeText(e,select_id)
{
    //if (document.getElementById('selectText').value)
    {
        if (hintsTimer) clearTimeout(hintsTimer);
        hintsTimer=setTimeout('showHints(e,"'+select_id+'")',500);
        //showHints(e,select_id);
    }
}
var sel_nr=0;
var selected=new Array();
var searched="";
function clearSearchText()
{
    searched="";
}
function showHints(e,select_id)
{
    if (hintsTimer)
    {
        clearTimeout(hintsTimer);
    }
    hintsTimer=setTimeout('clearSearchText()',700);
    var prev_selected;
    //alert(e.which+' '+e.keyCode+' '+e.charCode);
    if (selected[select_id])
    {
        prev_selected=selected[select_id];
    }
    switch (e.keyCode)
    {
        case 8:
        case 9:
        {
            hideOptions(select_id);
            return e.keycode;
        }
        case 13:
        case 27:
        {
            hideOptions(select_id);
            return false;
        }
        case 35: //end
        {
            var x=0;
            if (selected[select_id])
            {
                x=selected[select_id].id.split("__")[1];
            }
            while (document.getElementById(select_id+"__"+(x*1+1)))
            {
                x++;
            }
            selected[select_id]=document.getElementById(select_id+"__"+x);
            sel_nr=1;
            break;
        }
        case 36: //home
        {
            selected[select_id]=document.getElementById(select_id+"__0");
            sel_nr=1;
            break;
        }
        case 40:
        {
            if (selected[select_id])
            {
                var x=selected[select_id].id.split("__")[1];
                x=x*1+1;
                selected[select_id]=document.getElementById(select_id+"__"+x);
            }
            else selected[select_id]=document.getElementById(select_id+"__0");
            sel_nr=1;
            break;
        }
        case 38:
        {
            if (selected[select_id])
            {
                var x=selected[select_id].id.split("__")[1];
                x=x*1-1;
                selected[select_id]=document.getElementById(select_id+"__"+x);
            }
            else selected[select_id]=document.getElementById(select_id+"__0");
            sel_nr=1;
            break;
        }
        default:
        {
            var filled=false;
            var pressed=String.fromCharCode(e.which).toLowerCase();
            if (searched==pressed)
                sel_nr++;
            else
            {
                searched=searched+pressed;
                sel_nr=1;
            }
            //searched=document.getElementById(select_id+'_text').value;
            var options_div=document.getElementById(select_id+'_options');
            var elements=options_div.childNodes;
            var count=elements.length;
            var j=0;
            var firstMatch;
            for (i=0; i<count; i++)
            {
                var element=elements[i];
                if (element.tagName!="P") continue;
                if (element.innerHTML.toLowerCase().replace(/&nbsp;/g,"").match("^"+searched))
                {
                    j++;
                    if (j==1) selected[select_id]=element;
                    if (j==sel_nr)
                    {
                        selected[select_id]=element;
                        filled=true;
                        break;
                    }
                    if (j>sel_nr) break;
                }
            }
            if (!filled)
            {
                sel_nr=1;
            }
        }
    }
    if (selected[select_id])
    {
        if (prev_selected)
        {
            prev_selected.style.backgroundColor="#ffffff";
            prev_selected.style.color="#000000";
        }
        document.getElementById(select_id+'_value').value=selected[select_id].innerHTML.replace(/&nbsp;/g,"");
        document.getElementById(select_id).value=selected[select_id].className;
        selected[select_id].style.backgroundColor="#7099cc";
        selected[select_id].style.color="#ffffff";
        document.getElementById(select_id+'_options').scrollTop=selected[select_id].offsetTop;
    }
    if ((e.which>31) && (e.which<127))
        return false;
    //else return e.;
}
function highlight(element,select_id,direction)
{
    if (direction==1)
    {
        element.style.backgroundColor="#7099cc";
        element.style.color="#ffffff";
        //alert(selected[select_id].id+' '+document.getElementById(selected[select_id].id).style.backgroundColor);
        if (element.id!=selected[select_id].id)
        {
            document.getElementById(selected[select_id].id).style.backgroundColor="#ffffff"; //selected[select_id].style.backgroundColor="#ffffff";
            document.getElementById(selected[select_id].id).style.color="#000000"; //selected[select_id].style.color="#000000";
        }
        //alert(selected[select_id].id+' '+document.getElementById(selected[select_id].id).style.backgroundColor);
    }
    else
    {
        element.style.backgroundColor="#ffffff";
        element.style.color="#000000";
    }
}

//captcha checkers
function focuson()
  { document.comment.number.focus()}

function captchacheck()
    {
    var span=document.getElementById("numberspan");
    if(document.comment.number.value==0)
        {
        error("numberspan", '<img src="sitepic/wrong.png" alt=""> Add meg az ellenőrző kódot!');
        return false;
        document.comment.number.focus();
        }
    }
function error(mezo, szoveg)
{
var place=document.getElementById(mezo);
place.innerHTML=szoveg;
place.style.color="#570303";
}
