﻿var ammendged = false;
var allOK = true;
var msg = '';

function addPriceWatch() {
    allOK = true;
    unblock();
    msg = 'Please correct the following errors.\n Fields with errors are highlighted in red\n\n';
    
    var catid = ele('pwCatID').value;
    var brand = ele('pwBrand').value;
    var prodname = ele('pwName').value;
    var price = ele('pwPrice').value;
    var email = ele('pwEmail').value;

    if (price == null || price == '')
        ammend('pwPrice', 'Please enter a Watch Price for this Product.');
    if (email == null || email == '')
        ammend('pwEmail', 'Please enter an Email address.');
    if (!ele('pwAccept').checked)
        ammend('pwAccept', 'You must accept our Terms and Conditions.');

    if (allOK == true) {
        loadJSON('http://www.binbin.net/AddToDB.aspx?ty=pw&pn=' + prodname + '&cid=' + catid + '&br=' + brand + '&em=' + email + '&pr=' + price);
    }
    else
        alert(msg);

    return false;
}

function addReview() {
    allOK = true;
    unblock();
    msg = 'Please correct the following errors.\n Fields with errors are highlighted in red\n\n';

    var txt = document.getElementById('ReviewText').value;
    txt = txt.replace(new RegExp("\\&", "g"), "and");
    txt = txt.replace(new RegExp("\n", "g"), " zvx ");
    
    var catid = ele('revCatID').value;
    var brand = ele('revBrand').value;
    var prodname = ele('revName').value;
    var reviewTxt = txt; //ele('ReviewText').value.replace(new RegExp("\\&", "g"), "and");
    var rating = document.getElementsByName('rating');
    var disname = ele('displayName').value.replace(new RegExp("\\&", "g"), "and");
    var rate = '';
    
    var cnt = -1;
    for (var i = rating.length - 1; i > -1; i--) {
        if (rating[i].checked) { cnt = i; i = -1; }
    }
    if (cnt > -1)
        rate = rating[cnt].value;
        
    if (reviewTxt == null || reviewTxt == '')
        ammend('ReviewText', 'Please enter a Review for this Product.');
    if (rate == null || rate == '')
        ammend('rating', 'Please Rate this Product.');
    if (disname == null || disname == '')
        ammend('displayName', 'Please enter a Display Name.');

    if (allOK == true)
        loadJSON('http://www.binbin.net/AddToDB.aspx?ty=rev&pn=' + prodname + '&cid=' + catid + '&br=' + brand + '&rev=' + reviewTxt + '&dn=' + disname + '&rt=' + rate);
    else
        alert(msg);

    return false;
}

function askQuestion() {    
    allOK = true;
    unblock();
    msg = 'Please correct the following errors.\n Fields with errors are highlighted in red\n\n';

    var catid = ele('questCatID').value;
    var brand = ele('questBrand').value;
    var prodname = ele('questName').value;
    var disname = ele('questDisName').value.replace(new RegExp("\\&", "g"), "and");
    var qTxt = ele('QuestionText').value.replace(new RegExp("\\&", "g"), "and");

    if (disname == null || disname == '')
        ammend('questDisName', 'Please enter a Display Name.');
    if (qTxt == null || qTxt == '')
        ammend('QuestionText', 'Please ask a Question.');

    if (allOK == true)
        loadJSON('http://www.binbin.net/AddToDB.aspx?ty=quest&pn=' + prodname + '&cid=' + catid + '&br=' + brand + '&dn=' + disname + '&q=' + qTxt);
    else
        alert(msg);
        
    return false;
}

function DoAnswer(divid, qid) {
    document.getElementById(divid).style.display = 'inline';
}

function answerQuestion(qid) {
    var disname = ele('answerDisName' + qid).value.replace(new RegExp("\\&", "g"), "and");
    var aTxt = ele('answerText' + qid).value.replace(new RegExp("\\&", "g"), "and");

    if (disname == null || disname == '')
        ammend('answerDisName' + qid, 'Please enter a Display Name.');
    if (aTxt == null || aTxt == '')
        ammend('answerText' + qid, 'Please answer the Question.');

    if (allOK == true)
        loadJSON('http://www.binbin.net/AddToDB.aspx?ty=answ&qid=' + qid + '&dn=' + disname + '&q=' + aTxt);
    else
        alert(msg);
}

function ele(e) { return document.getElementById(e); }

function ammend(name, error) {
    allOK = false;
    ele(name).style.backgroundColor = '#FF0000';
    try {
        ele(name).focus();
    }
    catch (x) { }

    msg += error + '\n';
}

function unblock() {
    for (var i = 0; i < window.top.document.getElementsByTagName('SELECT').length; i++)
        window.top.document.getElementsByTagName('SELECT')[i].style.backgroundColor = "#ffffff";
    for (var i = 0; i < window.top.document.getElementsByTagName('INPUT').length; i++)
        if (window.top.document.getElementsByTagName('INPUT')[i].id != 'btnNext' && window.top.document.getElementsByTagName('INPUT')[i].id != 'pcaButtonTE35')
        window.top.document.getElementsByTagName('INPUT')[i].style.backgroundColor = "#ffffff";

    if (navigator.appVersion.indexOf('MSIE 6') > -1)
        for (var i = 0; i < window.top.document.getElementsByTagName('SELECT').length; i++) {
        try {
            var old = window.top.ge('txt' + window.top.document.getElementsByTagName('SELECT')[i].id);
            old.id = '';
            old.style.display = 'none';
            window.top.document.getElementsByTagName('SELECT')[i].style.display = 'inline';
        }
        catch (x) { x = x; }
    }
}

function loadJSON(url) {
    var headID = document.getElementsByTagName("head")[0];
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript';
    newScript.src = url;
    headID.appendChild(newScript);
}

function PWClearAll() {
    ele('pwEmail').value = '';
    ele('pwAccept').checked = false;

    alert('Thank You.\n\n Your Price Watch has been logged.');
}

function RevClearAll() {
    ele('ReviewText').value = '';
    ele('displayName').value = '';

    var rating = document.getElementsByName('rating');
    var cnt = -1;
    for (var i = rating.length - 1; i > -1; i--) {
        rating[i].checked = false;
    }

    alert('Thank You.\n\n Your Review has been submitted.');
}

function QuestClearAll() {
    ele('questDisName').value = '';
    ele('QuestionText').value = '';

    alert('Thank You.\n\n Your Question has been saved.');
}

function AnswerClearAll(qid) {
    ele('answerDisName' + qid).value = '';
    ele('answerText' + qid).value = '';
    document.getElementById('answerPan' + qid ).style.display = 'none';

    alert('Thank You.\n\n Your Answer has been submitted.');
}