function cms_forgot_password() {
    popupCenterWindowOnScreen('password.php', '', 400, 210);
//
//    var email = document.getElementById('email').value;
//    if (email.length == 0) {
//        alert('Please enter an email address to retrieve your password');
//        return;
//    }
//    window.open('forgot_password.php?email=' + email);
}


function ta_limit() {
    var taObj=event.srcElement;
    if (taObj.value.length>=taObj.maxlength*1) return false;
}

function ta_count(sobj) { 
    var taObj=event.srcElement;
    if (taObj.value.length>taObj.maxlength*1) taObj.value=taObj.value.substring(0,taObj.maxlength*1);
    var obj = document.getElementById(sobj);
    if (obj) obj.innerText=taObj.maxlength-taObj.value.length;
}

var qs=[];
p = location.search.substring(1).replace(/\+/g,' ').split("&");

for (var i=0;i<p.length;i++){
    qs[unescape(p[i].split("=")[0])] = unescape(p[i].split("=")[1]);
}
function r() {
    this.QueryString = new Function("p","return qs[p]");
}

var Request = new r();

function clean_url() {
    var args = clean_url.arguments;
    var s = location.href;
    var addr = s.split('?');
    var s_url = s.substring(0,addr[0].length);
    var s_args = s.substring(addr[0].length+1);
    var a = s_args.split('&');
    var qs = s_url + '?';
    var keep_arg;
    for (var i=0;i<a.length;i++) {
        keep_arg = true;
        for (var j=0;j<args.length;j++) {
            if (a[i].indexOf(args[j]) == 0) {
                keep_arg = false;
            }
        }
        if (keep_arg) {
            qs += a[i] + '&';
        }
    }
    return qs;
}

function submit_tab(s) {
    if (window.validateForm) {
        var b = validateForm(document.frMain);
        if (!b)
            return;
    }
    if (document.frMain.actual_followpage) {
        document.frMain.followpage.value = document.frMain.actual_followpage.value;
    } else {
        var url = clean_url('tabsid','method','id','send');
        url = url + '&tabsid=' + s + '&method=update&id=%id%';
        document.frMain.followpage.value = url;
    }
    if (document.frMain.onsubmit) {
        var s = document.frMain.onsubmit + '';
        var ss = s.substring(s.indexOf('{')+1);
        var sss = ss.substring(0, ss.lastIndexOf('}'));
        var s = sss.replace('this', 'document.frMain');
        s = 'function anonymous() {' + s + '}';
        eval(s);
        if (window.anonymous) {
            if (anonymous()) {
                document.frMain.submit();
                return false;
            } else {
                return;
            }
        } else {
            document.frMain.submit();
            return false;
        }
    } else {
        document.frMain.submit();
        return false;
    }
}

function submit_filter(obj) {
    var s_url = clean_url(obj.name, 'page');
    s_url = s_url + obj.name + '=' + obj.value;
    location.href = s_url;
    return false;
}

function clear_page_param(s_url) {
	var s = s_url;
	p_page = s.indexOf('page');
	p_amp = s.indexOf('&',p_page);
	p_name = s.substring(p_page,p_amp);
	re = eval("/" + p_name + "/");
	s = s.replace(re,'');
	return s;
}

function toggle(s, i, c) {
    var obj = document.getElementById(s);
    var fr_obj = document.getElementById(c);
    if (obj) {
        if (obj.style.display == 'none') {
            if (fr_obj.value.indexOf(',' + i + ',') == -1) {
                fr_obj.value += ',' + i + ',';
            }
            obj.style.display = 'block';
        } else {
            fr_obj.value = fr_obj.value.replace(',' + i + ',', '');
            obj.style.display = 'none';
        }
    }
}

function checkRadioControl(objFormField){
    var intControlLength = objFormField.length
    for (i=0;i<intControlLength;i++) {
        if (objFormField[i].checked) {
            return true;
        }
    }    
    return false; 
}

