/****************************
*|     Created: March 2008
*|    for: Shoprite
*|    by: stonewall+
*|    author: wicht
****************************/

function sendToFriend(){
    var documentURL = document.URL
    var fullURL     = "/pages/127416071/SendToFriend.asp?fullURL="+ documentURL
    // Option 1 - rerirect
    // ===================
    //window.location.href= fullURL

    // Option 2 Popup
    // ==============
    //window.open(fullURL, "sendtoafriend", "WIDTH=400, HEIGHT=300")

    // Options 3 - iFrame
    // ==================
    closePopUpWinow();
    var oDiv = document.createElement("DIV");
        oDiv.id = "WindowPopup";
        oDiv.innerHTML = '<IFRAME style="margin:35px; width: 395px; height:330px" src="'+ fullURL +'" frameborder=0></FRAME>';
    document.body.appendChild(oDiv);
}
function openVideo(pagePath){
    closePopUpWinow();
    var oDiv = document.createElement("DIV");
        oDiv.id = "WindowPopup";
        oDiv.innerHTML = '<IFRAME style="margin:35px; width: 395px; height:330px" src="'+ pagePath +'" frameborder=0></FRAME>';
    document.body.appendChild(oDiv);
}
function bookmarksite(){
    var title  = document.title
    var urlStr = "http://"+ document.URL
    if (window.sidebar){
        window.sidebar.addPanel(title, urlStr, "");
    }else if(window.opera && window.print){ // opera
        window.external.AddFavorite(urlStr, title);
    }else if(window.external){
        window.external.AddFavorite(urlStr, title);
    }

    //return false;
}
function closePopUpWinow(){
    try{
        var oDiv = document.getElementById("WindowPopup");
        document.body.removeChild(oDiv);
    }catch(e){}
}
function recallPromoRegion(){
    var regionStr = readCookie('poromoRegion');
    if(regionStr!=null && regionStr!=""){
        var selObj = document.homePagePromo.selectRegionalPromo;
        for(var i=0; i<selObj.length; i++){
            if(selObj[i].text==regionStr)selObj[i].selected = true;
        }
    }
}
function jumpPromo(obj){
    var selObj = document.homePagePromo.selectRegionalPromo;
    var selText = selObj[selObj.selectedIndex].text;
    // alert("Click OK to remember the '"+ selText +"' as your pefered region.")
    writeCookie('poromoRegion',selText,365);
    location.href=document.homePagePromo.selectRegionalPromo.value
}
function mapClick(region){
    writeCookie('poromoRegion',region,365);
}
function writeCookie(name,value,days) {
    if (days) {
        var valueStr = escape(value);
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
    }
    return null;
}

function showLayer(layerName)  {
    document.getElementById(layerName).style.visibility='visible';
}

function hideLayer(layerName)  {
    document.getElementById(layerName).style.visibility='hidden';
}

function openPDF(both){
    if(both!=""){
        var pdfFile = both.split(" | ");
        if(pdfFile[0].indexOf(".pdf")!=-1 || pdfFile[0].indexOf(".PDF")!=-1){
            window.open("/files/127416071/Consumer-Centre/PDF"+pdfFile[0]);
        }else{
            alert("Sorry, there is no DDF file with this article.")
        }
    }else{
        alert("Sorry, there is no DDF file with this article.")
    }

}

function openWord(both){
    if(both!=""){
        var strFile = both.split(" | ");
        if(strFile[1].indexOf(".doc")!=-1 || strFile[1].indexOf(".DOC")!=-1){
            window.open("/files/127416071/Consumer-Centre/Word"+strFile[1]);
        }else{
            alert("Sorry, there is no Word file with this article.")
        }
    }else{
        alert("Sorry, there is no Word file with this article.")
    }

}

