/*
 Name            :       myaccount.js
 Purpose         :       Handles all of the functionality on my account pages
 Author          :       ToddH
 Date            :       5/27/2009
 */
//----------------------------------------------------------------------------------------------------------------------------

/*
 Name            :       AddToFavorites
 Author          :       ToddH (Baudville)
 Date            :       5/27/2009
 */
function AddToFavorites(invtId) {
    var strDataSent = "";
    var szUrl = "/dialogs/xmlcallback.aspx?dlg=addtofavs";
    var strDataSent = "item=" + invtId;
    
    var strXmlReturn = saveDataPost(szUrl, strDataSent, false);
    strXmlReturn = strXmlReturn.replace("<Response><item>", "");
    strXmlReturn = strXmlReturn.replace("</item></Response>", "");
    strXmlReturn = strXmlReturn.replace('<?xml version="1.0" ?>', '');
    getElementId(_strCPId + 'lnkAddToFavorites').innerHTML = strXmlReturn;
    getElementId(_strCPId + 'lnkAddToFavorites').onclick = GotoFavorites;
} 

/*
 Name            :       GotoFavorites
 Author          :       NiKZ (Baudville)
 Date            :       1/21/2011
 */
function GotoFavorites() {
	window.location.href = "/myaccount/favorites.aspx";
} 

/*
 Name            :       ShowTrackingPopup
 Author          :       ToddH (Baudville)
 Date            :       5/28/2009
 */
function ShowTrackingPopup(strCarrier, strTrackingNbr) {
    var strSiteDomain = getElementId(_strCPId + 'hidSiteDomain').value
    // Ordertracking/carrier/trackingnum/ma
    var strUrl = strSiteDomain + "Ordertracking/" + strCarrier + "/" + strTrackingNbr + "/ma"
    window.radopen(strUrl, "radwShippingTracker");
}    


/*
 Name            :       PopulateShippingAddress
 Author          :       ToddH (Baudville)
 Date            :       6/2/2009
 */
function PopulateShippingAddress(strAddressID, strBorS) {
    var strPrefix = _strCPId + 'hid_' + strAddressID + "_";
    
    getElementId(_strCPId + 'tbxEmail_' + strBorS).value = getElementId(strPrefix + "abEmail").value;
    getElementId(_strCPId + 'tbxCompany_' + strBorS).value = getElementId(strPrefix + "abCompanyName").value;
    getElementId(_strCPId + 'tbxTitle_' + strBorS).value = getElementId(strPrefix + "abTitle").value;
    getElementId(_strCPId + 'tbxFName_' + strBorS).value = getElementId(strPrefix + "abFirstName").value;
    getElementId(_strCPId + 'tbxLName_' + strBorS).value = getElementId(strPrefix + "abLastName").value;
    getElementId(_strCPId + 'tbxAddr01_' + strBorS).value = getElementId(strPrefix + "abAddr1").value;
    getElementId(_strCPId + 'tbxAddr02_' + strBorS).value = getElementId(strPrefix + "abAddr2").value;
    getElementId(_strCPId + 'tbxCity_' + strBorS).value = getElementId(strPrefix + "abCity").value;
    getElementId(_strCPId + 'ddlState_' + strBorS).value = getElementId(strPrefix + "abState").value;
    getElementId(_strCPId + 'ddlCountry_' + strBorS).value = getElementId(strPrefix + "abCountry").value;
    getElementId(_strCPId + 'tbxZip_' + strBorS).value = getElementId(strPrefix + "abZip").value;
    if (getElementId(strPrefix + "abExt").value == "") {
        getElementId(_strCPId + 'tbxPhone_' + strBorS).value = getElementId(strPrefix + "abPhone").value;
    } else {
        getElementId(_strCPId + 'tbxPhone_' + strBorS).value = getElementId(strPrefix + "abPhone").value + " x" + getElementId(strPrefix + "abExt").value;
    }       
    getElementId(_strCPId + 'tbxFax_' + strBorS).value = getElementId(strPrefix + "abFax").value;
	getElementId(_strCPId + 'chbSameInfo').checked = false;
	//if (getElementId(_strCPId + 'chbSameInfo').checked) {
	//	getElementId(_strCPId + 'chbSameInfo').onclick();
    //}
    
    var tbxMasterEmail = getElementId(_strCPId + 'tbxEmail_Master');
    
    if(tbxMasterEmail != null) {
        tbxMasterEmail.value = getElementId(strPrefix + "abEmail").value;
    }
    
    //A saved address was pushed to the form, assume they dont need to save the address
    getElementId('ctl00_ContentPlaceHolder1_chbSaveAddr').checked = false;
}    

/*
 Name            :       PopulatePrimaryAddress
 Author          :       NikZ
 Date            :       6/13/2011
 */
function PopulatePrimaryAddress() {
	// Only populate the fields when First Name is not already populated, the user is logged in, and there is a primary address available
	if (getElementId(_strCPId + 'tbxFName_S').value == '' && isLoggedIn() && getElementId(_strCPId + 'hidPrimaryAddresssID').value != '') {
		PopulateShippingAddress(getElementId(_strCPId + 'hidPrimaryAddresssID').value,'S');
	}
}

/*
 Name            :       isLoggedIn
 Author          :       NikZ
 Date            :       6/13/2011
 */
function isLoggedIn() {
	var uid = getCookie('uid');
	
	if (uid != '' && uid > 0) {
		return true;
	} else {
		return false;
	}
}