function cms_validate(obj) {
    var msg = '';
    var done = '';
    for (var i=0;i<obj.elements.length;i++) {

        //alert(obj.elements[i].name + ': ' + obj.elements[i].required_text + ' = ' + obj.elements[i].required_type);
        if ((obj.elements[i].required_text || obj.elements[i].required_type) && done.indexOf(',' + obj.elements[i].name + ',') == -1) {
            var t = obj.elements[i].type;
            var rtext = obj.elements[i].required_text;
            var rtype = obj.elements[i].required_type;
            if (rtype) {
                t = rtype;
            }
            switch (t) {
            	case 'past_date':
            		msg += past_date(obj.elements[i]);
            		break;
                case 'email' :
                    if (rtext.length == 0 && obj.elements[i].value.length == 0) {
                        break;
                    }
                    msg += emailCheck(obj.elements[i].value);
                    break;
                case 'radio' :
                        var my_obj = obj.elements[i];
                        eval('var my_obj = obj.' + obj.elements[i].name + ';');
                        if (!checkRadioControl(my_obj)) {
                            msg += rtext + '\n';
                        }
                    break;
                case 'select-one' :
                        if (obj.elements[i].options[obj.elements[i].selectedIndex].value.length == 0 || obj.elements[i].options[obj.elements[i].selectedIndex].value == 0) {
                            msg += rtext + '\n';
                        }
                    break;
                default :
                        if (!obj.elements[i].value) {
                            msg += rtext + '\n';
                        }
                    break;
            }
            done += ',' + obj.elements[i].name + ',';
        }
    }
    if (msg.length > 0) {
        alert(msg);
        return false;
    }
    return true;
}

function past_date(obj) {
	var thedate = new Date(obj.value + ' 23:59:59');
	var todaysdate = new Date();
	if(thedate < todaysdate) {
		return 'Please select a date in the future.\n';
	}	
	return '';
}

function emailCheck (emailStr) {
    var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
        return "Email address seems incorrect (check @ and .'s)";
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
            return "Ths username contains invalid characters.";
        }
    }
    for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
            return "Ths domain name contains invalid characters.";
        }
    }
    if (user.match(userPat)==null) {
        return "The username doesn't seem to be valid.";
    }
    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                return "Destination IP address is invalid!";
            }
        }
        return '';
    }
    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
            return "The domain name does not seem to be valid.";
        }
     }
     if (checkTLD && domArr[domArr.length-1].length!=2 && 
         domArr[domArr.length-1].search(knownDomsPat)==-1) {
         return "The address must end in a well-known domain or two letter " + "country.";
     }
     if (len<2) {
         return "This address is missing a hostname!";
     }
     return '';
}

function popupCenterWindowOnScreen(URL,Settings,popW,popH) {

    w = screen.width;
    h = screen.height;

    var leftPos = (w-popW)/2, topPos = (h-popH)/2;

    var myPopup = window.open(URL,'_blank',Settings + ',top=' + topPos + ',left=' + leftPos + ',width=' + popW + ',height=' + popH);
    myPopup.opener = self;
    myPopup.focus();
}

function CenterWindowOnScreen(popW,popH) {

    w = screen.width;
    h = screen.height;

    var leftPos = (w-popW)/2, topPos = (h-popH)/2;
    window.moveTo(topPos, leftPos);
    window.resizeTo(w, h);
}

// Must finish bottom function
function fixPrice(id) {
    qty_sObj = 'qty[' + id + ']';
    status_sObj = 'status[' + id + ']';
    pricediv_sObj = 'pricediv[' + id + ']';
    price_sObj = 'price[' + id + ']';
    qty_obj = document.getElementById(qty_sObj);
    status_obj = document.getElementById(status_sObj);
    pricediv_obj = document.getElementById(pricediv_sObj);
    price_obj = document.getElementById(price_sObj);
    grand_total_obj = document.getElementById('grand_total');
    if (qty_obj.value > 0) {
        price = price_obj.value * qty_obj.value;
        pricediv_obj.innerHTML = 'R ' + price;
    }
}

function updateForm(val) {
    ccobj = document.getElementById('ccdiv');
    accobj = document.getElementById('accdiv');
    cccobj = document.getElementById('cccdiv');
    eftobj = document.getElementById('eftdiv');
    switch (val) {
        case '1':
        case '2':
        case '3':
        default:
            ccobj.style.display = 'block';
            cccobj.style.display = 'block';
            accobj.style.display = 'none';
            eftobj.style.display = 'none';
            break;
        case '4':
            ccobj.style.display = 'none';
            cccobj.style.display = 'none';
            accobj.style.display = 'none';
            eftobj.style.display = 'block';
            break;
        case '5':
            ccobj.style.display = 'none';
            cccobj.style.display = 'block';
            accobj.style.display = 'block';
            eftobj.style.display = 'none';
            break;
    }
}
function subscribe_validate() {
   cansubmit = true;
   if (!document.subscribe.email.value || document.subscribe.email.value == 'Your Email') {
       cansubmit = false;
       alert('You must fill in an email address');
       document.subscribe.email.focus();
    }
    return cansubmit;
}