function doProfile(id,selected) {

    if (document.getElementById) {

        for ( var i = 0; i <12; i++ ) {
try{
        document.getElementById('adblock' + i).style.display = 'none';
}catch(e){}
        }

        document.getElementById('adblock' + id).style.display = 'block';

        var nav_box = document.getElementById("consumer-services");
        var links = nav_box.getElementsByTagName("a");

        for(var i=0; i<links.length; i++)

        links[i].className = "";

        selected.className = "selected";

    } else

        alert('Please update your browser');

}

function doTabs(id,selected) {
    if (document.getElementById) {
         for ( var i = 0; i < 4; i++ ) {
            document.getElementById('consumer-content' + i).style.display = 'none';
         }
        document.getElementById('consumer-content' + id).style.display = '';
    } else
        alert('Please update your browser');
}


/*  promotions map  */

function doRegion(id,selected) {

    if (document.getElementById) {

        for ( var i = 0; i < 9; i++ ) {

        document.getElementById('region' + i).style.display = 'none';

        }

        document.getElementById('region' + id).style.display = 'block';

        var nav_box = document.getElementById("promo-right");
        var links = nav_box.getElementsByTagName("a");

        for(var i=0; i<links.length; i++)

        links[i].className = "";

        selected.className = "selected";

    } else

        alert('Please update your browser');

}




/* keyword search form ------------------ */

function validKeyword(keywordForm) {

    if (keywordForm.keyword.value == 'Type keyword') {
        alert("Please enter a search keyword");
        keywordForm.keyword.focus();
        return false;
    }

    return true;

}

function sortFromRegion(subscribeForm){
    var regionStr = subscribeForm.C_2.value
    var regionID = ""
    // Eastern Cape, Free State, Gauteng, KwaZulu-Natal, Mpumalanga, Northern Cape, Limpopo Province, North West Province, Western Cape
    if(regionStr=="Eastern Cape"){
    var regionID = 2
    }else if(regionStr=="Free State"){
    var regionID = 3
    }else if(regionStr=="Gauteng"){
    var regionID = 4
    }else if(regionStr=="KwaZulu-Natal"){
    var regionID = 5
    }else if(regionStr=="Mpumalanga"){
    var regionID = 6
    }else if(regionStr=="Northern Cape"){
    var regionID = 7
    }else if(regionStr=="Limpopo Province"){
    var regionID = 8
    }else if(regionStr=="North West Province"){
    var regionID = 9
    }else if(regionStr=="Western Cape"){
    var regionID = 10
    }
    subscribeForm.passML.value = regionID
}

/* Subscribe Form ------------------ */

function validSubscribe(subscribeForm) {

    if (subscribeForm.Name.value == '') {
        alert("Please enter your first name");
        subscribeForm.Name.focus();
        return false;
    }

    if (subscribeForm.Surname.value == '') {
        alert("Please enter your last name");
        subscribeForm.Surname.focus();
        return false;
    }

    if (subscribeForm.Cellphone.value == '') {
        alert("Please enter your cellphone number");
        subscribeForm.Cellphone.focus();
        return false;
    }

    if (!validNum(subscribeForm.Cellphone.value)) {
        alert("Please enter a valid number");
        subscribeForm.Cellphone.focus();
        return false;
    }

    if (subscribeForm.C_1.value == ' ') {
        alert("Please enter your cellphone model");
        subscribeForm.C_1.focus();
        return false;
    }

    if (subscribeForm.Email.value == '') {
        alert("Please enter your email");
        subscribeForm.Email.focus();
        return false;
    }

    if (!validEmail(subscribeForm.Email.value)) {
        alert("Please enter a valid email");
        subscribeForm.Email.focus();
        return false;
    }

    if (subscribeForm.C_4.value == ' ') {
        alert("Please select your country");
        subscribeForm.C_4.focus();
        return false;
    }

    /*if (subscribeForm.C_2.value == ' ') {
        alert("Please select your region");
        subscribeForm.C_2.focus();
        return false;
    }*/


    if (subscribeForm.C_3.value == '') {
        alert("Please enter your closest store");
        subscribeForm.C_3.focus();
        return false;
    }
    sortFromRegion(subscribeForm);
    return true;

}


