//------------------------------------------------------------------------------------
//Javascript function page
//------------------------------------------------------------------------------------


//display data in span specified and call fading function
//Parameters: SpanName - which span to update with data
//			  Data - data (dur...)
//------------------------------------------------------------------------------------
function DisplayText(SpanName, Data) {
	document.getElementById(SpanName).innerHTML = Data;
}

//Opens a new window (used mainly for flash)
//Parameters: URL - What page to open
//			  w - new window width
//			  h - new window height
//------------------------------------------------------------------------------------
function WindowOpener(url, w, h) {
	window.open(url, null, 'scrollbars=yes,width=' + w + ', height=' + h);
}

//Update iframe with what asp page to run
//Parameters: Source - What page to run with extra parameters
//			  FrameName - which frame to run this asp page in
//------------------------------------------------------------------------------------
function GetData(Source, FrameName) {
	document.getElementById(FrameName).src = Source;
	return false;
}

//Update hidden fields on product pages to distinguish which of the product has been selected
//Parameters: ProductID - which product has been selected to be added to cart
//------------------------------------------------------------------------------------
function UpdateHidden (ProductID) {
	document.getElementById('SKU').value = document.getElementById('SKU' + ProductID).value; 
	document.getElementById('Quantity').value = document.getElementById('Quantity' + ProductID).value;
}

//On edit cart - confirm deletion
//Parameters: SKU - which product sku the user wants to delete
//------------------------------------------------------------------------------------
function ConfirmDelete(SKU) {
	if (confirm('Are you sure you want to delete \'' + SKU + '\'?')) {
		return true;
	} else {
		return false;
	}
}

//On add product to cart - validate qty and size
//------------------------------------------------------------------------------------
function ValidateUpdateCart() {
	ErrMsg = "";
	
	for ( var i=0,n=document.f1.length; i<n; i++ ) {
		if ((document.f1(i).name != 'Action') && (document.f1(i).name != 'ReturnURL') && (document.f1(i).name != 'OrderLineID') && (document.f1(i).name != 'Update')) {
			if ((document.f1(i).value != '') && ((isNaN(document.f1(i).value)) || (Number(document.f1(i).value) < 1)) || (document.f1(i).value == '')) {
				ErrMsg = 'Please enter a valid positive numeric quanitity.\n';
			}	
		}
	}
		
	if (ErrMsg != '') {
		alert(ErrMsg);
		return false;	
	} else {
		return true;
	}
}


//function to check where there is an email address before trying to 
//send the password to a non existant email address
//------------------------------------------------------------------------------------
function GetEmail() {
	var strError = '';
	var oEmailAddress = document.getElementById('EmailAddress');
	var oForgotPassword = document.getElementById('ForgotPassword');

	if (oEmailAddress.value == '') {
		strError = strError + '\n - Please fill in your Email Address';
	}	

	//if there was an error then display it else go to the next page
	if (strError.length == 0) {
		oForgotPassword.href = oForgotPassword.href + oEmailAddress.value;
		return true;
	} else {
		alert('Please correct the following errors:'+strError);
		return false;
	}

}

//function to hide and show span for menu 
//SpanID to hide or display
//LocatorText is the text to display in span at top of page
//------------------------------------------------------------------------------------
function HideShow(SpanID, LocatorText) {
	if (document.getElementById(SpanID).style.display == 'block') {
		document.getElementById(SpanID).style.display = 'none';
	} else {
		document.getElementById(SpanID).style.display = 'block';
	}
	document.getElementById('Locator').innerHTML = LocatorText;
}

//function Show Category Notes/Text on front page
//Text - is text to display
//------------------------------------------------------------------------------------
function DisplayCatText(Text) {
	if (document.getElementById('CategoryText') != null) {
		document.getElementById('CategoryText').innerHTML = Text;	
		document.getElementById('FrontPage').innerHTML = '';	;
	}
}


//function to add up the qtys on the product detail page
//------------------------------------------------------------------------------------
function CreateQtyString() {

	var TmpStr_Qty;
	TmpStr_Qty = '';
	for ( var i=0,n=document.f1.length; i<n; i++ ) {
		if ((document.f1(i).name.substr(0, 4) == 'Qty_') && (document.f1(i).value != '')) {
			TmpStr_Qty += 'SKU:' + document.f1(i).name.substr(4, document.f1(i).name.indexOf('|')-4) + '|Qty:' + document.f1(i).value + '|';
		}
	}
	return TmpStr_Qty;
}

function ShowDetail(id) {
	ID = document.getElementById(id);
	ID2 = document.getElementById('Details' + id);
	if (ID.src.substr(ID.src.length - 16, 16) == 'FolderClosed.gif') {
		ID.src = 'images/FolderOpen.gif';	
		ID2.style.display = 'block';	
	} else {
		ID.src = 'images/FolderClosed.gif';	
		ID2.style.display = 'none';	
	}
}

//displays flash file
//Parameters: DivID = 
//			  WIDTH =
//			  HEIGHT =
//			  URL =
//------------------------------------------------------------------------------------
function CreateControl(DivID, WIDTH, HEIGHT, URL) {
	var d = document.getElementById(DivID);
	tmpStr = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + WIDTH + '" height="' + HEIGHT + '">' +
        '<param name="movie" value="' + URL + '">' +
        '<param name="quality" value="high">' + 
        '<param name="wmode" value="opaque">' + 
        '<embed src="' + URL + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + WIDTH + '" height="' + HEIGHT + '"></embed>' +
      '</object>';
	d.innerHTML = tmpStr
}

//Function to join wedding date fields in Bridal Comp
//------------------------------------------------------------------------------------
function UpdateWedField(oField) {
	oField.value = document.getElementById('Wed_Day').value + '/' + document.getElementById('Wed_Month').value + '/' + document.getElementById('Wed_Year').value;
}

//Validate Date Functions
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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){
	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){
				return false
	}
	if (strMonth.length<1 || month<1 || month>12){
				return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
				return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
				return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
				return false
	}
	
	return true
}
