/* Disable Right Click */
document.oncontextmenu = new Function("return false;");

/* START:Flash Detect*/
var swf = false;
function hasFlash(ver) {
  if(!ver) ver = 0;
  var n = navigator;
  if(n.plugins && n.plugins.length > 0) {
    var m,t,d,v;
    m = n.mimeTypes;
    t = 'application/x-shockwave-flash';
    if(m && m[t] && m[t].enabledPlugin && m[t].enabledPlugin.description) {
      d = m[t].enabledPlugin.description;
      v = d.charAt(d.indexOf('.')-1);
      swf = (v >= ver) ? true : false;
    }
  } else if(n.appVersion.indexOf("Mac") == -1 && window.execScript) {
    for(var i=ver; i<=7&&i!=1&&swf!=true; i++) {
      execScript('on error resume next: swf=IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash'+((i==0)?'':'.'+i)+'"))','VBScript');
    }
  } else {
    swf = false;
  }
  return swf;
}
/* END:Flash Detect*/


/* START:COMMON SCRIPT*/
function swapImg(imgid,id) {
  o = document.getElementById(imgid);
  o.src = o.src.replace(/_\d\./,'_'+id+'.');
}

/* OpenWindow
ACTION:
Open a new instance of browser
NOTES:
Multiple calls will re-use the same window;
Scrollable arg should be "scroll" or "no_scroll" for ease of reading(nothing cryptic)
*/
function OpenWindow(url,height,width,top,left,scrollable){
  var owcscroll = (scrollable=='scroll')?'yes':'no';
  cm_ow = window.open(url,'cm_ow','height='+height+',width='+width+',resizable=1,top='+ top +',left='+ left +',scrollbars='+owcscroll);
  cm_ow.focus();
}
/* END:COMMON SCRIPT*/
function OpenPopWindow(url,height,width,top,left,scrollable){
    var winstyle = ',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,unadorned=yes,unadorned:yes,';
    var fSO = 'scrolling=no noresize';
    cm_ow = window.open(url,'cm_ow','height=342'+',width='+width+',resizable=1,top='+ top +',left='+ left+winstyle+fSO);
    cm_ow.focus();
}
/* to avoid implementing section-based js files, include the js here. if it gets bulky separate it later */
function popUpThirdPartywindow(promoURL){
  var w = 404;
  var h= 309;
   
    if (document.all) {
        w = document.body.clientWidth;
        h = document.body.clientHeight;
    }
    else if (document.layers) {
      w = window.innerWidth;
      h = window.innerHeight;
    }
    var popW = 300, popH = 200;
    var leftPos = (w-popW)/2, topPos = (h-popH)/2;
    
    var thirdPartyWin = window.open("","","width=404,height=309,top=" + topPos + ",left=" + leftPos + ",location=0,status=0,scrollbars=true,menubar=0,resizable=0")
    var text = promoURL;
    
    var html =  "<Title>Third Party Site Notification</Title><body style='background: url(/eCommerceWeb/images/divBrands/common/bg-3rdparty-notice.gif)';>" + 
                "<table width=404 height=313 border=0>" + 
                "<tr><td height=160>&nbsp;</td><td height=160>&nbsp;</td></tr>"+
                "<tr><td width=50%>&nbsp;</td><td align=LEFT>"+
                "<a href=# onclick=\"opener.location='"+text+"';window.close();\"><img src='/eCommerceWeb/images/divBrands/common/button-3rdpartyok.gif' border='0'></a>&nbsp;&nbsp;&nbsp;" + 
                "<a href=Javascript:window.close();><img src='/eCommerceWeb/images/divBrands/common/button-3rdpartycancel.gif'  border='0'></a>" + 
                "</td></tr>" + 
                "</table></body>" ;
                
    thirdPartyWin.document.open();
    thirdPartyWin.document.write(html);
    thirdPartyWin.document.close();
}


/* START:INVESTOR SECTION JAVASCRIPT */
function ShowReport(ele){
  var val = ele.options[ele.selectedIndex].value;
  if(val!='') document.location.href=val;
}
//investor section also uses showNewsList() which is identical as in the news section

/* END:INVESTOR SECTION JAVASCRIPT*/


function selectList(ele) {
  var val = ele.options[ele.selectedIndex].value;
	if(document.location.toString().indexOf("abs_investorinformation")+1){
    val = '/abs_investorinformation/pressrelease/default.asp?'+val.split('?')[1];
  }
  if(val!='') document.location.href=val;
}



/* START:NEWS SECTION JAVASCRIPT */
function showNewsList(ele) { //also used in investor section
  var val = ele.options[ele.selectedIndex].value;
  if(val!='') document.location.href=val;
}
/* END:NEWS SECTION JAVASCRIPT*/

/* START: Force Numeric */
function ForceNumeric(field)
{
	var inputStr = field.value.toString();
	var strLength = inputStr.length;
	var newStr = "";
	for (var i = 0; i < strLength; i++) 
{
	var oneChar = inputStr.charAt(i); 
	if (!( isNaN(oneChar) || oneChar == ' ') || oneChar == '-' ) 
	{ 
		newStr = String(newStr) + String(field.value.substring(i,i+1));
	}
}
	if (newStr != field.value) 
	{
		field.value = newStr;
	}
}
function NumericKeyUp(fieldToEval,fieldToEvalLength,objToJumpTo) 
{
	ForceNumeric(fieldToEval);
	if ((fieldToEval.value.length == fieldToEvalLength)&& (fieldToEval.value != prevValue) && (objToJumpTo != null))
	{ 
		objToJumpTo.focus() 
	} 
		prevValue = fieldToEval.value;
}
/* END: Force Numeric */

/* START: Zip Code Validation */
function ValidateZip (sZip)
{
    //var regZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
    if (sZip == '')
    {
        alert('Please Enter Your Zip Code');
        return false;
    }
    
    else if (sZip != '' && !checkZipCode(sZip))
    {
        alert('The Zip Code Entered is Not Valid');
        return false;
    }
    else return true;
}
/* END: Zip Code Validation */

/* START: User name p/w Zip Code Validation */
function ValidateUserNamePwZip(username, password, sZip){
        //user name and p/w validation
        if(username == '' || password == '' ) {
                      alert('Please Enter Your Username and Password');
                      return false;
       }
       else {
       //zipcode validation
             if (sZip == '')   {
		        alert('Please Enter Your Zip Code');
		        return false;
		    }
		    
		    else if (sZip != '' && !checkZipCode(sZip))   {
		        alert('The Zip Code Entered is Not Valid');
		        return false;
		    }
		    else return true;
       }
       
     
}
/* END: User name p/w Zip Code Validation */

/* START: Validate PSC and Weekly Ads Form */
function ValidatePSCAdsForm (formName, fieldName, type)
{		
	var field = eval("document." + formName + "." + fieldName + ".value");
	var validated
			
	if (field.length == 0)
	{
		alert("Please Enter " + type + " Email Address");
		validated = EmailValidation (field);
	}
	else
	{	
		validated = EmailValidation (field);
	}	
	
	if (validated)
	{
		return true;
	}
	else
	{
		return false;
	}
}
/* END: Validate PSC and Weekly Ads Form */

/* START: Email Validation */
function EmailValidation (Email)
{
	var regEmail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
		
	if (!regEmail.test(Email))
	{
		alert('The Email Address Entered Is Not Valid');
		return false;
	}
	else return true;	
}
/* END: Email Validation */

/* END: CheckZipCode  */
function checkZipCode(sZip)
{
    var valid = true;
    var checkOK = "0123456789";
    
    if ((sZip.length != 5) )
    {
        valid = false;
    }
    else
    {
        for(i=0; i < sZip.length; i++)
        {
            ch = sZip.charAt(i);
            for (j = 0;  j < checkOK.length;  j++)
            {
                if (ch == checkOK.charAt(j))
                {
                    break;
                }
                if (j == (checkOK.length-1))
                {
                    valid = false;
                    break;
                }

            }        
        }
        
        if( valid == true)
        {
             if((sZip.charAt(0) == "0") && (sZip.charAt(1) == "0")) 
             { 
                valid = false;
             }
        }
    
    }

    return valid;
}

/* END: CheckZip*/

/* This method is used by the avenu pages of jewel-osco
  It does the validation for psc card numbers entered twice are matches are not
*/
	function check(field,brandid) {
	    var errMsg = ""
	    var avenusite = "http://avenu.jewelosco.com"
	    var loyaltycardname = "Preferred"
	    var division = 30;
	    
	    if (brandid == 2) {
	        avenusite = "http://avenu.acmemarkets.com"
	        loyaltycardname = "Super"
	        division = 50;
	    }
	    
	    if (document.card.value == "undefined" || document.card.card1.value == "") {
	        alert ("Please enter your "+loyaltycardname+" Card Number");
	    field.card.card1.focus();
	    } else if ( document.card.card2.value == "") {
	        alert ("Please enter your "+loyaltycardname+" Card Number");
	        field.card.card2.focus();
	    } else {
	        var card1Temp = document.card.card1.value.length;
	        var card2Temp = document.card.card2.value.length;
	        if (card1Temp < 10 || card1Temp > 14) {
	            errMsg = "Card number must be minimum of 10 to 14 digits!";
	            document.card.card1.focus();
	        }
	        if (card2Temp < 10 || card2Temp > 14) {
	            errMsg = "Card number must be minimum of 10 to 14 digits!";
	           document.card.card2.focus();
	        }
	        if (errMsg != "") {
	            alert (errMsg);
	        } else if (document.card.card1.value == document.card.card2.value){
            
	           document.card.action = avenusite+"/groceries/LoginPage?lcCard="
                                    +document.card.card1.value+
                                    "&division="+division+"&email=n";
	            document.card.submit();
	        } else {
            alert ("Card numbers entered did not match!");
            }
	    }
	}
    
    
/* This method is used by Avenu Program*/
 function updateParent(parentlink) { 
if(opener.window.closed) self.close();
    opener.document.location.href = parentlink;
    self.close();
    return false;
}
// Open a Party Platters Acme popup window
function winopen(url,stuff,morestuff) 
{
    var popwin = window.open(url,stuff,morestuff);
    
    // you may get "undefined" if a popup blocker did its thing...
    if( typeof(popwin) != "undefined" && popwin )
    {
        popwin.focus();
    }
    
    lastPopup = popwin;
}

function checkEmailSubscription(){
if(isBlank(document.forms[0].firstName.value)){
 alert("Please enter your first name");
        document.forms[0].firstName.focus();
}else if(!isAlphaNumeric(document.forms[0].firstName.value)) {
        alert("You entered special characters that cannot be read by our system. Please re-enter your first name");
        document.forms[0].firstName.focus();
    }else if (isNumericOnlyWithSpace(document.forms[0].firstName.value)){
        alert("First Name should not contain only numbers");
        document.forms[0].firstName.focus();
    }else if(isBlank(document.forms[0].email.value)){
        alert("Please enter your e-mail address");
        document.forms[0].email.focus();      
    }else if(checkemail(document.forms[0].email)){
        alert("Please enter a valid E-mail address, using the format username@subdomain.domain");
        document.forms[0].email.focus(); 
    }else if(isBlank(document.forms[0].zip.value)){
        alert("Please enter your zip code");
        document.forms[0].zip.focus();
    }else if(!checkLength(document.forms[0].zip.value, 5)){
        alert("Your zip code must be five digits long. Please re-enter your zip code");
        document.forms[0].zip.focus();
    }else if(!checkValidZip(document.forms[0].zip.value, 5)){
        alert("Please enter a valid zip code");
        document.forms[0].zip.focus();
    }else{
        //control to yesmail.
        document.forms[0].submit();
    }
}

function isAlphaNumeric(str)
{
    var str=trim(str);
    var ctrI = 0;
    var strLength = str.length;
    var validStr="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";
    for (ctrI = 0; ctrI < strLength; ctrI++) {
       if (validStr.indexOf(str.charAt(ctrI)) == -1){
          return false;
        }
    }
   return true;
}

function checkLength(param, len)
{ 
    var param=trim(param);
    var num="0123456789";
    var tempChar;
    if (param.length != len){        
        return false;
    }else{
  return true;
  }
}

function checkValidZip(param, len)
{ 
    var param=trim(param);
    var num="0123456789";
    var tempChar;
    
        if (param.length== len ){
        if(param.charAt(0)=="0" && param.charAt(1)=="0"){
            return false;
        }
        else{
        for(ctrI=0; ctrI < param.length; ctrI++){
             if (num.indexOf(param.charAt(ctrI)) == -1){
                 return false;
             }
         }
         }
         }
          return true;
}

function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { 
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { 
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { 
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
   }
   return retValue; 
} 

function isNumericOnlyWithSpace(str)
{
    var str = trim(str);
    var ctrI;
    var counter = 0;
    var strLength = str.length;
    var validStr = "1234567890 ";
    if(strLength == 0){
    return false;
    }
    for (ctrI = 0;ctrI < strLength;ctrI++){
    if (validStr.indexOf(str.charAt(ctrI)) != -1){
    counter++;}
    }if(strLength == counter){
   return true;
   }
   return false;
}