//global vars


function showTip(num) {
if (num == null) return;
var ht;

for (var i=1; i < 11; i++) {
 eval("tip_arrow_"+i+".style.display = 'none';");
 eval("tip_disp_"+i+".style.display = 'none';");
}

 eval("tip_arrow_"+num+".style.display = 'block';");
 eval("tip_disp_"+num+".style.display = 'block';");
 
 }
 
 /*  The following section relates to the storelocator */
 
 function toggleCities(targetID) {
var rgStr1, rgStr2, pos, loc, htmlStr, info, city, loc;
	var div = document.getElementsByTagName("div");
    for (i = 0; i < div.length; i++) {
       rgStr1 = div[i].id;
            
       rgStr1 = rgStr1.substr(0, 5);
       if (rgStr1 == "city^") div[i].style.display = "none";
       
       rgStr2 = div[i].id;
           
       rgStr2 = rgStr2.substr(0, 7);
       if (rgStr2 == "region^") div[i].style.display = "none";
   }
   
   var city_img_div = document.getElementById("city_img");
   
   if (city_img_div != null) city_img_div.style.display = "none";
   
   var info = document.getElementById("infohelp");
   info.style.display = "none";
   
   var target = document.getElementById(targetID);
   var no_store = document.getElementById("no_store");
   
   city = document.getElementById("citytxt");
   loc = document.getElementById("locationtxt");
   
   city.style.display = "block";
   loc.style.display = "block";
   
   loc = targetID.substr(5, targetID.length);
   loc = loc.toUpperCase();
   htmlStr = "<table border='0' cellspacing='0' cellpadding='0'>";
   htmlStr = htmlStr + "<tr><td colspan='2'><h2><span style='text-transform: uppercase;'>" + loc + "</span></h2></td></tr>";
   htmlStr = htmlStr + "<tr><td colspan='2'>Sorry. No Stores found in this Region.</td></tr></table>";
   
   if (target != null) {
    no_store.style.display = "none";
	target.style.display = "block";
   } else {
    no_store.style.display = "block";
    no_store.innerHTML = htmlStr;
   }  
   
}

function toggleRegions(targetID) {
var rgStr, pos, city_img_div;
	var div = document.getElementsByTagName("div");
    for (i = 0; i < div.length; i++) {
       rgStr = div[i].id;
       rgStr = rgStr.substr(0, 7);
        if (rgStr == "region^") div[i].style.display = "none";
    }
   
   var target = document.getElementById(targetID);
   
   city_img_div = document.getElementById("city_img");
   
   if (city_img_div != null) city_img_div.style.display = "block";
   
   if (target == null) return;
   target.style.display = "block";
}

/* The storelocator section ends */
 
/* The following section has global functions to validate dates and emails */ 

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var msg;

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		     return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr, dtFld){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		msg = msg + "The " + dtFld + " date format should be : dd/mm/yyyy. \n";
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		msg = msg + "Please enter a valid month for the " + dtFld + " date. \n";
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		msg = msg + "Please enter a valid day for the " + dtFld + " date. \n";
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		msg = msg + "Please enter a valid 4 digit year for the " + dtFld + " date between "+minYear+" and "+maxYear+". \n";
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		msg = msg + "Please enter a valid " + dtFld + " date. \n"
		return false
	}
return true
}

function enterText(txt) {
if (txt == null) return;

if (echeck(txt.value)==false) {
txt.value = "enter your email";
}
}

function clearText(txt) {
if (txt == null) return;

txt.value = "";
}

function openWin(url, h, w) {
if (url == "") return;
window.open(url,null,  'height=' + h + ',width=' + w + ',scrollbars=yes,status=yes,toolbar=no,menubar=no,location=no');

}

function checkEmail(evt) {
var email, err, bw;
msg = "Please fill in the following details: \n \n";
err = false;
bw = navigator.appName;
bw = bw.toLowerCase();

email = document.sub_form.email.value;
  
 if (email == "") {
    msg = msg + "Your email address. \n";
    err = true;
  }
  
  if (email != "" && echeck(email)==false) {
    msg = msg + "A valid email address. \n";
    err = true;
  }
  
  
  if (err == true) {
    alert(msg);
   if (bw == "microsoft internet explorer") { 
    event.returnValue = false;
   } else {
     evt.preventDefault();
   }  
    return false;
  } 
}