/* Contact Us Form ------------------ */
function validContact(contactForm) {


    if (contactForm.firstname.value == '') {
        contactForm.firstname.value = 'Please fill in the required field';
        contactForm.firstname.style.color = 'red';
        contactForm.firstname.style.backgroundColor = 'yellow';
        contactForm.firstname.style.fontWeight = 'bold';
        return false;
    }

    if (contactForm.firstname.value == 'Please fill in the required field') {
        contactForm.firstname.value = 'Please fill in the required field';
        contactForm.firstname.style.color = 'red';
        contactForm.firstname.style.backgroundColor = 'yellow';
        contactForm.firstname.style.fontWeight = 'bold';
        return false;
    }


    if (contactForm.surname.value == '') {
        contactForm.surname.value = 'Please fill in the required field';
        contactForm.surname.style.color = 'red';
        contactForm.surname.style.backgroundColor = 'yellow';
        contactForm.surname.style.fontWeight = 'bold';
        return false;
    }

    if (contactForm.surname.value == 'Please fill in the required field') {
        contactForm.surname.value = 'Please fill in the required field';
        contactForm.surname.style.color = 'red';
        contactForm.surname.style.backgroundColor = 'yellow';
        contactForm.surname.style.fontWeight = 'bold';
        return false;
    }

    if (contactForm.emailAddr.value == '') {
        contactForm.emailAddr.value = 'Please fill in the required field';
        contactForm.emailAddr.style.color = 'red';
        contactForm.emailAddr.style.backgroundColor = 'yellow';
        contactForm.emailAddr.style.fontWeight = 'bold';
        return false;
    }

    if (contactForm.emailAddr.value == 'Please fill in the required field') {
        contactForm.emailAddr.value = 'Please fill in the required field';
        contactForm.emailAddr.style.color = 'red';
        contactForm.emailAddr.style.backgroundColor = 'yellow';
        contactForm.emailAddr.style.fontWeight = 'bold';
        return false;
    }

    if (!validEmail(contactForm.emailAddr.value)) {
        contactForm.emailAddr.value = 'Please fill in the required field';
        contactForm.emailAddr.style.color = 'red';
        contactForm.emailAddr.style.backgroundColor = 'yellow';
        contactForm.emailAddr.style.fontWeight = 'bold';
        return false;
    }

    if (contactForm.telephone.value == '') {
        contactForm.telephone.value = 'Please fill in the required field';
        contactForm.telephone.style.color = 'red';
        contactForm.telephone.style.backgroundColor = 'yellow';
        contactForm.telephone.style.fontWeight = 'bold';
        return false;
    }

    if (contactForm.telephone.value == 'Please fill in the required field') {
        contactForm.telephone.value = 'Please fill in the required field';
        contactForm.telephone.style.color = 'red';
        contactForm.telephone.style.backgroundColor = 'yellow';
        contactForm.telephone.style.fontWeight = 'bold';
        return false;
    }

        if (!validNum(contactForm.telephone.value)) {
        contactForm.telephone.value = 'Please enter a valid number';
        contactForm.telephone.style.color = 'red';
        contactForm.telephone.style.backgroundColor = 'yellow';
        contactForm.telephone.style.fontWeight = 'bold';
        return false;
    }


    return true;

}


// checks for valid telephone characters
function validNum(telno) {

    var pattern = "0123456789+-)(. ";
    var i = 0;

    do {
        var pos = 0;
        for ( var j = 0 ; j < pattern.length ; j++ )
            if ( telno.charAt(i) == pattern.charAt(j) )
                pos = 1;
        i++;
    }
    while ( pos == 1 && i < telno.length )

    if ( pos == 0 ) return false;

    return true;
}



// check email address pattern
function validEmail(addr) {

    var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;

    if ( !regex.test(addr) ) {
        alert("Invalid email address");
        return false;
    }

    return true;
}
