﻿var IMG_ROOT = 'http://i.americanapparel.net/storefront/comingsoon/pics/';
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function GetID(id) {return (_ids && _ids[id] ? _ids[id] : id);}
function ShowList(data, msg, osid) {
    var s;

    var isrc = 'http://i.americanapparel.net/storefront/comingsoon/images/icon_email2.gif';
    if (osid == 1) {
      isrc = 'http://i.americanapparel.net/storefront/comingsoon/uil/clientside/images/icon-notify.gif';
    }
    
    var html = '';
    var href = '';
    if (data && data.length > 0) {
      for (var i = 0; i < data.length; i++) {
        s = data[i];

        href = 'details.aspx?i=' + s.id + '#';

        html += '<div class="productimage"><a href="' + href + '"><img alt="' + s.name + '" src="' + IMG_ROOT + s.numb + '[1L]' + s.extn;
        html += '" style="width:250px;height:300px;" /></a></div>';
        html += '<div class="productbox">';
        html += '	<div class="alertboxC">';
        html += '		<div class="titlename">';
        html += '			' + s.numb + '<br />';
        html += '			<span class="bigtitle">' + s.name + '</span>';
        html += '		</div>';
        html += '		<div class="description">' + s.desc + '</div>';
        html += '		<div class="alert lang">';
        html += '			<a href="' + href + '">';
        html += '			  <img alt="" src="' + isrc + '" width="24" height="14" style="margin:0 1px -1px 0" /> ';
        html += '			  ' + msg[3];
        html += '			</a>';
        html += '		</div>';
        html += '	</div>';
        if (s.cCount) {
          html += '<div class="commentsnumber lang">' + '<a href="' + href + '" >' + (s.cCount < 2 ? msg[1] : msg[2].replace('{0}', s.cCount)) + '</a>' + '</div>';
          html += '<div class="commentbox lang">';
          html += '	<div class="comment">' + s.cText + '</div>';
          html += '	<div class="commenter">';
          html += '		' + s.cUser + ' <img alt="" src="http://i.americanapparel.net/images/flags/small/o' + s.cOsid + '.gif" style="margin-bottom:-1px" /><br />';
          html += '		' + GetDate(s.cDate, s.o, s.l);
          html += '	</div>';
          html += '</div>';
        }
        else {
          html += '<div class="commentsnumber lang"><a href="' + href + '">' + msg[0] + '</a></div>';
        }
        html += '</div>';
      }
    }
    else {
      var e = document.getElementById('divNoItems');
      if (e) e.style.display = 'block';
    }
    document.body.innerHTML += html;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function ShowDetails(s, comentTitle, comments) {
    var elem = document.getElementById("pnDetails");

    elem.innerHTML = s.numb + '<br /><span class="bigtitle">' + s.name + '</span><br /><br />' + s.desc;

    elem = document.getElementById("imDetails");

    elem.src = IMG_ROOT + s.numb + "[2L]" + s.extn;
    elem.alt = s.name;

    elem = document.getElementById("pnComments");
    if (elem && comments && comments.length > 0) {
        var html = '<div class="commentsnumber lang" style="margin-bottom:-2px">' + comentTitle + '</div>';
        for (var i = 0; i < comments.length; i++) {
            html += '	<div class="commentboxextended">';
            html += '		<div class="commentextended">' + comments[i].text + '</div>';
            html += '		<div class="commenter">';
            html += '			' + comments[i].user + ' <img src="http://i.americanapparel.net/images/flags/small/o' + comments[i].osid + '.gif" style="margin-bottom:-1px" /><br />';
            html += '			' + GetDate(comments[i].date, s.o, s.l);
            html += '		</div>';
            html += '	</div>';
        }
        elem.innerHTML = html + '<div class="lines"></div>';
    }
    scrollTo(0, 0);
    var v;
    v = document.getElementById('HF_AlertMe').value;
    if (v) {
      //alert(v);
      var err = v.split('#');
      if(err && err.length == 2 && err[0] == '.error' && (v = parseInt(err[1])))
      {
        ValidateSignUp(v);
      }
      else
      {
        document.getElementById('pnAlertMeDone').style.display = '';
        document.getElementById('pnAlertMeShow').style.display = 'none';
        document.getElementById('TB_AlertMeMail').value = '';
        document.getElementById('TB_AlertMePhn1').value = '';
        document.getElementById('TB_AlertMePhn2').value = '';
        document.getElementById('TB_AlertMePhn3').value = '';
      }
    }
    v = document.getElementById('HF_Comment').value
    if (v) {
        //alert(v);
        document.getElementById('pnCommentDone').style.display = '';
        document.getElementById('pnCommentEdit').style.display = 'none';
        document.getElementById('pnCommentPrev').style.display = 'none';
        document.getElementById('TB_CommentUser').value = '';
        document.getElementById('TB_CommentMail').value = '';
        document.getElementById('TB_CommentText').value = '';
        scrollTo(0, 10000);
    }

}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function ValidateComment() {
    var pnError = document.getElementById('pnErrorComment');
    var lbError = document.getElementById('lbErrorComment');
    var tbCMail = document.getElementById(GetID('TB_CommentMail'));
    var tbCText = document.getElementById(GetID('TB_CommentText'));

    pnError.style.display = 'none';
    lbError.innerHTML = '';
    tbCMail.className = '';
    tbCText.className = '';

    var email = tbCMail.value;
    var isEmail = (email.indexOf(".", email.indexOf("@")) > 2) && (email.indexOf("@") > 0);
    var isComnt = (tbCText.value.length > 0)

    var e = 0;
    if (!isComnt && !isEmail) e = 5;
    else if (!isComnt) e = 4
    else if (!isEmail) e = 1
    else e = 0;
    if (e) {
        pnError.style.display = '';
        lbError.innerHTML = _err[e];
        if (e == 5) {
            tbCMail.className = 'error';
            tbCText.className = 'error';
        }
        else if (e == 4) {
            tbCText.className = 'error';
        }
        else if (e == 1) {
            tbCMail.className = 'error';
        }
        Preview(false);
        return false;
    }
    else {
        return true;
    }
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function GetDate(dateString, o, l) {
    var d = (dateString ? new Date(Date.parse(dateString)) : new Date());

    //if (!o) o = new Number(getQueryStringValue('o'));
    if (!l) l = new Number(getQueryStringValue('l'));
    //if (!o) o = 0;
    if (!l) l = 1;

    var month =
    [['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
    , ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre']
    , ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre']
    ];
    //alert(l);
    var h = d.getHours();
    var m = d.getMinutes();
    var time = (h < 10 ? ' 0' : ' ') + h + (m < 10 ? ':0' : ':') + m;
    var date = '';
    if (l == 10) {
        time += ' PT';
        date = d.getDate() + ' de ' + month[2][d.getMonth()] + ' de ' + d.getFullYear();
    } else if (l == 9 || l == 4) {
        time = (l == 4 ? '' : time + ' PT');
        date = d.getFullYear() + '/' + (1 + d.getMonth()) + '/' + d.getDate();
    } else if (l == 2) {
        time += ' HNP';
        date = d.getDate() + ' ' + month[1][d.getMonth()] + ' ' + d.getFullYear();
    }
    else {
        time += ' PT';
        date = month[0][d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear();
    }
    return date + time;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function Preview(show, user, l) {
    var displayPrev = 'none'
    var displayEdit = ''
    if (show && ValidateComment()) {
        document.getElementById('pnErrorComment').style.display = 'none';
        document.getElementById('lbErrorComment').innerHTML = '';
        document.getElementById(GetID('TB_CommentMail')).className = '';
        document.getElementById(GetID('TB_CommentText')).className = '';
        var username = document.getElementById(GetID('TB_CommentUser')).value;
        document.getElementById('lbCommentUser').innerHTML = (username ? username : user);
        document.getElementById('lbCommentText').innerHTML = document.getElementById(GetID('TB_CommentText')).value;
        document.getElementById('lbCommentDate').innerHTML = GetDate('',-1,l);
        displayPrev = ''
        displayEdit = 'none'
    }
    document.getElementById('pnCommentPrev').style.display = displayPrev;
    document.getElementById('pnCommentEdit').style.display = displayEdit;
    return false;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function ValidateSignUp(errorIndex) {
    var pnError = document.getElementById('pnErrorSignup');
    var lbError = document.getElementById('lbErrorSignup');
    var tbEmail = document.getElementById('TB_AlertMeMail');
    var tbPhone1 = document.getElementById('TB_AlertMePhn1');
    var tbPhone2 = document.getElementById('TB_AlertMePhn2');
    var tbPhone3 = document.getElementById('TB_AlertMePhn3');

    pnError.style.display = 'none';
    lbError.innerHTML = '';
    tbEmail.className = '';
    tbPhone1.className = '';
    tbPhone2.className = '';
    tbPhone3.className = '';

    var email = tbEmail.value;
    var phone = tbPhone1.value + tbPhone2.value + tbPhone3.value;

    var isEmail = (email.indexOf(".", email.indexOf("@")) > 2) && (email.indexOf("@") > 0);
    var isPhone = (phone.length == 7 && Number(phone));

    var e = 0;
    if (phone.length > 0) {
        if (phone.length < 10) e = 2;
        else if (!(Number(phone))) e = 3;
    } else {
        e = (e < 1 && !isEmail ? 1 : e);
    }
    if (!e && errorIndex && -1 < errorIndex && errorIndex < _err.length) e = errorIndex;
    var valid = true;
    if (e) {
        pnError.style.display = '';
        lbError.innerHTML = _err[e];
        if (e == 1) {
            tbEmail.className = 'error';
        }
        else if (e > 1) {
            tbPhone1.className = 'error';
            tbPhone2.className = 'error';
            tbPhone3.className = 'error';
        }
        valid = false;
    }
    var button = document.getElementById(GetID('BT_Singup'));
    if (button) button.style.visibility = (valid ? "hidden" : "visible");
    return valid;
}
function popIt(width, height, scrollbars, pname, url) {
    if (!width) width = "500";
    if (!height) height = "500";
    if (!scrollbars) scrollbars = 1;

    var leftPos, topPos;
    if (window.innerWidth) {
        leftPos = (window.innerWidth - width) / 2;
        topPos = ((window.innerHeight - height) / 4) - 50;
    }
    else {
        leftPos = (parseInt(window.screen.width) - width) / 2;
        topPos = ((parseInt(window.screen.height) - height) / 2) - 50;
    }
    var windowString = 'width=' + width +
                     ',height=' + height +
                     ',resizable=1,status=0,toolbar=0,location=0' +
                     ',scrollbars=' + scrollbars +
                     ',left=' + leftPos +
                     ',top=' + topPos +
                     ',scrolling=auto';
    var popItWindow = window.open(url, pname, windowString);
    if (window.focus) {
        popItWindow.focus();
    }
    return false;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function AddOnload(myfunc) {
    if (window.addEventListener)
        window.addEventListener('load', myfunc, false);
    else if (window.attachEvent)
        window.attachEvent('onload', myfunc);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function getQueryStringValue(key) {
    var val = ''
    var url = document.URL;
    var i0 = url.indexOf('&' + key + '=')
    if (i0 < 0) {
        i0 = url.indexOf('?' + key + '=')
    }
    if (i0 > -1) {
        i0 += key.length + 2;
        var i1 = url.indexOf('&', i0);
        if (i1 < 0) {
            i1 = url.length;
        }
        val = url.substring(i0, i1);
    }
    return val;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */